test_tc_http.cpp 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890
  1. //
  2. // Created by jarod on 2020/2/20.
  3. //
  4. #include "util/tc_http.h"
  5. #include "util/tc_common.h"
  6. #include "util/tc_file.h"
  7. #include "util/tc_http_async.h"
  8. #include "gtest/gtest.h"
  9. using namespace tars;
  10. class UtilHttpTest : public testing::Test
  11. {
  12. public:
  13. //添加日志
  14. static void SetUpTestCase()
  15. {
  16. // cout<<"SetUpTestCase"<<endl;
  17. }
  18. static void TearDownTestCase()
  19. {
  20. // cout<<"TearDownTestCase"<<endl;
  21. }
  22. virtual void SetUp() //TEST跑之前会执行SetUp
  23. {
  24. // cout<<"SetUp"<<endl;
  25. }
  26. virtual void TearDown() //TEST跑完之后会执行TearDown
  27. {
  28. // cout<<"TearDown"<<endl;
  29. }
  30. };
  31. TEST_F(UtilHttpTest, testCheckRequestURL) //此时使用的是TEST_F宏
  32. {
  33. string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
  34. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  35. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  36. +string("Accept-Encoding: gzip\r\n")
  37. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  38. +string("Connection: close\r\n")
  39. +string("Host: www.qq.com\r\n")
  40. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  41. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  42. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  43. TC_HttpRequest req;
  44. ASSERT_TRUE(req.decode(s));
  45. ASSERT_TRUE(req.getRequestUrl() == "/a/b");
  46. ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
  47. }
  48. TEST_F(UtilHttpTest, testHttp) //此时使用的是TEST_F宏
  49. {
  50. TC_HttpRequest stHttpReq;
  51. stHttpReq.setCacheControl("no-cache");
  52. stHttpReq.setGetRequest("http://www.qq.com/", true);
  53. TC_HttpResponse stHttpRsp;
  54. int ret = stHttpReq.doRequest(stHttpRsp, 3000);
  55. ASSERT_TRUE(ret == 0);
  56. // cout << ret << ":" << stHttpRsp.getContent() << endl;
  57. // cout << "\n\n==============================\n" << stHttpRsp.getContent().size() << endl;
  58. // cout << stHttpRsp.genHeader() << endl;
  59. }
  60. TEST_F(UtilHttpTest, testEncodeString) //此时使用的是TEST_F宏
  61. {
  62. string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
  63. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  64. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  65. +string("Accept-Encoding: gzip\r\n")
  66. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  67. +string("Connection: close\r\n")
  68. +string("Host: www.qq.com\r\n")
  69. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  70. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  71. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  72. // s += string("a", 1024);
  73. TC_HttpRequest req;
  74. req.decode(s);
  75. int64_t t = TC_Common::now2us();
  76. int count = 100000;
  77. int i = 0;
  78. while(++i<count) {
  79. string s;
  80. s = req.encode();
  81. }
  82. ASSERT_TRUE(req.decode(s));
  83. ASSERT_TRUE(req.getRequestUrl() == "/a/b");
  84. ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
  85. cout << "testEncodeString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
  86. }
  87. TEST_F(UtilHttpTest, testEncodeVector) //此时使用的是TEST_F宏
  88. {
  89. string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
  90. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  91. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  92. +string("Accept-Encoding: gzip\r\n")
  93. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  94. +string("Connection: close\r\n")
  95. +string("Host: www.qq.com\r\n")
  96. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  97. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  98. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  99. s += string("a", 1024);
  100. TC_HttpRequest req;
  101. req.decode(s);
  102. int64_t t = TC_Common::now2us();
  103. int count = 100000;
  104. int i = 0;
  105. while(++i<count) {
  106. vector<char> buff;
  107. req.encode(buff);
  108. }
  109. cout << "testEncodeVector::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
  110. }
  111. TEST_F(UtilHttpTest, testEncodeBuffString) //此时使用的是TEST_F宏
  112. {
  113. string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\\r\n")
  114. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  115. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  116. +string("Accept-Encoding: gzip\r\n")
  117. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  118. +string("Connection: close\r\n")
  119. +string("Host: www.qq.com\r\n")
  120. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  121. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  122. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  123. int64_t t = TC_Common::now2us();
  124. TC_HttpRequest req;
  125. req.decode(s);
  126. TC_NetWorkBuffer buff(NULL);
  127. int count = 100000;
  128. int i = 0;
  129. while(++i<count) {
  130. req.encode(buff);
  131. }
  132. cout << "testEncodeBuffString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
  133. }
  134. TEST_F(UtilHttpTest, testDecodeString) //此时使用的是TEST_F宏
  135. {
  136. string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
  137. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  138. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  139. +string("Accept-Encoding: gzip\r\n")
  140. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  141. +string("Connection: close\r\n")
  142. +string("Host: www.qq.com\r\n")
  143. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  144. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  145. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  146. int64_t t = TC_Common::now2us();
  147. int count = 100000;
  148. int i = 0;
  149. while(++i<count) {
  150. TC_HttpRequest req;
  151. req.decode(s);
  152. }
  153. cout << "testDecodeString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
  154. }
  155. TEST_F(UtilHttpTest, testDecodeBuffString) //此时使用的是TEST_F宏
  156. {
  157. string s = string("HTTP/1.1 200 OK\r\n")
  158. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  159. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  160. +string("Accept-Encoding: gzip\r\n")
  161. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  162. +string("Connection: close\r\n")
  163. +string("Host: www.qq.com\r\n")
  164. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  165. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  166. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  167. int count = 100000;
  168. int i = 0;
  169. vector<TC_NetWorkBuffer> vbuff;
  170. while(i<count) {
  171. TC_NetWorkBuffer buff(NULL);
  172. buff.addBuffer(s);
  173. vbuff.push_back(buff);
  174. ++i;
  175. }
  176. int64_t t = TC_Common::now2us();
  177. i = 0;
  178. while(i<count) {
  179. TC_HttpResponse req;
  180. ASSERT_FALSE(req.incrementDecode(vbuff[i]));
  181. ++i;
  182. }
  183. cout << "testDecodeBuffString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
  184. }
  185. TEST_F(UtilHttpTest, testCheckRequestString) //此时使用的是TEST_F宏
  186. {
  187. string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
  188. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  189. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  190. +string("Accept-Encoding: gzip\r\n")
  191. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  192. +string("Connection: close\r\n")
  193. +string("Host: www.qq.com\r\n")
  194. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  195. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  196. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  197. int64_t t = TC_Common::now2us();
  198. TC_HttpRequest req;
  199. int count = 100000;
  200. int i = 0;
  201. while(++i<count) {
  202. ASSERT_TRUE(req.checkRequest(s.c_str(), s.size()));
  203. }
  204. ASSERT_TRUE(req.decode(s));
  205. ASSERT_TRUE(req.getRequestUrl() == "/a/b");
  206. cout << req.getURL().getDomain() << endl;
  207. ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
  208. cout << "testCheckRequestString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
  209. }
  210. TEST_F(UtilHttpTest, testCheckRequestBuff) //此时使用的是TEST_F宏
  211. {
  212. string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
  213. +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
  214. +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
  215. +string("Accept-Encoding: gzip\r\n")
  216. +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
  217. +string("Connection: close\r\n")
  218. +string("Host: www.qq.com\r\n")
  219. +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
  220. +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
  221. +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  222. int64_t t = TC_Common::now2us();
  223. TC_NetWorkBuffer buff(NULL);
  224. buff.addBuffer(s.c_str(), s.size());
  225. TC_HttpRequest req;
  226. int count = 100000;
  227. int i = 0;
  228. while(++i<count) {
  229. ASSERT_TRUE(req.checkRequest(buff));
  230. }
  231. cout << "testCheckRequestBuff::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
  232. }
  233. TEST_F(UtilHttpTest, testHttpFinish) //此时使用的是TEST_F宏
  234. {
  235. string body = "abdefghigk";
  236. vector<string> sbuff;
  237. sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
  238. sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
  239. sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
  240. sbuff.push_back("Accept-Encoding: gzip\r\n");
  241. sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
  242. sbuff.push_back("Connection: close\r\n");
  243. sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  244. string header;
  245. for(auto s : sbuff)
  246. {
  247. header += s;
  248. }
  249. string s = header + body;
  250. TC_HttpRequest request;
  251. ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
  252. TC_NetWorkBuffer buff(NULL);
  253. buff.addBuffer(s.c_str(), s.size());
  254. ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_FULL);
  255. }
  256. TEST_F(UtilHttpTest, testHttpFinishNoLength) //此时使用的是TEST_F宏
  257. {
  258. vector<string> sbuff;
  259. sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
  260. sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
  261. sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
  262. sbuff.push_back("Accept-Encoding: gzip\r\n");
  263. sbuff.push_back("Connection: close\r\n");
  264. sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  265. string header;
  266. for(auto s : sbuff)
  267. {
  268. header += s;
  269. }
  270. string s = header;
  271. TC_HttpRequest request;
  272. ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
  273. TC_NetWorkBuffer buff(NULL);
  274. buff.addBuffer(s.c_str(), s.size());
  275. ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_FULL);
  276. }
  277. TEST_F(UtilHttpTest, testHttpNoFinish) //此时使用的是TEST_F宏
  278. {
  279. string body = "abdefghigk";
  280. vector<string> sbuff;
  281. sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
  282. sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
  283. sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
  284. sbuff.push_back("Accept-Encoding: gzip\r\n");
  285. sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size() + 1) + "\r\n");
  286. sbuff.push_back("Connection: close\r\n");
  287. sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  288. string header;
  289. for(auto s : sbuff)
  290. {
  291. header += s;
  292. }
  293. string s = header + body;
  294. TC_HttpRequest request;
  295. ASSERT_TRUE(!request.checkRequest(s.c_str(), s.size()));
  296. TC_NetWorkBuffer buff(NULL);
  297. buff.addBuffer(s.c_str(), s.size());
  298. ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_LESS);
  299. }
  300. TEST_F(UtilHttpTest, testHttpRequestChunked) //此时使用的是TEST_F宏
  301. {
  302. vector<string> body;
  303. body.push_back("abasdfadefghiadfagk1");
  304. body.push_back("abdasdfadfaefghigk2");
  305. body.push_back("abdsaefghigk3");
  306. body.push_back("abdeasdfafasfasfasfasdfasffghigk4");
  307. vector<string> sbuff;
  308. sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
  309. sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
  310. sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
  311. sbuff.push_back("Accept-Encoding: gzip\r\n");
  312. sbuff.push_back("Transfer-Encoding: chunked\r\n");
  313. sbuff.push_back("Connection: close\r\n");
  314. sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  315. stringstream data;
  316. for(auto s : sbuff)
  317. {
  318. data << s;
  319. }
  320. string sbody;
  321. for(auto s : body)
  322. {
  323. sbody += s;
  324. data << hex << s.size() << "\r\n" << s << "\r\n";
  325. }
  326. data << 0 << "\r\n\r\n";
  327. string s = data.str();
  328. TC_HttpRequest request;
  329. ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
  330. ASSERT_TRUE(request.decode(s));
  331. ASSERT_TRUE(request.getContent() == sbody);
  332. TC_NetWorkBuffer buff(NULL);
  333. buff.addBuffer(s.c_str(), s.size());
  334. request.reset();
  335. ASSERT_TRUE(request.checkRequest(buff));
  336. }
  337. TEST_F(UtilHttpTest, testHttpRequestChunkedNoFinish) //此时使用的是TEST_F宏
  338. {
  339. vector<string> body;
  340. body.push_back("abasdfadefghiadfagk1");
  341. body.push_back("abdasdfadfaefghigk2");
  342. body.push_back("abdsaefghigk3");
  343. body.push_back("abdeasdfafasfasfasfasdfasffghigk4");
  344. vector<string> sbuff;
  345. sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
  346. sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
  347. sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
  348. sbuff.push_back("Accept-Encoding: gzip\r\n");
  349. sbuff.push_back("Transfer-Encoding: chunked\r\n");
  350. sbuff.push_back("Connection: close\r\n");
  351. sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
  352. stringstream data;
  353. for(auto s : sbuff)
  354. {
  355. data << s;
  356. }
  357. data << hex << body[0].size() << "\r\n" << body[0] << "\r\n";
  358. data << hex << body[1].size() << "\r\n" << body[1] << "\r\n";
  359. data << hex << body[2].size() ;
  360. string s = data.str();
  361. TC_HttpRequest request;
  362. ASSERT_TRUE(!request.checkRequest(s.c_str(), s.size()));
  363. TC_NetWorkBuffer buff(NULL);
  364. buff.addBuffer(s.c_str(), s.size());
  365. ASSERT_TRUE(!request.checkRequest(buff));
  366. }
  367. TEST_F(UtilHttpTest, testHttpResponse) //此时使用的是TEST_F宏
  368. {
  369. string body = "abcdef";
  370. vector<string> sbuff;
  371. sbuff.push_back("HTTP/1.1 200 OK\r\n");
  372. sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
  373. sbuff.push_back("\r\n");
  374. string header;
  375. for(auto s : sbuff)
  376. {
  377. header += s;
  378. }
  379. string s = header + body;
  380. {
  381. TC_HttpResponse response;
  382. ASSERT_TRUE(response.decode(s));
  383. ASSERT_TRUE(response.getContent() == body);
  384. }
  385. {
  386. TC_HttpResponse response;
  387. TC_NetWorkBuffer buff(NULL);
  388. buff.addBuffer(s.c_str(), s.size());
  389. ASSERT_TRUE(response.incrementDecode(buff));
  390. ASSERT_TRUE(response.getContent() == body);
  391. }
  392. }
  393. TEST_F(UtilHttpTest, testHttpResponseNoFinish) //此时使用的是TEST_F宏
  394. {
  395. string body = "abcdef";
  396. vector<string> sbuff;
  397. sbuff.push_back("HTTP/1.1 200 OK\r\n");
  398. sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size() + 1) + "\r\n");
  399. sbuff.push_back("\r\n");
  400. string header;
  401. for(auto s : sbuff)
  402. {
  403. header += s;
  404. }
  405. string s = header + body;
  406. {
  407. TC_HttpResponse response;
  408. ASSERT_TRUE(!response.decode(s));
  409. }
  410. {
  411. TC_HttpResponse response;
  412. TC_NetWorkBuffer buff(NULL);
  413. buff.addBuffer(s.c_str(), s.size());
  414. ASSERT_TRUE(!response.incrementDecode(buff));
  415. }
  416. }
  417. TEST_F(UtilHttpTest, testHttpResponseNoLength) //此时使用的是TEST_F宏
  418. {
  419. vector<string> sbuff;
  420. sbuff.push_back("HTTP/1.1 200 OK\r\n");
  421. sbuff.push_back("Connection: close\r\n");
  422. sbuff.push_back("\r\n");
  423. string header;
  424. for(auto s : sbuff)
  425. {
  426. header += s;
  427. }
  428. string s = header;
  429. {
  430. TC_HttpResponse response;
  431. ASSERT_TRUE(response.decode(s));
  432. }
  433. {
  434. TC_HttpResponse response;
  435. TC_NetWorkBuffer buff(NULL);
  436. buff.addBuffer(s.c_str(), s.size());
  437. ASSERT_FALSE(response.incrementDecode(buff));
  438. }
  439. }
  440. TEST_F(UtilHttpTest, testHttpResponseIncrementFinish) //此时使用的是TEST_F宏
  441. {
  442. string body = "abcdeasdfadfsff";
  443. vector<string> sbuff;
  444. sbuff.push_back("HTTP/1.1 200 OK\r\n");
  445. sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
  446. sbuff.push_back("\r\n");
  447. string header;
  448. for(auto s : sbuff)
  449. {
  450. header += s;
  451. }
  452. string s = header;
  453. {
  454. TC_HttpResponse response;
  455. ASSERT_TRUE(!response.decode(s));
  456. }
  457. {
  458. TC_HttpResponse response;
  459. TC_NetWorkBuffer buff(NULL);
  460. buff.addBuffer(s.c_str(), s.size());
  461. ASSERT_TRUE(!response.incrementDecode(buff));
  462. }
  463. s = header + body;
  464. {
  465. TC_HttpResponse response;
  466. ASSERT_TRUE(response.decode(s));
  467. }
  468. {
  469. TC_HttpResponse response;
  470. TC_NetWorkBuffer buff(NULL);
  471. buff.addBuffer(s.c_str(), s.size());
  472. ASSERT_TRUE(response.incrementDecode(buff));
  473. }
  474. }
  475. TEST_F(UtilHttpTest, testHttpResponseChunked) //此时使用的是TEST_F宏
  476. {
  477. vector<string> body;
  478. body.push_back("abdefghiasdfasdfsadfsadfsagk1");
  479. body.push_back("abdefghasdfaaigk2");
  480. body.push_back("abdefghigadsfadsfk3");
  481. body.push_back("abdefgsfagasasdfasfdfdfsdfsfsdfdsffsdfsdfhigk4");
  482. vector<string> sbuff;
  483. sbuff.push_back("HTTP/1.1 200 OK\r\n");
  484. sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
  485. sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
  486. sbuff.push_back("Transfer-Encoding: chunked\r\n");
  487. sbuff.push_back("Connection: close\r\n\r\n");
  488. stringstream data;
  489. for(auto s : sbuff)
  490. {
  491. data << s;
  492. }
  493. string sbody;
  494. for(auto s : body)
  495. {
  496. sbody += s;
  497. data << hex << s.size() << "\r\n" << s << "\r\n";
  498. }
  499. data << 0 << "\r\n\r\n";
  500. string s = data.str();
  501. {
  502. TC_HttpResponse response;
  503. ASSERT_TRUE(response.decode(s));
  504. ASSERT_TRUE(response.getContent() == sbody);
  505. }
  506. {
  507. TC_HttpResponse response;
  508. TC_NetWorkBuffer buff(NULL);
  509. buff.addBuffer(s.c_str(), s.size());
  510. ASSERT_TRUE(response.incrementDecode(buff));
  511. ASSERT_TRUE(response.getContent() == sbody);
  512. }
  513. }
  514. TEST_F(UtilHttpTest, testHttpResponseChunkedNoFinish) //此时使用的是TEST_F宏
  515. {
  516. vector<string> body;
  517. body.push_back("abdefasdfasfasghigk1");
  518. body.push_back("asdfaabdeafghigk2");
  519. body.push_back("abasdfasdfasdfasdfasdfasdfasfasdefghigk3");
  520. body.push_back("abdefgfasdfasdfasdfasdfadfigk4");
  521. vector<string> sbuff;
  522. sbuff.push_back("HTTP/1.1 200 OK\r\n");
  523. sbuff.push_back("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n");
  524. sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
  525. sbuff.push_back("Transfer-Encoding: chunked\r\n");
  526. sbuff.push_back("Connection: close\r\n\r\n");
  527. stringstream data;
  528. for(auto s : sbuff)
  529. {
  530. data << s;
  531. }
  532. data << hex << body[0].size() << "\r\n" << body[0] << "\r\n";
  533. data << hex << body[1].size() << "\r\n" << body[1] << "\r\n";
  534. data << hex << body[2].size() << "\r\n" << "abc";
  535. string s = data.str();
  536. {
  537. TC_HttpResponse response;
  538. ASSERT_TRUE(!response.decode(s));
  539. }
  540. {
  541. TC_HttpResponse response;
  542. TC_NetWorkBuffer buff(NULL);
  543. buff.addBuffer(s.c_str(), s.size());
  544. ASSERT_TRUE(!response.incrementDecode(buff));
  545. }
  546. }
  547. void testCookie(const string &sRspURL, const string &sReqURL, const vector<string> &vsCookie)
  548. {
  549. // cout << sRspURL << "=>" << sReqURL << "-----------------------------------" << endl;
  550. TC_HttpCookie cookie;
  551. cookie.addCookie(sRspURL, vsCookie);
  552. // list<TC_HttpCookie::Cookie> vCookie = cookie.getAllCookie();
  553. // list<TC_HttpCookie::Cookie>::iterator it = vCookie.begin();
  554. // while(it != vCookie.end())
  555. // {
  556. // cout << TC_Common::tostr(it->_data.begin(), it->_data.end(), "; ") << ", " << it->_expires << ", " << it->_path << endl;
  557. // ++it;
  558. // }
  559. // cout << "-----------------------------------" << endl << endl;
  560. string sCookie;
  561. cookie.getCookieForURL(sReqURL, sCookie);
  562. // cout << TC_Common::tostr(sCookie) << endl;
  563. // cout << "-----------------------------------" << endl << endl;
  564. }
  565. TEST_F(UtilHttpTest, testCookie) //此时使用的是TEST_F宏
  566. {
  567. ASSERT_TRUE(TC_HttpCookie::matchDomain("qq.com", "www.qq.com") == true);
  568. ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "www.qq.com") == true);
  569. ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "qq.com") == true);
  570. ASSERT_TRUE(TC_HttpCookie::matchDomain("t.qq.com", "www.qq.com") == false);
  571. ASSERT_TRUE(TC_HttpCookie::matchDomain(".t.qq.com", "www.qq.com") == false);
  572. ASSERT_TRUE(TC_HttpCookie::matchDomain(".t.qq.com", "t.qq.com") == true);
  573. ASSERT_TRUE(TC_HttpCookie::matchDomain(".com", "www.qq.com") == false);
  574. ASSERT_TRUE(TC_HttpCookie::matchDomain(".com", "qq.com") == false);
  575. ASSERT_TRUE(TC_HttpCookie::matchDomain(".y.qq.com", "x.y.qq.com") == true);
  576. ASSERT_TRUE(TC_HttpCookie::matchDomain(".x.y.qq.com", "x.y.qq.com") == true);
  577. ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "x.y.qq.com") == true);
  578. ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "y.qq.com") == true);
  579. ASSERT_TRUE(TC_HttpCookie::matchDomain("qq.com", "y.qq.com") == true);
  580. // cout << TC_Common::now2GMTstr() << endl;
  581. string gmt = TC_Common::tm2GMTstr(time(NULL) + 10);
  582. string s = "HTTP/1.1 200 OK\r\n";// 200 Aouut Error\r\n";
  583. s += "Set-Cookie: n1=1; a=1; c=d; Path=/; Domain=qq.com; Expires=" + gmt + "\r\n";
  584. s += "Set-Cookie: n2=2; a=0; c=d; Path=/abc/def; Domain=.qq.com; Expires=" + gmt + "\r\n";
  585. s += "Set-Cookie: n3=3; a=5; c=d; Path=/abc/def/aaa; Domain=.qq.com; Expires=" + gmt + "\r\n";
  586. s += "Set-Cookie: n4=4; a=6; c=d; Path=/abc; Domain=.qq.com; Expires=" + gmt + "\r\n";
  587. s += "Set-Cookie: n5=5; a=2; c=d; Path=/; Domain=.qq.com; Expires=" + gmt + "\r\n";
  588. s += "Set-Cookie: n6=6; c=3; Path=/; Domain=y.qq.com; Expires=" + gmt + "\r\n";
  589. s += "Set-Cookie: n7=7; c=3; Path=/abc; Domain=.y.qq.com; Expires=" + gmt + "\r\n";
  590. s += "Set-Cookie: n8=6; c=3; Path=/; Domain=x.y.qq.com; Expires=" + gmt + "\r\n";
  591. s += "Set-Cookie: n9=7; c=4; Path=/; Domain=.x.y.qq.com; Expires=" + gmt + "\r\n";
  592. s += "Set-Cookie: n10=7; c=4; Path=/; Domain=qqq.com; Expires=" + gmt + "\r\n";
  593. s += "Set-Cookie: n11=7; c=4; Path=/; Domain=.qqq.com; Expires=" + gmt + "\r\n";
  594. s += "Set-Cookie: n12=8; c=4; Expires=" + gmt + "\r\n";
  595. s += "Accept-Ranges: bytes\r\n\r\n";
  596. TC_HttpResponse rsp;
  597. rsp.decode(s);
  598. // cout << "-----------------------------------" << endl;
  599. vector<string> vsCookie = rsp.getSetCookie();
  600. // cout << TC_Common::tostr(vsCookie.begin(), vsCookie.end(), "\r\n") << endl << endl;
  601. testCookie("http://www.qq.com", "http://www.qq.com", vsCookie);
  602. testCookie("http://www.qq.com/abc/def", "http://www.qq.com", vsCookie);
  603. testCookie("http://www.qq.com/abc/def", "http://www.qq.com/abc", vsCookie);
  604. // cout << endl;
  605. testCookie("http://www.qq.com", "http://qq.com", vsCookie);
  606. testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com/abc/def/aaa", vsCookie);
  607. testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com", vsCookie);
  608. testCookie("http://www.qq.com", "http://www.qq.com/abc/def", vsCookie);
  609. testCookie("http://qq.com", "http://qq.com/abc/def", vsCookie);
  610. testCookie("http://qq.com", "http://t.qq.com/abc/def", vsCookie);
  611. testCookie("http://qq.com", "http://y.qq.com/", vsCookie);
  612. testCookie("http://qq.com", "http://y.qq.com/abc", vsCookie);
  613. testCookie("http://x.y.qq.com", "http://x.y.qq.com", vsCookie);
  614. }
  615. TEST_F(UtilHttpTest, testQQ) //此时使用的是TEST_F宏
  616. {
  617. string url = "www.qq.com";
  618. TC_HttpRequest stHttpReq;
  619. // stHttpReq.setCacheControl("no-cache");
  620. // stHttpReq.setUserAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.122 Safari/537.36");
  621. stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
  622. stHttpReq.setHeader("Connection", "Close");
  623. stHttpReq.setAcceptEncoding("gzip, deflate, br");
  624. stHttpReq.setGetRequest(url);
  625. string sSendBuffer = stHttpReq.encode();
  626. TC_HttpResponse stHttpRsp;
  627. int iRet = stHttpReq.doRequest(stHttpRsp, 5000);
  628. ASSERT_TRUE(iRet == 0);
  629. ASSERT_TRUE(stHttpRsp.hasHeader("Location"));
  630. }
  631. TEST_F(UtilHttpTest, testBaidu) //此时使用的是TEST_F宏
  632. {
  633. string url = "www.baidu.com";
  634. TC_HttpRequest stHttpReq;
  635. stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
  636. stHttpReq.setHeader("Connection", "Close");
  637. stHttpReq.setAcceptEncoding("gzip, deflate, br");
  638. stHttpReq.setGetRequest(url);
  639. string sSendBuffer = stHttpReq.encode();
  640. TC_HttpResponse stHttpRsp;
  641. int iRet = stHttpReq.doRequest(stHttpRsp, 5000);
  642. ASSERT_TRUE(iRet == 0);
  643. //
  644. // string file = stHttpReq.getURL().getDomain() + ".html";
  645. //
  646. // auto headers = stHttpRsp.getHeaders();
  647. //
  648. // cout << "request:" << url << endl;
  649. // cout << TC_Common::tostr(headers.begin(), headers.end(), "\r\n") << endl;
  650. // TC_File::save2file(file, stHttpRsp.getContent());
  651. ASSERT_TRUE(stHttpRsp.getContentLength() == stHttpRsp.getContent().length());
  652. }
  653. TEST_F(UtilHttpTest, testIncrementDecode1)
  654. {
  655. TC_HttpResponse rsp;
  656. string data = "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd-0-139896025024448";
  657. string s = "HTTP/1.1 200 OK\r\n";
  658. s += "Content-Length : 115\r\n\r\n";
  659. s += data;
  660. TC_NetWorkBuffer::Buffer buff;
  661. buff.addBuffer(s);
  662. bool flag = rsp.incrementDecode(buff);
  663. // LOG_CONSOLE_DEBUG << rsp.getContent() << endl;
  664. ASSERT_TRUE(flag);
  665. ASSERT_TRUE(rsp.getContent() == data);
  666. }
  667. TEST_F(UtilHttpTest, testIncrementDecode2)
  668. {
  669. TC_HttpResponse rsp;
  670. string data = "ddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd-0-139896025024448";
  671. string s = "HTTP/1.1 200 OK\r\n";
  672. s += "Content-Length : 115\r\n\r\n";
  673. s += data;
  674. TC_NetWorkBuffer buff(NULL);
  675. buff.addBuffer(s);
  676. bool flag = rsp.incrementDecode(buff);
  677. // LOG_CONSOLE_DEBUG << rsp.getContent() << endl;
  678. ASSERT_TRUE(flag);
  679. ASSERT_TRUE(rsp.getContent() == data);
  680. }
  681. TEST_F(UtilHttpTest, testIncrementDecode3)
  682. {
  683. TC_HttpResponse rsp;
  684. string data = "400 Bad Request: malformed Host header";
  685. string s = "HTTP/1.1 400 Bad Request: malformed Host header\r\n";
  686. s += "Content-Type: text/plain; charset=utf-8\r\n";
  687. s += "Connection: close\r\n\r\n";
  688. s += data;
  689. TC_NetWorkBuffer buff(NULL);
  690. buff.addBuffer(s);
  691. rsp.incrementDecode(buff);
  692. LOG_CONSOLE_DEBUG << rsp.getContent() << endl;
  693. // ASSERT_TRUE(flag);
  694. ASSERT_TRUE(rsp.getContent() == data);
  695. }