123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855 |
- //
- // Created by jarod on 2020/2/20.
- //
- #include "util/tc_http.h"
- #include "util/tc_common.h"
- #include "util/tc_file.h"
- #include "gtest/gtest.h"
- using namespace tars;
- class HttpTest : public testing::Test
- {
- public:
- //添加日志
- static void SetUpTestCase()
- {
- cout<<"SetUpTestCase"<<endl;
- }
- static void TearDownTestCase()
- {
- cout<<"TearDownTestCase"<<endl;
- }
- virtual void SetUp() //TEST跑之前会执行SetUp
- {
- cout<<"SetUp"<<endl;
- }
- virtual void TearDown() //TEST跑完之后会执行TearDown
- {
- cout<<"TearDown"<<endl;
- }
- };
- TEST_F(HttpTest, testCheckRequestURL) //此时使用的是TEST_F宏
- {
- string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
- +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
- +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
- +string("Accept-Encoding: gzip\r\n")
- +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
- +string("Connection: close\r\n")
- +string("Host: www.qq.com\r\n")
- +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
- +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
- +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- TC_HttpRequest req;
- ASSERT_TRUE(req.decode(s));
- ASSERT_TRUE(req.getRequestUrl() == "/a/b");
- ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
- }
- TEST_F(HttpTest, testHttp) //此时使用的是TEST_F宏
- {
- TC_HttpRequest stHttpReq;
- stHttpReq.setCacheControl("no-cache");
- stHttpReq.setGetRequest("http://118.89.197.217/", true);
- TC_HttpResponse stHttpRsp;
- int ret = stHttpReq.doRequest(stHttpRsp, 3000);
- cout << ret << ":" << stHttpRsp.getContent() << endl;
- cout << "\n\n==============================\n" << stHttpRsp.getContent().size() << endl;
- cout << stHttpRsp.genHeader() << endl;
- }
- TEST_F(HttpTest, testEncodeString) //此时使用的是TEST_F宏
- {
- string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
- +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
- +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
- +string("Accept-Encoding: gzip\r\n")
- +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
- +string("Connection: close\r\n")
- +string("Host: www.qq.com\r\n")
- +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
- +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
- +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- // s += string("a", 1024);
- TC_HttpRequest req;
- req.decode(s);
- int64_t t = TC_Common::now2us();
- int count = 100000;
- int i = 0;
- while(++i<count) {
- string s;
- s = req.encode();
- }
- ASSERT_TRUE(req.decode(s));
- ASSERT_TRUE(req.getRequestUrl() == "/a/b");
- ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
- cout << "testEncodeString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
- }
- TEST_F(HttpTest, testEncodeVector) //此时使用的是TEST_F宏
- {
- string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
- +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
- +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
- +string("Accept-Encoding: gzip\r\n")
- +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
- +string("Connection: close\r\n")
- +string("Host: www.qq.com\r\n")
- +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
- +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
- +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- s += string("a", 1024);
- TC_HttpRequest req;
- req.decode(s);
- int64_t t = TC_Common::now2us();
- int count = 100000;
- int i = 0;
- while(++i<count) {
- vector<char> buff;
- req.encode(buff);
- }
- cout << "testEncodeVector::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
- }
- TEST_F(HttpTest, testEncodeBuffString) //此时使用的是TEST_F宏
- {
- string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\\r\n")
- +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
- +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
- +string("Accept-Encoding: gzip\r\n")
- +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
- +string("Connection: close\r\n")
- +string("Host: www.qq.com\r\n")
- +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
- +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
- +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- int64_t t = TC_Common::now2us();
- TC_HttpRequest req;
- req.decode(s);
- TC_NetWorkBuffer buff(NULL);
- int count = 100000;
- int i = 0;
- while(++i<count) {
- req.encode(buff);
- }
- cout << "testEncodeBuffString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
- }
- TEST_F(HttpTest, testDecodeString) //此时使用的是TEST_F宏
- {
- string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
- +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
- +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
- +string("Accept-Encoding: gzip\r\n")
- +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
- +string("Connection: close\r\n")
- +string("Host: www.qq.com\r\n")
- +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
- +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
- +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- int64_t t = TC_Common::now2us();
- int count = 100000;
- int i = 0;
- while(++i<count) {
- TC_HttpRequest req;
- req.decode(s);
- }
- cout << "testDecodeString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
- }
- TEST_F(HttpTest, testDecodeBuffString) //此时使用的是TEST_F宏
- {
- string s = string("HTTP/1.1 200 OK\r\n")
- +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
- +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
- +string("Accept-Encoding: gzip\r\n")
- +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
- +string("Connection: close\r\n")
- +string("Host: www.qq.com\r\n")
- +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
- +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
- +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- int count = 100000;
- int i = 0;
- vector<TC_NetWorkBuffer> vbuff;
- while(i<count) {
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s);
- vbuff.push_back(buff);
- ++i;
- }
- int64_t t = TC_Common::now2us();
- i = 0;
- while(i<count) {
- TC_HttpResponse req;
- ASSERT_TRUE(req.incrementDecode(vbuff[i]));
- ++i;
- }
- cout << "testDecodeBuffString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
- }
- TEST_F(HttpTest, testCheckRequestString) //此时使用的是TEST_F宏
- {
- string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
- +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
- +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
- +string("Accept-Encoding: gzip\r\n")
- +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
- +string("Connection: close\r\n")
- +string("Host: www.qq.com\r\n")
- +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
- +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
- +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- int64_t t = TC_Common::now2us();
- TC_HttpRequest req;
- int count = 100000;
- int i = 0;
- while(++i<count) {
- ASSERT_TRUE(req.checkRequest(s.c_str(), s.size()));
- }
- ASSERT_TRUE(req.decode(s));
- ASSERT_TRUE(req.getRequestUrl() == "/a/b");
- cout << req.getURL().getDomain() << endl;
- ASSERT_TRUE(req.getURL().getDomain() == "www.qq.com");
- cout << "testCheckRequestString::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
- }
- TEST_F(HttpTest, testCheckRequestBuff) //此时使用的是TEST_F宏
- {
- string s = string("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n")
- +string("Accept: application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5\r\n")
- +string("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n")
- +string("Accept-Encoding: gzip\r\n")
- +string("Accept-Language: zh-cn,zh;q=0.5\r\n")
- +string("Connection: close\r\n")
- +string("Host: www.qq.com\r\n")
- +string("Q-GUID: 08f0373a192a45778cc8567d1c641475\r\n")
- +string("Q-UA: SQB12_GA/120450&SMTT_3/020100&SYM3&201514&E71&V2\r\n")
- +string("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- int64_t t = TC_Common::now2us();
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- TC_HttpRequest req;
- int count = 100000;
- int i = 0;
- while(++i<count) {
- ASSERT_TRUE(req.checkRequest(buff));
- }
- cout << "testCheckRequestBuff::cost: " << TC_Common::now2us() - t << "us, " << 1.*(TC_Common::now2us() - t)/count << "us" << endl;
- }
- TEST_F(HttpTest, testHttpFinish) //此时使用的是TEST_F宏
- {
- string body = "abdefghigk";
- vector<string> sbuff;
- sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
- 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");
- sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
- sbuff.push_back("Accept-Encoding: gzip\r\n");
- sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
- sbuff.push_back("Connection: close\r\n");
- sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- string header;
- for(auto s : sbuff)
- {
- header += s;
- }
- string s = header + body;
- TC_HttpRequest request;
- ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_FULL);
- }
- TEST_F(HttpTest, testHttpFinishNoLength) //此时使用的是TEST_F宏
- {
- vector<string> sbuff;
- sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
- 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");
- sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
- sbuff.push_back("Accept-Encoding: gzip\r\n");
- sbuff.push_back("Connection: close\r\n");
- sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- string header;
- for(auto s : sbuff)
- {
- header += s;
- }
- string s = header;
- TC_HttpRequest request;
- ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_FULL);
- }
- TEST_F(HttpTest, testHttpNoFinish) //此时使用的是TEST_F宏
- {
- string body = "abdefghigk";
- vector<string> sbuff;
- sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
- 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");
- sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
- sbuff.push_back("Accept-Encoding: gzip\r\n");
- sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size() + 1) + "\r\n");
- sbuff.push_back("Connection: close\r\n");
- sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- string header;
- for(auto s : sbuff)
- {
- header += s;
- }
- string s = header + body;
- TC_HttpRequest request;
- ASSERT_TRUE(!request.checkRequest(s.c_str(), s.size()));
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(buff.checkHttp() == TC_NetWorkBuffer::PACKET_LESS);
- }
- TEST_F(HttpTest, testHttpRequestChunked) //此时使用的是TEST_F宏
- {
- vector<string> body;
- body.push_back("abasdfadefghiadfagk1");
- body.push_back("abdasdfadfaefghigk2");
- body.push_back("abdsaefghigk3");
- body.push_back("abdeasdfafasfasfasfasdfasffghigk4");
- vector<string> sbuff;
- sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
- 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");
- sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
- sbuff.push_back("Accept-Encoding: gzip\r\n");
- sbuff.push_back("Transfer-Encoding: chunked\r\n");
- sbuff.push_back("Connection: close\r\n");
- sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- stringstream data;
- for(auto s : sbuff)
- {
- data << s;
- }
- string sbody;
- for(auto s : body)
- {
- sbody += s;
- data << hex << s.size() << "\r\n" << s << "\r\n";
- }
- data << 0 << "\r\n\r\n";
- string s = data.str();
- TC_HttpRequest request;
- ASSERT_TRUE(request.checkRequest(s.c_str(), s.size()));
- ASSERT_TRUE(request.decode(s));
- ASSERT_TRUE(request.getContent() == sbody);
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- request.reset();
- ASSERT_TRUE(request.checkRequest(buff));
- }
- TEST_F(HttpTest, testHttpRequestChunkedNoFinish) //此时使用的是TEST_F宏
- {
- vector<string> body;
- body.push_back("abasdfadefghiadfagk1");
- body.push_back("abdasdfadfaefghigk2");
- body.push_back("abdsaefghigk3");
- body.push_back("abdeasdfafasfasfasfasdfasffghigk4");
- vector<string> sbuff;
- sbuff.push_back("GET /a/b?name=value&ccc=ddd HTTP/1.1\r\n");
- 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");
- sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
- sbuff.push_back("Accept-Encoding: gzip\r\n");
- sbuff.push_back("Transfer-Encoding: chunked\r\n");
- sbuff.push_back("Connection: close\r\n");
- sbuff.push_back("User-Agent: E71/SymbianOS/9.1 Series60/3.0\r\n\r\n");
- stringstream data;
- for(auto s : sbuff)
- {
- data << s;
- }
- data << hex << body[0].size() << "\r\n" << body[0] << "\r\n";
- data << hex << body[1].size() << "\r\n" << body[1] << "\r\n";
- data << hex << body[2].size() ;
- string s = data.str();
- TC_HttpRequest request;
- ASSERT_TRUE(!request.checkRequest(s.c_str(), s.size()));
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(!request.checkRequest(buff));
- }
- TEST_F(HttpTest, testHttpResponse) //此时使用的是TEST_F宏
- {
- string body = "abcdef";
- vector<string> sbuff;
- sbuff.push_back("HTTP/1.1 200 OK\r\n");
- sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
- sbuff.push_back("\r\n");
- string header;
- for(auto s : sbuff)
- {
- header += s;
- }
- string s = header + body;
- {
- TC_HttpResponse response;
- ASSERT_TRUE(response.decode(s));
- ASSERT_TRUE(response.getContent() == body);
- }
- {
- TC_HttpResponse response;
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(response.incrementDecode(buff));
- ASSERT_TRUE(response.getContent() == body);
- }
- }
- TEST_F(HttpTest, testHttpResponseNoFinish) //此时使用的是TEST_F宏
- {
- string body = "abcdef";
- vector<string> sbuff;
- sbuff.push_back("HTTP/1.1 200 OK\r\n");
- sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size() + 1) + "\r\n");
- sbuff.push_back("\r\n");
- string header;
- for(auto s : sbuff)
- {
- header += s;
- }
- string s = header + body;
- {
- TC_HttpResponse response;
- ASSERT_TRUE(!response.decode(s));
- }
- {
- TC_HttpResponse response;
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(!response.incrementDecode(buff));
- }
- }
- TEST_F(HttpTest, testHttpResponseNoLength) //此时使用的是TEST_F宏
- {
- vector<string> sbuff;
- sbuff.push_back("HTTP/1.1 200 OK\r\n");
- sbuff.push_back("Connection: close\r\n");
- sbuff.push_back("\r\n");
- string header;
- for(auto s : sbuff)
- {
- header += s;
- }
- string s = header;
- {
- TC_HttpResponse response;
- ASSERT_TRUE(response.decode(s));
- }
- {
- TC_HttpResponse response;
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(response.incrementDecode(buff));
- }
- }
- TEST_F(HttpTest, testHttpResponseIncrementFinish) //此时使用的是TEST_F宏
- {
- string body = "abcdeasdfadfsff";
- vector<string> sbuff;
- sbuff.push_back("HTTP/1.1 200 OK\r\n");
- sbuff.push_back("Content-Length: " + TC_Common::tostr(body.size()) + "\r\n");
- sbuff.push_back("\r\n");
- string header;
- for(auto s : sbuff)
- {
- header += s;
- }
- string s = header;
- {
- TC_HttpResponse response;
- ASSERT_TRUE(!response.decode(s));
- }
- {
- TC_HttpResponse response;
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(!response.incrementDecode(buff));
- }
- s = header + body;
- {
- TC_HttpResponse response;
- ASSERT_TRUE(response.decode(s));
- }
- {
- TC_HttpResponse response;
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(response.incrementDecode(buff));
- }
- }
- TEST_F(HttpTest, testHttpResponseChunked) //此时使用的是TEST_F宏
- {
- vector<string> body;
- body.push_back("abdefghiasdfasdfsadfsadfsagk1");
- body.push_back("abdefghasdfaaigk2");
- body.push_back("abdefghigadsfadsfk3");
- body.push_back("abdefgsfagasasdfasfdfdfsdfsfsdfdsffsdfsdfhigk4");
- vector<string> sbuff;
- sbuff.push_back("HTTP/1.1 200 OK\r\n");
- 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");
- sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
- sbuff.push_back("Transfer-Encoding: chunked\r\n");
- sbuff.push_back("Connection: close\r\n\r\n");
- stringstream data;
- for(auto s : sbuff)
- {
- data << s;
- }
- string sbody;
- for(auto s : body)
- {
- sbody += s;
- data << hex << s.size() << "\r\n" << s << "\r\n";
- }
- data << 0 << "\r\n\r\n";
- string s = data.str();
- {
- TC_HttpResponse response;
- ASSERT_TRUE(response.decode(s));
- ASSERT_TRUE(response.getContent() == sbody);
- }
- {
- TC_HttpResponse response;
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(response.incrementDecode(buff));
- ASSERT_TRUE(response.getContent() == sbody);
- }
- }
- TEST_F(HttpTest, testHttpResponseChunkedNoFinish) //此时使用的是TEST_F宏
- {
- vector<string> body;
- body.push_back("abdefasdfasfasghigk1");
- body.push_back("asdfaabdeafghigk2");
- body.push_back("abasdfasdfasdfasdfasdfasdfasfasdefghigk3");
- body.push_back("abdefgfasdfasdfasdfasdfadfigk4");
- vector<string> sbuff;
- sbuff.push_back("HTTP/1.1 200 OK\r\n");
- 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");
- sbuff.push_back("Accept-Charset: utf-8,gb2321;q=0.7,*;q=0.7\r\n");
- sbuff.push_back("Transfer-Encoding: chunked\r\n");
- sbuff.push_back("Connection: close\r\n\r\n");
- stringstream data;
- for(auto s : sbuff)
- {
- data << s;
- }
- data << hex << body[0].size() << "\r\n" << body[0] << "\r\n";
- data << hex << body[1].size() << "\r\n" << body[1] << "\r\n";
- data << hex << body[2].size() << "\r\n" << "abc";
- string s = data.str();
- {
- TC_HttpResponse response;
- ASSERT_TRUE(!response.decode(s));
- }
- {
- TC_HttpResponse response;
- TC_NetWorkBuffer buff(NULL);
- buff.addBuffer(s.c_str(), s.size());
- ASSERT_TRUE(!response.incrementDecode(buff));
- }
- }
- TEST_F(HttpTest, testWeb) //此时使用的是TEST_F宏
- {
- string url = "www.qq.com";
- TC_HttpRequest stHttpReq;
- // stHttpReq.setCacheControl("no-cache");
- // 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");
- stHttpReq.setUserAgent("E71/SymbianOS/9.1 Series60/3.0");
- stHttpReq.setHeader("Connection", "Close");
- stHttpReq.setAcceptEncoding("gzip, deflate, br");
- stHttpReq.setGetRequest(url);
- string sSendBuffer = stHttpReq.encode();
- TC_HttpResponse stHttpRsp;
- int iRet = stHttpReq.doRequest(stHttpRsp, 5000);
- if(iRet != 0)
- {
- cout << iRet << endl;
- }
- string file = stHttpReq.getURL().getDomain() + ".html";
- auto headers = stHttpRsp.getHeaders();
- cout << "request:" << url << endl;
- cout << TC_Common::tostr(headers.begin(), headers.end(), "\r\n") << endl;
- TC_File::save2file(file, stHttpRsp.getContent());
-
- // ASSERT_TRUE(stHttpRsp.getContentLength() == TC_File::getFileSize(file));
- }
- void testCookie(const string &sRspURL, const string &sReqURL, const vector<string> &vsCookie)
- {
- cout << sRspURL << "=>" << sReqURL << "-----------------------------------" << endl;
- TC_HttpCookie cookie;
- cookie.addCookie(sRspURL, vsCookie);
- list<TC_HttpCookie::Cookie> vCookie = cookie.getAllCookie();
- cout << "All Cookie:" << sRspURL << "-----------------------------------" << endl;
- list<TC_HttpCookie::Cookie>::iterator it = vCookie.begin();
- while(it != vCookie.end())
- {
- cout << TC_Common::tostr(it->_data.begin(), it->_data.end(), "; ") << ", " << it->_expires << ", " << it->_path << endl;
- ++it;
- }
- cout << "-----------------------------------" << endl << endl;
- string sCookie;
- cookie.getCookieForURL(sReqURL, sCookie);
- cout << TC_Common::tostr(sCookie) << endl;
- cout << "-----------------------------------" << endl << endl;
- }
- TEST_F(HttpTest, testCookie) //此时使用的是TEST_F宏
- {
- ASSERT_TRUE(TC_HttpCookie::matchDomain("qq.com", "www.qq.com") == true);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "www.qq.com") == true);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "qq.com") == true);
- ASSERT_TRUE(TC_HttpCookie::matchDomain("t.qq.com", "www.qq.com") == false);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".t.qq.com", "www.qq.com") == false);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".t.qq.com", "t.qq.com") == true);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".com", "www.qq.com") == false);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".com", "qq.com") == false);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".y.qq.com", "x.y.qq.com") == true);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".x.y.qq.com", "x.y.qq.com") == true);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "x.y.qq.com") == true);
- ASSERT_TRUE(TC_HttpCookie::matchDomain(".qq.com", "y.qq.com") == true);
- ASSERT_TRUE(TC_HttpCookie::matchDomain("qq.com", "y.qq.com") == true);
- cout << TC_Common::now2GMTstr() << endl;
- string gmt = TC_Common::tm2GMTstr(time(NULL) + 10);
- string s = "HTTP/1.1 200 OK\r\n";// 200 Aouut Error\r\n";
- s += "Set-Cookie: n1=1; a=1; c=d; Path=/; Domain=qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n2=2; a=0; c=d; Path=/abc/def; Domain=.qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n3=3; a=5; c=d; Path=/abc/def/aaa; Domain=.qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n4=4; a=6; c=d; Path=/abc; Domain=.qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n5=5; a=2; c=d; Path=/; Domain=.qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n6=6; c=3; Path=/; Domain=y.qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n7=7; c=3; Path=/abc; Domain=.y.qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n8=6; c=3; Path=/; Domain=x.y.qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n9=7; c=4; Path=/; Domain=.x.y.qq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n10=7; c=4; Path=/; Domain=qqq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n11=7; c=4; Path=/; Domain=.qqq.com; Expires=" + gmt + "\r\n";
- s += "Set-Cookie: n12=8; c=4; Expires=" + gmt + "\r\n";
- s += "Accept-Ranges: bytes\r\n\r\n";
- TC_HttpResponse rsp;
- rsp.decode(s);
- cout << "-----------------------------------" << endl;
- vector<string> vsCookie = rsp.getSetCookie();
- cout << TC_Common::tostr(vsCookie.begin(), vsCookie.end(), "\r\n") << endl << endl;
- testCookie("http://www.qq.com", "http://www.qq.com", vsCookie);
- testCookie("http://www.qq.com/abc/def", "http://www.qq.com", vsCookie);
- testCookie("http://www.qq.com/abc/def", "http://www.qq.com/abc", vsCookie);
- cout << endl;
- testCookie("http://www.qq.com", "http://qq.com", vsCookie);
- testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com/abc/def/aaa", vsCookie);
- testCookie("http://www.qq.com/abc/def/aaa", "http://www.qq.com", vsCookie);
- testCookie("http://www.qq.com", "http://www.qq.com/abc/def", vsCookie);
- testCookie("http://qq.com", "http://qq.com/abc/def", vsCookie);
- testCookie("http://qq.com", "http://t.qq.com/abc/def", vsCookie);
- testCookie("http://qq.com", "http://y.qq.com/", vsCookie);
- testCookie("http://qq.com", "http://y.qq.com/abc", vsCookie);
- testCookie("http://x.y.qq.com", "http://x.y.qq.com", vsCookie);
- }
- //
- //void Test_TC_Http::main()
- //{
- // testCheckRequestURL();
- //
- // testEncodeString();
- // testEncodeVector();
- // testEncodeBuffString();
- //
- // testDecodeString();
- //
- // testDecodeBuffString();
- //
- // testCheckRequestString();
- // testCheckRequestBuff();
- //
- // testHttpFinishNoLength();
- //
- // testHttpFinish();
- //
- // testHttpNoFinish();
- //
- // testHttpRequestChunked();
- //
- // testHttpResponse();
- //
- // testHttpResponseNoFinish();
- //
- // testHttpResponseNoLength();
- //
- // testHttpResponseIncrementFinish();
- //
- // testHttpResponseChunked();
- //
- // testHttpRequestChunkedNoFinish();
- //
- // testHttpResponseChunkedNoFinish();
- //
- //// testWeb("http://www.baidu.com");
- //// testWeb("http://www.httpwatch.com/httpgallery/chunked/chunkedimage.aspx");
- //// testWeb("http://www.qq.com");
- //
- //// testCookie();
- //}
|