Chromium Code Reviews| Index: net/server/http_server_unittest.cc |
| diff --git a/net/server/http_server_unittest.cc b/net/server/http_server_unittest.cc |
| index 9b6dee1bea9d3f0c2b1b179044eae736cf97294f..283786fc0f1bc7db94b5becb2b9018184b2da713 100644 |
| --- a/net/server/http_server_unittest.cc |
| +++ b/net/server/http_server_unittest.cc |
| @@ -133,6 +133,22 @@ class TestHttpClient { |
| return true; |
| } |
| + bool TestUsedThenDisconnectedWithNoData() { |
|
mmenke
2017/01/20 18:20:57
Rather than a bool, suggest:
void ExpectUsedThenD
Maks Orlovich
2017/01/20 19:00:20
Done.
|
| + // Check that the socket was opened... |
| + if (!socket_->WasEverUsed()) { |
| + return false; |
| + } |
|
mmenke
2017/01/20 18:20:57
nit: No braces when if + body take up one line ea
Maks Orlovich
2017/01/20 19:00:20
Made moot by above.
|
| + |
| + // ...then closed when the server disconnected. Verify that the socket was |
| + // closed by checking that a Read() fails. |
| + std::string response; |
| + if (Read(&response, 1u)) { |
| + return false; |
| + } |
|
mmenke
2017/01/20 18:20:57
nit: --braces
Maks Orlovich
2017/01/20 19:00:20
Likewise
|
| + |
| + return response.empty(); |
| + } |
| + |
| TCPClientSocket& socket() { return *socket_; } |
| private: |
| @@ -191,7 +207,8 @@ class TestHttpClient { |
| class HttpServerTest : public testing::Test, |
| public HttpServer::Delegate { |
| public: |
| - HttpServerTest() : quit_after_request_count_(0) {} |
| + HttpServerTest() |
| + : quit_after_request_count_(0), quit_on_close_connection_(-1) {} |
| void SetUp() override { |
| std::unique_ptr<ServerSocket> server_socket( |
| @@ -201,6 +218,12 @@ class HttpServerTest : public testing::Test, |
| ASSERT_THAT(server_->GetLocalAddress(&server_address_), IsOk()); |
| } |
| + void TearDown() override { |
| + // We need to run the event loop some to make sure that the memory |
|
mmenke
2017/01/20 18:20:57
nit: Avoid "we" in comments - it's a bit ambiguou
Maks Orlovich
2017/01/20 19:00:20
Done.
|
| + // handed over to DeleteSoon gets fully freed. |
| + base::RunLoop().RunUntilIdle(); |
| + } |
| + |
| void OnConnect(int connection_id) override { |
| DCHECK(connection_map_.find(connection_id) == connection_map_.end()); |
| connection_map_[connection_id] = true; |
| @@ -225,6 +248,9 @@ class HttpServerTest : public testing::Test, |
| void OnClose(int connection_id) override { |
| DCHECK(connection_map_.find(connection_id) != connection_map_.end()); |
| connection_map_[connection_id] = false; |
| + if (connection_id == quit_on_close_connection_) { |
| + run_loop_quit_func_.Run(); |
| + } |
|
mmenke
2017/01/20 18:20:57
nit: --braces
Maks Orlovich
2017/01/20 19:00:20
Done.
|
| } |
| bool RunUntilRequestsReceived(size_t count) { |
| @@ -239,10 +265,27 @@ class HttpServerTest : public testing::Test, |
| return success; |
| } |
| + bool RunUntilConnectionIdClosed(int connection_id) { |
| + quit_on_close_connection_ = connection_id; |
| + auto iter = connection_map_.find(connection_id); |
| + if (iter != connection_map_.end() && !iter->second) { |
| + // Already disconnected |
|
mmenke
2017/01/20 18:20:57
nit: Predominant style is to send more comment-y
Maks Orlovich
2017/01/20 19:00:20
Done.
|
| + return true; |
| + } |
| + |
| + base::RunLoop run_loop; |
| + run_loop_quit_func_ = run_loop.QuitClosure(); |
| + bool success = RunLoopWithTimeout(&run_loop); |
| + run_loop_quit_func_.Reset(); |
| + return success; |
| + } |
| + |
| HttpServerRequestInfo GetRequest(size_t request_index) { |
| return requests_[request_index].first; |
| } |
| + size_t num_requests() const { return requests_.size(); } |
| + |
| int GetConnectionId(size_t request_index) { |
| return requests_[request_index].second; |
| } |
| @@ -264,6 +307,7 @@ class HttpServerTest : public testing::Test, |
| private: |
| size_t quit_after_request_count_; |
| + int quit_on_close_connection_; |
| }; |
| namespace { |
| @@ -296,6 +340,15 @@ TEST_F(HttpServerTest, Request) { |
| base::CompareCase::SENSITIVE)); |
| } |
| +TEST_F(HttpServerTest, RequestBrokenTermination) { |
| + TestHttpClient client; |
| + ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| + client.Send("GET /test HTTP/1.1\r\n\r)"); |
| + ASSERT_TRUE(RunUntilConnectionIdClosed(1)); |
| + EXPECT_EQ(0u, num_requests()); |
| + EXPECT_TRUE(client.TestUsedThenDisconnectedWithNoData()); |
| +} |
| + |
| TEST_F(HttpServerTest, RequestWithHeaders) { |
| TestHttpClient client; |
| ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |
| @@ -503,14 +556,7 @@ TEST_F(HttpServerTest, WrongProtocolRequest) { |
| ASSERT_EQ(1u, connection_map().size()); |
| ASSERT_FALSE(connection_map().begin()->second); |
| - // Assert that the socket was opened... |
| - ASSERT_TRUE(client.socket().WasEverUsed()); |
| - |
| - // ...then closed when the server disconnected. Verify that the socket was |
| - // closed by checking that a Read() fails. |
| - std::string response; |
| - ASSERT_FALSE(client.Read(&response, 1u)); |
| - ASSERT_EQ(std::string(), response); |
| + ASSERT_TRUE(client.TestUsedThenDisconnectedWithNoData()); |
| // Reset the state of the connection map. |
| connection_map().clear(); |