| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/http/http_proxy_client_socket_pool.h" | 5 #include "net/http/http_proxy_client_socket_pool.h" |
| 6 | 6 |
| 7 #include "base/callback.h" | 7 #include "base/callback.h" |
| 8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
| 9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
| 10 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 | 56 |
| 57 typedef ::testing::TestWithParam<HttpProxyType> TestWithHttpParam; | 57 typedef ::testing::TestWithParam<HttpProxyType> TestWithHttpParam; |
| 58 | 58 |
| 59 const char kHttpProxyHost[] = "httpproxy.example.com"; | 59 const char kHttpProxyHost[] = "httpproxy.example.com"; |
| 60 const char kHttpsProxyHost[] = "httpsproxy.example.com"; | 60 const char kHttpsProxyHost[] = "httpsproxy.example.com"; |
| 61 | 61 |
| 62 class TestProxyDelegate : public ProxyDelegate { | 62 class TestProxyDelegate : public ProxyDelegate { |
| 63 public: | 63 public: |
| 64 TestProxyDelegate() | 64 TestProxyDelegate() |
| 65 : on_before_tunnel_request_called_(false), | 65 : on_before_tunnel_request_called_(false), |
| 66 on_tunnel_request_completed_called_(false), |
| 66 on_tunnel_headers_received_called_(false) { | 67 on_tunnel_headers_received_called_(false) { |
| 67 } | 68 } |
| 68 | 69 |
| 69 ~TestProxyDelegate() override {} | 70 ~TestProxyDelegate() override {} |
| 70 | 71 |
| 71 bool on_before_tunnel_request_called() const { | 72 bool on_before_tunnel_request_called() const { |
| 72 return on_before_tunnel_request_called_; | 73 return on_before_tunnel_request_called_; |
| 73 } | 74 } |
| 74 | 75 |
| 76 bool on_tunnel_request_completed_called() const { |
| 77 return on_tunnel_request_completed_called_; |
| 78 } |
| 79 |
| 75 bool on_tunnel_headers_received_called() const { | 80 bool on_tunnel_headers_received_called() const { |
| 76 return on_tunnel_headers_received_called_; | 81 return on_tunnel_headers_received_called_; |
| 77 } | 82 } |
| 78 | 83 |
| 84 void VerifyOnTunnelRequestCompleted(const std::string& endpoint, |
| 85 const std::string& proxy_server) const { |
| 86 EXPECT_TRUE(on_tunnel_request_completed_called_); |
| 87 EXPECT_TRUE(HostPortPair::FromString(endpoint).Equals( |
| 88 on_tunnel_request_completed_endpoint_)); |
| 89 EXPECT_TRUE(HostPortPair::FromString(proxy_server).Equals( |
| 90 on_tunnel_request_completed_proxy_server_)); |
| 91 } |
| 92 |
| 79 void VerifyOnTunnelHeadersReceived(const std::string& origin, | 93 void VerifyOnTunnelHeadersReceived(const std::string& origin, |
| 80 const std::string& proxy_server, | 94 const std::string& proxy_server, |
| 81 const std::string& status_line) const { | 95 const std::string& status_line) const { |
| 82 EXPECT_TRUE(on_tunnel_headers_received_called_); | 96 EXPECT_TRUE(on_tunnel_headers_received_called_); |
| 83 EXPECT_TRUE(HostPortPair::FromString(origin).Equals( | 97 EXPECT_TRUE(HostPortPair::FromString(origin).Equals( |
| 84 on_tunnel_headers_received_origin_)); | 98 on_tunnel_headers_received_origin_)); |
| 85 EXPECT_TRUE(HostPortPair::FromString(proxy_server).Equals( | 99 EXPECT_TRUE(HostPortPair::FromString(proxy_server).Equals( |
| 86 on_tunnel_headers_received_proxy_server_)); | 100 on_tunnel_headers_received_proxy_server_)); |
| 87 EXPECT_EQ(status_line, on_tunnel_headers_received_status_line_); | 101 EXPECT_EQ(status_line, on_tunnel_headers_received_status_line_); |
| 88 } | 102 } |
| 89 | 103 |
| 90 // ProxyDelegate: | 104 // ProxyDelegate: |
| 91 void OnResolveProxy(const GURL& url, | 105 void OnResolveProxy(const GURL& url, |
| 92 int load_flags, | 106 int load_flags, |
| 93 const ProxyService& proxy_service, | 107 const ProxyService& proxy_service, |
| 94 ProxyInfo* result) override {} | 108 ProxyInfo* result) override {} |
| 95 | 109 |
| 110 void OnTunnelConnectCompleted(const HostPortPair& endpoint, |
| 111 const HostPortPair& proxy_server, |
| 112 int net_error) override { |
| 113 on_tunnel_request_completed_called_ = true; |
| 114 on_tunnel_request_completed_endpoint_ = endpoint; |
| 115 on_tunnel_request_completed_proxy_server_ = proxy_server; |
| 116 } |
| 117 |
| 96 void OnFallback(const ProxyServer& bad_proxy, int net_error) override {} | 118 void OnFallback(const ProxyServer& bad_proxy, int net_error) override {} |
| 97 | 119 |
| 98 void OnBeforeSendHeaders(URLRequest* request, | 120 void OnBeforeSendHeaders(URLRequest* request, |
| 99 const ProxyInfo& proxy_info, | 121 const ProxyInfo& proxy_info, |
| 100 HttpRequestHeaders* headers) override {} | 122 HttpRequestHeaders* headers) override {} |
| 101 | 123 |
| 102 void OnBeforeTunnelRequest(const net::HostPortPair& proxy_server, | 124 void OnBeforeTunnelRequest(const net::HostPortPair& proxy_server, |
| 103 net::HttpRequestHeaders* extra_headers) override { | 125 net::HttpRequestHeaders* extra_headers) override { |
| 104 on_before_tunnel_request_called_ = true; | 126 on_before_tunnel_request_called_ = true; |
| 105 if (extra_headers) { | 127 if (extra_headers) { |
| 106 extra_headers->SetHeader("Foo", proxy_server.ToString()); | 128 extra_headers->SetHeader("Foo", proxy_server.ToString()); |
| 107 } | 129 } |
| 108 } | 130 } |
| 109 | 131 |
| 110 void OnTunnelHeadersReceived( | 132 void OnTunnelHeadersReceived( |
| 111 const net::HostPortPair& origin, | 133 const net::HostPortPair& origin, |
| 112 const net::HostPortPair& proxy_server, | 134 const net::HostPortPair& proxy_server, |
| 113 const net::HttpResponseHeaders& response_headers) override { | 135 const net::HttpResponseHeaders& response_headers) override { |
| 114 on_tunnel_headers_received_called_ = true; | 136 on_tunnel_headers_received_called_ = true; |
| 115 on_tunnel_headers_received_origin_ = origin; | 137 on_tunnel_headers_received_origin_ = origin; |
| 116 on_tunnel_headers_received_proxy_server_ = proxy_server; | 138 on_tunnel_headers_received_proxy_server_ = proxy_server; |
| 117 on_tunnel_headers_received_status_line_ = response_headers.GetStatusLine(); | 139 on_tunnel_headers_received_status_line_ = response_headers.GetStatusLine(); |
| 118 } | 140 } |
| 119 | 141 |
| 120 private: | 142 private: |
| 121 bool on_before_tunnel_request_called_; | 143 bool on_before_tunnel_request_called_; |
| 144 bool on_tunnel_request_completed_called_; |
| 122 bool on_tunnel_headers_received_called_; | 145 bool on_tunnel_headers_received_called_; |
| 146 HostPortPair on_tunnel_request_completed_endpoint_; |
| 147 HostPortPair on_tunnel_request_completed_proxy_server_; |
| 123 HostPortPair on_tunnel_headers_received_origin_; | 148 HostPortPair on_tunnel_headers_received_origin_; |
| 124 HostPortPair on_tunnel_headers_received_proxy_server_; | 149 HostPortPair on_tunnel_headers_received_proxy_server_; |
| 125 std::string on_tunnel_headers_received_status_line_; | 150 std::string on_tunnel_headers_received_status_line_; |
| 126 }; | 151 }; |
| 127 | 152 |
| 128 | 153 |
| 129 class HttpProxyClientSocketPoolTest | 154 class HttpProxyClientSocketPoolTest |
| 130 : public ::testing::TestWithParam<HttpProxyClientSocketPoolTestParams> { | 155 : public ::testing::TestWithParam<HttpProxyClientSocketPoolTestParams> { |
| 131 protected: | 156 protected: |
| 132 HttpProxyClientSocketPoolTest() | 157 HttpProxyClientSocketPoolTest() |
| (...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 int rv = handle_.Init("a", CreateNoTunnelParams(proxy_delegate.get()), LOW, | 362 int rv = handle_.Init("a", CreateNoTunnelParams(proxy_delegate.get()), LOW, |
| 338 CompletionCallback(), &pool_, BoundNetLog()); | 363 CompletionCallback(), &pool_, BoundNetLog()); |
| 339 EXPECT_EQ(OK, rv); | 364 EXPECT_EQ(OK, rv); |
| 340 EXPECT_TRUE(handle_.is_initialized()); | 365 EXPECT_TRUE(handle_.is_initialized()); |
| 341 ASSERT_TRUE(handle_.socket()); | 366 ASSERT_TRUE(handle_.socket()); |
| 342 HttpProxyClientSocket* tunnel_socket = | 367 HttpProxyClientSocket* tunnel_socket = |
| 343 static_cast<HttpProxyClientSocket*>(handle_.socket()); | 368 static_cast<HttpProxyClientSocket*>(handle_.socket()); |
| 344 EXPECT_TRUE(tunnel_socket->IsConnected()); | 369 EXPECT_TRUE(tunnel_socket->IsConnected()); |
| 345 EXPECT_FALSE(proxy_delegate->on_before_tunnel_request_called()); | 370 EXPECT_FALSE(proxy_delegate->on_before_tunnel_request_called()); |
| 346 EXPECT_FALSE(proxy_delegate->on_tunnel_headers_received_called()); | 371 EXPECT_FALSE(proxy_delegate->on_tunnel_headers_received_called()); |
| 372 EXPECT_TRUE(proxy_delegate->on_tunnel_request_completed_called()); |
| 347 } | 373 } |
| 348 | 374 |
| 349 // Make sure that HttpProxyConnectJob passes on its priority to its | 375 // Make sure that HttpProxyConnectJob passes on its priority to its |
| 350 // (non-SSL) socket request on Init. | 376 // (non-SSL) socket request on Init. |
| 351 TEST_P(HttpProxyClientSocketPoolTest, SetSocketRequestPriorityOnInit) { | 377 TEST_P(HttpProxyClientSocketPoolTest, SetSocketRequestPriorityOnInit) { |
| 352 Initialize(NULL, 0, NULL, 0, NULL, 0, NULL, 0); | 378 Initialize(NULL, 0, NULL, 0, NULL, 0, NULL, 0); |
| 353 EXPECT_EQ(OK, | 379 EXPECT_EQ(OK, |
| 354 handle_.Init("a", CreateNoTunnelParams(NULL), HIGHEST, | 380 handle_.Init("a", CreateNoTunnelParams(NULL), HIGHEST, |
| 355 CompletionCallback(), &pool_, BoundNetLog())); | 381 CompletionCallback(), &pool_, BoundNetLog())); |
| 356 EXPECT_EQ(HIGHEST, GetLastTransportRequestPriority()); | 382 EXPECT_EQ(HIGHEST, GetLastTransportRequestPriority()); |
| (...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 447 EXPECT_EQ(OK, rv); | 473 EXPECT_EQ(OK, rv); |
| 448 EXPECT_TRUE(handle_.is_initialized()); | 474 EXPECT_TRUE(handle_.is_initialized()); |
| 449 ASSERT_TRUE(handle_.socket()); | 475 ASSERT_TRUE(handle_.socket()); |
| 450 HttpProxyClientSocket* tunnel_socket = | 476 HttpProxyClientSocket* tunnel_socket = |
| 451 static_cast<HttpProxyClientSocket*>(handle_.socket()); | 477 static_cast<HttpProxyClientSocket*>(handle_.socket()); |
| 452 EXPECT_TRUE(tunnel_socket->IsConnected()); | 478 EXPECT_TRUE(tunnel_socket->IsConnected()); |
| 453 proxy_delegate->VerifyOnTunnelHeadersReceived( | 479 proxy_delegate->VerifyOnTunnelHeadersReceived( |
| 454 "www.google.com:443", | 480 "www.google.com:443", |
| 455 proxy_host_port.c_str(), | 481 proxy_host_port.c_str(), |
| 456 "HTTP/1.1 200 Connection Established"); | 482 "HTTP/1.1 200 Connection Established"); |
| 483 proxy_delegate->VerifyOnTunnelRequestCompleted( |
| 484 "www.google.com:443", |
| 485 proxy_host_port.c_str()); |
| 457 } | 486 } |
| 458 | 487 |
| 459 TEST_P(HttpProxyClientSocketPoolTest, AsyncHaveAuth) { | 488 TEST_P(HttpProxyClientSocketPoolTest, AsyncHaveAuth) { |
| 489 std::string proxy_host_port = |
| 490 GetParam().proxy_type == HTTP ? |
| 491 (kHttpProxyHost + std::string(":80")) : |
| 492 (kHttpsProxyHost + std::string(":443")); |
| 493 std::string request = |
| 494 "CONNECT www.google.com:443 HTTP/1.1\r\n" |
| 495 "Host: www.google.com\r\n" |
| 496 "Proxy-Connection: keep-alive\r\n" |
| 497 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n" |
| 498 "Foo: " + proxy_host_port + "\r\n\r\n"; |
| 460 MockWrite writes[] = { | 499 MockWrite writes[] = { |
| 461 MockWrite(ASYNC, 0, "CONNECT www.google.com:443 HTTP/1.1\r\n" | 500 MockWrite(ASYNC, 0, request.c_str()), |
| 462 "Host: www.google.com\r\n" | |
| 463 "Proxy-Connection: keep-alive\r\n" | |
| 464 "Proxy-Authorization: Basic Zm9vOmJhcg==\r\n\r\n"), | |
| 465 }; | 501 }; |
| 466 MockRead reads[] = { | 502 MockRead reads[] = { |
| 467 MockRead(ASYNC, 1, "HTTP/1.1 200 Connection Established\r\n\r\n"), | 503 MockRead(ASYNC, 1, "HTTP/1.1 200 Connection Established\r\n\r\n"), |
| 468 }; | 504 }; |
| 469 | 505 |
| 470 scoped_ptr<SpdyFrame> req( | 506 scoped_ptr<SpdyFrame> req( |
| 471 spdy_util_.ConstructSpdyConnect(kAuthHeaders, kAuthHeadersSize, 1, LOW)); | 507 spdy_util_.ConstructSpdyConnect(kAuthHeaders, kAuthHeadersSize, 1, LOW)); |
| 472 MockWrite spdy_writes[] = { | 508 MockWrite spdy_writes[] = { |
| 473 CreateMockWrite(*req, 0, ASYNC) | 509 CreateMockWrite(*req, 0, ASYNC) |
| 474 }; | 510 }; |
| 475 scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1)); | 511 scoped_ptr<SpdyFrame> resp(spdy_util_.ConstructSpdyGetSynReply(NULL, 0, 1)); |
| 476 MockRead spdy_reads[] = { | 512 MockRead spdy_reads[] = { |
| 477 CreateMockRead(*resp, 1, ASYNC), | 513 CreateMockRead(*resp, 1, ASYNC), |
| 478 MockRead(ASYNC, 0, 2) | 514 MockRead(ASYNC, 0, 2) |
| 479 }; | 515 }; |
| 480 | 516 |
| 481 Initialize(reads, arraysize(reads), writes, arraysize(writes), | 517 Initialize(reads, arraysize(reads), writes, arraysize(writes), |
| 482 spdy_reads, arraysize(spdy_reads), spdy_writes, | 518 spdy_reads, arraysize(spdy_reads), spdy_writes, |
| 483 arraysize(spdy_writes)); | 519 arraysize(spdy_writes)); |
| 484 AddAuthToCache(); | 520 AddAuthToCache(); |
| 485 | 521 |
| 486 int rv = handle_.Init("a", CreateTunnelParams(NULL), LOW, | 522 scoped_ptr<TestProxyDelegate> proxy_delegate(new TestProxyDelegate()); |
| 523 int rv = handle_.Init("a", CreateTunnelParams(proxy_delegate.get()), LOW, |
| 487 callback_.callback(), &pool_, BoundNetLog()); | 524 callback_.callback(), &pool_, BoundNetLog()); |
| 488 EXPECT_EQ(ERR_IO_PENDING, rv); | 525 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 489 EXPECT_FALSE(handle_.is_initialized()); | 526 EXPECT_FALSE(handle_.is_initialized()); |
| 490 EXPECT_FALSE(handle_.socket()); | 527 EXPECT_FALSE(handle_.socket()); |
| 491 | 528 |
| 492 data_->RunFor(2); | 529 data_->RunFor(2); |
| 493 EXPECT_EQ(OK, callback_.WaitForResult()); | 530 EXPECT_EQ(OK, callback_.WaitForResult()); |
| 494 EXPECT_TRUE(handle_.is_initialized()); | 531 EXPECT_TRUE(handle_.is_initialized()); |
| 495 ASSERT_TRUE(handle_.socket()); | 532 ASSERT_TRUE(handle_.socket()); |
| 496 HttpProxyClientSocket* tunnel_socket = | 533 HttpProxyClientSocket* tunnel_socket = |
| 497 static_cast<HttpProxyClientSocket*>(handle_.socket()); | 534 static_cast<HttpProxyClientSocket*>(handle_.socket()); |
| 498 EXPECT_TRUE(tunnel_socket->IsConnected()); | 535 EXPECT_TRUE(tunnel_socket->IsConnected()); |
| 536 proxy_delegate->VerifyOnTunnelRequestCompleted( |
| 537 "www.google.com:443", |
| 538 proxy_host_port.c_str()); |
| 499 } | 539 } |
| 500 | 540 |
| 501 // Make sure that HttpProxyConnectJob passes on its priority to its | 541 // Make sure that HttpProxyConnectJob passes on its priority to its |
| 502 // SPDY session's socket request on Init (if applicable). | 542 // SPDY session's socket request on Init (if applicable). |
| 503 TEST_P(HttpProxyClientSocketPoolTest, | 543 TEST_P(HttpProxyClientSocketPoolTest, |
| 504 SetSpdySessionSocketRequestPriorityOnInit) { | 544 SetSpdySessionSocketRequestPriorityOnInit) { |
| 505 if (GetParam().proxy_type != SPDY) | 545 if (GetParam().proxy_type != SPDY) |
| 506 return; | 546 return; |
| 507 | 547 |
| 508 scoped_ptr<SpdyFrame> req( | 548 scoped_ptr<SpdyFrame> req( |
| (...skipping 295 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 804 EXPECT_TRUE(headers->IsRedirect(&location)); | 844 EXPECT_TRUE(headers->IsRedirect(&location)); |
| 805 EXPECT_EQ(location, redirectTarget); | 845 EXPECT_EQ(location, redirectTarget); |
| 806 } | 846 } |
| 807 } | 847 } |
| 808 | 848 |
| 809 // It would be nice to also test the timeouts in HttpProxyClientSocketPool. | 849 // It would be nice to also test the timeouts in HttpProxyClientSocketPool. |
| 810 | 850 |
| 811 } // namespace | 851 } // namespace |
| 812 | 852 |
| 813 } // namespace net | 853 } // namespace net |
| OLD | NEW |