From 72d60c2c3ab73b9c9d6142129fa078cfc09eefbd Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Sat, 3 Jan 2009 08:59:47 +0000 Subject: Add ability to send an HTTPRequest to a socket and to parse an HTTPResponse from a socket, to create a simple HTTP client. --- test/httpserver/testhttpserver.cpp | 93 +++++++++++++++++++++++++++++++++++--- 1 file changed, 86 insertions(+), 7 deletions(-) (limited to 'test/httpserver') diff --git a/test/httpserver/testhttpserver.cpp b/test/httpserver/testhttpserver.cpp index 2ff013c9..69cee873 100644 --- a/test/httpserver/testhttpserver.cpp +++ b/test/httpserver/testhttpserver.cpp @@ -16,6 +16,7 @@ #include "HTTPServer.h" #include "HTTPRequest.h" #include "HTTPResponse.h" +#include "IOStreamGetLine.h" #include "ServerControl.h" #include "MemLeakFindOn.h" @@ -71,6 +72,7 @@ void TestWebServer::Handle(const HTTPRequest &rRequest, HTTPResponse &rResponse) case HTTPRequest::Method_GET: m = "GET "; break; case HTTPRequest::Method_HEAD: m = "HEAD"; break; case HTTPRequest::Method_POST: m = "POST"; break; + default: m = "UNKNOWN"; } rResponse.Write(m, 4); } @@ -125,16 +127,93 @@ int test(int argc, const char *argv[]) // Start the server int pid = LaunchServer("./test server testfiles/httpserver.conf", "testfiles/httpserver.pid"); TEST_THAT(pid != -1 && pid != 0); - if(pid > 0) + if(pid <= 0) { - // Run the request script - TEST_THAT(::system("perl testfiles/testrequests.pl") == 0); - - // Kill it - TEST_THAT(KillServer(pid)); - TestRemoteProcessMemLeaks("generic-httpserver.memleaks"); + return 0; } + // Run the request script + TEST_THAT(::system("perl testfiles/testrequests.pl") == 0); + + signal(SIGPIPE, SIG_IGN); + + SocketStream sock; + sock.Open(Socket::TypeINET, "localhost", 1080); + + for (int i = 0; i < 4; i++) + { + HTTPRequest request(HTTPRequest::Method_GET, + "/test-one/34/341s/234?p1=vOne&p2=vTwo"); + + if (i >= 2) + { + // first set of passes has keepalive off by default, + // so when i == 1 the socket has already been closed + // by the server, and we'll get -EPIPE when we try + // to send the request. + request.SetClientKeepAliveRequested(true); + } + + if (i == 1) + { + TEST_CHECK_THROWS(request.Write(sock, + IOStream::TimeOutInfinite), + ConnectionException, SocketWriteError); + sock.Close(); + sock.Open(Socket::TypeINET, "localhost", 1080); + continue; + } + else + { + request.Write(sock, IOStream::TimeOutInfinite); + } + + HTTPResponse response; + response.Receive(sock); + + TEST_THAT(response.GetResponseCode() == HTTPResponse::Code_OK); + TEST_THAT(response.GetContentType() == "text/html"); + + IOStreamGetLine getline(response); + std::string line; + + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("TEST SERVER RESPONSE", + line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

Test response

", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

URI: /test-one/34/341s/234

", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

Query string: p1=vOne&p2=vTwo

", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

Method: GET

", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

Decoded query:
", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("PARAM:p1=vOne
", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("PARAM:p2=vTwo

", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

Content type:

", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

Content length: -1

", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

Cookies:
", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("

", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("", line); + TEST_THAT(getline.GetLine(line)); + TEST_EQUAL("", line); + } + + // Kill it + TEST_THAT(KillServer(pid)); + TestRemoteProcessMemLeaks("generic-httpserver.memleaks"); + return 0; } -- cgit v1.2.3