Changeset 2435 for box/trunk/test
- Timestamp:
- 04/01/2009 13:57:32 (3 years ago)
- File:
-
- 1 edited
-
box/trunk/test/httpserver/testhttpserver.cpp (modified) (7 diffs)
Legend:
- Unmodified
- Added
- Removed
-
box/trunk/test/httpserver/testhttpserver.cpp
r2428 r2435 111 111 } 112 112 113 114 115 113 TestWebServer::TestWebServer() {} 116 114 TestWebServer::~TestWebServer() {} 117 115 116 class S3Simulator : public HTTPServer 117 { 118 public: 119 S3Simulator() { } 120 ~S3Simulator() { } 121 122 virtual void Handle(const HTTPRequest &rRequest, 123 HTTPResponse &rResponse); 124 }; 125 126 void S3Simulator::Handle(const HTTPRequest &rRequest, HTTPResponse &rResponse) 127 { 128 // if anything goes wrong, return a 500 error 129 rResponse.SetResponseCode(HTTPResponse::Code_InternalServerError); 130 rResponse.SetContentType("text/plain"); 131 132 if (rRequest.GetMethod() != HTTPRequest::Method_GET) 133 { 134 rResponse.SetResponseCode(HTTPResponse::Code_MethodNotAllowed); 135 return; 136 } 137 138 std::string path = "testfiles"; 139 path += rRequest.GetRequestURI(); 140 std::auto_ptr<FileStream> apFile; 141 142 try 143 { 144 apFile.reset(new FileStream(path)); 145 } 146 catch (CommonException &ce) 147 { 148 if (ce.GetSubType() == CommonException::OSFileOpenError) 149 { 150 rResponse.SetResponseCode(HTTPResponse::Code_NotFound); 151 } 152 else if (ce.GetSubType() == CommonException::AccessDenied) 153 { 154 rResponse.SetResponseCode(HTTPResponse::Code_Forbidden); 155 } 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; 172 } 173 174 // http://docs.amazonwebservices.com/AmazonS3/2006-03-01/UsingRESTOperations.html 175 apFile->CopyStreamTo(rResponse); 176 rResponse.AddHeader("x-amz-id-2", "qBmKRcEWBBhH6XAqsKU/eg24V3jf/kWKN9dJip1L/FpbYr9FDy7wWFurfdQOEMcY"); 177 rResponse.AddHeader("x-amz-request-id", "F2A8CCCA26B4B26D"); 178 rResponse.AddHeader("Date", "Wed, 01 Mar 2006 12:00:00 GMT"); 179 rResponse.AddHeader("Last-Modified", "Sun, 1 Jan 2006 12:00:00 GMT"); 180 rResponse.AddHeader("ETag", "\"828ef3fdfa96f00ad9f27c383fc9ac7f\""); 181 rResponse.SetContentType("text/plain"); 182 rResponse.AddHeader("Server", "AmazonS3"); 183 rResponse.SetResponseCode(HTTPResponse::Code_OK); 184 } 185 118 186 int test(int argc, const char *argv[]) 119 187 { … … 122 190 // Run a server 123 191 TestWebServer server; 192 return server.Main("doesnotexist", argc - 1, argv + 1); 193 } 194 195 if(argc >= 2 && ::strcmp(argv[1], "s3server") == 0) 196 { 197 // Run a server 198 S3Simulator server; 124 199 return server.Main("doesnotexist", argc - 1, argv + 1); 125 200 } … … 146 221 "/test-one/34/341s/234?p1=vOne&p2=vTwo"); 147 222 148 if (i >=2)223 if (i < 2) 149 224 { 150 225 // first set of passes has keepalive off by default, … … 152 227 // by the server, and we'll get -EPIPE when we try 153 228 // to send the request. 229 request.SetClientKeepAliveRequested(false); 230 } 231 else 232 { 154 233 request.SetClientKeepAliveRequested(true); 155 234 } … … 157 236 if (i == 1) 158 237 { 159 TEST_CHECK_THROWS(request.Write(sock, 238 sleep(1); // need time for our process to realise 239 // that the peer has died, otherwise no SIGPIPE :( 240 TEST_CHECK_THROWS(request.Send(sock, 160 241 IOStream::TimeOutInfinite), 161 242 ConnectionException, SocketWriteError); … … 166 247 else 167 248 { 168 request. Write(sock, IOStream::TimeOutInfinite);249 request.Send(sock, IOStream::TimeOutInfinite); 169 250 } 170 251 … … 215 296 TestRemoteProcessMemLeaks("generic-httpserver.memleaks"); 216 297 298 // Start the S3Simulator server 299 pid = LaunchServer("./test s3server testfiles/httpserver.conf", 300 "testfiles/httpserver.pid"); 301 TEST_THAT(pid != -1 && pid != 0); 302 if(pid <= 0) 303 { 304 return 0; 305 } 306 307 sock.Close(); 308 sock.Open(Socket::TypeINET, "localhost", 1080); 309 310 { 311 HTTPRequest request(HTTPRequest::Method_GET, "/nonexist"); 312 request.SetHostName("quotes.s3.amazonaws.com"); 313 request.AddHeader("Date", "Wed, 01 Mar 2006 12:00:00 GMT"); 314 request.AddHeader("Authorization", "AWS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXAMPLE="); 315 request.SetClientKeepAliveRequested(true); 316 request.Send(sock, IOStream::TimeOutInfinite); 317 318 HTTPResponse response; 319 response.Receive(sock); 320 std::string value; 321 TEST_EQUAL(404, response.GetResponseCode()); 322 } 323 324 { 325 TEST_THAT(chmod("testfiles/testrequests.pl", 0) == 0); 326 HTTPRequest request(HTTPRequest::Method_GET, 327 "/testrequests.pl"); 328 request.SetHostName("quotes.s3.amazonaws.com"); 329 request.AddHeader("Date", "Wed, 01 Mar 2006 12:00:00 GMT"); 330 request.AddHeader("Authorization", "AWS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXAMPLE="); 331 request.SetClientKeepAliveRequested(true); 332 request.Send(sock, IOStream::TimeOutInfinite); 333 334 HTTPResponse response; 335 response.Receive(sock); 336 std::string value; 337 TEST_EQUAL(403, response.GetResponseCode()); 338 TEST_THAT(chmod("testfiles/testrequests.pl", 0755) == 0); 339 } 340 341 { 342 HTTPRequest request(HTTPRequest::Method_GET, 343 "/testrequests.pl"); 344 request.SetHostName("quotes.s3.amazonaws.com"); 345 request.AddHeader("Date", "Wed, 01 Mar 2006 12:00:00 GMT"); 346 request.AddHeader("Authorization", "AWS 15B4D3461F177624206A:xQE0diMbLRepdf3YB+FIEXAMPLE="); 347 request.SetClientKeepAliveRequested(true); 348 request.Send(sock, IOStream::TimeOutInfinite); 349 350 HTTPResponse response; 351 response.Receive(sock); 352 std::string value; 353 TEST_EQUAL(200, response.GetResponseCode()); 354 TEST_EQUAL("qBmKRcEWBBhH6XAqsKU/eg24V3jf/kWKN9dJip1L/FpbYr9FDy7wWFurfdQOEMcY", response.GetHeaderValue("x-amz-id-2")); 355 TEST_EQUAL("F2A8CCCA26B4B26D", response.GetHeaderValue("x-amz-request-id")); 356 TEST_EQUAL("Wed, 01 Mar 2006 12:00:00 GMT", response.GetHeaderValue("Date")); 357 TEST_EQUAL("Sun, 1 Jan 2006 12:00:00 GMT", response.GetHeaderValue("Last-Modified")); 358 TEST_EQUAL("\"828ef3fdfa96f00ad9f27c383fc9ac7f\"", response.GetHeaderValue("ETag")); 359 TEST_EQUAL("text/plain", response.GetContentType()); 360 TEST_EQUAL("AmazonS3", response.GetHeaderValue("Server")); 361 362 FileStream file("testfiles/testrequests.pl"); 363 TEST_THAT(file.CompareWith(response)); 364 } 365 366 // Kill it 367 TEST_THAT(KillServer(pid)); 368 TestRemoteProcessMemLeaks("generic-httpserver.memleaks"); 369 217 370 return 0; 218 371 }
Note: See TracChangeset
for help on using the changeset viewer.
