X-Git-Url: https://git.mxchange.org/?a=blobdiff_plain;f=simgear%2Fio%2Ftest_HTTP.cxx;h=43d660c3a7536bebd2f39f3b53338c320976a01b;hb=201cb61f842ef50a19438e3872ba22e588fa1afc;hp=1dce8d29e355f59451811a31e9c7ec5fcd7f7e8b;hpb=19119cc4ae078dfb648f624cb5cff18ad86186a5;p=simgear.git diff --git a/simgear/io/test_HTTP.cxx b/simgear/io/test_HTTP.cxx index 1dce8d29..43d660c3 100644 --- a/simgear/io/test_HTTP.cxx +++ b/simgear/io/test_HTTP.cxx @@ -22,6 +22,14 @@ using std::stringstream; using namespace simgear; const char* BODY1 = "The quick brown fox jumps over a lazy dog."; +const char* BODY3 = "Cras ut neque nulla. Duis ut velit neque, sit amet " +"pharetra risus. In est ligula, lacinia vitae congue in, sollicitudin at " +"libero. Mauris pharetra pretium elit, nec placerat dui semper et. Maecenas " +"magna magna, placerat sed luctus ac, commodo et ligula. Mauris at purus et " +"nisl molestie auctor placerat at quam. Donec sapien magna, venenatis sed " +"iaculis id, fringilla vel arcu. Duis sed neque nisi. Cras a arcu sit amet " +"risus ultrices varius. Integer sagittis euismod dui id varius. Cras vel " +"justo gravida metus."; const unsigned int body2Size = 8 * 1024; char body2[body2Size]; @@ -29,7 +37,7 @@ char body2[body2Size]; #define COMPARE(a, b) \ if ((a) != (b)) { \ cerr << "failed:" << #a << " != " << #b << endl; \ - cerr << "\tgot:" << a << endl; \ + cerr << "\tgot:'" << a << "'" << endl; \ exit(1); \ } @@ -43,16 +51,38 @@ class TestRequest : public HTTP::Request { public: bool complete; + bool failed; string bodyData; - TestRequest(const std::string& url) : - HTTP::Request(url), + TestRequest(const std::string& url, const std::string method = "GET") : + HTTP::Request(url, method), complete(false) { - } + std::map sendHeaders; + std::map headers; protected: + string_list requestHeaders() const + { + string_list r; + std::map::const_iterator it; + for (it = sendHeaders.begin(); it != sendHeaders.end(); ++it) { + r.push_back(it->first); + } + return r; + } + + string header(const string& name) const + { + std::map::const_iterator it = sendHeaders.find(name); + if (it == sendHeaders.end()) { + return string(); + } + + return it->second; + } + virtual void responseHeadersComplete() { } @@ -62,10 +92,21 @@ protected: complete = true; } + virtual void failure() + { + failed = true; + } + virtual void gotBodyData(const char* s, int n) { + //std::cout << "got body data:'" << string(s, n) << "'" < requestHeaders; + std::map args; + int requestContentLength; }; class TestServer : public NetChannel @@ -227,25 +416,40 @@ public: { simgear::IPAddress addr ; int handle = accept ( &addr ) ; - + //cout << "did accept from " << addr.getHost() << ":" << addr.getPort() << endl; TestServerChannel* chan = new TestServerChannel(); chan->setHandle(handle); } }; -void waitForComplete(TestRequest* tr) +void waitForComplete(HTTP::Client* cl, TestRequest* tr) { SGTimeStamp start(SGTimeStamp::now()); while (start.elapsedMSec() < 1000) { - NetChannel::poll(10); + cl->update(); if (tr->complete) { return; } + SGTimeStamp::sleepForMSec(1); } cerr << "timed out" << endl; } +void waitForFailed(HTTP::Client* cl, TestRequest* tr) +{ + SGTimeStamp start(SGTimeStamp::now()); + while (start.elapsedMSec() < 1000) { + cl->update(); + if (tr->failed) { + return; + } + SGTimeStamp::sleepForMSec(1); + } + + cerr << "timed out waiting for failure" << endl; +} + int main(int argc, char* argv[]) { TestServer s; @@ -273,12 +477,52 @@ int main(int argc, char* argv[]) HTTP::Request_ptr own(tr); cl.makeRequest(tr); - waitForComplete(tr); + waitForComplete(&cl, tr); COMPARE(tr->responseCode(), 200); - COMPARE(tr->contentLength(), strlen(BODY1)); + COMPARE(tr->responseReason(), string("OK")); + COMPARE(tr->responseLength(), strlen(BODY1)); + COMPARE(tr->responseBytesReceived(), strlen(BODY1)); COMPARE(tr->bodyData, string(BODY1)); } + + { + TestRequest* tr = new TestRequest("http://localhost:2000/testLorem"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 200); + COMPARE(tr->responseReason(), string("OK")); + COMPARE(tr->responseLength(), strlen(BODY3)); + COMPARE(tr->responseBytesReceived(), strlen(BODY3)); + COMPARE(tr->bodyData, string(BODY3)); + } + + { + TestRequest* tr = new TestRequest("http://localhost:2000/test_args?foo=abc&bar=1234&username=johndoe"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 200); + } + + cerr << "done args" << endl; + + { + TestRequest* tr = new TestRequest("http://localhost:2000/test_headers"); + HTTP::Request_ptr own(tr); + tr->sendHeaders["X-Foo"] = "Bar"; + tr->sendHeaders["X-AnotherHeader"] = "A longer value"; + cl.makeRequest(tr); + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 200); + COMPARE(tr->responseReason(), string("OK")); + COMPARE(tr->responseLength(), strlen(BODY1)); + COMPARE(tr->responseBytesReceived(), strlen(BODY1)); + COMPARE(tr->bodyData, string(BODY1)); + } + // larger get request for (unsigned int i=0; i> 2); @@ -288,45 +532,189 @@ int main(int argc, char* argv[]) TestRequest* tr = new TestRequest("http://localhost:2000/test2"); HTTP::Request_ptr own(tr); cl.makeRequest(tr); - waitForComplete(tr); + waitForComplete(&cl, tr); COMPARE(tr->responseCode(), 200); - COMPARE(tr->contentLength(), body2Size); + COMPARE(tr->responseBytesReceived(), body2Size); COMPARE(tr->bodyData, string(body2, body2Size)); } + cerr << "testing chunked" << endl; + { + TestRequest* tr = new TestRequest("http://localhost:2000/testchunked"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 200); + COMPARE(tr->responseReason(), string("OK")); + COMPARE(tr->responseBytesReceived(), 30); + COMPARE(tr->bodyData, "ABCDEFGHABCDEFABCDSTUVABCDSTUV"); + // check trailers made it too + COMPARE(tr->headers["x-foobar"], string("wibble")); + } + // test 404 { TestRequest* tr = new TestRequest("http://localhost:2000/not-found"); HTTP::Request_ptr own(tr); cl.makeRequest(tr); - waitForComplete(tr); + waitForComplete(&cl, tr); COMPARE(tr->responseCode(), 404); - COMPARE(tr->contentLength(), 0); + COMPARE(tr->responseReason(), string("not found")); + COMPARE(tr->responseLength(), 0); } - + + cout << "done 404 test" << endl; + + { + TestRequest* tr = new TestRequest("http://localhost:2000/test_args?foo=abc&bar=1234&username=johndoe"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 200); + } + + cout << "done1" << endl; +// test HTTP/1.0 + { + TestRequest* tr = new TestRequest("http://localhost:2000/test_1_0"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 200); + COMPARE(tr->responseLength(), strlen(BODY1)); + COMPARE(tr->bodyData, string(BODY1)); + } + + cout << "done2" << endl; +// test HTTP/1.1 Connection::close + { + TestRequest* tr = new TestRequest("http://localhost:2000/test_close"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 200); + COMPARE(tr->responseLength(), strlen(BODY1)); + COMPARE(tr->bodyData, string(BODY1)); + } + cout << "done3" << endl; +// test connectToHost failure +/* + { + TestRequest* tr = new TestRequest("http://not.found/something"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + waitForFailed(tr); + COMPARE(tr->responseCode(), -1); + } + */ // test proxy { - cl.setProxy("localhost:2000"); + cl.setProxy("localhost", 2000); TestRequest* tr = new TestRequest("http://www.google.com/test2"); HTTP::Request_ptr own(tr); cl.makeRequest(tr); - waitForComplete(tr); + waitForComplete(&cl, tr); COMPARE(tr->responseCode(), 200); - COMPARE(tr->contentLength(), body2Size); + COMPARE(tr->responseLength(), body2Size); COMPARE(tr->bodyData, string(body2, body2Size)); } { - cl.setProxy("localhost:2000", "ABCDEF"); + cl.setProxy("localhost", 2000, "ABCDEF"); TestRequest* tr = new TestRequest("http://www.google.com/test3"); HTTP::Request_ptr own(tr); cl.makeRequest(tr); - waitForComplete(tr); + waitForComplete(&cl, tr); COMPARE(tr->responseCode(), 200); - COMPARE(tr->contentLength(), body2Size); + COMPARE(tr->responseBytesReceived(), body2Size); COMPARE(tr->bodyData, string(body2, body2Size)); } +// pipelining + cout << "testing HTTP 1.1 pipelineing" << endl; + + { + cl.setProxy("", 80); + TestRequest* tr = new TestRequest("http://localhost:2000/test1"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + + + TestRequest* tr2 = new TestRequest("http://localhost:2000/testLorem"); + HTTP::Request_ptr own2(tr2); + cl.makeRequest(tr2); + + TestRequest* tr3 = new TestRequest("http://localhost:2000/test1"); + HTTP::Request_ptr own3(tr3); + cl.makeRequest(tr3); + + waitForComplete(&cl, tr3); + VERIFY(tr->complete); + VERIFY(tr2->complete); + COMPARE(tr->bodyData, string(BODY1)); + + COMPARE(tr2->responseLength(), strlen(BODY3)); + COMPARE(tr2->responseBytesReceived(), strlen(BODY3)); + COMPARE(tr2->bodyData, string(BODY3)); + + COMPARE(tr3->bodyData, string(BODY1)); + } + +// multiple requests with an HTTP 1.0 server + { + cout << "http 1.0 multiple requests" << endl; + + cl.setProxy("", 80); + TestRequest* tr = new TestRequest("http://localhost:2000/test_1_0/A"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + + TestRequest* tr2 = new TestRequest("http://localhost:2000/test_1_0/B"); + HTTP::Request_ptr own2(tr2); + cl.makeRequest(tr2); + + TestRequest* tr3 = new TestRequest("http://localhost:2000/test_1_0/C"); + HTTP::Request_ptr own3(tr3); + cl.makeRequest(tr3); + + waitForComplete(&cl, tr3); + VERIFY(tr->complete); + VERIFY(tr2->complete); + + COMPARE(tr->responseLength(), strlen(BODY1)); + COMPARE(tr->responseBytesReceived(), strlen(BODY1)); + COMPARE(tr->bodyData, string(BODY1)); + + COMPARE(tr2->responseLength(), strlen(BODY3)); + COMPARE(tr2->responseBytesReceived(), strlen(BODY3)); + COMPARE(tr2->bodyData, string(BODY3)); + COMPARE(tr3->bodyData, string(BODY1)); + } + +// POST + { + cout << "POST" << endl; + TestRequest* tr = new TestRequest("http://localhost:2000/test_post?foo=abc&bar=1234&username=johndoe", "POST"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 204); + } + + // test_zero_length_content + { + cout << "zero-length-content-response" << endl; + TestRequest* tr = new TestRequest("http://localhost:2000/test_zero_length_content"); + HTTP::Request_ptr own(tr); + cl.makeRequest(tr); + waitForComplete(&cl, tr); + COMPARE(tr->responseCode(), 200); + COMPARE(tr->bodyData, string()); + COMPARE(tr->responseBytesReceived(), 0); + } + + cout << "all tests passed ok" << endl; return EXIT_SUCCESS; }