| 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 {} |
| 193 |
| 192 virtual void OnHttpRequest(int connection_id, | 194 virtual void OnHttpRequest(int connection_id, |
| 193 const HttpServerRequestInfo& info) OVERRIDE { | 195 const HttpServerRequestInfo& info) OVERRIDE { |
| 194 requests_.push_back(std::make_pair(info, connection_id)); | 196 requests_.push_back(std::make_pair(info, connection_id)); |
| 195 if (requests_.size() == quit_after_request_count_) | 197 if (requests_.size() == quit_after_request_count_) |
| 196 run_loop_quit_func_.Run(); | 198 run_loop_quit_func_.Run(); |
| 197 } | 199 } |
| 198 | 200 |
| 199 virtual void OnWebSocketRequest(int connection_id, | 201 virtual void OnWebSocketRequest(int connection_id, |
| 200 const HttpServerRequestInfo& info) OVERRIDE { | 202 const HttpServerRequestInfo& info) OVERRIDE { |
| 201 NOTREACHED(); | 203 NOTREACHED(); |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 236 protected: | 238 protected: |
| 237 scoped_ptr<HttpServer> server_; | 239 scoped_ptr<HttpServer> server_; |
| 238 IPEndPoint server_address_; | 240 IPEndPoint server_address_; |
| 239 base::Closure run_loop_quit_func_; | 241 base::Closure run_loop_quit_func_; |
| 240 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; | 242 std::vector<std::pair<HttpServerRequestInfo, int> > requests_; |
| 241 | 243 |
| 242 private: | 244 private: |
| 243 size_t quit_after_request_count_; | 245 size_t quit_after_request_count_; |
| 244 }; | 246 }; |
| 245 | 247 |
| 248 namespace { |
| 249 |
| 246 class WebSocketTest : public HttpServerTest { | 250 class WebSocketTest : public HttpServerTest { |
| 247 virtual void OnHttpRequest(int connection_id, | 251 virtual void OnHttpRequest(int connection_id, |
| 248 const HttpServerRequestInfo& info) OVERRIDE { | 252 const HttpServerRequestInfo& info) OVERRIDE { |
| 249 NOTREACHED(); | 253 NOTREACHED(); |
| 250 } | 254 } |
| 251 | 255 |
| 252 virtual void OnWebSocketRequest(int connection_id, | 256 virtual void OnWebSocketRequest(int connection_id, |
| 253 const HttpServerRequestInfo& info) OVERRIDE { | 257 const HttpServerRequestInfo& info) OVERRIDE { |
| 254 HttpServerTest::OnHttpRequest(connection_id, info); | 258 HttpServerTest::OnHttpRequest(connection_id, info); |
| 255 } | 259 } |
| (...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 454 server_->SendRaw(GetConnectionId(0), "Raw Data "); | 458 server_->SendRaw(GetConnectionId(0), "Raw Data "); |
| 455 server_->SendRaw(GetConnectionId(0), "More Data"); | 459 server_->SendRaw(GetConnectionId(0), "More Data"); |
| 456 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); | 460 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); |
| 457 | 461 |
| 458 const std::string expected_response("Raw Data More DataThird Piece of Data"); | 462 const std::string expected_response("Raw Data More DataThird Piece of Data"); |
| 459 std::string response; | 463 std::string response; |
| 460 ASSERT_TRUE(client.Read(&response, expected_response.length())); | 464 ASSERT_TRUE(client.Read(&response, expected_response.length())); |
| 461 ASSERT_EQ(expected_response, response); | 465 ASSERT_EQ(expected_response, response); |
| 462 } | 466 } |
| 463 | 467 |
| 464 namespace { | |
| 465 | |
| 466 class MockStreamSocket : public StreamSocket { | 468 class MockStreamSocket : public StreamSocket { |
| 467 public: | 469 public: |
| 468 MockStreamSocket() | 470 MockStreamSocket() |
| 469 : connected_(true), | 471 : connected_(true), |
| 470 read_buf_(NULL), | 472 read_buf_(NULL), |
| 471 read_buf_len_(0) {} | 473 read_buf_len_(0) {} |
| 472 | 474 |
| 473 // StreamSocket | 475 // StreamSocket |
| 474 virtual int Connect(const CompletionCallback& callback) OVERRIDE { | 476 virtual int Connect(const CompletionCallback& callback) OVERRIDE { |
| 475 return ERR_NOT_IMPLEMENTED; | 477 return ERR_NOT_IMPLEMENTED; |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 550 bool connected_; | 552 bool connected_; |
| 551 scoped_refptr<IOBuffer> read_buf_; | 553 scoped_refptr<IOBuffer> read_buf_; |
| 552 int read_buf_len_; | 554 int read_buf_len_; |
| 553 CompletionCallback read_callback_; | 555 CompletionCallback read_callback_; |
| 554 std::string pending_read_data_; | 556 std::string pending_read_data_; |
| 555 BoundNetLog net_log_; | 557 BoundNetLog net_log_; |
| 556 | 558 |
| 557 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket); | 559 DISALLOW_COPY_AND_ASSIGN(MockStreamSocket); |
| 558 }; | 560 }; |
| 559 | 561 |
| 560 } // namespace | |
| 561 | |
| 562 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { | 562 TEST_F(HttpServerTest, RequestWithBodySplitAcrossPackets) { |
| 563 MockStreamSocket* socket = new MockStreamSocket(); | 563 MockStreamSocket* socket = new MockStreamSocket(); |
| 564 HandleAcceptResult(make_scoped_ptr<StreamSocket>(socket)); | 564 HandleAcceptResult(make_scoped_ptr<StreamSocket>(socket)); |
| 565 std::string body("body"); | 565 std::string body("body"); |
| 566 std::string request_text = base::StringPrintf( | 566 std::string request_text = base::StringPrintf( |
| 567 "GET /test HTTP/1.1\r\n" | 567 "GET /test HTTP/1.1\r\n" |
| 568 "SomeHeader: 1\r\n" | 568 "SomeHeader: 1\r\n" |
| 569 "Content-Length: %" PRIuS "\r\n\r\n%s", | 569 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 570 body.length(), | 570 body.length(), |
| 571 body.c_str()); | 571 body.c_str()); |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 612 ASSERT_EQ("/test3", GetRequest(2).path); | 612 ASSERT_EQ("/test3", GetRequest(2).path); |
| 613 | 613 |
| 614 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 614 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
| 615 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 615 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
| 616 std::string response3; | 616 std::string response3; |
| 617 ASSERT_TRUE(client.ReadResponse(&response3)); | 617 ASSERT_TRUE(client.ReadResponse(&response3)); |
| 618 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 618 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
| 619 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 619 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
| 620 } | 620 } |
| 621 | 621 |
| 622 class CloseOnConnectHttpServerTest : public HttpServerTest { |
| 623 public: |
| 624 virtual void OnConnect(int connection_id) OVERRIDE { |
| 625 connection_ids_.push_back(connection_id); |
| 626 server_->Close(connection_id); |
| 627 } |
| 628 |
| 629 protected: |
| 630 std::vector<int> connection_ids_; |
| 631 }; |
| 632 |
| 633 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { |
| 634 TestHttpClient client; |
| 635 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 636 ASSERT_EQ(1ul, connection_ids_.size()); |
| 637 client.Send("GET / HTTP/1.1\r\n\r\n"); |
| 638 ASSERT_FALSE(RunUntilRequestsReceived(1)); |
| 639 ASSERT_EQ(0ul, requests_.size()); |
| 640 } |
| 641 |
| 642 } // namespace |
| 643 |
| 622 } // namespace net | 644 } // namespace net |
| OLD | NEW |