123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 |
- //
- // Created by jarod on 2020/2/20.
- //
- #include "util/tc_http.h"
- #include "util/tc_common.h"
- #include "util/tc_http_async.h"
- #include "util/tc_md5.h"
- #include "test_server.h"
- #include "gtest/gtest.h"
- #include "certs.h"
- using namespace tars;
- class UtilHttpAsyncTest : public testing::Test
- {
- public:
- //添加日志
- static void SetUpTestCase()
- {
- }
- static void TearDownTestCase()
- {
- }
- virtual void SetUp() //TEST跑之前会执行SetUp
- {
- }
- virtual void TearDown() //TEST跑完之后会执行TearDown
- {
- }
- void startServer(MyHttpServer &server, TC_EpollServer::SERVER_OPEN_COROUTINE openCoroutine)
- {
- #if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
- TC_Common::ignorePipe();
- #endif
- server.initialize();
- server._epollServer->setOpenCoroutine(openCoroutine);
- server.bindHttp("tcp -h 0.0.0.0 -p 18077 -t 60000");
- #if TARS_SSL
- server.bindHttpSSL("ssl -h 0.0.0.0 -p 18080 -t 60000");
- server.bindHttpSSLVerify("ssl -h 0.0.0.0 -p 18180 -t 60000");
- #endif
- server.waitForShutdown();
- }
- void stopServer(MyHttpServer &server)
- {
- server.terminate();
- }
- };
- class AsyncHttpCallback : public TC_HttpAsync::RequestCallback
- {
- public:
- AsyncHttpCallback(const string &sUrl) : _sUrl(sUrl)
- {
- }
- virtual bool onContinue(TC_HttpResponse &stHttpResponse) { return true; }
- virtual void onSucc(TC_HttpResponse &stHttpResponse)
- {
- _rsp = stHttpResponse;
- }
- virtual void onFailed(FAILED_CODE ret, const string &info)
- {
- LOG_CONSOLE_DEBUG << "onFailed, code:" << ret << ", info:" << info << "," << _sUrl << endl;
- }
- virtual void onClose()
- {
- // LOG_CONSOLE_DEBUG << "onClose:" << _sUrl << endl;
- }
- public:
- string _sUrl;
- TC_HttpResponse _rsp;
- };
- class DownloadCallback : public TC_HttpAsync::RequestCallback
- {
- public:
- DownloadCallback()
- : _fp(NULL)
- {
- }
- virtual void onSucc(TC_HttpResponse &stHttpResponse)
- {
- if (_fp)
- {
- fclose(_fp);
- _fp = NULL;
- }
- _md5 = TC_MD5::md5file("winrar");
- // LOG_CONSOLE_DEBUG << "onSucc:" << TC_File::getFileSize("winrar") << endl;
- };
- virtual bool onContinue(TC_HttpResponse &stHttpResponse)
- {
- // LOG_CONSOLE_DEBUG << "recv length:" << stHttpResponse.getContent().length() << endl;
- if (_fp == NULL)
- {
- string file = "winrar";
- _fp = fopen(file.c_str(), "wb");
- }
- string content = stHttpResponse.getContent();
- fwrite(content.c_str(), 1, content.length(), _fp);
- stHttpResponse.clearContent();
- return true;
- }
- virtual void onFailed(FAILED_CODE ret, const string &info) {
- // cout << "onFailed:" << ret << ", " << info << endl;
- };
- virtual void onClose()
- {
- if (_fp)
- {
- fclose(_fp);
- _fp = NULL;
- }
- }
- string _md5;
- FILE *_fp;
- };
- TEST_F(UtilHttpAsyncTest, testSync)
- {
- int type = 2;
- for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
- {
- MyHttpServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
- int i = 100;
- while (i-- > 0)
- {
- string url = "http://127.0.0.1:18077";
- TC_HttpRequest stHttpReq;
- stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
- stHttpReq.setHeader("Connection", "Close");
- stHttpReq.setAcceptEncoding("gzip, deflate, br");
- stHttpReq.setPostRequest(url, "test info");
- string sSendBuffer = stHttpReq.encode();
- // LOG_CONSOLE_DEBUG << sSendBuffer << endl;
- TC_HttpResponse stHttpRsp;
- int iRet = stHttpReq.doRequest(stHttpRsp, 5000);
- ASSERT_TRUE(iRet == 0);
- ASSERT_TRUE(stHttpRsp.getContent() == stHttpReq.getContent());
- }
- stopServer(server);
- }
- }
- TEST_F(UtilHttpAsyncTest, testAsync)
- {
- int type = 2;
- for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
- {
- MyHttpServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
- string url = "http://127.0.0.1:18077";
- TC_HttpRequest stHttpReq;
- stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
- stHttpReq.setHeader("Connection", "Close");
- stHttpReq.setAcceptEncoding("gzip, deflate, br");
- stHttpReq.setPostRequest(url, string("test info"), true);
- TC_HttpAsync ast;
- ast.setTimeout(10000);
- ast.start();
- int i = 100;
- while (i-- > 0)
- {
- AsyncHttpCallback *callback = new AsyncHttpCallback(url);
- TC_HttpAsync::RequestCallbackPtr p(callback);
- ast.doAsyncRequest(stHttpReq, p);
- TC_Common::msleep(50);
- ASSERT_TRUE(callback->_rsp.getContent() == stHttpReq.getContent());
- }
- ast.waitForAllDone();
- stopServer(server);
- }
- }
- TEST_F(UtilHttpAsyncTest, testDownload)
- {
- int type = 2;
- for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++) {
- MyHttpServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
- TC_HttpAsync ast;
- ast.setTimeout(10000);
- ast.start();
- string url = "http://cdn.tarsyun.com/src/release-1.10.0.tar.gz";
- TC_HttpRequest stHttpReq;
- stHttpReq.setGetRequest(url);
- DownloadCallback *callback = new DownloadCallback();
- TC_HttpAsync::RequestCallbackPtr p(callback);
- ast.doAsyncRequest(stHttpReq, p);
- ast.waitForAllDone();
- ASSERT_TRUE(callback->_md5 == "ecd1fa65e7de707cd5c00bdac56022cd");
- stopServer(server);
- }
- }
- #if TARS_SSL
- TEST_F(UtilHttpAsyncTest, testAsyncSSL)
- {
- int type = 2;
- for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
- {
- MyHttpServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
- string url = "https://127.0.0.1:18080";
- TC_HttpRequest stHttpReq;
- stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
- stHttpReq.setHeader("Connection", "Close");
- stHttpReq.setAcceptEncoding("gzip, deflate, br");
- stHttpReq.setPostRequest(url, string("test info"), true);
- TC_HttpAsync ast;
- ast.setTimeout(10000);
- ast.start();
- int i = 100;
- while (i-- > 0)
- {
- AsyncHttpCallback *callback = new AsyncHttpCallback(url);
- TC_HttpAsync::RequestCallbackPtr p(callback);
- ast.doAsyncRequest(stHttpReq, p);
- TC_Common::msleep(50);
- ASSERT_TRUE(callback->_rsp.getContent() == stHttpReq.getContent());
- }
- ast.waitForAllDone();
- stopServer(server);
- }
- }
- TEST_F(UtilHttpAsyncTest, testAsyncSSLCtxVerify)
- {
- string ciphers = "";
- bool verifyClient = true;
- shared_ptr<TC_OpenSSL::CTX> ctx = TC_OpenSSL::newCtx(SERVER_CERT, CLIENT_CERT, CLIENT_KEY, verifyClient, ciphers);
- int type = 2;
- for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
- {
- MyHttpServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
- string url = "https://127.0.0.1:18180";
- TC_HttpRequest stHttpReq;
- stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
- stHttpReq.setHeader("Connection", "Close");
- stHttpReq.setAcceptEncoding("gzip, deflate, br");
- stHttpReq.setPostRequest(url, string("test info"), true);
- TC_HttpAsync ast;
- ast.setCtx(ctx);
- ast.setTimeout(10000);
- ast.start();
- int i = 100;
- while (i-- > 0)
- {
- AsyncHttpCallback *callback = new AsyncHttpCallback(url);
- TC_HttpAsync::RequestCallbackPtr p(callback);
- ast.doAsyncRequest(stHttpReq, p);
- TC_Common::msleep(50);
- ASSERT_TRUE(callback->_rsp.getContent() == stHttpReq.getContent());
- }
- ast.waitForAllDone();
- stopServer(server);
- }
- }
- TEST_F(UtilHttpAsyncTest, testAsyncSSLCtx)
- {
- bool verifyClient = false;
- string ciphers = "";
- shared_ptr<TC_OpenSSL::CTX> ctx = TC_OpenSSL::newCtx(SERVER_CERT, "", "", verifyClient, ciphers);
- int type = 2;
- for(type = 0; type <= TC_EpollServer::NET_THREAD_MERGE_HANDLES_CO; type++)
- {
- MyHttpServer server;
- startServer(server, (TC_EpollServer::SERVER_OPEN_COROUTINE) type);
- string url = "https://127.0.0.1:18080";
- TC_HttpRequest stHttpReq;
- stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
- stHttpReq.setHeader("Connection", "Close");
- stHttpReq.setAcceptEncoding("gzip, deflate, br");
- stHttpReq.setPostRequest(url, string("test info"), true);
- TC_HttpAsync ast;
- ast.setCtx(ctx);
- ast.setTimeout(10000);
- ast.start();
- int i = 100;
- while (i-- > 0)
- {
- AsyncHttpCallback *callback = new AsyncHttpCallback(url);
- TC_HttpAsync::RequestCallbackPtr p(callback);
- ast.doAsyncRequest(stHttpReq, p);
- TC_Common::msleep(50);
- ASSERT_TRUE(callback->_rsp.getContent() == stHttpReq.getContent());
- }
- ast.waitForAllDone();
- stopServer(server);
- }
- }
- TEST_F(UtilHttpAsyncTest, testRemoteAsyncSSL)
- {
- string url = "https://www.qq.com";
- TC_HttpRequest stHttpReq;
- stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
- stHttpReq.setHeader("Connection", "Close");
- stHttpReq.setAcceptEncoding("gzip, deflate, br");
- stHttpReq.setGetRequest(url);
- TC_HttpAsync ast;
- ast.setTimeout(10000);
- ast.start();
- int i = 100;
- while (i-- > 0)
- {
- AsyncHttpCallback *callback = new AsyncHttpCallback(url);
- TC_HttpAsync::RequestCallbackPtr p(callback);
- ast.doAsyncRequest(stHttpReq, p);
- TC_Common::msleep(50);
- ASSERT_TRUE(callback->_rsp.getStatus() == 200);
- }
- ast.waitForAllDone();
- }
- #endif
|