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
3 // found in the LICENSE file.
5 #include "net/http/http_stream_factory_impl.h"
12 #include "base/basictypes.h"
13 #include "base/compiler_specific.h"
14 #include "net/base/port_util.h"
15 #include "net/base/test_completion_callback.h"
16 #include "net/cert/mock_cert_verifier.h"
17 #include "net/dns/mock_host_resolver.h"
18 #include "net/http/http_auth_handler_factory.h"
19 #include "net/http/http_network_session.h"
20 #include "net/http/http_network_session_peer.h"
21 #include "net/http/http_network_transaction.h"
22 #include "net/http/http_request_info.h"
23 #include "net/http/http_server_properties.h"
24 #include "net/http/http_server_properties_impl.h"
25 #include "net/http/http_stream.h"
26 #include "net/http/transport_security_state.h"
27 #include "net/log/net_log.h"
28 #include "net/proxy/proxy_info.h"
29 #include "net/proxy/proxy_service.h"
30 #include "net/socket/client_socket_handle.h"
31 #include "net/socket/mock_client_socket_pool_manager.h"
32 #include "net/socket/next_proto.h"
33 #include "net/socket/socket_test_util.h"
34 #include "net/spdy/spdy_session.h"
35 #include "net/spdy/spdy_session_pool.h"
36 #include "net/spdy/spdy_test_util_common.h"
37 #include "net/ssl/ssl_config_service.h"
38 #include "net/ssl/ssl_config_service_defaults.h"
39 // This file can be included from net/http even though
40 // it is in net/websockets because it doesn't
41 // introduce any link dependency to net/websockets.
42 #include "net/websockets/websocket_handshake_stream_base.h"
43 #include "testing/gtest/include/gtest/gtest.h"
49 class MockWebSocketHandshakeStream
: public WebSocketHandshakeStreamBase
{
56 explicit MockWebSocketHandshakeStream(StreamType type
) : type_(type
) {}
58 ~MockWebSocketHandshakeStream() override
{}
60 StreamType
type() const {
65 int InitializeStream(const HttpRequestInfo
* request_info
,
66 RequestPriority priority
,
67 const BoundNetLog
& net_log
,
68 const CompletionCallback
& callback
) override
{
69 return ERR_IO_PENDING
;
71 int SendRequest(const HttpRequestHeaders
& request_headers
,
72 HttpResponseInfo
* response
,
73 const CompletionCallback
& callback
) override
{
74 return ERR_IO_PENDING
;
76 int ReadResponseHeaders(const CompletionCallback
& callback
) override
{
77 return ERR_IO_PENDING
;
79 int ReadResponseBody(IOBuffer
* buf
,
81 const CompletionCallback
& callback
) override
{
82 return ERR_IO_PENDING
;
84 void Close(bool not_reusable
) override
{}
85 bool IsResponseBodyComplete() const override
{ return false; }
86 bool IsConnectionReused() const override
{ return false; }
87 void SetConnectionReused() override
{}
88 bool CanReuseConnection() const override
{ return false; }
89 int64
GetTotalReceivedBytes() const override
{ return 0; }
90 int64_t GetTotalSentBytes() const override
{ return 0; }
91 bool GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
{
94 void GetSSLInfo(SSLInfo
* ssl_info
) override
{}
95 void GetSSLCertRequestInfo(SSLCertRequestInfo
* cert_request_info
) override
{}
96 void Drain(HttpNetworkSession
* session
) override
{}
97 void SetPriority(RequestPriority priority
) override
{}
98 UploadProgress
GetUploadProgress() const override
{ return UploadProgress(); }
99 HttpStream
* RenewStreamForAuth() override
{ return nullptr; }
101 scoped_ptr
<WebSocketStream
> Upgrade() override
{
102 return scoped_ptr
<WebSocketStream
>();
106 const StreamType type_
;
109 // HttpStreamFactoryImpl subclass that can wait until a preconnect is complete.
110 class MockHttpStreamFactoryImplForPreconnect
: public HttpStreamFactoryImpl
{
112 MockHttpStreamFactoryImplForPreconnect(HttpNetworkSession
* session
,
114 : HttpStreamFactoryImpl(session
, for_websockets
),
115 preconnect_done_(false),
116 waiting_for_preconnect_(false) {}
119 void WaitForPreconnects() {
120 while (!preconnect_done_
) {
121 waiting_for_preconnect_
= true;
122 base::MessageLoop::current()->Run();
123 waiting_for_preconnect_
= false;
128 // HttpStreamFactoryImpl methods.
129 void OnPreconnectsCompleteInternal() override
{
130 preconnect_done_
= true;
131 if (waiting_for_preconnect_
)
132 base::MessageLoop::current()->Quit();
135 bool preconnect_done_
;
136 bool waiting_for_preconnect_
;
139 class StreamRequestWaiter
: public HttpStreamRequest::Delegate
{
141 StreamRequestWaiter()
142 : waiting_for_stream_(false),
143 stream_done_(false) {}
145 // HttpStreamRequest::Delegate
147 void OnStreamReady(const SSLConfig
& used_ssl_config
,
148 const ProxyInfo
& used_proxy_info
,
149 HttpStream
* stream
) override
{
151 if (waiting_for_stream_
)
152 base::MessageLoop::current()->Quit();
153 stream_
.reset(stream
);
154 used_ssl_config_
= used_ssl_config
;
155 used_proxy_info_
= used_proxy_info
;
158 void OnWebSocketHandshakeStreamReady(
159 const SSLConfig
& used_ssl_config
,
160 const ProxyInfo
& used_proxy_info
,
161 WebSocketHandshakeStreamBase
* stream
) override
{
163 if (waiting_for_stream_
)
164 base::MessageLoop::current()->Quit();
165 websocket_stream_
.reset(stream
);
166 used_ssl_config_
= used_ssl_config
;
167 used_proxy_info_
= used_proxy_info
;
170 void OnStreamFailed(int status
,
171 const SSLConfig
& used_ssl_config
,
172 SSLFailureState ssl_failure_state
) override
{}
174 void OnCertificateError(int status
,
175 const SSLConfig
& used_ssl_config
,
176 const SSLInfo
& ssl_info
) override
{}
178 void OnNeedsProxyAuth(const HttpResponseInfo
& proxy_response
,
179 const SSLConfig
& used_ssl_config
,
180 const ProxyInfo
& used_proxy_info
,
181 HttpAuthController
* auth_controller
) override
{}
183 void OnNeedsClientAuth(const SSLConfig
& used_ssl_config
,
184 SSLCertRequestInfo
* cert_info
) override
{}
186 void OnHttpsProxyTunnelResponse(const HttpResponseInfo
& response_info
,
187 const SSLConfig
& used_ssl_config
,
188 const ProxyInfo
& used_proxy_info
,
189 HttpStream
* stream
) override
{}
191 void WaitForStream() {
192 while (!stream_done_
) {
193 waiting_for_stream_
= true;
194 base::MessageLoop::current()->Run();
195 waiting_for_stream_
= false;
199 const SSLConfig
& used_ssl_config() const {
200 return used_ssl_config_
;
203 const ProxyInfo
& used_proxy_info() const {
204 return used_proxy_info_
;
207 HttpStream
* stream() {
208 return stream_
.get();
211 MockWebSocketHandshakeStream
* websocket_stream() {
212 return static_cast<MockWebSocketHandshakeStream
*>(websocket_stream_
.get());
215 bool stream_done() const { return stream_done_
; }
218 bool waiting_for_stream_
;
220 scoped_ptr
<HttpStream
> stream_
;
221 scoped_ptr
<WebSocketHandshakeStreamBase
> websocket_stream_
;
222 SSLConfig used_ssl_config_
;
223 ProxyInfo used_proxy_info_
;
225 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter
);
228 class WebSocketSpdyHandshakeStream
: public MockWebSocketHandshakeStream
{
230 explicit WebSocketSpdyHandshakeStream(
231 const base::WeakPtr
<SpdySession
>& spdy_session
)
232 : MockWebSocketHandshakeStream(kStreamTypeSpdy
),
233 spdy_session_(spdy_session
) {}
235 ~WebSocketSpdyHandshakeStream() override
{}
237 SpdySession
* spdy_session() { return spdy_session_
.get(); }
240 base::WeakPtr
<SpdySession
> spdy_session_
;
243 class WebSocketBasicHandshakeStream
: public MockWebSocketHandshakeStream
{
245 explicit WebSocketBasicHandshakeStream(
246 scoped_ptr
<ClientSocketHandle
> connection
)
247 : MockWebSocketHandshakeStream(kStreamTypeBasic
),
248 connection_(connection
.Pass()) {}
250 ~WebSocketBasicHandshakeStream() override
{
251 connection_
->socket()->Disconnect();
254 ClientSocketHandle
* connection() { return connection_
.get(); }
257 scoped_ptr
<ClientSocketHandle
> connection_
;
260 class WebSocketStreamCreateHelper
261 : public WebSocketHandshakeStreamBase::CreateHelper
{
263 ~WebSocketStreamCreateHelper() override
{}
265 WebSocketHandshakeStreamBase
* CreateBasicStream(
266 scoped_ptr
<ClientSocketHandle
> connection
,
267 bool using_proxy
) override
{
268 return new WebSocketBasicHandshakeStream(connection
.Pass());
271 WebSocketHandshakeStreamBase
* CreateSpdyStream(
272 const base::WeakPtr
<SpdySession
>& spdy_session
,
273 bool use_relative_url
) override
{
274 return new WebSocketSpdyHandshakeStream(spdy_session
);
283 TestCase kTests
[] = {
290 void PreconnectHelperForURL(int num_streams
,
292 HttpNetworkSession
* session
) {
293 HttpNetworkSessionPeer
peer(session
);
294 MockHttpStreamFactoryImplForPreconnect
* mock_factory
=
295 new MockHttpStreamFactoryImplForPreconnect(session
, false);
296 peer
.SetHttpStreamFactory(scoped_ptr
<HttpStreamFactory
>(mock_factory
));
297 SSLConfig ssl_config
;
298 session
->ssl_config_service()->GetSSLConfig(&ssl_config
);
300 HttpRequestInfo request
;
301 request
.method
= "GET";
303 request
.load_flags
= 0;
305 session
->http_stream_factory()->PreconnectStreams(num_streams
, request
,
306 ssl_config
, ssl_config
);
307 mock_factory
->WaitForPreconnects();
310 void PreconnectHelper(const TestCase
& test
,
311 HttpNetworkSession
* session
) {
312 GURL url
= test
.ssl
? GURL("https://www.google.com") :
313 GURL("http://www.google.com");
314 PreconnectHelperForURL(test
.num_streams
, url
, session
);
317 template<typename ParentPool
>
318 class CapturePreconnectsSocketPool
: public ParentPool
{
320 CapturePreconnectsSocketPool(HostResolver
* host_resolver
,
321 CertVerifier
* cert_verifier
);
323 int last_num_streams() const {
324 return last_num_streams_
;
327 int RequestSocket(const std::string
& group_name
,
328 const void* socket_params
,
329 RequestPriority priority
,
330 ClientSocketHandle
* handle
,
331 const CompletionCallback
& callback
,
332 const BoundNetLog
& net_log
) override
{
334 return ERR_UNEXPECTED
;
337 void RequestSockets(const std::string
& group_name
,
338 const void* socket_params
,
340 const BoundNetLog
& net_log
) override
{
341 last_num_streams_
= num_sockets
;
344 void CancelRequest(const std::string
& group_name
,
345 ClientSocketHandle
* handle
) override
{
348 void ReleaseSocket(const std::string
& group_name
,
349 scoped_ptr
<StreamSocket
> socket
,
353 void CloseIdleSockets() override
{ ADD_FAILURE(); }
354 int IdleSocketCount() const override
{
358 int IdleSocketCountInGroup(const std::string
& group_name
) const override
{
362 LoadState
GetLoadState(const std::string
& group_name
,
363 const ClientSocketHandle
* handle
) const override
{
365 return LOAD_STATE_IDLE
;
367 base::TimeDelta
ConnectionTimeout() const override
{
368 return base::TimeDelta();
372 int last_num_streams_
;
375 typedef CapturePreconnectsSocketPool
<TransportClientSocketPool
>
376 CapturePreconnectsTransportSocketPool
;
377 typedef CapturePreconnectsSocketPool
<HttpProxyClientSocketPool
>
378 CapturePreconnectsHttpProxySocketPool
;
379 typedef CapturePreconnectsSocketPool
<SOCKSClientSocketPool
>
380 CapturePreconnectsSOCKSSocketPool
;
381 typedef CapturePreconnectsSocketPool
<SSLClientSocketPool
>
382 CapturePreconnectsSSLSocketPool
;
384 template <typename ParentPool
>
385 CapturePreconnectsSocketPool
<ParentPool
>::CapturePreconnectsSocketPool(
386 HostResolver
* host_resolver
,
387 CertVerifier
* /* cert_verifier */)
388 : ParentPool(0, 0, host_resolver
, nullptr, nullptr), last_num_streams_(-1) {
392 CapturePreconnectsHttpProxySocketPool::CapturePreconnectsSocketPool(
393 HostResolver
* /* host_resolver */,
394 CertVerifier
* /* cert_verifier */)
395 : HttpProxyClientSocketPool(0, 0, nullptr, nullptr, nullptr),
396 last_num_streams_(-1) {
400 CapturePreconnectsSSLSocketPool::CapturePreconnectsSocketPool(
401 HostResolver
* /* host_resolver */,
402 CertVerifier
* cert_verifier
)
403 : SSLClientSocketPool(0,
406 nullptr, // channel_id_store
407 nullptr, // transport_security_state
408 nullptr, // cert_transparency_verifier
409 nullptr, // cert_policy_enforcer
410 std::string(), // ssl_session_cache_shard
411 nullptr, // deterministic_socket_factory
412 nullptr, // transport_socket_pool
415 nullptr, // ssl_config_service
417 last_num_streams_(-1) {
420 class HttpStreamFactoryTest
: public ::testing::Test
,
421 public ::testing::WithParamInterface
<NextProto
> {
424 INSTANTIATE_TEST_CASE_P(NextProto
,
425 HttpStreamFactoryTest
,
426 testing::Values(kProtoSPDY31
,
429 TEST_P(HttpStreamFactoryTest
, PreconnectDirect
) {
430 for (size_t i
= 0; i
< arraysize(kTests
); ++i
) {
431 SpdySessionDependencies
session_deps(
432 GetParam(), ProxyService::CreateDirect());
433 scoped_refptr
<HttpNetworkSession
> session(
434 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
435 HttpNetworkSessionPeer
peer(session
);
436 CapturePreconnectsTransportSocketPool
* transport_conn_pool
=
437 new CapturePreconnectsTransportSocketPool(
438 session_deps
.host_resolver
.get(),
439 session_deps
.cert_verifier
.get());
440 CapturePreconnectsSSLSocketPool
* ssl_conn_pool
=
441 new CapturePreconnectsSSLSocketPool(
442 session_deps
.host_resolver
.get(),
443 session_deps
.cert_verifier
.get());
444 scoped_ptr
<MockClientSocketPoolManager
> mock_pool_manager(
445 new MockClientSocketPoolManager
);
446 mock_pool_manager
->SetTransportSocketPool(transport_conn_pool
);
447 mock_pool_manager
->SetSSLSocketPool(ssl_conn_pool
);
448 peer
.SetClientSocketPoolManager(mock_pool_manager
.Pass());
449 PreconnectHelper(kTests
[i
], session
.get());
451 EXPECT_EQ(kTests
[i
].num_streams
, ssl_conn_pool
->last_num_streams());
453 EXPECT_EQ(kTests
[i
].num_streams
, transport_conn_pool
->last_num_streams());
457 TEST_P(HttpStreamFactoryTest
, PreconnectHttpProxy
) {
458 for (size_t i
= 0; i
< arraysize(kTests
); ++i
) {
459 SpdySessionDependencies
session_deps(
460 GetParam(), ProxyService::CreateFixed("http_proxy"));
461 scoped_refptr
<HttpNetworkSession
> session(
462 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
463 HttpNetworkSessionPeer
peer(session
);
464 HostPortPair
proxy_host("http_proxy", 80);
465 CapturePreconnectsHttpProxySocketPool
* http_proxy_pool
=
466 new CapturePreconnectsHttpProxySocketPool(
467 session_deps
.host_resolver
.get(),
468 session_deps
.cert_verifier
.get());
469 CapturePreconnectsSSLSocketPool
* ssl_conn_pool
=
470 new CapturePreconnectsSSLSocketPool(
471 session_deps
.host_resolver
.get(),
472 session_deps
.cert_verifier
.get());
473 scoped_ptr
<MockClientSocketPoolManager
> mock_pool_manager(
474 new MockClientSocketPoolManager
);
475 mock_pool_manager
->SetSocketPoolForHTTPProxy(proxy_host
, http_proxy_pool
);
476 mock_pool_manager
->SetSocketPoolForSSLWithProxy(proxy_host
, ssl_conn_pool
);
477 peer
.SetClientSocketPoolManager(mock_pool_manager
.Pass());
478 PreconnectHelper(kTests
[i
], session
.get());
480 EXPECT_EQ(kTests
[i
].num_streams
, ssl_conn_pool
->last_num_streams());
482 EXPECT_EQ(kTests
[i
].num_streams
, http_proxy_pool
->last_num_streams());
486 TEST_P(HttpStreamFactoryTest
, PreconnectSocksProxy
) {
487 for (size_t i
= 0; i
< arraysize(kTests
); ++i
) {
488 SpdySessionDependencies
session_deps(
489 GetParam(), ProxyService::CreateFixed("socks4://socks_proxy:1080"));
490 scoped_refptr
<HttpNetworkSession
> session(
491 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
492 HttpNetworkSessionPeer
peer(session
);
493 HostPortPair
proxy_host("socks_proxy", 1080);
494 CapturePreconnectsSOCKSSocketPool
* socks_proxy_pool
=
495 new CapturePreconnectsSOCKSSocketPool(
496 session_deps
.host_resolver
.get(),
497 session_deps
.cert_verifier
.get());
498 CapturePreconnectsSSLSocketPool
* ssl_conn_pool
=
499 new CapturePreconnectsSSLSocketPool(
500 session_deps
.host_resolver
.get(),
501 session_deps
.cert_verifier
.get());
502 scoped_ptr
<MockClientSocketPoolManager
> mock_pool_manager(
503 new MockClientSocketPoolManager
);
504 mock_pool_manager
->SetSocketPoolForSOCKSProxy(proxy_host
, socks_proxy_pool
);
505 mock_pool_manager
->SetSocketPoolForSSLWithProxy(proxy_host
, ssl_conn_pool
);
506 peer
.SetClientSocketPoolManager(mock_pool_manager
.Pass());
507 PreconnectHelper(kTests
[i
], session
.get());
509 EXPECT_EQ(kTests
[i
].num_streams
, ssl_conn_pool
->last_num_streams());
511 EXPECT_EQ(kTests
[i
].num_streams
, socks_proxy_pool
->last_num_streams());
515 TEST_P(HttpStreamFactoryTest
, PreconnectDirectWithExistingSpdySession
) {
516 for (size_t i
= 0; i
< arraysize(kTests
); ++i
) {
517 SpdySessionDependencies
session_deps(
518 GetParam(), ProxyService::CreateDirect());
519 scoped_refptr
<HttpNetworkSession
> session(
520 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
521 HttpNetworkSessionPeer
peer(session
);
523 // Put a SpdySession in the pool.
524 HostPortPair
host_port_pair("www.google.com", 443);
525 SpdySessionKey
key(host_port_pair
, ProxyServer::Direct(),
526 PRIVACY_MODE_DISABLED
);
527 ignore_result(CreateFakeSpdySession(session
->spdy_session_pool(), key
));
529 CapturePreconnectsTransportSocketPool
* transport_conn_pool
=
530 new CapturePreconnectsTransportSocketPool(
531 session_deps
.host_resolver
.get(),
532 session_deps
.cert_verifier
.get());
533 CapturePreconnectsSSLSocketPool
* ssl_conn_pool
=
534 new CapturePreconnectsSSLSocketPool(
535 session_deps
.host_resolver
.get(),
536 session_deps
.cert_verifier
.get());
537 scoped_ptr
<MockClientSocketPoolManager
> mock_pool_manager(
538 new MockClientSocketPoolManager
);
539 mock_pool_manager
->SetTransportSocketPool(transport_conn_pool
);
540 mock_pool_manager
->SetSSLSocketPool(ssl_conn_pool
);
541 peer
.SetClientSocketPoolManager(mock_pool_manager
.Pass());
542 PreconnectHelper(kTests
[i
], session
.get());
543 // We shouldn't be preconnecting if we have an existing session, which is
544 // the case for https://www.google.com.
546 EXPECT_EQ(-1, ssl_conn_pool
->last_num_streams());
548 EXPECT_EQ(kTests
[i
].num_streams
,
549 transport_conn_pool
->last_num_streams());
553 // Verify that preconnects to unsafe ports are cancelled before they reach
555 TEST_P(HttpStreamFactoryTest
, PreconnectUnsafePort
) {
556 ASSERT_FALSE(IsPortAllowedForScheme(7, "http"));
558 SpdySessionDependencies
session_deps(
559 GetParam(), ProxyService::CreateDirect());
560 scoped_refptr
<HttpNetworkSession
> session(
561 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
562 HttpNetworkSessionPeer
peer(session
);
563 CapturePreconnectsTransportSocketPool
* transport_conn_pool
=
564 new CapturePreconnectsTransportSocketPool(
565 session_deps
.host_resolver
.get(),
566 session_deps
.cert_verifier
.get());
567 scoped_ptr
<MockClientSocketPoolManager
> mock_pool_manager(
568 new MockClientSocketPoolManager
);
569 mock_pool_manager
->SetTransportSocketPool(transport_conn_pool
);
570 peer
.SetClientSocketPoolManager(mock_pool_manager
.Pass());
572 PreconnectHelperForURL(1, GURL("http://www.google.com:7"), session
.get());
573 EXPECT_EQ(-1, transport_conn_pool
->last_num_streams());
576 TEST_P(HttpStreamFactoryTest
, JobNotifiesProxy
) {
577 const char* kProxyString
= "PROXY bad:99; PROXY maybe:80; DIRECT";
578 SpdySessionDependencies
session_deps(
579 GetParam(), ProxyService::CreateFixedFromPacResult(kProxyString
));
581 // First connection attempt fails
582 StaticSocketDataProvider socket_data1
;
583 socket_data1
.set_connect_data(MockConnect(ASYNC
, ERR_ADDRESS_UNREACHABLE
));
584 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data1
);
586 // Second connection attempt succeeds
587 StaticSocketDataProvider socket_data2
;
588 socket_data2
.set_connect_data(MockConnect(ASYNC
, OK
));
589 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data2
);
591 scoped_refptr
<HttpNetworkSession
> session(
592 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
594 // Now request a stream. It should succeed using the second proxy in the
596 HttpRequestInfo request_info
;
597 request_info
.method
= "GET";
598 request_info
.url
= GURL("http://www.google.com");
600 SSLConfig ssl_config
;
601 StreamRequestWaiter waiter
;
602 scoped_ptr
<HttpStreamRequest
> request(
603 session
->http_stream_factory()->RequestStream(
604 request_info
, DEFAULT_PRIORITY
, ssl_config
, ssl_config
,
605 &waiter
, BoundNetLog()));
606 waiter
.WaitForStream();
608 // The proxy that failed should now be known to the proxy_service as bad.
609 const ProxyRetryInfoMap
& retry_info
=
610 session
->proxy_service()->proxy_retry_info();
611 EXPECT_EQ(1u, retry_info
.size());
612 ProxyRetryInfoMap::const_iterator iter
= retry_info
.find("bad:99");
613 EXPECT_TRUE(iter
!= retry_info
.end());
616 TEST_P(HttpStreamFactoryTest
, UnreachableQuicProxyMarkedAsBad
) {
617 for (int i
= 1; i
<= 2; i
++) {
619 i
== 1 ? ERR_QUIC_PROTOCOL_ERROR
: ERR_QUIC_HANDSHAKE_FAILED
;
621 scoped_ptr
<ProxyService
> proxy_service
;
623 ProxyService::CreateFixedFromPacResult("QUIC bad:99; DIRECT"));
625 HttpNetworkSession::Params params
;
626 params
.enable_quic
= true;
627 params
.enable_quic_for_proxies
= true;
628 scoped_refptr
<SSLConfigServiceDefaults
> ssl_config_service(
629 new SSLConfigServiceDefaults
);
630 HttpServerPropertiesImpl http_server_properties
;
631 MockClientSocketFactory socket_factory
;
632 params
.client_socket_factory
= &socket_factory
;
633 MockHostResolver host_resolver
;
634 params
.host_resolver
= &host_resolver
;
635 TransportSecurityState transport_security_state
;
636 params
.transport_security_state
= &transport_security_state
;
637 params
.proxy_service
= proxy_service
.get();
638 params
.ssl_config_service
= ssl_config_service
.get();
639 params
.http_server_properties
= http_server_properties
.GetWeakPtr();
641 scoped_refptr
<HttpNetworkSession
> session
;
642 session
= new HttpNetworkSession(params
);
643 session
->quic_stream_factory()->set_require_confirmation(false);
645 StaticSocketDataProvider socket_data1
;
646 socket_data1
.set_connect_data(MockConnect(ASYNC
, mock_error
));
647 socket_factory
.AddSocketDataProvider(&socket_data1
);
649 // Second connection attempt succeeds.
650 StaticSocketDataProvider socket_data2
;
651 socket_data2
.set_connect_data(MockConnect(ASYNC
, OK
));
652 socket_factory
.AddSocketDataProvider(&socket_data2
);
654 // Now request a stream. It should succeed using the second proxy in the
656 HttpRequestInfo request_info
;
657 request_info
.method
= "GET";
658 request_info
.url
= GURL("http://www.google.com");
660 SSLConfig ssl_config
;
661 StreamRequestWaiter waiter
;
662 scoped_ptr
<HttpStreamRequest
> request(
663 session
->http_stream_factory()->RequestStream(
664 request_info
, DEFAULT_PRIORITY
, ssl_config
, ssl_config
, &waiter
,
666 waiter
.WaitForStream();
668 // The proxy that failed should now be known to the proxy_service as bad.
669 const ProxyRetryInfoMap
& retry_info
=
670 session
->proxy_service()->proxy_retry_info();
671 EXPECT_EQ(1u, retry_info
.size()) << i
;
672 EXPECT_TRUE(waiter
.used_proxy_info().is_direct());
674 ProxyRetryInfoMap::const_iterator iter
= retry_info
.find("quic://bad:99");
675 EXPECT_TRUE(iter
!= retry_info
.end()) << i
;
681 TEST_P(HttpStreamFactoryTest
, QuicLossyProxyMarkedAsBad
) {
683 scoped_ptr
<ProxyService
> proxy_service
;
685 ProxyService::CreateFixedFromPacResult("QUIC bad:99; DIRECT"));
687 HttpNetworkSession::Params params
;
688 params
.enable_quic
= true;
689 params
.enable_quic_for_proxies
= true;
690 scoped_refptr
<SSLConfigServiceDefaults
> ssl_config_service(
691 new SSLConfigServiceDefaults
);
692 HttpServerPropertiesImpl http_server_properties
;
693 MockClientSocketFactory socket_factory
;
694 params
.client_socket_factory
= &socket_factory
;
695 MockHostResolver host_resolver
;
696 params
.host_resolver
= &host_resolver
;
697 TransportSecurityState transport_security_state
;
698 params
.transport_security_state
= &transport_security_state
;
699 params
.proxy_service
= proxy_service
.get();
700 params
.ssl_config_service
= ssl_config_service
.get();
701 params
.http_server_properties
= http_server_properties
.GetWeakPtr();
702 params
.quic_max_number_of_lossy_connections
= 2;
704 scoped_refptr
<HttpNetworkSession
> session
;
705 session
= new HttpNetworkSession(params
);
706 session
->quic_stream_factory()->set_require_confirmation(false);
708 session
->quic_stream_factory()->number_of_lossy_connections_
[99] =
709 params
.quic_max_number_of_lossy_connections
;
711 StaticSocketDataProvider socket_data2
;
712 socket_data2
.set_connect_data(MockConnect(ASYNC
, OK
));
713 socket_factory
.AddSocketDataProvider(&socket_data2
);
715 // Now request a stream. It should succeed using the second proxy in the
717 HttpRequestInfo request_info
;
718 request_info
.method
= "GET";
719 request_info
.url
= GURL("http://www.google.com");
721 SSLConfig ssl_config
;
722 StreamRequestWaiter waiter
;
723 scoped_ptr
<HttpStreamRequest
> request(
724 session
->http_stream_factory()->RequestStream(
725 request_info
, DEFAULT_PRIORITY
, ssl_config
, ssl_config
, &waiter
,
727 waiter
.WaitForStream();
729 // The proxy that failed should now be known to the proxy_service as bad.
730 const ProxyRetryInfoMap
& retry_info
=
731 session
->proxy_service()->proxy_retry_info();
732 EXPECT_EQ(1u, retry_info
.size());
733 EXPECT_TRUE(waiter
.used_proxy_info().is_direct());
735 ProxyRetryInfoMap::const_iterator iter
= retry_info
.find("quic://bad:99");
736 EXPECT_TRUE(iter
!= retry_info
.end());
741 TEST_P(HttpStreamFactoryTest
, PrivacyModeDisablesChannelId
) {
742 SpdySessionDependencies
session_deps(
743 GetParam(), ProxyService::CreateDirect());
745 StaticSocketDataProvider socket_data
;
746 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
747 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
749 SSLSocketDataProvider
ssl(ASYNC
, OK
);
750 session_deps
.socket_factory
->AddSSLSocketDataProvider(&ssl
);
752 scoped_refptr
<HttpNetworkSession
> session(
753 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
755 // Set an existing SpdySession in the pool.
756 HostPortPair
host_port_pair("www.google.com", 443);
757 SpdySessionKey
key(host_port_pair
, ProxyServer::Direct(),
758 PRIVACY_MODE_ENABLED
);
760 HttpRequestInfo request_info
;
761 request_info
.method
= "GET";
762 request_info
.url
= GURL("https://www.google.com");
763 request_info
.load_flags
= 0;
764 request_info
.privacy_mode
= PRIVACY_MODE_DISABLED
;
766 SSLConfig ssl_config
;
767 StreamRequestWaiter waiter
;
768 scoped_ptr
<HttpStreamRequest
> request(
769 session
->http_stream_factory()->RequestStream(
770 request_info
, DEFAULT_PRIORITY
, ssl_config
, ssl_config
,
771 &waiter
, BoundNetLog()));
772 waiter
.WaitForStream();
774 // The stream shouldn't come from spdy as we are using different privacy mode
775 EXPECT_FALSE(request
->using_spdy());
777 SSLConfig used_ssl_config
= waiter
.used_ssl_config();
778 EXPECT_EQ(used_ssl_config
.channel_id_enabled
, ssl_config
.channel_id_enabled
);
783 // Return count of distinct groups in given socket pool.
784 int GetSocketPoolGroupCount(ClientSocketPool
* pool
) {
786 scoped_ptr
<base::DictionaryValue
> dict(pool
->GetInfoAsValue("", "", false));
787 EXPECT_TRUE(dict
!= nullptr);
788 base::DictionaryValue
* groups
= nullptr;
789 if (dict
->GetDictionary("groups", &groups
) && (groups
!= nullptr)) {
790 count
= static_cast<int>(groups
->size());
795 // Return count of distinct spdy sessions.
796 int GetSpdySessionCount(HttpNetworkSession
* session
) {
797 scoped_ptr
<base::Value
> value(
798 session
->spdy_session_pool()->SpdySessionPoolInfoToValue());
799 base::ListValue
* session_list
;
800 if (!value
|| !value
->GetAsList(&session_list
))
802 return session_list
->GetSize();
807 TEST_P(HttpStreamFactoryTest
, PrivacyModeUsesDifferentSocketPoolGroup
) {
808 SpdySessionDependencies
session_deps(
809 GetParam(), ProxyService::CreateDirect());
811 StaticSocketDataProvider socket_data
;
812 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
813 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
815 SSLSocketDataProvider
ssl(ASYNC
, OK
);
816 session_deps
.socket_factory
->AddSSLSocketDataProvider(&ssl
);
818 scoped_refptr
<HttpNetworkSession
> session(
819 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
820 SSLClientSocketPool
* ssl_pool
= session
->GetSSLSocketPool(
821 HttpNetworkSession::NORMAL_SOCKET_POOL
);
823 EXPECT_EQ(GetSocketPoolGroupCount(ssl_pool
), 0);
825 HttpRequestInfo request_info
;
826 request_info
.method
= "GET";
827 request_info
.url
= GURL("https://www.google.com");
828 request_info
.load_flags
= 0;
829 request_info
.privacy_mode
= PRIVACY_MODE_DISABLED
;
831 SSLConfig ssl_config
;
832 StreamRequestWaiter waiter
;
834 scoped_ptr
<HttpStreamRequest
> request1(
835 session
->http_stream_factory()->RequestStream(
836 request_info
, DEFAULT_PRIORITY
, ssl_config
, ssl_config
,
837 &waiter
, BoundNetLog()));
838 waiter
.WaitForStream();
840 EXPECT_EQ(GetSocketPoolGroupCount(ssl_pool
), 1);
842 scoped_ptr
<HttpStreamRequest
> request2(
843 session
->http_stream_factory()->RequestStream(
844 request_info
, DEFAULT_PRIORITY
, ssl_config
, ssl_config
,
845 &waiter
, BoundNetLog()));
846 waiter
.WaitForStream();
848 EXPECT_EQ(GetSocketPoolGroupCount(ssl_pool
), 1);
850 request_info
.privacy_mode
= PRIVACY_MODE_ENABLED
;
851 scoped_ptr
<HttpStreamRequest
> request3(
852 session
->http_stream_factory()->RequestStream(
853 request_info
, DEFAULT_PRIORITY
, ssl_config
, ssl_config
,
854 &waiter
, BoundNetLog()));
855 waiter
.WaitForStream();
857 EXPECT_EQ(GetSocketPoolGroupCount(ssl_pool
), 2);
860 TEST_P(HttpStreamFactoryTest
, GetLoadState
) {
861 SpdySessionDependencies
session_deps(
862 GetParam(), ProxyService::CreateDirect());
864 StaticSocketDataProvider socket_data
;
865 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
866 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
868 scoped_refptr
<HttpNetworkSession
> session(
869 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
871 HttpRequestInfo request_info
;
872 request_info
.method
= "GET";
873 request_info
.url
= GURL("http://www.google.com");
875 SSLConfig ssl_config
;
876 StreamRequestWaiter waiter
;
877 scoped_ptr
<HttpStreamRequest
> request(
878 session
->http_stream_factory()->RequestStream(
879 request_info
, DEFAULT_PRIORITY
, ssl_config
, ssl_config
,
880 &waiter
, BoundNetLog()));
882 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST
, request
->GetLoadState());
884 waiter
.WaitForStream();
887 TEST_P(HttpStreamFactoryTest
, RequestHttpStream
) {
888 SpdySessionDependencies
session_deps(
889 GetParam(), ProxyService::CreateDirect());
891 StaticSocketDataProvider socket_data
;
892 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
893 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
895 scoped_refptr
<HttpNetworkSession
> session(
896 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
898 // Now request a stream. It should succeed using the second proxy in the
900 HttpRequestInfo request_info
;
901 request_info
.method
= "GET";
902 request_info
.url
= GURL("http://www.google.com");
903 request_info
.load_flags
= 0;
905 SSLConfig ssl_config
;
906 StreamRequestWaiter waiter
;
907 scoped_ptr
<HttpStreamRequest
> request(
908 session
->http_stream_factory()->RequestStream(
915 waiter
.WaitForStream();
916 EXPECT_TRUE(waiter
.stream_done());
917 ASSERT_TRUE(nullptr != waiter
.stream());
918 EXPECT_TRUE(nullptr == waiter
.websocket_stream());
920 EXPECT_EQ(0, GetSpdySessionCount(session
.get()));
921 EXPECT_EQ(1, GetSocketPoolGroupCount(
922 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
923 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSSLSocketPool(
924 HttpNetworkSession::NORMAL_SOCKET_POOL
)));
925 EXPECT_EQ(0, GetSocketPoolGroupCount(
926 session
->GetTransportSocketPool(
927 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
928 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSSLSocketPool(
929 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
930 EXPECT_TRUE(waiter
.used_proxy_info().is_direct());
933 TEST_P(HttpStreamFactoryTest
, RequestHttpStreamOverSSL
) {
934 SpdySessionDependencies
session_deps(
935 GetParam(), ProxyService::CreateDirect());
937 MockRead
mock_read(ASYNC
, OK
);
938 StaticSocketDataProvider
socket_data(&mock_read
, 1, nullptr, 0);
939 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
940 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
942 SSLSocketDataProvider
ssl_socket_data(ASYNC
, OK
);
943 session_deps
.socket_factory
->AddSSLSocketDataProvider(&ssl_socket_data
);
945 scoped_refptr
<HttpNetworkSession
> session(
946 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
948 // Now request a stream.
949 HttpRequestInfo request_info
;
950 request_info
.method
= "GET";
951 request_info
.url
= GURL("https://www.google.com");
952 request_info
.load_flags
= 0;
954 SSLConfig ssl_config
;
955 StreamRequestWaiter waiter
;
956 scoped_ptr
<HttpStreamRequest
> request(
957 session
->http_stream_factory()->RequestStream(
964 waiter
.WaitForStream();
965 EXPECT_TRUE(waiter
.stream_done());
966 ASSERT_TRUE(nullptr != waiter
.stream());
967 EXPECT_TRUE(nullptr == waiter
.websocket_stream());
969 EXPECT_EQ(0, GetSpdySessionCount(session
.get()));
970 EXPECT_EQ(1, GetSocketPoolGroupCount(
971 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
972 EXPECT_EQ(1, GetSocketPoolGroupCount(
973 session
->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
974 EXPECT_EQ(0, GetSocketPoolGroupCount(
975 session
->GetTransportSocketPool(
976 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
977 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSSLSocketPool(
978 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
979 EXPECT_TRUE(waiter
.used_proxy_info().is_direct());
982 TEST_P(HttpStreamFactoryTest
, RequestHttpStreamOverProxy
) {
983 SpdySessionDependencies
session_deps(
984 GetParam(), ProxyService::CreateFixed("myproxy:8888"));
986 StaticSocketDataProvider socket_data
;
987 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
988 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
990 scoped_refptr
<HttpNetworkSession
> session(
991 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
993 // Now request a stream. It should succeed using the second proxy in the
995 HttpRequestInfo request_info
;
996 request_info
.method
= "GET";
997 request_info
.url
= GURL("http://www.google.com");
998 request_info
.load_flags
= 0;
1000 SSLConfig ssl_config
;
1001 StreamRequestWaiter waiter
;
1002 scoped_ptr
<HttpStreamRequest
> request(
1003 session
->http_stream_factory()->RequestStream(
1010 waiter
.WaitForStream();
1011 EXPECT_TRUE(waiter
.stream_done());
1012 ASSERT_TRUE(nullptr != waiter
.stream());
1013 EXPECT_TRUE(nullptr == waiter
.websocket_stream());
1015 EXPECT_EQ(0, GetSpdySessionCount(session
.get()));
1016 EXPECT_EQ(0, GetSocketPoolGroupCount(
1017 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1018 EXPECT_EQ(0, GetSocketPoolGroupCount(
1019 session
->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1020 EXPECT_EQ(1, GetSocketPoolGroupCount(session
->GetSocketPoolForHTTPProxy(
1021 HttpNetworkSession::NORMAL_SOCKET_POOL
,
1022 HostPortPair("myproxy", 8888))));
1023 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSocketPoolForSSLWithProxy(
1024 HttpNetworkSession::NORMAL_SOCKET_POOL
,
1025 HostPortPair("myproxy", 8888))));
1026 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSocketPoolForHTTPProxy(
1027 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
,
1028 HostPortPair("myproxy", 8888))));
1029 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSocketPoolForSSLWithProxy(
1030 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
,
1031 HostPortPair("myproxy", 8888))));
1032 EXPECT_FALSE(waiter
.used_proxy_info().is_direct());
1035 TEST_P(HttpStreamFactoryTest
, RequestWebSocketBasicHandshakeStream
) {
1036 SpdySessionDependencies
session_deps(
1037 GetParam(), ProxyService::CreateDirect());
1039 StaticSocketDataProvider socket_data
;
1040 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
1041 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
1043 scoped_refptr
<HttpNetworkSession
> session(
1044 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
1046 // Now request a stream.
1047 HttpRequestInfo request_info
;
1048 request_info
.method
= "GET";
1049 request_info
.url
= GURL("ws://www.google.com");
1050 request_info
.load_flags
= 0;
1052 SSLConfig ssl_config
;
1053 StreamRequestWaiter waiter
;
1054 WebSocketStreamCreateHelper create_helper
;
1055 scoped_ptr
<HttpStreamRequest
> request(
1056 session
->http_stream_factory_for_websocket()
1057 ->RequestWebSocketHandshakeStream(request_info
,
1064 waiter
.WaitForStream();
1065 EXPECT_TRUE(waiter
.stream_done());
1066 EXPECT_TRUE(nullptr == waiter
.stream());
1067 ASSERT_TRUE(nullptr != waiter
.websocket_stream());
1068 EXPECT_EQ(MockWebSocketHandshakeStream::kStreamTypeBasic
,
1069 waiter
.websocket_stream()->type());
1070 EXPECT_EQ(0, GetSocketPoolGroupCount(
1071 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1072 EXPECT_EQ(0, GetSocketPoolGroupCount(
1073 session
->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1074 EXPECT_EQ(0, GetSocketPoolGroupCount(
1075 session
->GetSSLSocketPool(HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1076 EXPECT_TRUE(waiter
.used_proxy_info().is_direct());
1079 TEST_P(HttpStreamFactoryTest
, RequestWebSocketBasicHandshakeStreamOverSSL
) {
1080 SpdySessionDependencies
session_deps(
1081 GetParam(), ProxyService::CreateDirect());
1083 MockRead
mock_read(ASYNC
, OK
);
1084 StaticSocketDataProvider
socket_data(&mock_read
, 1, nullptr, 0);
1085 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
1086 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
1088 SSLSocketDataProvider
ssl_socket_data(ASYNC
, OK
);
1089 session_deps
.socket_factory
->AddSSLSocketDataProvider(&ssl_socket_data
);
1091 scoped_refptr
<HttpNetworkSession
> session(
1092 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
1094 // Now request a stream.
1095 HttpRequestInfo request_info
;
1096 request_info
.method
= "GET";
1097 request_info
.url
= GURL("wss://www.google.com");
1098 request_info
.load_flags
= 0;
1100 SSLConfig ssl_config
;
1101 StreamRequestWaiter waiter
;
1102 WebSocketStreamCreateHelper create_helper
;
1103 scoped_ptr
<HttpStreamRequest
> request(
1104 session
->http_stream_factory_for_websocket()
1105 ->RequestWebSocketHandshakeStream(request_info
,
1112 waiter
.WaitForStream();
1113 EXPECT_TRUE(waiter
.stream_done());
1114 EXPECT_TRUE(nullptr == waiter
.stream());
1115 ASSERT_TRUE(nullptr != waiter
.websocket_stream());
1116 EXPECT_EQ(MockWebSocketHandshakeStream::kStreamTypeBasic
,
1117 waiter
.websocket_stream()->type());
1118 EXPECT_EQ(0, GetSocketPoolGroupCount(
1119 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1120 EXPECT_EQ(0, GetSocketPoolGroupCount(
1121 session
->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1122 EXPECT_EQ(1, GetSocketPoolGroupCount(
1123 session
->GetSSLSocketPool(HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1124 EXPECT_TRUE(waiter
.used_proxy_info().is_direct());
1127 TEST_P(HttpStreamFactoryTest
, RequestWebSocketBasicHandshakeStreamOverProxy
) {
1128 SpdySessionDependencies
session_deps(
1129 GetParam(), ProxyService::CreateFixed("myproxy:8888"));
1131 MockRead
read(SYNCHRONOUS
, "HTTP/1.0 200 Connection established\r\n\r\n");
1132 StaticSocketDataProvider
socket_data(&read
, 1, 0, 0);
1133 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
1134 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
1136 scoped_refptr
<HttpNetworkSession
> session(
1137 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
1139 // Now request a stream.
1140 HttpRequestInfo request_info
;
1141 request_info
.method
= "GET";
1142 request_info
.url
= GURL("ws://www.google.com");
1143 request_info
.load_flags
= 0;
1145 SSLConfig ssl_config
;
1146 StreamRequestWaiter waiter
;
1147 WebSocketStreamCreateHelper create_helper
;
1148 scoped_ptr
<HttpStreamRequest
> request(
1149 session
->http_stream_factory_for_websocket()
1150 ->RequestWebSocketHandshakeStream(request_info
,
1157 waiter
.WaitForStream();
1158 EXPECT_TRUE(waiter
.stream_done());
1159 EXPECT_TRUE(nullptr == waiter
.stream());
1160 ASSERT_TRUE(nullptr != waiter
.websocket_stream());
1161 EXPECT_EQ(MockWebSocketHandshakeStream::kStreamTypeBasic
,
1162 waiter
.websocket_stream()->type());
1163 EXPECT_EQ(0, GetSocketPoolGroupCount(
1164 session
->GetTransportSocketPool(
1165 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1166 EXPECT_EQ(0, GetSocketPoolGroupCount(
1167 session
->GetSSLSocketPool(HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1168 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSocketPoolForHTTPProxy(
1169 HttpNetworkSession::NORMAL_SOCKET_POOL
,
1170 HostPortPair("myproxy", 8888))));
1171 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSocketPoolForSSLWithProxy(
1172 HttpNetworkSession::NORMAL_SOCKET_POOL
,
1173 HostPortPair("myproxy", 8888))));
1174 EXPECT_EQ(1, GetSocketPoolGroupCount(session
->GetSocketPoolForHTTPProxy(
1175 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
,
1176 HostPortPair("myproxy", 8888))));
1177 EXPECT_EQ(0, GetSocketPoolGroupCount(session
->GetSocketPoolForSSLWithProxy(
1178 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
,
1179 HostPortPair("myproxy", 8888))));
1180 EXPECT_FALSE(waiter
.used_proxy_info().is_direct());
1183 TEST_P(HttpStreamFactoryTest
, RequestSpdyHttpStream
) {
1184 SpdySessionDependencies
session_deps(GetParam(),
1185 ProxyService::CreateDirect());
1187 MockRead
mock_read(SYNCHRONOUS
, ERR_IO_PENDING
);
1188 SequencedSocketData
socket_data(&mock_read
, 1, nullptr, 0);
1189 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
1190 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
1192 SSLSocketDataProvider
ssl_socket_data(ASYNC
, OK
);
1193 ssl_socket_data
.SetNextProto(GetParam());
1194 session_deps
.socket_factory
->AddSSLSocketDataProvider(&ssl_socket_data
);
1196 HostPortPair
host_port_pair("www.google.com", 443);
1197 scoped_refptr
<HttpNetworkSession
> session(
1198 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
1200 // Now request a stream.
1201 HttpRequestInfo request_info
;
1202 request_info
.method
= "GET";
1203 request_info
.url
= GURL("https://www.google.com");
1204 request_info
.load_flags
= 0;
1206 SSLConfig ssl_config
;
1207 StreamRequestWaiter waiter
;
1208 scoped_ptr
<HttpStreamRequest
> request(
1209 session
->http_stream_factory()->RequestStream(
1216 waiter
.WaitForStream();
1217 EXPECT_TRUE(waiter
.stream_done());
1218 EXPECT_TRUE(nullptr == waiter
.websocket_stream());
1219 ASSERT_TRUE(nullptr != waiter
.stream());
1221 EXPECT_EQ(1, GetSpdySessionCount(session
.get()));
1222 EXPECT_EQ(1, GetSocketPoolGroupCount(
1223 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1224 EXPECT_EQ(1, GetSocketPoolGroupCount(
1225 session
->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1226 EXPECT_EQ(0, GetSocketPoolGroupCount(
1227 session
->GetTransportSocketPool(
1228 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1229 EXPECT_EQ(0, GetSocketPoolGroupCount(
1230 session
->GetSSLSocketPool(HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1231 EXPECT_TRUE(waiter
.used_proxy_info().is_direct());
1234 // TODO(ricea): This test can be removed once the new WebSocket stack supports
1235 // SPDY. Currently, even if we connect to a SPDY-supporting server, we need to
1237 TEST_P(HttpStreamFactoryTest
, RequestWebSocketSpdyHandshakeStreamButGetSSL
) {
1238 SpdySessionDependencies
session_deps(GetParam(),
1239 ProxyService::CreateDirect());
1241 MockRead
mock_read(SYNCHRONOUS
, ERR_IO_PENDING
);
1242 StaticSocketDataProvider
socket_data(&mock_read
, 1, nullptr, 0);
1243 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
1244 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
1246 SSLSocketDataProvider
ssl_socket_data(ASYNC
, OK
);
1247 session_deps
.socket_factory
->AddSSLSocketDataProvider(&ssl_socket_data
);
1249 HostPortPair
host_port_pair("www.google.com", 80);
1250 scoped_refptr
<HttpNetworkSession
>
1251 session(SpdySessionDependencies::SpdyCreateSession(&session_deps
));
1253 // Now request a stream.
1254 HttpRequestInfo request_info
;
1255 request_info
.method
= "GET";
1256 request_info
.url
= GURL("wss://www.google.com");
1257 request_info
.load_flags
= 0;
1259 SSLConfig ssl_config
;
1260 StreamRequestWaiter waiter1
;
1261 WebSocketStreamCreateHelper create_helper
;
1262 scoped_ptr
<HttpStreamRequest
> request1(
1263 session
->http_stream_factory_for_websocket()
1264 ->RequestWebSocketHandshakeStream(request_info
,
1271 waiter1
.WaitForStream();
1272 EXPECT_TRUE(waiter1
.stream_done());
1273 ASSERT_TRUE(nullptr != waiter1
.websocket_stream());
1274 EXPECT_EQ(MockWebSocketHandshakeStream::kStreamTypeBasic
,
1275 waiter1
.websocket_stream()->type());
1276 EXPECT_TRUE(nullptr == waiter1
.stream());
1278 EXPECT_EQ(0, GetSocketPoolGroupCount(
1279 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1280 EXPECT_EQ(0, GetSocketPoolGroupCount(
1281 session
->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1282 EXPECT_EQ(1, GetSocketPoolGroupCount(
1283 session
->GetSSLSocketPool(HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1284 EXPECT_TRUE(waiter1
.used_proxy_info().is_direct());
1287 // TODO(ricea): Re-enable once WebSocket-over-SPDY is implemented.
1288 TEST_P(HttpStreamFactoryTest
, DISABLED_RequestWebSocketSpdyHandshakeStream
) {
1289 SpdySessionDependencies
session_deps(GetParam(),
1290 ProxyService::CreateDirect());
1292 MockRead
mock_read(SYNCHRONOUS
, ERR_IO_PENDING
);
1293 StaticSocketDataProvider
socket_data(&mock_read
, 1, nullptr, 0);
1294 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
1295 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
1297 SSLSocketDataProvider
ssl_socket_data(ASYNC
, OK
);
1298 ssl_socket_data
.SetNextProto(GetParam());
1299 session_deps
.socket_factory
->AddSSLSocketDataProvider(&ssl_socket_data
);
1301 HostPortPair
host_port_pair("www.google.com", 80);
1302 scoped_refptr
<HttpNetworkSession
>
1303 session(SpdySessionDependencies::SpdyCreateSession(&session_deps
));
1305 // Now request a stream.
1306 HttpRequestInfo request_info
;
1307 request_info
.method
= "GET";
1308 request_info
.url
= GURL("wss://www.google.com");
1309 request_info
.load_flags
= 0;
1311 SSLConfig ssl_config
;
1312 StreamRequestWaiter waiter1
;
1313 WebSocketStreamCreateHelper create_helper
;
1314 scoped_ptr
<HttpStreamRequest
> request1(
1315 session
->http_stream_factory_for_websocket()
1316 ->RequestWebSocketHandshakeStream(request_info
,
1323 waiter1
.WaitForStream();
1324 EXPECT_TRUE(waiter1
.stream_done());
1325 ASSERT_TRUE(nullptr != waiter1
.websocket_stream());
1326 EXPECT_EQ(MockWebSocketHandshakeStream::kStreamTypeSpdy
,
1327 waiter1
.websocket_stream()->type());
1328 EXPECT_TRUE(nullptr == waiter1
.stream());
1330 StreamRequestWaiter waiter2
;
1331 scoped_ptr
<HttpStreamRequest
> request2(
1332 session
->http_stream_factory_for_websocket()
1333 ->RequestWebSocketHandshakeStream(request_info
,
1340 waiter2
.WaitForStream();
1341 EXPECT_TRUE(waiter2
.stream_done());
1342 ASSERT_TRUE(nullptr != waiter2
.websocket_stream());
1343 EXPECT_EQ(MockWebSocketHandshakeStream::kStreamTypeSpdy
,
1344 waiter2
.websocket_stream()->type());
1345 EXPECT_TRUE(nullptr == waiter2
.stream());
1346 EXPECT_NE(waiter2
.websocket_stream(), waiter1
.websocket_stream());
1347 EXPECT_EQ(static_cast<WebSocketSpdyHandshakeStream
*>(
1348 waiter2
.websocket_stream())->spdy_session(),
1349 static_cast<WebSocketSpdyHandshakeStream
*>(
1350 waiter1
.websocket_stream())->spdy_session());
1352 EXPECT_EQ(0, GetSocketPoolGroupCount(
1353 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1354 EXPECT_EQ(0, GetSocketPoolGroupCount(
1355 session
->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1356 EXPECT_EQ(1, GetSocketPoolGroupCount(
1357 session
->GetTransportSocketPool(
1358 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1359 EXPECT_EQ(1, GetSocketPoolGroupCount(
1360 session
->GetSSLSocketPool(HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1361 EXPECT_TRUE(waiter1
.used_proxy_info().is_direct());
1364 // TODO(ricea): Re-enable once WebSocket over SPDY is implemented.
1365 TEST_P(HttpStreamFactoryTest
, DISABLED_OrphanedWebSocketStream
) {
1366 SpdySessionDependencies
session_deps(GetParam(),
1367 ProxyService::CreateDirect());
1368 session_deps
.use_alternative_services
= true;
1370 MockRead
mock_read(ASYNC
, OK
);
1371 SequencedSocketData
socket_data(&mock_read
, 1, nullptr, 0);
1372 socket_data
.set_connect_data(MockConnect(ASYNC
, OK
));
1373 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data
);
1375 MockRead
mock_read2(ASYNC
, OK
);
1376 SequencedSocketData
socket_data2(&mock_read2
, 1, nullptr, 0);
1377 socket_data2
.set_connect_data(MockConnect(ASYNC
, ERR_IO_PENDING
));
1378 session_deps
.socket_factory
->AddSocketDataProvider(&socket_data2
);
1380 SSLSocketDataProvider
ssl_socket_data(ASYNC
, OK
);
1381 ssl_socket_data
.SetNextProto(GetParam());
1382 session_deps
.socket_factory
->AddSSLSocketDataProvider(&ssl_socket_data
);
1384 scoped_refptr
<HttpNetworkSession
> session(
1385 SpdySessionDependencies::SpdyCreateSession(&session_deps
));
1387 // Now request a stream.
1388 HttpRequestInfo request_info
;
1389 request_info
.method
= "GET";
1390 request_info
.url
= GURL("ws://www.google.com:8888");
1391 request_info
.load_flags
= 0;
1393 base::Time expiration
= base::Time::Now() + base::TimeDelta::FromDays(1);
1394 session
->http_server_properties()->SetAlternativeService(
1395 HostPortPair("www.google.com", 8888),
1396 AlternativeService(NPN_HTTP_2
, "www.google.com", 9999), 1.0, expiration
);
1398 SSLConfig ssl_config
;
1399 StreamRequestWaiter waiter
;
1400 WebSocketStreamCreateHelper create_helper
;
1401 scoped_ptr
<HttpStreamRequest
> request(
1402 session
->http_stream_factory_for_websocket()
1403 ->RequestWebSocketHandshakeStream(request_info
,
1410 waiter
.WaitForStream();
1411 EXPECT_TRUE(waiter
.stream_done());
1412 EXPECT_TRUE(nullptr == waiter
.stream());
1413 ASSERT_TRUE(nullptr != waiter
.websocket_stream());
1414 EXPECT_EQ(MockWebSocketHandshakeStream::kStreamTypeSpdy
,
1415 waiter
.websocket_stream()->type());
1417 // Make sure that there was an alternative connection
1418 // which consumes extra connections.
1419 EXPECT_EQ(0, GetSocketPoolGroupCount(
1420 session
->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1421 EXPECT_EQ(0, GetSocketPoolGroupCount(
1422 session
->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL
)));
1423 EXPECT_EQ(2, GetSocketPoolGroupCount(
1424 session
->GetTransportSocketPool(
1425 HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1426 EXPECT_EQ(1, GetSocketPoolGroupCount(
1427 session
->GetSSLSocketPool(HttpNetworkSession::WEBSOCKET_SOCKET_POOL
)));
1428 EXPECT_TRUE(waiter
.used_proxy_info().is_direct());
1430 // Make sure there is no orphaned job. it is already canceled.
1431 ASSERT_EQ(0u, static_cast<HttpStreamFactoryImpl
*>(
1432 session
->http_stream_factory_for_websocket())->num_orphaned_jobs());