test_tc_http.cpp 25 KB

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