OLD | NEW |
1 /* | 1 /* |
2 * Copyright 2007 The WebRTC Project Authors. All rights reserved. | 2 * Copyright 2007 The WebRTC Project Authors. All rights reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/base/gunit.h" | 11 #include "webrtc/base/gunit.h" |
12 #include "webrtc/base/httpserver.h" | 12 #include "webrtc/base/httpserver.h" |
13 #include "webrtc/base/testutils.h" | 13 #include "webrtc/base/testutils.h" |
14 | 14 |
15 using namespace testing; | 15 using namespace testing; |
16 | 16 |
17 namespace rtc { | 17 namespace rtc { |
18 | 18 |
19 namespace { | 19 namespace { |
20 const char* const kRequest = | 20 const char* const kRequest = |
21 "GET /index.html HTTP/1.1\r\n" | 21 "GET /index.html HTTP/1.1\r\n" |
22 "Host: localhost\r\n" | 22 "Host: localhost\r\n" |
23 "\r\n"; | 23 "\r\n"; |
24 | 24 |
25 struct HttpServerMonitor : public sigslot::has_slots<> { | 25 struct HttpServerMonitor : public sigslot::has_slots<> { |
26 HttpServerTransaction* transaction; | 26 HttpServerTransaction* transaction; |
27 bool server_closed, connection_closed; | 27 bool server_closed, connection_closed; |
28 | 28 |
29 HttpServerMonitor(HttpServer* server) | 29 HttpServerMonitor(HttpServer* server) |
30 : transaction(NULL), server_closed(false), connection_closed(false) { | 30 : transaction(nullptr), server_closed(false), connection_closed(false) { |
31 server->SignalCloseAllComplete.connect(this, | 31 server->SignalCloseAllComplete.connect(this, |
32 &HttpServerMonitor::OnClosed); | 32 &HttpServerMonitor::OnClosed); |
33 server->SignalHttpRequest.connect(this, &HttpServerMonitor::OnRequest); | 33 server->SignalHttpRequest.connect(this, &HttpServerMonitor::OnRequest); |
34 server->SignalHttpRequestComplete.connect(this, | 34 server->SignalHttpRequestComplete.connect(this, |
35 &HttpServerMonitor::OnRequestComplete); | 35 &HttpServerMonitor::OnRequestComplete); |
36 server->SignalConnectionClosed.connect(this, | 36 server->SignalConnectionClosed.connect(this, |
37 &HttpServerMonitor::OnConnectionClosed); | 37 &HttpServerMonitor::OnConnectionClosed); |
38 } | 38 } |
39 void OnRequest(HttpServer*, HttpServerTransaction* t) { | 39 void OnRequest(HttpServer*, HttpServerTransaction* t) { |
40 ASSERT_FALSE(transaction); | 40 ASSERT_FALSE(transaction); |
41 transaction = t; | 41 transaction = t; |
42 transaction->response.set_success(); | 42 transaction->response.set_success(); |
43 transaction->response.setHeader(HH_CONNECTION, "Close"); | 43 transaction->response.setHeader(HH_CONNECTION, "Close"); |
44 } | 44 } |
45 void OnRequestComplete(HttpServer*, HttpServerTransaction* t, int) { | 45 void OnRequestComplete(HttpServer*, HttpServerTransaction* t, int) { |
46 ASSERT_EQ(transaction, t); | 46 ASSERT_EQ(transaction, t); |
47 transaction = NULL; | 47 transaction = nullptr; |
48 } | 48 } |
49 void OnClosed(HttpServer*) { | 49 void OnClosed(HttpServer*) { |
50 server_closed = true; | 50 server_closed = true; |
51 } | 51 } |
52 void OnConnectionClosed(HttpServer*, int, StreamInterface* stream) { | 52 void OnConnectionClosed(HttpServer*, int, StreamInterface* stream) { |
53 connection_closed = true; | 53 connection_closed = true; |
54 delete stream; | 54 delete stream; |
55 } | 55 } |
56 }; | 56 }; |
57 | 57 |
(...skipping 13 matching lines...) Expand all Loading... |
71 } | 71 } |
72 } | 72 } |
73 } // anonymous namespace | 73 } // anonymous namespace |
74 | 74 |
75 TEST(HttpServer, DoesNotSignalCloseUnlessCloseAllIsCalled) { | 75 TEST(HttpServer, DoesNotSignalCloseUnlessCloseAllIsCalled) { |
76 HttpServer server; | 76 HttpServer server; |
77 HttpServerMonitor monitor(&server); | 77 HttpServerMonitor monitor(&server); |
78 // Add an active client connection | 78 // Add an active client connection |
79 CreateClientConnection(server, monitor, true); | 79 CreateClientConnection(server, monitor, true); |
80 // Simulate a response | 80 // Simulate a response |
81 ASSERT_TRUE(NULL != monitor.transaction); | 81 ASSERT_TRUE(nullptr != monitor.transaction); |
82 server.Respond(monitor.transaction); | 82 server.Respond(monitor.transaction); |
83 EXPECT_FALSE(monitor.transaction); | 83 EXPECT_FALSE(monitor.transaction); |
84 // Connection has closed, but no server close signal | 84 // Connection has closed, but no server close signal |
85 EXPECT_FALSE(monitor.server_closed); | 85 EXPECT_FALSE(monitor.server_closed); |
86 EXPECT_TRUE(monitor.connection_closed); | 86 EXPECT_TRUE(monitor.connection_closed); |
87 } | 87 } |
88 | 88 |
89 TEST(HttpServer, SignalsCloseWhenNoConnectionsAreActive) { | 89 TEST(HttpServer, SignalsCloseWhenNoConnectionsAreActive) { |
90 HttpServer server; | 90 HttpServer server; |
91 HttpServerMonitor monitor(&server); | 91 HttpServerMonitor monitor(&server); |
92 // Add an idle client connection | 92 // Add an idle client connection |
93 CreateClientConnection(server, monitor, false); | 93 CreateClientConnection(server, monitor, false); |
94 // Perform graceful close | 94 // Perform graceful close |
95 server.CloseAll(false); | 95 server.CloseAll(false); |
96 // Connections have all closed | 96 // Connections have all closed |
97 EXPECT_TRUE(monitor.server_closed); | 97 EXPECT_TRUE(monitor.server_closed); |
98 EXPECT_TRUE(monitor.connection_closed); | 98 EXPECT_TRUE(monitor.connection_closed); |
99 } | 99 } |
100 | 100 |
101 TEST(HttpServer, SignalsCloseAfterGracefulCloseAll) { | 101 TEST(HttpServer, SignalsCloseAfterGracefulCloseAll) { |
102 HttpServer server; | 102 HttpServer server; |
103 HttpServerMonitor monitor(&server); | 103 HttpServerMonitor monitor(&server); |
104 // Add an active client connection | 104 // Add an active client connection |
105 CreateClientConnection(server, monitor, true); | 105 CreateClientConnection(server, monitor, true); |
106 // Initiate a graceful close | 106 // Initiate a graceful close |
107 server.CloseAll(false); | 107 server.CloseAll(false); |
108 EXPECT_FALSE(monitor.server_closed); | 108 EXPECT_FALSE(monitor.server_closed); |
109 // Simulate a response | 109 // Simulate a response |
110 ASSERT_TRUE(NULL != monitor.transaction); | 110 ASSERT_TRUE(nullptr != monitor.transaction); |
111 server.Respond(monitor.transaction); | 111 server.Respond(monitor.transaction); |
112 EXPECT_FALSE(monitor.transaction); | 112 EXPECT_FALSE(monitor.transaction); |
113 // Connections have all closed | 113 // Connections have all closed |
114 EXPECT_TRUE(monitor.server_closed); | 114 EXPECT_TRUE(monitor.server_closed); |
115 EXPECT_TRUE(monitor.connection_closed); | 115 EXPECT_TRUE(monitor.connection_closed); |
116 } | 116 } |
117 | 117 |
118 TEST(HttpServer, SignalsCloseAfterForcedCloseAll) { | 118 TEST(HttpServer, SignalsCloseAfterForcedCloseAll) { |
119 HttpServer server; | 119 HttpServer server; |
120 HttpServerMonitor monitor(&server); | 120 HttpServerMonitor monitor(&server); |
121 // Add an active client connection | 121 // Add an active client connection |
122 CreateClientConnection(server, monitor, true); | 122 CreateClientConnection(server, monitor, true); |
123 // Initiate a forceful close | 123 // Initiate a forceful close |
124 server.CloseAll(true); | 124 server.CloseAll(true); |
125 // Connections have all closed | 125 // Connections have all closed |
126 EXPECT_TRUE(monitor.server_closed); | 126 EXPECT_TRUE(monitor.server_closed); |
127 EXPECT_TRUE(monitor.connection_closed); | 127 EXPECT_TRUE(monitor.connection_closed); |
128 } | 128 } |
129 | 129 |
130 } // namespace rtc | 130 } // namespace rtc |
OLD | NEW |