OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_stream_factory_impl.h" | 5 #include "net/http/http_stream_factory_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "net/base/cert_verifier.h" | 10 #include "net/base/cert_verifier.h" |
11 #include "net/base/mock_host_resolver.h" | 11 #include "net/base/mock_host_resolver.h" |
12 #include "net/base/net_log.h" | 12 #include "net/base/net_log.h" |
13 #include "net/base/ssl_config_service_defaults.h" | 13 #include "net/base/ssl_config_service_defaults.h" |
14 #include "net/base/test_completion_callback.h" | 14 #include "net/base/test_completion_callback.h" |
15 #include "net/http/http_auth_handler_factory.h" | 15 #include "net/http/http_auth_handler_factory.h" |
16 #include "net/http/http_network_session.h" | 16 #include "net/http/http_network_session.h" |
17 #include "net/http/http_network_session_peer.h" | 17 #include "net/http/http_network_session_peer.h" |
18 #include "net/http/http_request_info.h" | 18 #include "net/http/http_request_info.h" |
| 19 #include "net/http/http_stream.h" |
19 #include "net/proxy/proxy_info.h" | 20 #include "net/proxy/proxy_info.h" |
20 #include "net/proxy/proxy_service.h" | 21 #include "net/proxy/proxy_service.h" |
21 #include "net/socket/socket_test_util.h" | 22 #include "net/socket/socket_test_util.h" |
22 #include "net/spdy/spdy_session.h" | 23 #include "net/spdy/spdy_session.h" |
23 #include "net/spdy/spdy_session_pool.h" | 24 #include "net/spdy/spdy_session_pool.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
25 | 26 |
26 namespace net { | 27 namespace net { |
27 | 28 |
28 namespace { | 29 namespace { |
(...skipping 19 matching lines...) Expand all Loading... |
48 virtual void OnPreconnectsCompleteInternal() { | 49 virtual void OnPreconnectsCompleteInternal() { |
49 preconnect_done_ = true; | 50 preconnect_done_ = true; |
50 if (waiting_for_preconnect_) | 51 if (waiting_for_preconnect_) |
51 MessageLoop::current()->Quit(); | 52 MessageLoop::current()->Quit(); |
52 } | 53 } |
53 | 54 |
54 bool preconnect_done_; | 55 bool preconnect_done_; |
55 bool waiting_for_preconnect_; | 56 bool waiting_for_preconnect_; |
56 }; | 57 }; |
57 | 58 |
| 59 class StreamRequestWaiter : public HttpStreamRequest::Delegate { |
| 60 public: |
| 61 StreamRequestWaiter() |
| 62 : waiting_for_stream_(false), |
| 63 stream_done_(false) {} |
| 64 |
| 65 // HttpStreamRequest::Delegate |
| 66 |
| 67 virtual void OnStreamReady( |
| 68 const SSLConfig& used_ssl_config, |
| 69 const ProxyInfo& used_proxy_info, |
| 70 HttpStream* stream) OVERRIDE { |
| 71 stream_done_ = true; |
| 72 if (waiting_for_stream_) |
| 73 MessageLoop::current()->Quit(); |
| 74 stream_.reset(stream); |
| 75 } |
| 76 |
| 77 virtual void OnStreamFailed( |
| 78 int status, |
| 79 const SSLConfig& used_ssl_config) OVERRIDE {} |
| 80 |
| 81 virtual void OnCertificateError( |
| 82 int status, |
| 83 const SSLConfig& used_ssl_config, |
| 84 const SSLInfo& ssl_info) OVERRIDE {} |
| 85 |
| 86 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response, |
| 87 const SSLConfig& used_ssl_config, |
| 88 const ProxyInfo& used_proxy_info, |
| 89 HttpAuthController* auth_controller) OVERRIDE {} |
| 90 |
| 91 virtual void OnNeedsClientAuth(const SSLConfig& used_ssl_config, |
| 92 SSLCertRequestInfo* cert_info) OVERRIDE {} |
| 93 |
| 94 virtual void OnHttpsProxyTunnelResponse(const HttpResponseInfo& response_info, |
| 95 const SSLConfig& used_ssl_config, |
| 96 const ProxyInfo& used_proxy_info, |
| 97 HttpStream* stream) OVERRIDE {} |
| 98 |
| 99 void WaitForStream() { |
| 100 while (!stream_done_) { |
| 101 waiting_for_stream_ = true; |
| 102 MessageLoop::current()->Run(); |
| 103 waiting_for_stream_ = false; |
| 104 } |
| 105 } |
| 106 |
| 107 private: |
| 108 bool waiting_for_stream_; |
| 109 bool stream_done_; |
| 110 scoped_ptr<HttpStream> stream_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter); |
| 113 }; |
| 114 |
58 struct SessionDependencies { | 115 struct SessionDependencies { |
59 // Custom proxy service dependency. | 116 // Custom proxy service dependency. |
60 explicit SessionDependencies(ProxyService* proxy_service) | 117 explicit SessionDependencies(ProxyService* proxy_service) |
61 : host_resolver(new MockHostResolver), | 118 : host_resolver(new MockHostResolver), |
62 cert_verifier(new CertVerifier), | 119 cert_verifier(new CertVerifier), |
63 proxy_service(proxy_service), | 120 proxy_service(proxy_service), |
64 ssl_config_service(new SSLConfigServiceDefaults), | 121 ssl_config_service(new SSLConfigServiceDefaults), |
65 http_auth_handler_factory( | 122 http_auth_handler_factory( |
66 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), | 123 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
67 net_log(NULL) {} | 124 net_log(NULL) {} |
(...skipping 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
308 PreconnectHelper(kTests[i], session); | 365 PreconnectHelper(kTests[i], session); |
309 // We shouldn't be preconnecting if we have an existing session, which is | 366 // We shouldn't be preconnecting if we have an existing session, which is |
310 // the case for https://www.google.com. | 367 // the case for https://www.google.com. |
311 if (kTests[i].ssl) | 368 if (kTests[i].ssl) |
312 EXPECT_EQ(-1, ssl_conn_pool->last_num_streams()); | 369 EXPECT_EQ(-1, ssl_conn_pool->last_num_streams()); |
313 else | 370 else |
314 EXPECT_EQ(kTests[i].num_streams, transport_conn_pool->last_num_streams()); | 371 EXPECT_EQ(kTests[i].num_streams, transport_conn_pool->last_num_streams()); |
315 } | 372 } |
316 } | 373 } |
317 | 374 |
| 375 TEST(HttpStreamFactoryTest, JobNotifiesProxy) { |
| 376 const char* kProxyString = "PROXY bad:99; PROXY maybe:80; DIRECT"; |
| 377 SessionDependencies session_deps( |
| 378 ProxyService::CreateFixedFromPacResult(kProxyString)); |
| 379 |
| 380 // First connection attempt fails |
| 381 StaticSocketDataProvider socket_data1; |
| 382 socket_data1.set_connect_data(MockConnect(true, ERR_ADDRESS_UNREACHABLE)); |
| 383 session_deps.socket_factory.AddSocketDataProvider(&socket_data1); |
| 384 |
| 385 // Second connection attempt succeeds |
| 386 StaticSocketDataProvider socket_data2; |
| 387 socket_data2.set_connect_data(MockConnect(true, OK)); |
| 388 session_deps.socket_factory.AddSocketDataProvider(&socket_data2); |
| 389 |
| 390 CapturingBoundNetLog log(CapturingNetLog::kUnbounded); |
| 391 EXPECT_TRUE(log.bound().IsLoggingAllEvents()); |
| 392 |
| 393 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
| 394 |
| 395 // Now request a stream. It should succeed using the second proxy in the |
| 396 // list. |
| 397 HttpRequestInfo request_info; |
| 398 request_info.method = "GET"; |
| 399 request_info.url = GURL("http://www.google.com"); |
| 400 request_info.load_flags = 0; |
| 401 |
| 402 SSLConfig ssl_config; |
| 403 StreamRequestWaiter waiter; |
| 404 scoped_ptr<HttpStreamRequest> request( |
| 405 session->http_stream_factory()->RequestStream(request_info, ssl_config, |
| 406 &waiter, log.bound())); |
| 407 waiter.WaitForStream(); |
| 408 |
| 409 // The proxy that failed should now be known to the proxy_service as bad. |
| 410 const ProxyRetryInfoMap& retry_info = |
| 411 session->proxy_service()->proxy_retry_info(); |
| 412 EXPECT_EQ(1u, retry_info.size()); |
| 413 ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); |
| 414 EXPECT_TRUE(iter != retry_info.end()); |
| 415 } |
| 416 |
318 } // namespace | 417 } // namespace |
319 | 418 |
320 } // namespace net | 419 } // namespace net |
OLD | NEW |