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..1aaff76c5f3fdd6d5fc3121a086c4cca9d5177db 100644 |
| --- a/net/server/http_server_unittest.cc |
| +++ b/net/server/http_server_unittest.cc |
| @@ -191,7 +191,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( |
| @@ -225,6 +226,12 @@ 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_) { |
| + // Once we return, some DeleteSoon's may be posted, so we want to delay |
| + // exiting the test's event loop until right after that. |
| + base::ThreadTaskRunnerHandle::Get()->PostDelayedTask( |
| + FROM_HERE, run_loop_quit_func_, base::TimeDelta::FromMilliseconds(1)); |
|
mmenke
2017/01/20 15:55:11
Not a fan of this sort of fuziness, particularly i
Maks Orlovich
2017/01/20 16:38:28
That is a nicer option, indeed. Did that (in TearD
|
| + } |
| } |
| bool RunUntilRequestsReceived(size_t count) { |
| @@ -239,10 +246,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 |
| + 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 GetNumRequests() { return requests_.size(); } |
|
mmenke
2017/01/20 15:55:11
optional nit: Think it's a little more common to
mmenke
2017/01/20 15:55:11
nit const
Maks Orlovich
2017/01/20 16:38:28
Done.
Maks Orlovich
2017/01/20 16:38:28
Done.
|
| + |
| int GetConnectionId(size_t request_index) { |
| return requests_[request_index].second; |
| } |
| @@ -264,6 +288,7 @@ class HttpServerTest : public testing::Test, |
| private: |
| size_t quit_after_request_count_; |
| + int quit_on_close_connection_; |
| }; |
| namespace { |
| @@ -296,6 +321,14 @@ 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, GetNumRequests()); |
|
mmenke
2017/01/20 15:55:11
Wait for the client socket to be closed, and verif
Maks Orlovich
2017/01/20 16:38:28
WrongProtocolRequest did that, so I factored out a
|
| +} |
| + |
| TEST_F(HttpServerTest, RequestWithHeaders) { |
| TestHttpClient client; |
| ASSERT_THAT(client.ConnectAndWait(server_address_), IsOk()); |