test_tc_http_async.cpp 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419
  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_http_async.h"
  7. #include "util/tc_md5.h"
  8. #include "test_server.h"
  9. #include "gtest/gtest.h"
  10. #include "certs.h"
  11. using namespace tars;
  12. class UtilHttpAsyncTest : public testing::Test
  13. {
  14. public:
  15. //添加日志
  16. static void SetUpTestCase()
  17. {
  18. }
  19. static void TearDownTestCase()
  20. {
  21. }
  22. virtual void SetUp() //TEST跑之前会执行SetUp
  23. {
  24. }
  25. virtual void TearDown() //TEST跑完之后会执行TearDown
  26. {
  27. }
  28. void startServer(MyHttpServer &server, TC_EpollServer::SERVER_OPEN_COROUTINE openCoroutine)
  29. {
  30. #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
  31. TC_Common::ignorePipe();
  32. #endif
  33. server.initialize();
  34. server._epollServer->setOpenCoroutine(openCoroutine);
  35. server.bindHttp("tcp -h 0.0.0.0 -p 18077 -t 60000");
  36. #if TARS_SSL
  37. server.bindHttpSSL("ssl -h 0.0.0.0 -p 18080 -t 60000");
  38. server.bindHttpSSLVerify("ssl -h 0.0.0.0 -p 18180 -t 60000");
  39. #endif
  40. server.waitForShutdown();
  41. }
  42. void stopServer(MyHttpServer &server)
  43. {
  44. server.terminate();
  45. }
  46. };
  47. class AsyncHttpCallback : public TC_HttpAsync::RequestCallback
  48. {
  49. public:
  50. AsyncHttpCallback(const string &sUrl) : _sUrl(sUrl)
  51. {
  52. }
  53. virtual bool onContinue(TC_HttpResponse &stHttpResponse) { return true; }
  54. virtual void onSucc(TC_HttpResponse &stHttpResponse)
  55. {
  56. _rsp = stHttpResponse;
  57. }
  58. virtual void onFailed(FAILED_CODE ret, const string &info)
  59. {
  60. LOG_CONSOLE_DEBUG << "onFailed, code:" << ret << ", info:" << info << "," << _sUrl << endl;
  61. }
  62. virtual void onClose()
  63. {
  64. // LOG_CONSOLE_DEBUG << "onClose:" << _sUrl << endl;
  65. }
  66. public:
  67. string _sUrl;
  68. TC_HttpResponse _rsp;
  69. };
  70. class DownloadCallback : public TC_HttpAsync::RequestCallback
  71. {
  72. public:
  73. DownloadCallback()
  74. : _fp(NULL)
  75. {
  76. }
  77. virtual void onSucc(TC_HttpResponse &stHttpResponse)
  78. {
  79. if (_fp)
  80. {
  81. fclose(_fp);
  82. _fp = NULL;
  83. }
  84. _md5 = TC_MD5::md5file("winrar");
  85. // LOG_CONSOLE_DEBUG << "onSucc:" << TC_File::getFileSize("winrar") << endl;
  86. };
  87. virtual bool onContinue(TC_HttpResponse &stHttpResponse)
  88. {
  89. // LOG_CONSOLE_DEBUG << "recv length:" << stHttpResponse.getContent().length() << endl;
  90. if (_fp == NULL)
  91. {
  92. string file = "winrar";
  93. _fp = fopen(file.c_str(), "wb");
  94. }
  95. string content = stHttpResponse.getContent();
  96. fwrite(content.c_str(), 1, content.length(), _fp);
  97. stHttpResponse.clearContent();
  98. return true;
  99. }
  100. virtual void onFailed(FAILED_CODE ret, const string &info) {
  101. // cout << "onFailed:" << ret << ", " << info << endl;
  102. };
  103. virtual void onClose()
  104. {
  105. if (_fp)
  106. {
  107. fclose(_fp);
  108. _fp = NULL;
  109. }
  110. }
  111. string _md5;
  112. FILE *_fp;
  113. };
  114. TEST_F(UtilHttpAsyncTest, testSync)
  115. {
  116. int type = 2;
  117. for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
  118. {
  119. MyHttpServer server;
  120. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
  121. int i = 100;
  122. while (i-- > 0)
  123. {
  124. string url = "http://127.0.0.1:18077";
  125. TC_HttpRequest stHttpReq;
  126. stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
  127. stHttpReq.setHeader("Connection", "Close");
  128. stHttpReq.setAcceptEncoding("gzip, deflate, br");
  129. stHttpReq.setPostRequest(url, "test info");
  130. string sSendBuffer = stHttpReq.encode();
  131. // LOG_CONSOLE_DEBUG << sSendBuffer << endl;
  132. TC_HttpResponse stHttpRsp;
  133. int iRet = stHttpReq.doRequest(stHttpRsp, 5000);
  134. ASSERT_TRUE(iRet == 0);
  135. ASSERT_TRUE(stHttpRsp.getContent() == stHttpReq.getContent());
  136. }
  137. stopServer(server);
  138. }
  139. }
  140. TEST_F(UtilHttpAsyncTest, testAsync)
  141. {
  142. int type = 2;
  143. for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
  144. {
  145. MyHttpServer server;
  146. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
  147. string url = "http://127.0.0.1:18077";
  148. TC_HttpRequest stHttpReq;
  149. stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
  150. stHttpReq.setHeader("Connection", "Close");
  151. stHttpReq.setAcceptEncoding("gzip, deflate, br");
  152. stHttpReq.setPostRequest(url, string("test info"), true);
  153. TC_HttpAsync ast;
  154. ast.setTimeout(10000);
  155. ast.start();
  156. int i = 100;
  157. while (i-- > 0)
  158. {
  159. AsyncHttpCallback *callback = new AsyncHttpCallback(url);
  160. TC_HttpAsync::RequestCallbackPtr p(callback);
  161. ast.doAsyncRequest(stHttpReq, p);
  162. TC_Common::msleep(50);
  163. ASSERT_TRUE(callback->_rsp.getContent() == stHttpReq.getContent());
  164. }
  165. ast.waitForAllDone();
  166. stopServer(server);
  167. }
  168. }
  169. TEST_F(UtilHttpAsyncTest, testDownload)
  170. {
  171. int type = 2;
  172. for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++) {
  173. MyHttpServer server;
  174. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
  175. TC_HttpAsync ast;
  176. ast.setTimeout(10000);
  177. ast.start();
  178. string url = "http://cdn.tarsyun.com/src/release-1.10.0.tar.gz";
  179. TC_HttpRequest stHttpReq;
  180. stHttpReq.setGetRequest(url);
  181. DownloadCallback *callback = new DownloadCallback();
  182. TC_HttpAsync::RequestCallbackPtr p(callback);
  183. ast.doAsyncRequest(stHttpReq, p);
  184. ast.waitForAllDone();
  185. ASSERT_TRUE(callback->_md5 == "ecd1fa65e7de707cd5c00bdac56022cd");
  186. stopServer(server);
  187. }
  188. }
  189. #if TARS_SSL
  190. TEST_F(UtilHttpAsyncTest, testAsyncSSL)
  191. {
  192. int type = 2;
  193. for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
  194. {
  195. MyHttpServer server;
  196. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
  197. string url = "https://127.0.0.1:18080";
  198. TC_HttpRequest stHttpReq;
  199. stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
  200. stHttpReq.setHeader("Connection", "Close");
  201. stHttpReq.setAcceptEncoding("gzip, deflate, br");
  202. stHttpReq.setPostRequest(url, string("test info"), true);
  203. TC_HttpAsync ast;
  204. ast.setTimeout(10000);
  205. ast.start();
  206. int i = 100;
  207. while (i-- > 0)
  208. {
  209. AsyncHttpCallback *callback = new AsyncHttpCallback(url);
  210. TC_HttpAsync::RequestCallbackPtr p(callback);
  211. ast.doAsyncRequest(stHttpReq, p);
  212. TC_Common::msleep(50);
  213. ASSERT_TRUE(callback->_rsp.getContent() == stHttpReq.getContent());
  214. }
  215. ast.waitForAllDone();
  216. stopServer(server);
  217. }
  218. }
  219. TEST_F(UtilHttpAsyncTest, testAsyncSSLCtxVerify)
  220. {
  221. string ciphers = "";
  222. bool verifyClient = true;
  223. shared_ptr<TC_OpenSSL::CTX> ctx = TC_OpenSSL::newCtx(SERVER_CERT, CLIENT_CERT, CLIENT_KEY, verifyClient, ciphers);
  224. int type = 2;
  225. for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
  226. {
  227. MyHttpServer server;
  228. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
  229. string url = "https://127.0.0.1:18180";
  230. TC_HttpRequest stHttpReq;
  231. stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
  232. stHttpReq.setHeader("Connection", "Close");
  233. stHttpReq.setAcceptEncoding("gzip, deflate, br");
  234. stHttpReq.setPostRequest(url, string("test info"), true);
  235. TC_HttpAsync ast;
  236. ast.setCtx(ctx);
  237. ast.setTimeout(10000);
  238. ast.start();
  239. int i = 100;
  240. while (i-- > 0)
  241. {
  242. AsyncHttpCallback *callback = new AsyncHttpCallback(url);
  243. TC_HttpAsync::RequestCallbackPtr p(callback);
  244. ast.doAsyncRequest(stHttpReq, p);
  245. TC_Common::msleep(50);
  246. ASSERT_TRUE(callback->_rsp.getContent() == stHttpReq.getContent());
  247. }
  248. ast.waitForAllDone();
  249. stopServer(server);
  250. }
  251. }
  252. TEST_F(UtilHttpAsyncTest, testAsyncSSLCtx)
  253. {
  254. bool verifyClient = false;
  255. string ciphers = "";
  256. shared_ptr<TC_OpenSSL::CTX> ctx = TC_OpenSSL::newCtx(SERVER_CERT, "", "", verifyClient, ciphers);
  257. int type = 2;
  258. for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
  259. {
  260. MyHttpServer server;
  261. startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
  262. string url = "https://127.0.0.1:18080";
  263. TC_HttpRequest stHttpReq;
  264. stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
  265. stHttpReq.setHeader("Connection", "Close");
  266. stHttpReq.setAcceptEncoding("gzip, deflate, br");
  267. stHttpReq.setPostRequest(url, string("test info"), true);
  268. TC_HttpAsync ast;
  269. ast.setCtx(ctx);
  270. ast.setTimeout(10000);
  271. ast.start();
  272. int i = 100;
  273. while (i-- > 0)
  274. {
  275. AsyncHttpCallback *callback = new AsyncHttpCallback(url);
  276. TC_HttpAsync::RequestCallbackPtr p(callback);
  277. ast.doAsyncRequest(stHttpReq, p);
  278. TC_Common::msleep(50);
  279. ASSERT_TRUE(callback->_rsp.getContent() == stHttpReq.getContent());
  280. }
  281. ast.waitForAllDone();
  282. stopServer(server);
  283. }
  284. }
  285. TEST_F(UtilHttpAsyncTest, testRemoteAsyncSSL)
  286. {
  287. string url = "https://www.qq.com";
  288. TC_HttpRequest stHttpReq;
  289. stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
  290. stHttpReq.setHeader("Connection", "Close");
  291. stHttpReq.setAcceptEncoding("gzip, deflate, br");
  292. stHttpReq.setGetRequest(url);
  293. TC_HttpAsync ast;
  294. ast.setTimeout(10000);
  295. ast.start();
  296. int i = 100;
  297. while (i-- > 0)
  298. {
  299. AsyncHttpCallback *callback = new AsyncHttpCallback(url);
  300. TC_HttpAsync::RequestCallbackPtr p(callback);
  301. ast.doAsyncRequest(stHttpReq, p);
  302. TC_Common::msleep(50);
  303. ASSERT_TRUE(callback->_rsp.getStatus() == 200);
  304. }
  305. ast.waitForAllDone();
  306. }
  307. #endif