Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include <algorithm> | |
| 5 #include <utility> | 6 #include <utility> |
| 6 #include <vector> | 7 #include <vector> |
| 7 | 8 |
| 8 #include "base/bind.h" | 9 #include "base/bind.h" |
| 9 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/callback_helpers.h" | |
| 10 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 11 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 12 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 13 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 14 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 15 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 16 #include "base/message_loop/message_loop_proxy.h" | 18 #include "base/message_loop/message_loop_proxy.h" |
| 17 #include "base/run_loop.h" | 19 #include "base/run_loop.h" |
| 20 #include "base/strings/string_number_conversions.h" | |
| 18 #include "base/strings/string_split.h" | 21 #include "base/strings/string_split.h" |
| 19 #include "base/strings/string_util.h" | 22 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 23 #include "base/strings/stringprintf.h" |
| 21 #include "base/time/time.h" | 24 #include "base/time/time.h" |
| 22 #include "net/base/address_list.h" | 25 #include "net/base/address_list.h" |
| 23 #include "net/base/io_buffer.h" | 26 #include "net/base/io_buffer.h" |
| 24 #include "net/base/ip_endpoint.h" | 27 #include "net/base/ip_endpoint.h" |
| 25 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 26 #include "net/base/net_log.h" | 29 #include "net/base/net_log.h" |
| 30 #include "net/base/net_util.h" | |
| 27 #include "net/base/test_completion_callback.h" | 31 #include "net/base/test_completion_callback.h" |
| 28 #include "net/server/http_server.h" | 32 #include "net/server/http_server.h" |
| 29 #include "net/server/http_server_request_info.h" | 33 #include "net/server/http_server_request_info.h" |
| 30 #include "net/socket/tcp_client_socket.h" | 34 #include "net/socket/tcp_client_socket.h" |
| 31 #include "net/socket/tcp_listen_socket.h" | 35 #include "net/socket/tcp_server_socket.h" |
| 32 #include "net/url_request/url_fetcher.h" | 36 #include "net/url_request/url_fetcher.h" |
| 33 #include "net/url_request/url_fetcher_delegate.h" | 37 #include "net/url_request/url_fetcher_delegate.h" |
| 34 #include "net/url_request/url_request_context.h" | 38 #include "net/url_request/url_request_context.h" |
| 35 #include "net/url_request/url_request_context_getter.h" | 39 #include "net/url_request/url_request_context_getter.h" |
| 36 #include "net/url_request/url_request_test_util.h" | 40 #include "net/url_request/url_request_test_util.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 41 #include "testing/gtest/include/gtest/gtest.h" |
| 38 | 42 |
| 39 namespace net { | 43 namespace net { |
| 40 | 44 |
| 41 namespace { | 45 namespace { |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 83 return ERR_TIMED_OUT; | 87 return ERR_TIMED_OUT; |
| 84 return connect_result_; | 88 return connect_result_; |
| 85 } | 89 } |
| 86 | 90 |
| 87 void Send(const std::string& data) { | 91 void Send(const std::string& data) { |
| 88 write_buffer_ = | 92 write_buffer_ = |
| 89 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); | 93 new DrainableIOBuffer(new StringIOBuffer(data), data.length()); |
| 90 Write(); | 94 Write(); |
| 91 } | 95 } |
| 92 | 96 |
| 93 bool Read(std::string* message) { | |
| 94 return Read(message, 1); | |
| 95 } | |
| 96 | |
| 97 bool Read(std::string* message, int expected_bytes) { | 97 bool Read(std::string* message, int expected_bytes) { |
| 98 int total_bytes_received = 0; | 98 int total_bytes_received = 0; |
| 99 message->clear(); | 99 message->clear(); |
| 100 while (total_bytes_received < expected_bytes) { | 100 while (total_bytes_received < expected_bytes) { |
| 101 net::TestCompletionCallback callback; | 101 net::TestCompletionCallback callback; |
| 102 ReadInternal(callback.callback()); | 102 ReadInternal(callback.callback()); |
| 103 int bytes_received = callback.WaitForResult(); | 103 int bytes_received = callback.WaitForResult(); |
| 104 if (bytes_received <= 0) | 104 if (bytes_received <= 0) |
| 105 return false; | 105 return false; |
| 106 | 106 |
| 107 total_bytes_received += bytes_received; | 107 total_bytes_received += bytes_received; |
| 108 message->append(read_buffer_->data(), bytes_received); | 108 message->append(read_buffer_->data(), bytes_received); |
| 109 } | 109 } |
| 110 return true; | 110 return true; |
| 111 } | 111 } |
| 112 | 112 |
| 113 bool ReadResponse(std::string* message) { | |
| 114 if (!Read(message, 1)) | |
| 115 return false; | |
|
mmenke
2014/08/25 18:19:54
I don't think this is needed.
byungchul
2014/08/25 21:11:25
I wanted to read data directly in message first. I
| |
| 116 while (!IsCompleteResponse(*message)) { | |
| 117 std::string chunk; | |
| 118 if (!Read(&chunk, 1)) | |
| 119 return false; | |
|
mmenke
2014/08/25 18:19:54
optional: May be simpler to read one byte at a ti
byungchul
2014/08/25 21:11:25
Then, we need another Read() to read only the exac
| |
| 120 message->append(chunk); | |
| 121 } | |
| 122 return true; | |
| 123 } | |
| 124 | |
| 113 private: | 125 private: |
| 114 void OnConnect(const base::Closure& quit_loop, int result) { | 126 void OnConnect(const base::Closure& quit_loop, int result) { |
| 115 connect_result_ = result; | 127 connect_result_ = result; |
| 116 quit_loop.Run(); | 128 quit_loop.Run(); |
| 117 } | 129 } |
| 118 | 130 |
| 119 void Write() { | 131 void Write() { |
| 120 int result = socket_->Write( | 132 int result = socket_->Write( |
| 121 write_buffer_.get(), | 133 write_buffer_.get(), |
| 122 write_buffer_->BytesRemaining(), | 134 write_buffer_->BytesRemaining(), |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 134 | 146 |
| 135 void ReadInternal(const net::CompletionCallback& callback) { | 147 void ReadInternal(const net::CompletionCallback& callback) { |
| 136 read_buffer_ = new IOBufferWithSize(kMaxExpectedResponseLength); | 148 read_buffer_ = new IOBufferWithSize(kMaxExpectedResponseLength); |
| 137 int result = socket_->Read(read_buffer_, | 149 int result = socket_->Read(read_buffer_, |
| 138 kMaxExpectedResponseLength, | 150 kMaxExpectedResponseLength, |
| 139 callback); | 151 callback); |
| 140 if (result != ERR_IO_PENDING) | 152 if (result != ERR_IO_PENDING) |
| 141 callback.Run(result); | 153 callback.Run(result); |
| 142 } | 154 } |
| 143 | 155 |
| 156 bool IsCompleteResponse(const std::string response) { | |
|
mmenke
2014/08/25 18:19:54
nit: std::string&
byungchul
2014/08/25 21:11:25
Done.
| |
| 157 // Check if CRLF exist. | |
| 158 size_t end_of_http_headers = response.find("\r\n\r\n"); | |
| 159 if (end_of_http_headers == std::string::npos) | |
| 160 return false; | |
| 161 end_of_http_headers += 4; // Skip CRLF CRLF. | |
| 162 | |
| 163 // Find content-length | |
| 164 size_t content_length_header_pos = | |
| 165 response.find(HttpRequestHeaders::kContentLength); | |
| 166 if (content_length_header_pos == std::string::npos) { | |
| 167 // Response doesn't have body. | |
| 168 return true; | |
| 169 } | |
| 170 if (content_length_header_pos > end_of_http_headers) { | |
| 171 // Content-length of next response. No body for the first response. | |
| 172 return true; | |
| 173 } | |
|
mmenke
2014/08/25 18:19:54
I don't think this is needed? We only read in one
byungchul
2014/08/25 21:11:25
Not necessary any more.
| |
| 174 | |
| 175 // Parse content-length. Don't expect a malformed value. | |
| 176 size_t content_length_value_pos = | |
| 177 response.find(':', content_length_header_pos); | |
| 178 DCHECK_NE(std::string::npos, content_length_value_pos); | |
| 179 ++content_length_value_pos; // Skip ':'. | |
| 180 DCHECK_GT(end_of_http_headers, content_length_value_pos); | |
|
mmenke
2014/08/25 18:19:54
scoped_refptr<HttpResponseHeaders> headers(
ne
byungchul
2014/08/25 21:11:25
Done.
| |
| 181 | |
| 182 int content_length = 0; | |
| 183 // StringToInt() returns false because response doesn't end with content | |
| 184 // length value. | |
| 185 DCHECK(!base::StringToInt(response.substr(content_length_value_pos), | |
| 186 &content_length)); | |
| 187 DCHECK_LE(0, content_length); | |
| 188 | |
| 189 int body_size = static_cast<int>(response.size() - end_of_http_headers); | |
| 190 DCHECK_LE(0, body_size); | |
| 191 // Return true if response has data equal to or more than content length. | |
| 192 return body_size >= content_length; | |
| 193 } | |
| 194 | |
| 144 scoped_refptr<IOBufferWithSize> read_buffer_; | 195 scoped_refptr<IOBufferWithSize> read_buffer_; |
| 145 scoped_refptr<DrainableIOBuffer> write_buffer_; | 196 scoped_refptr<DrainableIOBuffer> write_buffer_; |
| 146 scoped_ptr<TCPClientSocket> socket_; | 197 scoped_ptr<TCPClientSocket> socket_; |
| 147 int connect_result_; | 198 int connect_result_; |
| 148 }; | 199 }; |
| 149 | 200 |
| 150 } // namespace | 201 } // namespace |
| 151 | 202 |
| 152 class HttpServerTest : public testing::Test, | 203 class HttpServerTest : public testing::Test, |
| 153 public HttpServer::Delegate { | 204 public HttpServer::Delegate { |
| 154 public: | 205 public: |
| 155 HttpServerTest() : quit_after_request_count_(0) {} | 206 HttpServerTest() : quit_after_request_count_(0) {} |
| 156 | 207 |
| 157 virtual void SetUp() OVERRIDE { | 208 virtual void SetUp() OVERRIDE { |
| 158 TCPListenSocketFactory socket_factory("127.0.0.1", 0); | 209 scoped_ptr<ServerSocket> server_socket( |
| 159 server_ = new HttpServer(socket_factory, this); | 210 new TCPServerSocket(NULL, net::NetLog::Source())); |
| 211 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); | |
| 212 server_.reset(new HttpServer(server_socket.Pass(), this)); | |
| 160 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); | 213 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); |
| 161 } | 214 } |
| 162 | 215 |
| 163 virtual void OnHttpRequest(int connection_id, | 216 virtual void OnHttpRequest(int connection_id, |
| 164 const HttpServerRequestInfo& info) OVERRIDE { | 217 const HttpServerRequestInfo& info) OVERRIDE { |
| 165 requests_.push_back(std::make_pair(info, connection_id)); | 218 requests_.push_back(std::make_pair(info, connection_id)); |
| 166 if (requests_.size() == quit_after_request_count_) | 219 if (requests_.size() == quit_after_request_count_) |
| 167 run_loop_quit_func_.Run(); | 220 run_loop_quit_func_.Run(); |
| 168 } | 221 } |
| 169 | 222 |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 192 } | 245 } |
| 193 | 246 |
| 194 HttpServerRequestInfo GetRequest(size_t request_index) { | 247 HttpServerRequestInfo GetRequest(size_t request_index) { |
| 195 return requests_[request_index].first; | 248 return requests_[request_index].first; |
| 196 } | 249 } |
| 197 | 250 |
| 198 int GetConnectionId(size_t request_index) { | 251 int GetConnectionId(size_t request_index) { |
| 199 return requests_[request_index].second; | 252 return requests_[request_index].second; |
| 200 } | 253 } |
| 201 | 254 |
| 255 void HandleAcceptResult(scoped_ptr<StreamSocket> socket) { | |
| 256 server_->accepted_socket_.reset(socket.release()); | |
| 257 server_->HandleAcceptResult(OK); | |
| 258 } | |
| 259 | |
| 202 protected: | 260 protected: |
| 203 scoped_refptr<HttpServer> server_; | 261 scoped_ptr<HttpServer> server_; |
| 204 IPEndPoint server_address_; | 262 IPEndPoint server_address_; |
| 205 base::Closure run_loop_quit_func_; | 263 base::Closure run_loop_quit_func_; |
| 206 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; | 264 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
| 207 | 265 |
| 208 private: | 266 private: |
| 209 size_t quit_after_request_count_; | 267 size_t quit_after_request_count_; |
| 210 }; | 268 }; |
| 211 | 269 |
| 212 class WebSocketTest : public HttpServerTest { | 270 class WebSocketTest : public HttpServerTest { |
| 213 virtual void OnHttpRequest(int connection_id, | 271 virtual void OnHttpRequest(int connection_id, |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 400 } | 458 } |
| 401 | 459 |
| 402 TEST_F(HttpServerTest, Send200) { | 460 TEST_F(HttpServerTest, Send200) { |
| 403 TestHttpClient client; | 461 TestHttpClient client; |
| 404 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 462 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 405 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 463 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 406 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 464 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 407 server_->Send200(GetConnectionId(0), "Response!", "text/plain"); | 465 server_->Send200(GetConnectionId(0), "Response!", "text/plain"); |
| 408 | 466 |
| 409 std::string response; | 467 std::string response; |
| 410 ASSERT_TRUE(client.Read(&response)); | 468 ASSERT_TRUE(client.ReadResponse(&response)); |
| 411 ASSERT_TRUE(StartsWithASCII(response, "HTTP/1.1 200 OK", true)); | 469 ASSERT_TRUE(StartsWithASCII(response, "HTTP/1.1 200 OK", true)); |
| 412 ASSERT_TRUE(EndsWith(response, "Response!", true)); | 470 ASSERT_TRUE(EndsWith(response, "Response!", true)); |
| 413 } | 471 } |
| 414 | 472 |
| 415 TEST_F(HttpServerTest, SendRaw) { | 473 TEST_F(HttpServerTest, SendRaw) { |
| 416 TestHttpClient client; | 474 TestHttpClient client; |
| 417 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 475 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 418 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 476 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 419 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 477 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 420 server_->SendRaw(GetConnectionId(0), "Raw Data "); | 478 server_->SendRaw(GetConnectionId(0), "Raw Data "); |
| 421 server_->SendRaw(GetConnectionId(0), "More Data"); | 479 server_->SendRaw(GetConnectionId(0), "More Data"); |
| 422 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); | 480 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); |
| 423 | 481 |
| 424 const std::string expected_response("Raw Data More DataThird Piece of Data"); | 482 const std::string expected_response("Raw Data More DataThird Piece of Data"); |
| 425 std::string response; | 483 std::string response; |
| 426 ASSERT_TRUE(client.Read(&response, expected_response.length())); | 484 ASSERT_TRUE(client.Read(&response, expected_response.length())); |
| 427 ASSERT_EQ(expected_response, response); | 485 ASSERT_EQ(expected_response, response); |
| 428 } | 486 } |
| 429 | 487 |
| 430 namespace { | 488 namespace { |
| 431 | 489 |
| 432 class MockStreamListenSocket : public StreamListenSocket { | 490 class MockStreamSocket : public StreamSocket { |
| 433 public: | 491 public: |
| 434 MockStreamListenSocket(StreamListenSocket::Delegate* delegate) | 492 MockStreamSocket() |
| 435 : StreamListenSocket(kInvalidSocket, delegate) {} | 493 : connected_(true), |
| 494 read_buf_(NULL), | |
| 495 read_buf_len_(0) {} | |
| 436 | 496 |
| 437 virtual void Accept() OVERRIDE { NOTREACHED(); } | 497 // StreamSocket |
| 498 virtual int Connect(const CompletionCallback& callback) OVERRIDE { | |
| 499 return ERR_NOT_IMPLEMENTED; | |
| 500 } | |
| 501 virtual void Disconnect() OVERRIDE { | |
| 502 connected_ = false; | |
| 503 if (!read_callback_.is_null()) { | |
| 504 read_buf_ = NULL; | |
| 505 read_buf_len_ = 0; | |
| 506 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED); | |
| 507 } | |
| 508 } | |
| 509 virtual bool IsConnected() const OVERRIDE { return connected_; } | |
| 510 virtual bool IsConnectedAndIdle() const OVERRIDE { return IsConnected(); } | |
| 511 virtual int GetPeerAddress(IPEndPoint* address) const OVERRIDE { | |
| 512 return ERR_NOT_IMPLEMENTED; | |
| 513 } | |
| 514 virtual int GetLocalAddress(IPEndPoint* address) const OVERRIDE { | |
| 515 return ERR_NOT_IMPLEMENTED; | |
| 516 } | |
| 517 virtual const BoundNetLog& NetLog() const OVERRIDE { return net_log_; } | |
| 518 virtual void SetSubresourceSpeculation() OVERRIDE {} | |
| 519 virtual void SetOmniboxSpeculation() OVERRIDE {} | |
| 520 virtual bool WasEverUsed() const OVERRIDE { return true; } | |
| 521 virtual bool UsingTCPFastOpen() const OVERRIDE { return false; } | |
| 522 virtual bool WasNpnNegotiated() const OVERRIDE { return false; } | |
| 523 virtual NextProto GetNegotiatedProtocol() const OVERRIDE { | |
| 524 return kProtoUnknown; | |
| 525 } | |
| 526 virtual bool GetSSLInfo(SSLInfo* ssl_info) OVERRIDE { return false; } | |
| 527 | |
| 528 // Socket | |
| 529 virtual int Read(IOBuffer* buf, int buf_len, | |
| 530 const CompletionCallback& callback) OVERRIDE { | |
| 531 if (!connected_) { | |
| 532 return ERR_SOCKET_NOT_CONNECTED; | |
| 533 } | |
| 534 if (pending_read_data_.empty()) { | |
| 535 read_buf_ = buf; | |
| 536 read_buf_len_ = buf_len; | |
| 537 read_callback_ = callback; | |
| 538 return ERR_IO_PENDING; | |
| 539 } | |
| 540 DCHECK_GT(buf_len, 0); | |
| 541 int read_len = std::min(static_cast<int>(pending_read_data_.size()), | |
| 542 buf_len); | |
| 543 memcpy(buf->data(), pending_read_data_.data(), read_len); | |
| 544 pending_read_data_.erase(0, read_len); | |
| 545 return read_len; | |
| 546 } | |
| 547 virtual int Write(IOBuffer* buf, int buf_len, | |
| 548 const CompletionCallback& callback) OVERRIDE { | |
| 549 return ERR_NOT_IMPLEMENTED; | |
| 550 } | |
| 551 virtual int SetReceiveBufferSize(int32 size) OVERRIDE { | |
| 552 return ERR_NOT_IMPLEMENTED; | |
| 553 } | |
| 554 virtual int SetSendBufferSize(int32 size) OVERRIDE { | |
| 555 return ERR_NOT_IMPLEMENTED; | |
| 556 } | |
| 557 | |
| 558 void DidRead(const char* data, int data_len) { | |
| 559 if (!read_buf_) { | |
| 560 pending_read_data_.append(data, data_len); | |
| 561 return; | |
| 562 } | |
| 563 int read_len = std::min(data_len, read_buf_len_); | |
| 564 memcpy(read_buf_->data(), data, read_len); | |
| 565 pending_read_data_.assign(data + read_len, data_len - read_len); | |
| 566 read_buf_ = NULL; | |
| 567 read_buf_len_ = 0; | |
| 568 base::ResetAndReturn(&read_callback_).Run(read_len); | |
| 569 } | |
| 438 | 570 |
| 439 private: | 571 private: |
| 440 virtual ~MockStreamListenSocket() {} | 572 virtual ~MockStreamSocket() {} |
| 573 | |
| 574 bool connected_; | |
| 575 scoped_refptr<IOBuffer> read_buf_; | |
| 576 int read_buf_len_; | |
| 577 CompletionCallback read_callback_; | |
| 578 std::string pending_read_data_; | |
| 579 BoundNetLog net_log_; | |
| 580 | |
| 581 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket); | |
| 441 }; | 582 }; |
| 442 | 583 |
| 443 } // namespace | 584 } // namespace |
| 444 | 585 |
| 445 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { | 586 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { |
| 446 StreamListenSocket* socket = | 587 MockStreamSocket* socket = new MockStreamSocket(); |
| 447 new MockStreamListenSocket(server_.get()); | 588 HandleAcceptResult(make_scoped_ptr<StreamSocket>(socket)); |
| 448 server_->DidAccept(NULL, make_scoped_ptr(socket)); | |
| 449 std::string body("body"); | 589 std::string body("body"); |
| 450 std::string request_text = base::StringPrintf( | 590 std::string request_text = base::StringPrintf( |
| 451 "GET /test HTTP/1.1\r\n" | 591 "GET /test HTTP/1.1\r\n" |
| 452 "SomeHeader: 1\r\n" | 592 "SomeHeader: 1\r\n" |
| 453 "Content-Length: %" PRIuS "\r\n\r\n%s", | 593 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 454 body.length(), | 594 body.length(), |
| 455 body.c_str()); | 595 body.c_str()); |
| 456 server_->DidRead(socket, request_text.c_str(), request_text.length() - 2); | 596 socket->DidRead(request_text.c_str(), request_text.length() - 2); |
| 457 ASSERT_EQ(0u, requests_.size()); | 597 ASSERT_EQ(0u, requests_.size()); |
| 458 server_->DidRead(socket, request_text.c_str() + request_text.length() - 2, 2); | 598 socket->DidRead(request_text.c_str() + request_text.length() - 2, 2); |
| 459 ASSERT_EQ(1u, requests_.size()); | 599 ASSERT_EQ(1u, requests_.size()); |
| 460 ASSERT_EQ(body, GetRequest(0).data); | 600 ASSERT_EQ(body, GetRequest(0).data); |
| 461 } | 601 } |
| 462 | 602 |
| 463 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { | 603 TEST_F(HttpServerTest, MultipleRequestsOnSameConnection) { |
| 464 // The idea behind this test is that requests with or without bodies should | 604 // The idea behind this test is that requests with or without bodies should |
| 465 // not break parsing of the next request. | 605 // not break parsing of the next request. |
| 466 TestHttpClient client; | 606 TestHttpClient client; |
| 467 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 607 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 468 std::string body = "body"; | 608 std::string body = "body"; |
| 469 client.Send(base::StringPrintf( | 609 client.Send(base::StringPrintf( |
| 470 "GET /test HTTP/1.1\r\n" | 610 "GET /test HTTP/1.1\r\n" |
| 471 "Content-Length: %" PRIuS "\r\n\r\n%s", | 611 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 472 body.length(), | 612 body.length(), |
| 473 body.c_str())); | 613 body.c_str())); |
| 474 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 614 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 475 ASSERT_EQ(body, GetRequest(0).data); | 615 ASSERT_EQ(body, GetRequest(0).data); |
| 476 | 616 |
| 477 int client_connection_id = GetConnectionId(0); | 617 int client_connection_id = GetConnectionId(0); |
| 478 server_->Send200(client_connection_id, "Content for /test", "text/plain"); | 618 server_->Send200(client_connection_id, "Content for /test", "text/plain"); |
| 479 std::string response1; | 619 std::string response1; |
| 480 ASSERT_TRUE(client.Read(&response1)); | 620 ASSERT_TRUE(client.ReadResponse(&response1)); |
| 481 ASSERT_TRUE(StartsWithASCII(response1, "HTTP/1.1 200 OK", true)); | 621 ASSERT_TRUE(StartsWithASCII(response1, "HTTP/1.1 200 OK", true)); |
| 482 ASSERT_TRUE(EndsWith(response1, "Content for /test", true)); | 622 ASSERT_TRUE(EndsWith(response1, "Content for /test", true)); |
| 483 | 623 |
| 484 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); | 624 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); |
| 485 ASSERT_TRUE(RunUntilRequestsReceived(2)); | 625 ASSERT_TRUE(RunUntilRequestsReceived(2)); |
| 486 ASSERT_EQ("/test2", GetRequest(1).path); | 626 ASSERT_EQ("/test2", GetRequest(1).path); |
| 487 | 627 |
| 488 ASSERT_EQ(client_connection_id, GetConnectionId(1)); | 628 ASSERT_EQ(client_connection_id, GetConnectionId(1)); |
| 489 server_->Send404(client_connection_id); | 629 server_->Send404(client_connection_id); |
| 490 std::string response2; | 630 std::string response2; |
| 491 ASSERT_TRUE(client.Read(&response2)); | 631 ASSERT_TRUE(client.ReadResponse(&response2)); |
| 492 ASSERT_TRUE(StartsWithASCII(response2, "HTTP/1.1 404 Not Found", true)); | 632 ASSERT_TRUE(StartsWithASCII(response2, "HTTP/1.1 404 Not Found", true)); |
| 493 | 633 |
| 494 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); | 634 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); |
| 495 ASSERT_TRUE(RunUntilRequestsReceived(3)); | 635 ASSERT_TRUE(RunUntilRequestsReceived(3)); |
| 496 ASSERT_EQ("/test3", GetRequest(2).path); | 636 ASSERT_EQ("/test3", GetRequest(2).path); |
| 497 | 637 |
| 498 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 638 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
| 499 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 639 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
| 500 std::string response3; | 640 std::string response3; |
| 501 ASSERT_TRUE(client.Read(&response3)); | 641 ASSERT_TRUE(client.ReadResponse(&response3)); |
| 502 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 642 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
| 503 #if 0 | |
| 504 // TODO(byungchul): Figure out why it fails in windows build bot. | |
| 505 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 643 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
| 506 #endif | |
| 507 } | 644 } |
| 508 | 645 |
| 509 } // namespace net | 646 } // namespace net |
| OLD | NEW |