| 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 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 263 |
| 264 TEST_F(HttpServerTest, Request) { | 264 TEST_F(HttpServerTest, Request) { |
| 265 TestHttpClient client; | 265 TestHttpClient client; |
| 266 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 266 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 267 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 267 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 268 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 268 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 269 ASSERT_EQ("GET", GetRequest(0).method); | 269 ASSERT_EQ("GET", GetRequest(0).method); |
| 270 ASSERT_EQ("/test", GetRequest(0).path); | 270 ASSERT_EQ("/test", GetRequest(0).path); |
| 271 ASSERT_EQ("", GetRequest(0).data); | 271 ASSERT_EQ("", GetRequest(0).data); |
| 272 ASSERT_EQ(0u, GetRequest(0).headers.size()); | 272 ASSERT_EQ(0u, GetRequest(0).headers.size()); |
| 273 ASSERT_TRUE(StartsWithASCII(GetRequest(0).peer.ToString(), | 273 ASSERT_TRUE( |
| 274 "127.0.0.1", | 274 base::StartsWithASCII(GetRequest(0).peer.ToString(), "127.0.0.1", true)); |
| 275 true)); | |
| 276 } | 275 } |
| 277 | 276 |
| 278 TEST_F(HttpServerTest, RequestWithHeaders) { | 277 TEST_F(HttpServerTest, RequestWithHeaders) { |
| 279 TestHttpClient client; | 278 TestHttpClient client; |
| 280 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 279 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 281 const char* const kHeaders[][3] = { | 280 const char* const kHeaders[][3] = { |
| 282 {"Header", ": ", "1"}, | 281 {"Header", ": ", "1"}, |
| 283 {"HeaderWithNoWhitespace", ":", "1"}, | 282 {"HeaderWithNoWhitespace", ":", "1"}, |
| 284 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "}, | 283 {"HeaderWithWhitespace", " : \t ", "1 1 1 \t "}, |
| 285 {"HeaderWithColon", ": ", "1:1"}, | 284 {"HeaderWithColon", ": ", "1:1"}, |
| (...skipping 150 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 436 | 435 |
| 437 TEST_F(HttpServerTest, Send200) { | 436 TEST_F(HttpServerTest, Send200) { |
| 438 TestHttpClient client; | 437 TestHttpClient client; |
| 439 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 438 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 440 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 439 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 441 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 440 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 442 server_->Send200(GetConnectionId(0), "Response!", "text/plain"); | 441 server_->Send200(GetConnectionId(0), "Response!", "text/plain"); |
| 443 | 442 |
| 444 std::string response; | 443 std::string response; |
| 445 ASSERT_TRUE(client.ReadResponse(&response)); | 444 ASSERT_TRUE(client.ReadResponse(&response)); |
| 446 ASSERT_TRUE(StartsWithASCII(response, "HTTP/1.1 200 OK", true)); | 445 ASSERT_TRUE(base::StartsWithASCII(response, "HTTP/1.1 200 OK", true)); |
| 447 ASSERT_TRUE(EndsWith(response, "Response!", true)); | 446 ASSERT_TRUE(EndsWith(response, "Response!", true)); |
| 448 } | 447 } |
| 449 | 448 |
| 450 TEST_F(HttpServerTest, SendRaw) { | 449 TEST_F(HttpServerTest, SendRaw) { |
| 451 TestHttpClient client; | 450 TestHttpClient client; |
| 452 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 451 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 453 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 452 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 454 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 453 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 455 server_->SendRaw(GetConnectionId(0), "Raw Data "); | 454 server_->SendRaw(GetConnectionId(0), "Raw Data "); |
| 456 server_->SendRaw(GetConnectionId(0), "More Data"); | 455 server_->SendRaw(GetConnectionId(0), "More Data"); |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 "Content-Length: %" PRIuS "\r\n\r\n%s", | 584 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 586 body.length(), | 585 body.length(), |
| 587 body.c_str())); | 586 body.c_str())); |
| 588 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 587 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 589 ASSERT_EQ(body, GetRequest(0).data); | 588 ASSERT_EQ(body, GetRequest(0).data); |
| 590 | 589 |
| 591 int client_connection_id = GetConnectionId(0); | 590 int client_connection_id = GetConnectionId(0); |
| 592 server_->Send200(client_connection_id, "Content for /test", "text/plain"); | 591 server_->Send200(client_connection_id, "Content for /test", "text/plain"); |
| 593 std::string response1; | 592 std::string response1; |
| 594 ASSERT_TRUE(client.ReadResponse(&response1)); | 593 ASSERT_TRUE(client.ReadResponse(&response1)); |
| 595 ASSERT_TRUE(StartsWithASCII(response1, "HTTP/1.1 200 OK", true)); | 594 ASSERT_TRUE(base::StartsWithASCII(response1, "HTTP/1.1 200 OK", true)); |
| 596 ASSERT_TRUE(EndsWith(response1, "Content for /test", true)); | 595 ASSERT_TRUE(EndsWith(response1, "Content for /test", true)); |
| 597 | 596 |
| 598 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); | 597 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); |
| 599 ASSERT_TRUE(RunUntilRequestsReceived(2)); | 598 ASSERT_TRUE(RunUntilRequestsReceived(2)); |
| 600 ASSERT_EQ("/test2", GetRequest(1).path); | 599 ASSERT_EQ("/test2", GetRequest(1).path); |
| 601 | 600 |
| 602 ASSERT_EQ(client_connection_id, GetConnectionId(1)); | 601 ASSERT_EQ(client_connection_id, GetConnectionId(1)); |
| 603 server_->Send404(client_connection_id); | 602 server_->Send404(client_connection_id); |
| 604 std::string response2; | 603 std::string response2; |
| 605 ASSERT_TRUE(client.ReadResponse(&response2)); | 604 ASSERT_TRUE(client.ReadResponse(&response2)); |
| 606 ASSERT_TRUE(StartsWithASCII(response2, "HTTP/1.1 404 Not Found", true)); | 605 ASSERT_TRUE(base::StartsWithASCII(response2, "HTTP/1.1 404 Not Found", true)); |
| 607 | 606 |
| 608 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); | 607 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); |
| 609 ASSERT_TRUE(RunUntilRequestsReceived(3)); | 608 ASSERT_TRUE(RunUntilRequestsReceived(3)); |
| 610 ASSERT_EQ("/test3", GetRequest(2).path); | 609 ASSERT_EQ("/test3", GetRequest(2).path); |
| 611 | 610 |
| 612 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 611 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
| 613 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 612 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
| 614 std::string response3; | 613 std::string response3; |
| 615 ASSERT_TRUE(client.ReadResponse(&response3)); | 614 ASSERT_TRUE(client.ReadResponse(&response3)); |
| 616 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 615 ASSERT_TRUE(base::StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); |
| 617 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 616 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); |
| 618 } | 617 } |
| 619 | 618 |
| 620 class CloseOnConnectHttpServerTest : public HttpServerTest { | 619 class CloseOnConnectHttpServerTest : public HttpServerTest { |
| 621 public: | 620 public: |
| 622 void OnConnect(int connection_id) override { | 621 void OnConnect(int connection_id) override { |
| 623 connection_ids_.push_back(connection_id); | 622 connection_ids_.push_back(connection_id); |
| 624 server_->Close(connection_id); | 623 server_->Close(connection_id); |
| 625 } | 624 } |
| 626 | 625 |
| 627 protected: | 626 protected: |
| 628 std::vector<int> connection_ids_; | 627 std::vector<int> connection_ids_; |
| 629 }; | 628 }; |
| 630 | 629 |
| 631 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { | 630 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { |
| 632 TestHttpClient client; | 631 TestHttpClient client; |
| 633 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 632 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 634 client.Send("GET / HTTP/1.1\r\n\r\n"); | 633 client.Send("GET / HTTP/1.1\r\n\r\n"); |
| 635 ASSERT_FALSE(RunUntilRequestsReceived(1)); | 634 ASSERT_FALSE(RunUntilRequestsReceived(1)); |
| 636 ASSERT_EQ(1ul, connection_ids_.size()); | 635 ASSERT_EQ(1ul, connection_ids_.size()); |
| 637 ASSERT_EQ(0ul, requests_.size()); | 636 ASSERT_EQ(0ul, requests_.size()); |
| 638 } | 637 } |
| 639 | 638 |
| 640 } // namespace | 639 } // namespace |
| 641 | 640 |
| 642 } // namespace net | 641 } // namespace net |
| OLD | NEW |