Changeset 2428 for box/trunk/test


Ignore:
Timestamp:
03/01/2009 08:59:47 (3 years ago)
Author:
chris
Message:

Add ability to send an HTTPRequest to a socket and to parse an
HTTPResponse from a socket, to create a simple HTTP client.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • box/trunk/test/httpserver/testhttpserver.cpp

    r2423 r2428  
    1717#include "HTTPRequest.h" 
    1818#include "HTTPResponse.h" 
     19#include "IOStreamGetLine.h" 
    1920#include "ServerControl.h" 
    2021 
     
    7273                case HTTPRequest::Method_HEAD: m = "HEAD"; break; 
    7374                case HTTPRequest::Method_POST: m = "POST"; break; 
     75                default: m = "UNKNOWN"; 
    7476                } 
    7577                rResponse.Write(m, 4); 
     
    126128        int pid = LaunchServer("./test server testfiles/httpserver.conf", "testfiles/httpserver.pid"); 
    127129        TEST_THAT(pid != -1 && pid != 0); 
    128         if(pid > 0) 
    129         { 
    130                 // Run the request script 
    131                 TEST_THAT(::system("perl testfiles/testrequests.pl") == 0); 
     130        if(pid <= 0) 
     131        { 
     132                return 0; 
     133        } 
     134 
     135        // Run the request script 
     136        TEST_THAT(::system("perl testfiles/testrequests.pl") == 0); 
     137 
     138        signal(SIGPIPE, SIG_IGN); 
     139 
     140        SocketStream sock; 
     141        sock.Open(Socket::TypeINET, "localhost", 1080); 
     142 
     143        for (int i = 0; i < 4; i++) 
     144        { 
     145                HTTPRequest request(HTTPRequest::Method_GET, 
     146                        "/test-one/34/341s/234?p1=vOne&p2=vTwo"); 
     147 
     148                if (i >= 2) 
     149                { 
     150                        // first set of passes has keepalive off by default, 
     151                        // so when i == 1 the socket has already been closed 
     152                        // by the server, and we'll get -EPIPE when we try 
     153                        // to send the request. 
     154                        request.SetClientKeepAliveRequested(true); 
     155                } 
     156 
     157                if (i == 1) 
     158                { 
     159                        TEST_CHECK_THROWS(request.Write(sock, 
     160                                IOStream::TimeOutInfinite), 
     161                                ConnectionException, SocketWriteError); 
     162                        sock.Close(); 
     163                        sock.Open(Socket::TypeINET, "localhost", 1080); 
     164                        continue; 
     165                } 
     166                else 
     167                { 
     168                        request.Write(sock, IOStream::TimeOutInfinite); 
     169                } 
     170 
     171                HTTPResponse response; 
     172                response.Receive(sock); 
     173                 
     174                TEST_THAT(response.GetResponseCode() == HTTPResponse::Code_OK); 
     175                TEST_THAT(response.GetContentType() == "text/html"); 
     176 
     177                IOStreamGetLine getline(response); 
     178                std::string line; 
     179 
     180                TEST_THAT(getline.GetLine(line)); 
     181                TEST_EQUAL("<html>", line); 
     182                TEST_THAT(getline.GetLine(line)); 
     183                TEST_EQUAL("<head><title>TEST SERVER RESPONSE</title></head>", 
     184                        line); 
     185                TEST_THAT(getline.GetLine(line)); 
     186                TEST_EQUAL("<body><h1>Test response</h1>", line); 
     187                TEST_THAT(getline.GetLine(line)); 
     188                TEST_EQUAL("<p><b>URI:</b> /test-one/34/341s/234</p>", line); 
     189                TEST_THAT(getline.GetLine(line)); 
     190                TEST_EQUAL("<p><b>Query string:</b> p1=vOne&p2=vTwo</p>", line); 
     191                TEST_THAT(getline.GetLine(line)); 
     192                TEST_EQUAL("<p><b>Method:</b> GET </p>", line); 
     193                TEST_THAT(getline.GetLine(line)); 
     194                TEST_EQUAL("<p><b>Decoded query:</b><br>", line); 
     195                TEST_THAT(getline.GetLine(line)); 
     196                TEST_EQUAL("PARAM:p1=vOne<br>", line); 
     197                TEST_THAT(getline.GetLine(line)); 
     198                TEST_EQUAL("PARAM:p2=vTwo<br></p>", line); 
     199                TEST_THAT(getline.GetLine(line)); 
     200                TEST_EQUAL("<p><b>Content type:</b> </p>", line); 
     201                TEST_THAT(getline.GetLine(line)); 
     202                TEST_EQUAL("<p><b>Content length:</b> -1</p>", line); 
     203                TEST_THAT(getline.GetLine(line)); 
     204                TEST_EQUAL("<p><b>Cookies:</b><br>", line); 
     205                TEST_THAT(getline.GetLine(line)); 
     206                TEST_EQUAL("</p>", line); 
     207                TEST_THAT(getline.GetLine(line)); 
     208                TEST_EQUAL("</body>", line); 
     209                TEST_THAT(getline.GetLine(line)); 
     210                TEST_EQUAL("</html>", line); 
     211        } 
    132212         
    133                 // Kill it 
    134                 TEST_THAT(KillServer(pid)); 
    135                 TestRemoteProcessMemLeaks("generic-httpserver.memleaks"); 
    136         } 
     213        // Kill it 
     214        TEST_THAT(KillServer(pid)); 
     215        TestRemoteProcessMemLeaks("generic-httpserver.memleaks"); 
    137216 
    138217        return 0; 
Note: See TracChangeset for help on using the changeset viewer.