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 1de2100ab68fc20a5ba409a635cf11b060afc6cc..46dd0bad1ba7bef4b76f27f3343bb5d928ccd924 100644 |
| --- a/net/server/http_server_unittest.cc |
| +++ b/net/server/http_server_unittest.cc |
| @@ -38,6 +38,8 @@ namespace net { |
| namespace { |
| +int kMaxExpectedResponseLength = 2048; |
|
mef
2014/03/27 15:03:45
const
gunsch
2014/03/27 15:52:39
Done.
|
| + |
| void SetTimedOutAndQuitLoop(const base::WeakPtr<bool> timed_out, |
| const base::Closure& quit_loop_func) { |
| if (timed_out) { |
| @@ -86,6 +88,15 @@ class TestHttpClient { |
| Write(); |
| } |
| + bool Read(std::string* data) { |
| + base::RunLoop run_loop; |
| + Read(run_loop.QuitClosure()); |
| + bool success = RunLoopWithTimeout(&run_loop); |
| + std::string io_buffer_contents(read_buffer_->data()); |
| + *data = io_buffer_contents.substr(0, bytes_received_); |
| + return success; |
| + } |
| + |
| private: |
| void OnConnect(const base::Closure& quit_loop, int result) { |
| connect_result_ = result; |
| @@ -108,8 +119,26 @@ class TestHttpClient { |
| Write(); |
| } |
| + void Read(const base::Closure& quit_loop) { |
| + read_buffer_ = new IOBufferWithSize(kMaxExpectedResponseLength); |
| + int result = socket_->Read(read_buffer_, kMaxExpectedResponseLength, |
|
mef
2014/03/27 15:03:45
nit: I believe kMaxExpectedResponseLength goes on
gunsch
2014/03/27 15:52:39
Done.
|
| + base::Bind(&TestHttpClient::OnRead, |
| + base::Unretained(this), |
| + quit_loop)); |
| + if (result != ERR_IO_PENDING) |
| + OnRead(quit_loop, result); |
| + } |
| + |
| + void OnRead(const base::Closure& quit_loop, int result) { |
| + bytes_received_ = result; |
| + quit_loop.Run(); |
| + } |
| + |
| + base::Closure run_loop_quit_func_; |
|
Ryan Sleevi
2014/03/27 20:59:46
You shouldn't need this separate RunLoop helper. Y
gunsch
2014/03/27 23:41:17
Done, thanks for the tip. That's a lot simpler.
|
| + scoped_refptr<IOBufferWithSize> read_buffer_; |
| scoped_refptr<DrainableIOBuffer> write_buffer_; |
| scoped_ptr<TCPClientSocket> socket_; |
| + int bytes_received_; |
| int connect_result_; |
| }; |
| @@ -129,6 +158,7 @@ class HttpServerTest : public testing::Test, |
| virtual void OnHttpRequest(int connection_id, |
| const HttpServerRequestInfo& info) OVERRIDE { |
| requests_.push_back(info); |
| + connection_ids_.push_back(connection_id); |
| if (requests_.size() == quit_after_request_count_) |
| run_loop_quit_func_.Run(); |
| } |
| @@ -162,6 +192,7 @@ class HttpServerTest : public testing::Test, |
| IPEndPoint server_address_; |
| base::Closure run_loop_quit_func_; |
| std::vector<HttpServerRequestInfo> requests_; |
| + std::vector<int> connection_ids_; |
| private: |
| size_t quit_after_request_count_; |
| @@ -261,6 +292,33 @@ TEST_F(HttpServerTest, RequestWithTooLargeBody) { |
| ASSERT_EQ(0u, requests_.size()); |
| } |
| +TEST_F(HttpServerTest, Send200) { |
| + TestHttpClient client; |
| + ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| + client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| + ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| + server_->Send200(connection_ids_[0], "Response!", "text/plain"); |
| + |
| + std::string response; |
| + ASSERT_TRUE(client.Read(&response)); |
| + ASSERT_TRUE(StartsWithASCII(response, "HTTP/1.1 200 OK", true)); |
| + ASSERT_TRUE(response.find("Response!") != std::string::npos); |
| +} |
| + |
| +TEST_F(HttpServerTest, SendRaw) { |
| + TestHttpClient client; |
| + ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| + client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| + ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| + server_->SendRaw(connection_ids_[0], "Raw Data "); |
| + server_->SendRaw(connection_ids_[0], "More Data"); |
| + server_->SendRaw(connection_ids_[0], "Third Piece of Data"); |
| + |
| + std::string response; |
| + ASSERT_TRUE(client.Read(&response)); |
|
mef
2014/03/27 15:03:45
Would it make sense to also test a scenario where
gunsch
2014/03/27 15:52:39
Sure, I extended the MultipleRequestsOnSameConnect
gunsch
2014/03/27 18:40:32
I just went ahead and made this a std::pair. I thi
|
| + ASSERT_EQ("Raw Data More DataThird Piece of Data", response); |
| +} |
| + |
| namespace { |
| class MockStreamListenSocket : public StreamListenSocket { |