Changeset 2441 for box/trunk/test


Ignore:
Timestamp:
05/01/2009 00:50:30 (3 years ago)
Author:
chris
Message:

Add support for HTTP PUT requests (uploads) in S3 simulator.

File:
1 edited

Legend:

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

    r2435 r2441  
    2828        ~TestWebServer(); 
    2929 
    30         virtual void Handle(const HTTPRequest &rRequest, HTTPResponse &rResponse); 
     30        virtual void Handle(HTTPRequest &rRequest, HTTPResponse &rResponse); 
    3131 
    3232}; 
    3333 
    3434// Build a nice HTML response, so this can also be tested neatly in a browser 
    35 void TestWebServer::Handle(const HTTPRequest &rRequest, HTTPResponse &rResponse) 
     35void TestWebServer::Handle(HTTPRequest &rRequest, HTTPResponse &rResponse) 
    3636{ 
    3737        // Test redirection mechanism 
     
    120120        ~S3Simulator() { } 
    121121 
    122         virtual void Handle(const HTTPRequest &rRequest, 
    123                 HTTPResponse &rResponse); 
     122        virtual void Handle(HTTPRequest &rRequest, HTTPResponse &rResponse); 
     123        virtual void HandleGet(HTTPRequest &rRequest, HTTPResponse &rResponse); 
     124        virtual void HandlePut(HTTPRequest &rRequest, HTTPResponse &rResponse); 
    124125}; 
    125126 
    126 void S3Simulator::Handle(const HTTPRequest &rRequest, HTTPResponse &rResponse) 
     127void S3Simulator::Handle(HTTPRequest &rRequest, HTTPResponse &rResponse) 
    127128{ 
    128129        // if anything goes wrong, return a 500 error 
     
    130131        rResponse.SetContentType("text/plain"); 
    131132 
    132         if (rRequest.GetMethod() != HTTPRequest::Method_GET) 
    133         { 
    134                 rResponse.SetResponseCode(HTTPResponse::Code_MethodNotAllowed); 
    135                 return; 
    136         } 
    137  
     133        try 
     134        {                
     135                if (rRequest.GetMethod() == HTTPRequest::Method_GET) 
     136                { 
     137                        HandleGet(rRequest, rResponse); 
     138                } 
     139                else if (rRequest.GetMethod() == HTTPRequest::Method_PUT) 
     140                { 
     141                        HandlePut(rRequest, rResponse); 
     142                } 
     143                else 
     144                { 
     145                        rResponse.SetResponseCode(HTTPResponse::Code_MethodNotAllowed); 
     146                } 
     147        } 
     148        catch (CommonException &ce) 
     149        { 
     150                rResponse.IOStream::Write(ce.what()); 
     151        } 
     152        catch (std::exception &e) 
     153        { 
     154                rResponse.IOStream::Write(e.what()); 
     155        } 
     156        catch (...) 
     157        { 
     158                rResponse.IOStream::Write("Unknown error"); 
     159        } 
     160         
     161        return; 
     162} 
     163 
     164void S3Simulator::HandleGet(HTTPRequest &rRequest, HTTPResponse &rResponse) 
     165{ 
    138166        std::string path = "testfiles"; 
    139167        path += rRequest.GetRequestURI(); 
     
    154182                        rResponse.SetResponseCode(HTTPResponse::Code_Forbidden); 
    155183                } 
    156                 else 
    157                 { 
    158                         rResponse.SetResponseCode(HTTPResponse::Code_InternalServerError); 
    159                 } 
    160                 rResponse.IOStream::Write(ce.what()); 
    161                 return; 
    162         } 
    163         catch (std::exception &e) 
    164         { 
    165                 rResponse.IOStream::Write(e.what()); 
    166                 return; 
    167         } 
    168         catch (...) 
    169         { 
    170                 rResponse.IOStream::Write("Unknown error"); 
    171                 return; 
     184                throw; 
    172185        } 
    173186 
     
    179192        rResponse.AddHeader("Last-Modified", "Sun, 1 Jan 2006 12:00:00 GMT"); 
    180193        rResponse.AddHeader("ETag", "\"828ef3fdfa96f00ad9f27c383fc9ac7f\""); 
    181         rResponse.SetContentType("text/plain"); 
     194        rResponse.AddHeader("Server", "AmazonS3"); 
     195        rResponse.SetResponseCode(HTTPResponse::Code_OK); 
     196} 
     197 
     198void S3Simulator::HandlePut(HTTPRequest &rRequest, HTTPResponse &rResponse) 
     199{ 
     200        std::string path = "testfiles"; 
     201        path += rRequest.GetRequestURI(); 
     202        std::auto_ptr<FileStream> apFile; 
     203 
     204        try 
     205        { 
     206                apFile.reset(new FileStream(path, O_CREAT | O_WRONLY)); 
     207        } 
     208        catch (CommonException &ce) 
     209        { 
     210                if (ce.GetSubType() == CommonException::OSFileOpenError) 
     211                { 
     212                        rResponse.SetResponseCode(HTTPResponse::Code_NotFound); 
     213                } 
     214                else if (ce.GetSubType() == CommonException::AccessDenied) 
     215                { 
     216                        rResponse.SetResponseCode(HTTPResponse::Code_Forbidden); 
     217                } 
     218                throw; 
     219        } 
     220 
     221        if (rRequest.IsExpectingContinue()) 
     222        { 
     223                rResponse.SendContinue(); 
     224        } 
     225 
     226        rRequest.ReadContent(*apFile); 
     227 
     228        // http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTObjectPUT.html 
     229        rResponse.AddHeader("x-amz-id-2", "LriYPLdmOdAiIfgSm/F1YsViT1LW94/xUQxMsF7xiEb1a0wiIOIxl+zbwZ163pt7"); 
     230        rResponse.AddHeader("x-amz-request-id", "F2A8CCCA26B4B26D"); 
     231        rResponse.AddHeader("Date", "Wed, 01 Mar  2006 12:00:00 GMT"); 
     232        rResponse.AddHeader("Last-Modified", "Sun, 1 Jan 2006 12:00:00 GMT"); 
     233        rResponse.AddHeader("ETag", "\"828ef3fdfa96f00ad9f27c383fc9ac7f\""); 
     234        rResponse.SetContentType(""); 
    182235        rResponse.AddHeader("Server", "AmazonS3"); 
    183236        rResponse.SetResponseCode(HTTPResponse::Code_OK); 
     
    364417        } 
    365418 
     419        { 
     420                HTTPRequest request(HTTPRequest::Method_PUT, 
     421                        "/newfile"); 
     422                request.SetHostName("quotes.s3.amazonaws.com"); 
     423                request.AddHeader("Date", "Wed, 01 Mar  2006 12:00:00 GMT"); 
     424                request.AddHeader("Authorization", "AWS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXAMPLE="); 
     425                request.AddHeader("Content-Type", "text/plain"); 
     426                FileStream fs("testfiles/testrequests.pl"); 
     427                HTTPResponse response; 
     428                request.SendWithStream(sock, 
     429                        IOStream::TimeOutInfinite /* or 10000 milliseconds */, 
     430                        &fs, response); 
     431                std::string value; 
     432                TEST_EQUAL(200, response.GetResponseCode()); 
     433                TEST_EQUAL("LriYPLdmOdAiIfgSm/F1YsViT1LW94/xUQxMsF7xiEb1a0wiIOIxl+zbwZ163pt7", response.GetHeaderValue("x-amz-id-2")); 
     434                TEST_EQUAL("F2A8CCCA26B4B26D", response.GetHeaderValue("x-amz-request-id")); 
     435                TEST_EQUAL("Wed, 01 Mar  2006 12:00:00 GMT", response.GetHeaderValue("Date")); 
     436                TEST_EQUAL("Sun, 1 Jan 2006 12:00:00 GMT", response.GetHeaderValue("Last-Modified")); 
     437                TEST_EQUAL("\"828ef3fdfa96f00ad9f27c383fc9ac7f\"", response.GetHeaderValue("ETag")); 
     438                TEST_EQUAL("", response.GetContentType()); 
     439                TEST_EQUAL("AmazonS3", response.GetHeaderValue("Server")); 
     440                TEST_EQUAL(0, response.GetSize()); 
     441 
     442                FileStream f1("testfiles/testrequests.pl"); 
     443                FileStream f2("testfiles/newfile"); 
     444                TEST_THAT(f1.CompareWith(f2)); 
     445        } 
     446 
    366447        // Kill it 
    367448        TEST_THAT(KillServer(pid)); 
Note: See TracChangeset for help on using the changeset viewer.