summaryrefslogtreecommitdiff
path: root/test/httpserver/testhttpserver.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'test/httpserver/testhttpserver.cpp')
-rw-r--r--test/httpserver/testhttpserver.cpp137
1 files changed, 75 insertions, 62 deletions
diff --git a/test/httpserver/testhttpserver.cpp b/test/httpserver/testhttpserver.cpp
index 160cb32f..469fa383 100644
--- a/test/httpserver/testhttpserver.cpp
+++ b/test/httpserver/testhttpserver.cpp
@@ -34,6 +34,8 @@
#include "MemLeakFindOn.h"
+#define SHORT_TIMEOUT 5000
+
class TestWebServer : public HTTPServer
{
public:
@@ -134,25 +136,27 @@ int test(int argc, const char *argv[])
TestWebServer server;
return server.Main("doesnotexist", argc - 1, argv + 1);
}
-
+
if(argc >= 2 && ::strcmp(argv[1], "s3server") == 0)
{
// Run a server
S3Simulator server;
return server.Main("doesnotexist", argc - 1, argv + 1);
}
-
+
+#ifndef WIN32
+ TEST_THAT(system("rm -rf *.memleaks") == 0);
+#endif
+
// Start the server
- int pid = LaunchServer("./test server testfiles/httpserver.conf", "testfiles/httpserver.pid");
- TEST_THAT(pid != -1 && pid != 0);
- if(pid <= 0)
- {
- return 0;
- }
+ int pid = StartDaemon(0, TEST_EXECUTABLE " server testfiles/httpserver.conf",
+ "testfiles/httpserver.pid");
+ TEST_THAT_OR(pid > 0, return 1);
// Run the request script
TEST_THAT(::system("perl testfiles/testrequests.pl") == 0);
+#ifdef ENABLE_KEEPALIVE_SUPPORT // incomplete, need chunked encoding support
#ifndef WIN32
signal(SIGPIPE, SIG_IGN);
#endif
@@ -182,8 +186,8 @@ int test(int argc, const char *argv[])
{
sleep(1); // need time for our process to realise
// that the peer has died, otherwise no SIGPIPE :(
- TEST_CHECK_THROWS(request.Send(sock,
- IOStream::TimeOutInfinite),
+ TEST_CHECK_THROWS(
+ request.Send(sock, SHORT_TIMEOUT),
ConnectionException, SocketWriteError);
sock.Close();
sock.Open(Socket::TypeINET, "localhost", 1080);
@@ -191,12 +195,12 @@ int test(int argc, const char *argv[])
}
else
{
- request.Send(sock, IOStream::TimeOutInfinite);
+ request.Send(sock, SHORT_TIMEOUT);
}
HTTPResponse response;
- response.Receive(sock);
-
+ response.Receive(sock, SHORT_TIMEOUT);
+
TEST_THAT(response.GetResponseCode() == HTTPResponse::Code_OK);
TEST_THAT(response.GetContentType() == "text/html");
@@ -234,16 +238,25 @@ int test(int argc, const char *argv[])
TEST_EQUAL("</body>", line);
TEST_THAT(getline.GetLine(line));
TEST_EQUAL("</html>", line);
+
+ if(!response.IsKeepAlive())
+ {
+ BOX_TRACE("Server will close the connection, closing our end too.");
+ sock.Close();
+ sock.Open(Socket::TypeINET, "localhost", 1080);
+ }
+ else
+ {
+ BOX_TRACE("Server will keep the connection open for more requests.");
+ }
}
-
- // Kill it
- TEST_THAT(KillServer(pid));
- #ifdef WIN32
- TEST_THAT(unlink("testfiles/httpserver.pid") == 0);
- #else
- TestRemoteProcessMemLeaks("generic-httpserver.memleaks");
- #endif
+ sock.Close();
+#endif // ENABLE_KEEPALIVE_SUPPORT
+
+ // Kill it
+ TEST_THAT(StopDaemon(pid, "testfiles/httpserver.pid",
+ "generic-httpserver.memleaks", true));
// correct, official signature should succeed, with lower-case header
{
@@ -253,16 +266,16 @@ int test(int argc, const char *argv[])
request.AddHeader("date", "Tue, 27 Mar 2007 19:36:42 +0000");
request.AddHeader("authorization",
"AWS 0PN5J17HBGZHT7JJ3X82:xXjDGYUmKxnwqr5KXNPGldn5LbA=");
-
+
S3Simulator simulator;
simulator.Configure("testfiles/s3simulator.conf");
-
+
CollectInBufferStream response_buffer;
HTTPResponse response(&response_buffer);
-
+
simulator.Handle(request, response);
TEST_EQUAL(200, response.GetResponseCode());
-
+
std::string response_data((const char *)response.GetBuffer(),
response.GetSize());
TEST_EQUAL("omgpuppies!\n", response_data);
@@ -276,16 +289,16 @@ int test(int argc, const char *argv[])
request.AddHeader("date", "Tue, 27 Mar 2007 19:36:42 +0000");
request.AddHeader("authorization",
"AWS 0PN5J17HBGZHT7JJ3X82:xXjDGYUmKxnwqr5KXNPGldn5LbB=");
-
+
S3Simulator simulator;
simulator.Configure("testfiles/s3simulator.conf");
-
+
CollectInBufferStream response_buffer;
HTTPResponse response(&response_buffer);
-
+
simulator.Handle(request, response);
TEST_EQUAL(401, response.GetResponseCode());
-
+
std::string response_data((const char *)response.GetBuffer(),
response.GetSize());
TEST_EQUAL("<html><head>"
@@ -297,7 +310,7 @@ int test(int argc, const char *argv[])
"</html>\n", response_data);
}
- // S3Client tests
+ // S3Client tests with S3Simulator in-process server for debugging
{
S3Simulator simulator;
simulator.Configure("testfiles/s3simulator.conf");
@@ -332,26 +345,27 @@ int test(int argc, const char *argv[])
}
{
- HTTPRequest request(HTTPRequest::Method_PUT,
- "/newfile");
+ HTTPRequest request(HTTPRequest::Method_PUT, "/newfile");
request.SetHostName("quotes.s3.amazonaws.com");
request.AddHeader("date", "Wed, 01 Mar 2006 12:00:00 GMT");
- request.AddHeader("authorization", "AWS 0PN5J17HBGZHT7JJ3X82:XtMYZf0hdOo4TdPYQknZk0Lz7rw=");
+ request.AddHeader("authorization",
+ "AWS 0PN5J17HBGZHT7JJ3X82:XtMYZf0hdOo4TdPYQknZk0Lz7rw=");
request.AddHeader("Content-Type", "text/plain");
-
+
FileStream fs("testfiles/testrequests.pl");
fs.CopyStreamTo(request);
request.SetForReading();
CollectInBufferStream response_buffer;
HTTPResponse response(&response_buffer);
-
+
S3Simulator simulator;
simulator.Configure("testfiles/s3simulator.conf");
simulator.Handle(request, response);
-
+
TEST_EQUAL(200, response.GetResponseCode());
- TEST_EQUAL("LriYPLdmOdAiIfgSm/F1YsViT1LW94/xUQxMsF7xiEb1a0wiIOIxl+zbwZ163pt7", response.GetHeaderValue("x-amz-id-2"));
+ TEST_EQUAL("LriYPLdmOdAiIfgSm/F1YsViT1LW94/xUQxMsF7xiEb1a0wiIOIxl+zbwZ163pt7",
+ response.GetHeaderValue("x-amz-id-2"));
TEST_EQUAL("F2A8CCCA26B4B26D", response.GetHeaderValue("x-amz-request-id"));
TEST_EQUAL("Wed, 01 Mar 2006 12:00:00 GMT", response.GetHeaderValue("Date"));
TEST_EQUAL("Sun, 1 Jan 2006 12:00:00 GMT", response.GetHeaderValue("Last-Modified"));
@@ -367,27 +381,23 @@ int test(int argc, const char *argv[])
}
// Start the S3Simulator server
- pid = LaunchServer("./test s3server testfiles/s3simulator.conf",
+ pid = StartDaemon(0, TEST_EXECUTABLE " s3server testfiles/s3simulator.conf",
"testfiles/s3simulator.pid");
- TEST_THAT(pid != -1 && pid != 0);
- if(pid <= 0)
- {
- return 0;
- }
-
- sock.Close();
- sock.Open(Socket::TypeINET, "localhost", 1080);
+ TEST_THAT_OR(pid > 0, return 1);
{
+ SocketStream sock;
+ sock.Open(Socket::TypeINET, "localhost", 1080);
+
HTTPRequest request(HTTPRequest::Method_GET, "/nonexist");
request.SetHostName("quotes.s3.amazonaws.com");
request.AddHeader("Date", "Wed, 01 Mar 2006 12:00:00 GMT");
request.AddHeader("Authorization", "AWS 0PN5J17HBGZHT7JJ3X82:0cSX/YPdtXua1aFFpYmH1tc0ajA=");
request.SetClientKeepAliveRequested(true);
- request.Send(sock, IOStream::TimeOutInfinite);
+ request.Send(sock, SHORT_TIMEOUT);
HTTPResponse response;
- response.Receive(sock);
+ response.Receive(sock, SHORT_TIMEOUT);
std::string value;
TEST_EQUAL(404, response.GetResponseCode());
}
@@ -396,6 +406,9 @@ int test(int argc, const char *argv[])
// Make file inaccessible, should cause server to return a 403 error,
// unless of course the test is run as root :)
{
+ SocketStream sock;
+ sock.Open(Socket::TypeINET, "localhost", 1080);
+
TEST_THAT(chmod("testfiles/testrequests.pl", 0) == 0);
HTTPRequest request(HTTPRequest::Method_GET,
"/testrequests.pl");
@@ -403,10 +416,10 @@ int test(int argc, const char *argv[])
request.AddHeader("Date", "Wed, 01 Mar 2006 12:00:00 GMT");
request.AddHeader("Authorization", "AWS 0PN5J17HBGZHT7JJ3X82:qc1e8u8TVl2BpIxwZwsursIb8U8=");
request.SetClientKeepAliveRequested(true);
- request.Send(sock, IOStream::TimeOutInfinite);
+ request.Send(sock, SHORT_TIMEOUT);
HTTPResponse response;
- response.Receive(sock);
+ response.Receive(sock, SHORT_TIMEOUT);
std::string value;
TEST_EQUAL(403, response.GetResponseCode());
TEST_THAT(chmod("testfiles/testrequests.pl", 0755) == 0);
@@ -414,16 +427,19 @@ int test(int argc, const char *argv[])
#endif
{
+ SocketStream sock;
+ sock.Open(Socket::TypeINET, "localhost", 1080);
+
HTTPRequest request(HTTPRequest::Method_GET,
"/testrequests.pl");
request.SetHostName("quotes.s3.amazonaws.com");
request.AddHeader("Date", "Wed, 01 Mar 2006 12:00:00 GMT");
request.AddHeader("Authorization", "AWS 0PN5J17HBGZHT7JJ3X82:qc1e8u8TVl2BpIxwZwsursIb8U8=");
request.SetClientKeepAliveRequested(true);
- request.Send(sock, IOStream::TimeOutInfinite);
+ request.Send(sock, SHORT_TIMEOUT);
HTTPResponse response;
- response.Receive(sock);
+ response.Receive(sock, SHORT_TIMEOUT);
std::string value;
TEST_EQUAL(200, response.GetResponseCode());
TEST_EQUAL("qBmKRcEWBBhH6XAqsKU/eg24V3jf/kWKN9dJip1L/FpbYr9FDy7wWFurfdQOEMcY", response.GetHeaderValue("x-amz-id-2"));
@@ -439,6 +455,9 @@ int test(int argc, const char *argv[])
}
{
+ SocketStream sock;
+ sock.Open(Socket::TypeINET, "localhost", 1080);
+
HTTPRequest request(HTTPRequest::Method_PUT,
"/newfile");
request.SetHostName("quotes.s3.amazonaws.com");
@@ -447,9 +466,7 @@ int test(int argc, const char *argv[])
request.AddHeader("Content-Type", "text/plain");
FileStream fs("testfiles/testrequests.pl");
HTTPResponse response;
- request.SendWithStream(sock,
- IOStream::TimeOutInfinite /* or 10000 milliseconds */,
- &fs, response);
+ request.SendWithStream(sock, SHORT_TIMEOUT, &fs, response);
std::string value;
TEST_EQUAL(200, response.GetResponseCode());
TEST_EQUAL("LriYPLdmOdAiIfgSm/F1YsViT1LW94/xUQxMsF7xiEb1a0wiIOIxl+zbwZ163pt7", response.GetHeaderValue("x-amz-id-2"));
@@ -466,14 +483,10 @@ int test(int argc, const char *argv[])
TEST_THAT(f1.CompareWith(f2));
}
- // Kill it
- TEST_THAT(KillServer(pid));
- #ifdef WIN32
- TEST_THAT(unlink("testfiles/s3simulator.pid") == 0);
- #else
- TestRemoteProcessMemLeaks("generic-httpserver.memleaks");
- #endif
+ // Kill it
+ TEST_THAT(StopDaemon(pid, "testfiles/s3simulator.pid",
+ "s3simulator.memleaks", true));
return 0;
}