| 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 <algorithm> |
| 6 #include <utility> | 6 #include <utility> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 182 HttpServerTest() : quit_after_request_count_(0) {} | 182 HttpServerTest() : quit_after_request_count_(0) {} |
| 183 | 183 |
| 184 virtual void SetUp() override { | 184 virtual void SetUp() override { |
| 185 scoped_ptr<ServerSocket> server_socket( | 185 scoped_ptr<ServerSocket> server_socket( |
| 186 new TCPServerSocket(NULL, net::NetLog::Source())); | 186 new TCPServerSocket(NULL, net::NetLog::Source())); |
| 187 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); | 187 server_socket->ListenWithAddressAndPort("127.0.0.1", 0, 1); |
| 188 server_.reset(new HttpServer(server_socket.Pass(), this)); | 188 server_.reset(new HttpServer(server_socket.Pass(), this)); |
| 189 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); | 189 ASSERT_EQ(OK, server_->GetLocalAddress(&server_address_)); |
| 190 } | 190 } |
| 191 | 191 |
| 192 virtual void OnConnect(int connection_id) override {} | 192 void OnConnect(int connection_id) override {} |
| 193 | 193 |
| 194 virtual void OnHttpRequest(int connection_id, | 194 void OnHttpRequest(int connection_id, |
| 195 const HttpServerRequestInfo& info) override { | 195 const HttpServerRequestInfo& info) override { |
| 196 requests_.push_back(std::make_pair(info, connection_id)); | 196 requests_.push_back(std::make_pair(info, connection_id)); |
| 197 if (requests_.size() == quit_after_request_count_) | 197 if (requests_.size() == quit_after_request_count_) |
| 198 run_loop_quit_func_.Run(); | 198 run_loop_quit_func_.Run(); |
| 199 } | 199 } |
| 200 | 200 |
| 201 virtual void OnWebSocketRequest(int connection_id, | 201 void OnWebSocketRequest(int connection_id, |
| 202 const HttpServerRequestInfo& info) override { | 202 const HttpServerRequestInfo& info) override { |
| 203 NOTREACHED(); | 203 NOTREACHED(); |
| 204 } | 204 } |
| 205 | 205 |
| 206 virtual void OnWebSocketMessage(int connection_id, | 206 void OnWebSocketMessage(int connection_id, const std::string& data) override { |
| 207 const std::string& data) override { | |
| 208 NOTREACHED(); | 207 NOTREACHED(); |
| 209 } | 208 } |
| 210 | 209 |
| 211 virtual void OnClose(int connection_id) override {} | 210 void OnClose(int connection_id) override {} |
| 212 | 211 |
| 213 bool RunUntilRequestsReceived(size_t count) { | 212 bool RunUntilRequestsReceived(size_t count) { |
| 214 quit_after_request_count_ = count; | 213 quit_after_request_count_ = count; |
| 215 if (requests_.size() == count) | 214 if (requests_.size() == count) |
| 216 return true; | 215 return true; |
| 217 | 216 |
| 218 base::RunLoop run_loop; | 217 base::RunLoop run_loop; |
| 219 run_loop_quit_func_ = run_loop.QuitClosure(); | 218 run_loop_quit_func_ = run_loop.QuitClosure(); |
| 220 bool success = RunLoopWithTimeout(&run_loop); | 219 bool success = RunLoopWithTimeout(&run_loop); |
| 221 run_loop_quit_func_.Reset(); | 220 run_loop_quit_func_.Reset(); |
| (...skipping 19 matching lines...) Expand all Loading... |
| 241 base::Closure run_loop_quit_func_; | 240 base::Closure run_loop_quit_func_; |
| 242 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; | 241 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
| 243 | 242 |
| 244 private: | 243 private: |
| 245 size_t quit_after_request_count_; | 244 size_t quit_after_request_count_; |
| 246 }; | 245 }; |
| 247 | 246 |
| 248 namespace { | 247 namespace { |
| 249 | 248 |
| 250 class WebSocketTest : public HttpServerTest { | 249 class WebSocketTest : public HttpServerTest { |
| 251 virtual void OnHttpRequest(int connection_id, | 250 void OnHttpRequest(int connection_id, |
| 252 const HttpServerRequestInfo& info) override { | 251 const HttpServerRequestInfo& info) override { |
| 253 NOTREACHED(); | 252 NOTREACHED(); |
| 254 } | 253 } |
| 255 | 254 |
| 256 virtual void OnWebSocketRequest(int connection_id, | 255 void OnWebSocketRequest(int connection_id, |
| 257 const HttpServerRequestInfo& info) override { | 256 const HttpServerRequestInfo& info) override { |
| 258 HttpServerTest::OnHttpRequest(connection_id, info); | 257 HttpServerTest::OnHttpRequest(connection_id, info); |
| 259 } | 258 } |
| 260 | 259 |
| 261 virtual void OnWebSocketMessage(int connection_id, | 260 void OnWebSocketMessage(int connection_id, const std::string& data) override { |
| 262 const std::string& data) override { | |
| 263 } | 261 } |
| 264 }; | 262 }; |
| 265 | 263 |
| 266 TEST_F(HttpServerTest, Request) { | 264 TEST_F(HttpServerTest, Request) { |
| 267 TestHttpClient client; | 265 TestHttpClient client; |
| 268 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 266 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 269 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 267 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 270 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 268 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 271 ASSERT_EQ("GET", GetRequest(0).method); | 269 ASSERT_EQ("GET", GetRequest(0).method); |
| 272 ASSERT_EQ("/test", GetRequest(0).path); | 270 ASSERT_EQ("/test", GetRequest(0).path); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 400 "Sec-WebSocket-Key: key\r\n" | 398 "Sec-WebSocket-Key: key\r\n" |
| 401 "\r\n"); | 399 "\r\n"); |
| 402 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 400 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 403 } | 401 } |
| 404 | 402 |
| 405 TEST_F(HttpServerTest, RequestWithTooLargeBody) { | 403 TEST_F(HttpServerTest, RequestWithTooLargeBody) { |
| 406 class TestURLFetcherDelegate : public URLFetcherDelegate { | 404 class TestURLFetcherDelegate : public URLFetcherDelegate { |
| 407 public: | 405 public: |
| 408 TestURLFetcherDelegate(const base::Closure& quit_loop_func) | 406 TestURLFetcherDelegate(const base::Closure& quit_loop_func) |
| 409 : quit_loop_func_(quit_loop_func) {} | 407 : quit_loop_func_(quit_loop_func) {} |
| 410 virtual ~TestURLFetcherDelegate() {} | 408 ~TestURLFetcherDelegate() override {} |
| 411 | 409 |
| 412 virtual void OnURLFetchComplete(const URLFetcher* source) override { | 410 void OnURLFetchComplete(const URLFetcher* source) override { |
| 413 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, source->GetResponseCode()); | 411 EXPECT_EQ(HTTP_INTERNAL_SERVER_ERROR, source->GetResponseCode()); |
| 414 quit_loop_func_.Run(); | 412 quit_loop_func_.Run(); |
| 415 } | 413 } |
| 416 | 414 |
| 417 private: | 415 private: |
| 418 base::Closure quit_loop_func_; | 416 base::Closure quit_loop_func_; |
| 419 }; | 417 }; |
| 420 | 418 |
| 421 base::RunLoop run_loop; | 419 base::RunLoop run_loop; |
| 422 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); | 420 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 } | 464 } |
| 467 | 465 |
| 468 class MockStreamSocket : public StreamSocket { | 466 class MockStreamSocket : public StreamSocket { |
| 469 public: | 467 public: |
| 470 MockStreamSocket() | 468 MockStreamSocket() |
| 471 : connected_(true), | 469 : connected_(true), |
| 472 read_buf_(NULL), | 470 read_buf_(NULL), |
| 473 read_buf_len_(0) {} | 471 read_buf_len_(0) {} |
| 474 | 472 |
| 475 // StreamSocket | 473 // StreamSocket |
| 476 virtual int Connect(const CompletionCallback& callback) override { | 474 int Connect(const CompletionCallback& callback) override { |
| 477 return ERR_NOT_IMPLEMENTED; | 475 return ERR_NOT_IMPLEMENTED; |
| 478 } | 476 } |
| 479 virtual void Disconnect() override { | 477 void Disconnect() override { |
| 480 connected_ = false; | 478 connected_ = false; |
| 481 if (!read_callback_.is_null()) { | 479 if (!read_callback_.is_null()) { |
| 482 read_buf_ = NULL; | 480 read_buf_ = NULL; |
| 483 read_buf_len_ = 0; | 481 read_buf_len_ = 0; |
| 484 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED); | 482 base::ResetAndReturn(&read_callback_).Run(ERR_CONNECTION_CLOSED); |
| 485 } | 483 } |
| 486 } | 484 } |
| 487 virtual bool IsConnected() const override { return connected_; } | 485 bool IsConnected() const override { return connected_; } |
| 488 virtual bool IsConnectedAndIdle() const override { return IsConnected(); } | 486 bool IsConnectedAndIdle() const override { return IsConnected(); } |
| 489 virtual int GetPeerAddress(IPEndPoint* address) const override { | 487 int GetPeerAddress(IPEndPoint* address) const override { |
| 490 return ERR_NOT_IMPLEMENTED; | 488 return ERR_NOT_IMPLEMENTED; |
| 491 } | 489 } |
| 492 virtual int GetLocalAddress(IPEndPoint* address) const override { | 490 int GetLocalAddress(IPEndPoint* address) const override { |
| 493 return ERR_NOT_IMPLEMENTED; | 491 return ERR_NOT_IMPLEMENTED; |
| 494 } | 492 } |
| 495 virtual const BoundNetLog& NetLog() const override { return net_log_; } | 493 const BoundNetLog& NetLog() const override { return net_log_; } |
| 496 virtual void SetSubresourceSpeculation() override {} | 494 void SetSubresourceSpeculation() override {} |
| 497 virtual void SetOmniboxSpeculation() override {} | 495 void SetOmniboxSpeculation() override {} |
| 498 virtual bool WasEverUsed() const override { return true; } | 496 bool WasEverUsed() const override { return true; } |
| 499 virtual bool UsingTCPFastOpen() const override { return false; } | 497 bool UsingTCPFastOpen() const override { return false; } |
| 500 virtual bool WasNpnNegotiated() const override { return false; } | 498 bool WasNpnNegotiated() const override { return false; } |
| 501 virtual NextProto GetNegotiatedProtocol() const override { | 499 NextProto GetNegotiatedProtocol() const override { return kProtoUnknown; } |
| 502 return kProtoUnknown; | 500 bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } |
| 503 } | |
| 504 virtual bool GetSSLInfo(SSLInfo* ssl_info) override { return false; } | |
| 505 | 501 |
| 506 // Socket | 502 // Socket |
| 507 virtual int Read(IOBuffer* buf, int buf_len, | 503 int Read(IOBuffer* buf, |
| 508 const CompletionCallback& callback) override { | 504 int buf_len, |
| 505 const CompletionCallback& callback) override { |
| 509 if (!connected_) { | 506 if (!connected_) { |
| 510 return ERR_SOCKET_NOT_CONNECTED; | 507 return ERR_SOCKET_NOT_CONNECTED; |
| 511 } | 508 } |
| 512 if (pending_read_data_.empty()) { | 509 if (pending_read_data_.empty()) { |
| 513 read_buf_ = buf; | 510 read_buf_ = buf; |
| 514 read_buf_len_ = buf_len; | 511 read_buf_len_ = buf_len; |
| 515 read_callback_ = callback; | 512 read_callback_ = callback; |
| 516 return ERR_IO_PENDING; | 513 return ERR_IO_PENDING; |
| 517 } | 514 } |
| 518 DCHECK_GT(buf_len, 0); | 515 DCHECK_GT(buf_len, 0); |
| 519 int read_len = std::min(static_cast<int>(pending_read_data_.size()), | 516 int read_len = std::min(static_cast<int>(pending_read_data_.size()), |
| 520 buf_len); | 517 buf_len); |
| 521 memcpy(buf->data(), pending_read_data_.data(), read_len); | 518 memcpy(buf->data(), pending_read_data_.data(), read_len); |
| 522 pending_read_data_.erase(0, read_len); | 519 pending_read_data_.erase(0, read_len); |
| 523 return read_len; | 520 return read_len; |
| 524 } | 521 } |
| 525 virtual int Write(IOBuffer* buf, int buf_len, | 522 int Write(IOBuffer* buf, |
| 526 const CompletionCallback& callback) override { | 523 int buf_len, |
| 524 const CompletionCallback& callback) override { |
| 527 return ERR_NOT_IMPLEMENTED; | 525 return ERR_NOT_IMPLEMENTED; |
| 528 } | 526 } |
| 529 virtual int SetReceiveBufferSize(int32 size) override { | 527 int SetReceiveBufferSize(int32 size) override { return ERR_NOT_IMPLEMENTED; } |
| 530 return ERR_NOT_IMPLEMENTED; | 528 int SetSendBufferSize(int32 size) override { return ERR_NOT_IMPLEMENTED; } |
| 531 } | |
| 532 virtual int SetSendBufferSize(int32 size) override { | |
| 533 return ERR_NOT_IMPLEMENTED; | |
| 534 } | |
| 535 | 529 |
| 536 void DidRead(const char* data, int data_len) { | 530 void DidRead(const char* data, int data_len) { |
| 537 if (!read_buf_.get()) { | 531 if (!read_buf_.get()) { |
| 538 pending_read_data_.append(data, data_len); | 532 pending_read_data_.append(data, data_len); |
| 539 return; | 533 return; |
| 540 } | 534 } |
| 541 int read_len = std::min(data_len, read_buf_len_); | 535 int read_len = std::min(data_len, read_buf_len_); |
| 542 memcpy(read_buf_->data(), data, read_len); | 536 memcpy(read_buf_->data(), data, read_len); |
| 543 pending_read_data_.assign(data + read_len, data_len - read_len); | 537 pending_read_data_.assign(data + read_len, data_len - read_len); |
| 544 read_buf_ = NULL; | 538 read_buf_ = NULL; |
| 545 read_buf_len_ = 0; | 539 read_buf_len_ = 0; |
| 546 base::ResetAndReturn(&read_callback_).Run(read_len); | 540 base::ResetAndReturn(&read_callback_).Run(read_len); |
| 547 } | 541 } |
| 548 | 542 |
| 549 private: | 543 private: |
| 550 virtual ~MockStreamSocket() {} | 544 ~MockStreamSocket() override {} |
| 551 | 545 |
| 552 bool connected_; | 546 bool connected_; |
| 553 scoped_refptr<IOBuffer> read_buf_; | 547 scoped_refptr<IOBuffer> read_buf_; |
| 554 int read_buf_len_; | 548 int read_buf_len_; |
| 555 CompletionCallback read_callback_; | 549 CompletionCallback read_callback_; |
| 556 std::string pending_read_data_; | 550 std::string pending_read_data_; |
| 557 BoundNetLog net_log_; | 551 BoundNetLog net_log_; |
| 558 | 552 |
| 559 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket); | 553 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket); |
| 560 }; | 554 }; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 614 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 608 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
| 615 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 609 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
| 616 std::string response3; | 610 std::string response3; |
| 617 ASSERT_TRUE(client.ReadResponse(&response3)); | 611 ASSERT_TRUE(client.ReadResponse(&response3)); |
| 618 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 612 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
| 619 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 613 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
| 620 } | 614 } |
| 621 | 615 |
| 622 class CloseOnConnectHttpServerTest : public HttpServerTest { | 616 class CloseOnConnectHttpServerTest : public HttpServerTest { |
| 623 public: | 617 public: |
| 624 virtual void OnConnect(int connection_id) override { | 618 void OnConnect(int connection_id) override { |
| 625 connection_ids_.push_back(connection_id); | 619 connection_ids_.push_back(connection_id); |
| 626 server_->Close(connection_id); | 620 server_->Close(connection_id); |
| 627 } | 621 } |
| 628 | 622 |
| 629 protected: | 623 protected: |
| 630 std::vector<int> connection_ids_; | 624 std::vector<int> connection_ids_; |
| 631 }; | 625 }; |
| 632 | 626 |
| 633 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { | 627 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { |
| 634 TestHttpClient client; | 628 TestHttpClient client; |
| 635 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 629 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 636 client.Send("GET / HTTP/1.1\r\n\r\n"); | 630 client.Send("GET / HTTP/1.1\r\n\r\n"); |
| 637 ASSERT_FALSE(RunUntilRequestsReceived(1)); | 631 ASSERT_FALSE(RunUntilRequestsReceived(1)); |
| 638 ASSERT_EQ(1ul, connection_ids_.size()); | 632 ASSERT_EQ(1ul, connection_ids_.size()); |
| 639 ASSERT_EQ(0ul, requests_.size()); | 633 ASSERT_EQ(0ul, requests_.size()); |
| 640 } | 634 } |
| 641 | 635 |
| 642 } // namespace | 636 } // namespace |
| 643 | 637 |
| 644 } // namespace net | 638 } // namespace net |
| OLD | NEW |