blob: 0dde6c5bde512a47842ec3c136ce584eefd5db31 [file] [log] [blame]
deadbeeff137e972017-03-23 15:45:49 -07001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#include "rtc_base/httpserver.h"
12#include "rtc_base/gunit.h"
13#include "rtc_base/testutils.h"
deadbeeff137e972017-03-23 15:45:49 -070014
kwibergd0d81482017-04-18 03:18:22 -070015using namespace webrtc::testing;
deadbeeff137e972017-03-23 15:45:49 -070016
17namespace rtc {
18
19namespace {
Yves Gerey665174f2018-06-19 15:03:05 +020020const char* const kRequest =
deadbeeff137e972017-03-23 15:45:49 -070021 "GET /index.html HTTP/1.1\r\n"
22 "Host: localhost\r\n"
23 "\r\n";
24
Yves Gerey665174f2018-06-19 15:03:05 +020025struct HttpServerMonitor : public sigslot::has_slots<> {
26 HttpServerTransaction* transaction;
27 bool server_closed, connection_closed;
deadbeeff137e972017-03-23 15:45:49 -070028
Yves Gerey665174f2018-06-19 15:03:05 +020029 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);
deadbeeff137e972017-03-23 15:45:49 -070037 }
Yves Gerey665174f2018-06-19 15:03:05 +020038 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
55void 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}
deadbeeff137e972017-03-23 15:45:49 -070070} // anonymous namespace
71
72TEST(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
86TEST(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
98TEST(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
115TEST(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 Gerey665174f2018-06-19 15:03:05 +0200127} // namespace rtc