deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2007 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 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 |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "rtc_base/httpserver.h" |
| 12 | #include "rtc_base/gunit.h" |
| 13 | #include "rtc_base/testutils.h" |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 14 | |
kwiberg | d0d8148 | 2017-04-18 03:18:22 -0700 | [diff] [blame] | 15 | using namespace webrtc::testing; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 16 | |
| 17 | namespace rtc { |
| 18 | |
| 19 | namespace { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 20 | const char* const kRequest = |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 21 | "GET /index.html HTTP/1.1\r\n" |
| 22 | "Host: localhost\r\n" |
| 23 | "\r\n"; |
| 24 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 25 | struct HttpServerMonitor : public sigslot::has_slots<> { |
| 26 | HttpServerTransaction* transaction; |
| 27 | bool server_closed, connection_closed; |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 28 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 29 | HttpServerMonitor(HttpServer* server) |
| 30 | : transaction(nullptr), server_closed(false), connection_closed(false) { |
| 31 | server->SignalCloseAllComplete.connect(this, &HttpServerMonitor::OnClosed); |
| 32 | server->SignalHttpRequest.connect(this, &HttpServerMonitor::OnRequest); |
| 33 | server->SignalHttpRequestComplete.connect( |
| 34 | this, &HttpServerMonitor::OnRequestComplete); |
| 35 | server->SignalConnectionClosed.connect( |
| 36 | this, &HttpServerMonitor::OnConnectionClosed); |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 37 | } |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 38 | void OnRequest(HttpServer*, HttpServerTransaction* t) { |
| 39 | ASSERT_FALSE(transaction); |
| 40 | transaction = t; |
| 41 | transaction->response.set_success(); |
| 42 | transaction->response.setHeader(HH_CONNECTION, "Close"); |
| 43 | } |
| 44 | void OnRequestComplete(HttpServer*, HttpServerTransaction* t, int) { |
| 45 | ASSERT_EQ(transaction, t); |
| 46 | transaction = nullptr; |
| 47 | } |
| 48 | void OnClosed(HttpServer*) { server_closed = true; } |
| 49 | void OnConnectionClosed(HttpServer*, int, StreamInterface* stream) { |
| 50 | connection_closed = true; |
| 51 | delete stream; |
| 52 | } |
| 53 | }; |
| 54 | |
| 55 | void CreateClientConnection(HttpServer& server, |
| 56 | HttpServerMonitor& monitor, |
| 57 | bool send_request) { |
| 58 | StreamSource* client = new StreamSource; |
| 59 | client->SetState(SS_OPEN); |
| 60 | server.HandleConnection(client); |
| 61 | EXPECT_FALSE(monitor.server_closed); |
| 62 | EXPECT_FALSE(monitor.transaction); |
| 63 | |
| 64 | if (send_request) { |
| 65 | // Simulate a request |
| 66 | client->QueueString(kRequest); |
| 67 | EXPECT_FALSE(monitor.server_closed); |
| 68 | } |
| 69 | } |
deadbeef | f137e97 | 2017-03-23 15:45:49 -0700 | [diff] [blame] | 70 | } // anonymous namespace |
| 71 | |
| 72 | TEST(HttpServer, DoesNotSignalCloseUnlessCloseAllIsCalled) { |
| 73 | HttpServer server; |
| 74 | HttpServerMonitor monitor(&server); |
| 75 | // Add an active client connection |
| 76 | CreateClientConnection(server, monitor, true); |
| 77 | // Simulate a response |
| 78 | ASSERT_TRUE(nullptr != monitor.transaction); |
| 79 | server.Respond(monitor.transaction); |
| 80 | EXPECT_FALSE(monitor.transaction); |
| 81 | // Connection has closed, but no server close signal |
| 82 | EXPECT_FALSE(monitor.server_closed); |
| 83 | EXPECT_TRUE(monitor.connection_closed); |
| 84 | } |
| 85 | |
| 86 | TEST(HttpServer, SignalsCloseWhenNoConnectionsAreActive) { |
| 87 | HttpServer server; |
| 88 | HttpServerMonitor monitor(&server); |
| 89 | // Add an idle client connection |
| 90 | CreateClientConnection(server, monitor, false); |
| 91 | // Perform graceful close |
| 92 | server.CloseAll(false); |
| 93 | // Connections have all closed |
| 94 | EXPECT_TRUE(monitor.server_closed); |
| 95 | EXPECT_TRUE(monitor.connection_closed); |
| 96 | } |
| 97 | |
| 98 | TEST(HttpServer, SignalsCloseAfterGracefulCloseAll) { |
| 99 | HttpServer server; |
| 100 | HttpServerMonitor monitor(&server); |
| 101 | // Add an active client connection |
| 102 | CreateClientConnection(server, monitor, true); |
| 103 | // Initiate a graceful close |
| 104 | server.CloseAll(false); |
| 105 | EXPECT_FALSE(monitor.server_closed); |
| 106 | // Simulate a response |
| 107 | ASSERT_TRUE(nullptr != monitor.transaction); |
| 108 | server.Respond(monitor.transaction); |
| 109 | EXPECT_FALSE(monitor.transaction); |
| 110 | // Connections have all closed |
| 111 | EXPECT_TRUE(monitor.server_closed); |
| 112 | EXPECT_TRUE(monitor.connection_closed); |
| 113 | } |
| 114 | |
| 115 | TEST(HttpServer, SignalsCloseAfterForcedCloseAll) { |
| 116 | HttpServer server; |
| 117 | HttpServerMonitor monitor(&server); |
| 118 | // Add an active client connection |
| 119 | CreateClientConnection(server, monitor, true); |
| 120 | // Initiate a forceful close |
| 121 | server.CloseAll(true); |
| 122 | // Connections have all closed |
| 123 | EXPECT_TRUE(monitor.server_closed); |
| 124 | EXPECT_TRUE(monitor.connection_closed); |
| 125 | } |
| 126 | |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 127 | } // namespace rtc |