| 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" |
| 11 #include "base/callback_helpers.h" | 11 #include "base/callback_helpers.h" |
| 12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
| 13 #include "base/format_macros.h" | 13 #include "base/format_macros.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/memory/scoped_ptr.h" | 15 #include "base/memory/scoped_ptr.h" |
| 16 #include "base/memory/weak_ptr.h" | 16 #include "base/memory/weak_ptr.h" |
| 17 #include "base/message_loop/message_loop.h" | 17 #include "base/message_loop/message_loop.h" |
| 18 #include "base/message_loop/message_loop_proxy.h" | |
| 19 #include "base/run_loop.h" | 18 #include "base/run_loop.h" |
| 20 #include "base/strings/string_split.h" | 19 #include "base/strings/string_split.h" |
| 21 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
| 22 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
| 22 #include "base/thread_task_runner_handle.h" |
| 23 #include "base/time/time.h" | 23 #include "base/time/time.h" |
| 24 #include "net/base/address_list.h" | 24 #include "net/base/address_list.h" |
| 25 #include "net/base/io_buffer.h" | 25 #include "net/base/io_buffer.h" |
| 26 #include "net/base/ip_endpoint.h" | 26 #include "net/base/ip_endpoint.h" |
| 27 #include "net/base/net_errors.h" | 27 #include "net/base/net_errors.h" |
| 28 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
| 29 #include "net/base/test_completion_callback.h" | 29 #include "net/base/test_completion_callback.h" |
| 30 #include "net/http/http_response_headers.h" | 30 #include "net/http/http_response_headers.h" |
| 31 #include "net/http/http_util.h" | 31 #include "net/http/http_util.h" |
| 32 #include "net/log/net_log.h" | 32 #include "net/log/net_log.h" |
| (...skipping 230 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(base::StartsWith(GetRequest(0).peer.ToString(), "127.0.0.1", |
| 274 "127.0.0.1", | 274 base::CompareCase::SENSITIVE)); |
| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 413 } | 412 } |
| 414 | 413 |
| 415 private: | 414 private: |
| 416 base::Closure quit_loop_func_; | 415 base::Closure quit_loop_func_; |
| 417 }; | 416 }; |
| 418 | 417 |
| 419 base::RunLoop run_loop; | 418 base::RunLoop run_loop; |
| 420 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); | 419 TestURLFetcherDelegate delegate(run_loop.QuitClosure()); |
| 421 | 420 |
| 422 scoped_refptr<URLRequestContextGetter> request_context_getter( | 421 scoped_refptr<URLRequestContextGetter> request_context_getter( |
| 423 new TestURLRequestContextGetter(base::MessageLoopProxy::current())); | 422 new TestURLRequestContextGetter(base::ThreadTaskRunnerHandle::Get())); |
| 424 scoped_ptr<URLFetcher> fetcher = | 423 scoped_ptr<URLFetcher> fetcher = |
| 425 URLFetcher::Create(GURL(base::StringPrintf("http://127.0.0.1:%d/test", | 424 URLFetcher::Create(GURL(base::StringPrintf("http://127.0.0.1:%d/test", |
| 426 server_address_.port())), | 425 server_address_.port())), |
| 427 URLFetcher::GET, &delegate); | 426 URLFetcher::GET, &delegate); |
| 428 fetcher->SetRequestContext(request_context_getter.get()); | 427 fetcher->SetRequestContext(request_context_getter.get()); |
| 429 fetcher->AddExtraRequestHeader( | 428 fetcher->AddExtraRequestHeader( |
| 430 base::StringPrintf("content-length:%d", 1 << 30)); | 429 base::StringPrintf("content-length:%d", 1 << 30)); |
| 431 fetcher->Start(); | 430 fetcher->Start(); |
| 432 | 431 |
| 433 ASSERT_TRUE(RunLoopWithTimeout(&run_loop)); | 432 ASSERT_TRUE(RunLoopWithTimeout(&run_loop)); |
| 434 ASSERT_EQ(0u, requests_.size()); | 433 ASSERT_EQ(0u, requests_.size()); |
| 435 } | 434 } |
| 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::StartsWith(response, "HTTP/1.1 200 OK", |
| 447 ASSERT_TRUE(EndsWith(response, "Response!", true)); | 446 base::CompareCase::SENSITIVE)); |
| 447 ASSERT_TRUE( |
| 448 base::EndsWith(response, "Response!", base::CompareCase::SENSITIVE)); |
| 448 } | 449 } |
| 449 | 450 |
| 450 TEST_F(HttpServerTest, SendRaw) { | 451 TEST_F(HttpServerTest, SendRaw) { |
| 451 TestHttpClient client; | 452 TestHttpClient client; |
| 452 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 453 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 453 client.Send("GET /test HTTP/1.1\r\n\r\n"); | 454 client.Send("GET /test HTTP/1.1\r\n\r\n"); |
| 454 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 455 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 455 server_->SendRaw(GetConnectionId(0), "Raw Data "); | 456 server_->SendRaw(GetConnectionId(0), "Raw Data "); |
| 456 server_->SendRaw(GetConnectionId(0), "More Data"); | 457 server_->SendRaw(GetConnectionId(0), "More Data"); |
| 457 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); | 458 server_->SendRaw(GetConnectionId(0), "Third Piece of Data"); |
| (...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 585 "Content-Length: %" PRIuS "\r\n\r\n%s", | 586 "Content-Length: %" PRIuS "\r\n\r\n%s", |
| 586 body.length(), | 587 body.length(), |
| 587 body.c_str())); | 588 body.c_str())); |
| 588 ASSERT_TRUE(RunUntilRequestsReceived(1)); | 589 ASSERT_TRUE(RunUntilRequestsReceived(1)); |
| 589 ASSERT_EQ(body, GetRequest(0).data); | 590 ASSERT_EQ(body, GetRequest(0).data); |
| 590 | 591 |
| 591 int client_connection_id = GetConnectionId(0); | 592 int client_connection_id = GetConnectionId(0); |
| 592 server_->Send200(client_connection_id, "Content for /test", "text/plain"); | 593 server_->Send200(client_connection_id, "Content for /test", "text/plain"); |
| 593 std::string response1; | 594 std::string response1; |
| 594 ASSERT_TRUE(client.ReadResponse(&response1)); | 595 ASSERT_TRUE(client.ReadResponse(&response1)); |
| 595 ASSERT_TRUE(StartsWithASCII(response1, "HTTP/1.1 200 OK", true)); | 596 ASSERT_TRUE(base::StartsWith(response1, "HTTP/1.1 200 OK", |
| 596 ASSERT_TRUE(EndsWith(response1, "Content for /test", true)); | 597 base::CompareCase::SENSITIVE)); |
| 598 ASSERT_TRUE(base::EndsWith(response1, "Content for /test", |
| 599 base::CompareCase::SENSITIVE)); |
| 597 | 600 |
| 598 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); | 601 client.Send("GET /test2 HTTP/1.1\r\n\r\n"); |
| 599 ASSERT_TRUE(RunUntilRequestsReceived(2)); | 602 ASSERT_TRUE(RunUntilRequestsReceived(2)); |
| 600 ASSERT_EQ("/test2", GetRequest(1).path); | 603 ASSERT_EQ("/test2", GetRequest(1).path); |
| 601 | 604 |
| 602 ASSERT_EQ(client_connection_id, GetConnectionId(1)); | 605 ASSERT_EQ(client_connection_id, GetConnectionId(1)); |
| 603 server_->Send404(client_connection_id); | 606 server_->Send404(client_connection_id); |
| 604 std::string response2; | 607 std::string response2; |
| 605 ASSERT_TRUE(client.ReadResponse(&response2)); | 608 ASSERT_TRUE(client.ReadResponse(&response2)); |
| 606 ASSERT_TRUE(StartsWithASCII(response2, "HTTP/1.1 404 Not Found", true)); | 609 ASSERT_TRUE(base::StartsWith(response2, "HTTP/1.1 404 Not Found", |
| 610 base::CompareCase::SENSITIVE)); |
| 607 | 611 |
| 608 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); | 612 client.Send("GET /test3 HTTP/1.1\r\n\r\n"); |
| 609 ASSERT_TRUE(RunUntilRequestsReceived(3)); | 613 ASSERT_TRUE(RunUntilRequestsReceived(3)); |
| 610 ASSERT_EQ("/test3", GetRequest(2).path); | 614 ASSERT_EQ("/test3", GetRequest(2).path); |
| 611 | 615 |
| 612 ASSERT_EQ(client_connection_id, GetConnectionId(2)); | 616 ASSERT_EQ(client_connection_id, GetConnectionId(2)); |
| 613 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); | 617 server_->Send200(client_connection_id, "Content for /test3", "text/plain"); |
| 614 std::string response3; | 618 std::string response3; |
| 615 ASSERT_TRUE(client.ReadResponse(&response3)); | 619 ASSERT_TRUE(client.ReadResponse(&response3)); |
| 616 ASSERT_TRUE(StartsWithASCII(response3, "HTTP/1.1 200 OK", true)); | 620 ASSERT_TRUE(base::StartsWith(response3, "HTTP/1.1 200 OK", |
| 617 ASSERT_TRUE(EndsWith(response3, "Content for /test3", true)); | 621 base::CompareCase::SENSITIVE)); |
| 622 ASSERT_TRUE(base::EndsWith(response3, "Content for /test3", |
| 623 base::CompareCase::SENSITIVE)); |
| 618 } | 624 } |
| 619 | 625 |
| 620 class CloseOnConnectHttpServerTest : public HttpServerTest { | 626 class CloseOnConnectHttpServerTest : public HttpServerTest { |
| 621 public: | 627 public: |
| 622 void OnConnect(int connection_id) override { | 628 void OnConnect(int connection_id) override { |
| 623 connection_ids_.push_back(connection_id); | 629 connection_ids_.push_back(connection_id); |
| 624 server_->Close(connection_id); | 630 server_->Close(connection_id); |
| 625 } | 631 } |
| 626 | 632 |
| 627 protected: | 633 protected: |
| 628 std::vector<int> connection_ids_; | 634 std::vector<int> connection_ids_; |
| 629 }; | 635 }; |
| 630 | 636 |
| 631 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { | 637 TEST_F(CloseOnConnectHttpServerTest, ServerImmediatelyClosesConnection) { |
| 632 TestHttpClient client; | 638 TestHttpClient client; |
| 633 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); | 639 ASSERT_EQ(OK, client.ConnectAndWait(server_address_)); |
| 634 client.Send("GET / HTTP/1.1\r\n\r\n"); | 640 client.Send("GET / HTTP/1.1\r\n\r\n"); |
| 635 ASSERT_FALSE(RunUntilRequestsReceived(1)); | 641 ASSERT_FALSE(RunUntilRequestsReceived(1)); |
| 636 ASSERT_EQ(1ul, connection_ids_.size()); | 642 ASSERT_EQ(1ul, connection_ids_.size()); |
| 637 ASSERT_EQ(0ul, requests_.size()); | 643 ASSERT_EQ(0ul, requests_.size()); |
| 638 } | 644 } |
| 639 | 645 |
| 640 } // namespace | 646 } // namespace |
| 641 | 647 |
| 642 } // namespace net | 648 } // namespace net |
| OLD | NEW |