1 // Copyright 2007 Google Inc.
2 // All Rights Reserved.
5 #include "talk/base/gunit.h"
6 #include "talk/base/httpserver.h"
7 #include "talk/base/testutils.h"
9 using namespace testing;
14 const char* const kRequest =
15 "GET /index.html HTTP/1.1\r\n"
19 struct HttpServerMonitor : public sigslot::has_slots<> {
20 HttpServerTransaction* transaction;
21 bool server_closed, connection_closed;
23 HttpServerMonitor(HttpServer* server)
24 : transaction(NULL), server_closed(false), connection_closed(false) {
25 server->SignalCloseAllComplete.connect(this,
26 &HttpServerMonitor::OnClosed);
27 server->SignalHttpRequest.connect(this, &HttpServerMonitor::OnRequest);
28 server->SignalHttpRequestComplete.connect(this,
29 &HttpServerMonitor::OnRequestComplete);
30 server->SignalConnectionClosed.connect(this,
31 &HttpServerMonitor::OnConnectionClosed);
33 void OnRequest(HttpServer*, HttpServerTransaction* t) {
34 ASSERT_FALSE(transaction);
36 transaction->response.set_success();
37 transaction->response.setHeader(HH_CONNECTION, "Close");
39 void OnRequestComplete(HttpServer*, HttpServerTransaction* t, int) {
40 ASSERT_EQ(transaction, t);
43 void OnClosed(HttpServer*) {
46 void OnConnectionClosed(HttpServer*, int, StreamInterface* stream) {
47 connection_closed = true;
52 void CreateClientConnection(HttpServer& server,
53 HttpServerMonitor& monitor,
55 StreamSource* client = new StreamSource;
56 client->SetState(SS_OPEN);
57 server.HandleConnection(client);
58 EXPECT_FALSE(monitor.server_closed);
59 EXPECT_FALSE(monitor.transaction);
63 client->QueueString(kRequest);
64 EXPECT_FALSE(monitor.server_closed);
67 } // anonymous namespace
69 TEST(HttpServer, DoesNotSignalCloseUnlessCloseAllIsCalled) {
71 HttpServerMonitor monitor(&server);
72 // Add an active client connection
73 CreateClientConnection(server, monitor, true);
74 // Simulate a response
75 ASSERT_TRUE(NULL != monitor.transaction);
76 server.Respond(monitor.transaction);
77 EXPECT_FALSE(monitor.transaction);
78 // Connection has closed, but no server close signal
79 EXPECT_FALSE(monitor.server_closed);
80 EXPECT_TRUE(monitor.connection_closed);
83 TEST(HttpServer, SignalsCloseWhenNoConnectionsAreActive) {
85 HttpServerMonitor monitor(&server);
86 // Add an idle client connection
87 CreateClientConnection(server, monitor, false);
88 // Perform graceful close
89 server.CloseAll(false);
90 // Connections have all closed
91 EXPECT_TRUE(monitor.server_closed);
92 EXPECT_TRUE(monitor.connection_closed);
95 TEST(HttpServer, SignalsCloseAfterGracefulCloseAll) {
97 HttpServerMonitor monitor(&server);
98 // Add an active client connection
99 CreateClientConnection(server, monitor, true);
100 // Initiate a graceful close
101 server.CloseAll(false);
102 EXPECT_FALSE(monitor.server_closed);
103 // Simulate a response
104 ASSERT_TRUE(NULL != monitor.transaction);
105 server.Respond(monitor.transaction);
106 EXPECT_FALSE(monitor.transaction);
107 // Connections have all closed
108 EXPECT_TRUE(monitor.server_closed);
109 EXPECT_TRUE(monitor.connection_closed);
112 TEST(HttpServer, SignalsCloseAfterForcedCloseAll) {
114 HttpServerMonitor monitor(&server);
115 // Add an active client connection
116 CreateClientConnection(server, monitor, true);
117 // Initiate a forceful close
118 server.CloseAll(true);
119 // Connections have all closed
120 EXPECT_TRUE(monitor.server_closed);
121 EXPECT_TRUE(monitor.connection_closed);
124 } // namespace talk_base