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_stream_factory_impl.h" | 5 #include "net/http/http_stream_factory_impl.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | |
8 | 9 |
9 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
10 #include "net/base/net_log.h" | 11 #include "net/base/net_log.h" |
11 #include "net/base/test_completion_callback.h" | 12 #include "net/base/test_completion_callback.h" |
12 #include "net/cert/mock_cert_verifier.h" | 13 #include "net/cert/mock_cert_verifier.h" |
13 #include "net/dns/mock_host_resolver.h" | 14 #include "net/dns/mock_host_resolver.h" |
14 #include "net/http/http_auth_handler_factory.h" | 15 #include "net/http/http_auth_handler_factory.h" |
15 #include "net/http/http_network_session.h" | 16 #include "net/http/http_network_session.h" |
16 #include "net/http/http_network_session_peer.h" | 17 #include "net/http/http_network_session_peer.h" |
17 #include "net/http/http_network_transaction.h" | 18 #include "net/http/http_network_transaction.h" |
18 #include "net/http/http_request_info.h" | 19 #include "net/http/http_request_info.h" |
20 #include "net/http/http_server_properties.h" | |
19 #include "net/http/http_server_properties_impl.h" | 21 #include "net/http/http_server_properties_impl.h" |
20 #include "net/http/http_stream.h" | 22 #include "net/http/http_stream.h" |
21 #include "net/http/transport_security_state.h" | 23 #include "net/http/transport_security_state.h" |
22 #include "net/proxy/proxy_info.h" | 24 #include "net/proxy/proxy_info.h" |
23 #include "net/proxy/proxy_service.h" | 25 #include "net/proxy/proxy_service.h" |
26 #include "net/socket/client_socket_handle.h" | |
24 #include "net/socket/mock_client_socket_pool_manager.h" | 27 #include "net/socket/mock_client_socket_pool_manager.h" |
28 #include "net/socket/next_proto.h" | |
25 #include "net/socket/socket_test_util.h" | 29 #include "net/socket/socket_test_util.h" |
26 #include "net/spdy/spdy_session.h" | 30 #include "net/spdy/spdy_session.h" |
27 #include "net/spdy/spdy_session_pool.h" | 31 #include "net/spdy/spdy_session_pool.h" |
32 #include "net/spdy/spdy_test_util_common.h" | |
33 #include "net/ssl/ssl_config_service.h" | |
28 #include "net/ssl/ssl_config_service_defaults.h" | 34 #include "net/ssl/ssl_config_service_defaults.h" |
35 // This file can be included from net/http even though | |
36 // it is in net/websockets because it doesn't | |
37 // introduce any link dependency to net/websockets. | |
38 #include "net/websockets/websocket_stream_base.h" | |
29 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
30 | 40 |
31 namespace net { | 41 namespace net { |
32 | 42 |
33 namespace { | 43 namespace { |
34 | 44 |
35 class MockHttpStreamFactoryImpl : public HttpStreamFactoryImpl { | 45 class UseAlternateProtocolsScopedSetter { |
36 public: | 46 public: |
37 MockHttpStreamFactoryImpl(HttpNetworkSession* session) | 47 UseAlternateProtocolsScopedSetter(bool use_alternate_protocols) |
38 : HttpStreamFactoryImpl(session), | 48 : use_alternate_protocols_(HttpStreamFactory::use_alternate_protocols()) { |
49 HttpStreamFactory::set_use_alternate_protocols(use_alternate_protocols); | |
50 } | |
51 ~UseAlternateProtocolsScopedSetter() { | |
52 HttpStreamFactory::set_use_alternate_protocols(use_alternate_protocols_); | |
53 } | |
54 | |
55 private: | |
56 bool use_alternate_protocols_; | |
57 }; | |
58 | |
59 class MockWebSocketStream : public WebSocketStreamBase { | |
60 public: | |
61 enum StreamType { | |
62 kStreamTypeBasic, | |
63 kStreamTypeSpdy, | |
64 }; | |
65 | |
66 explicit MockWebSocketStream(StreamType type) : type_(type) {} | |
67 | |
68 virtual ~MockWebSocketStream() {} | |
69 | |
70 virtual WebSocketStream* AsWebSocketStream() OVERRIDE { return NULL; } | |
71 | |
72 StreamType type() const { | |
73 return type_; | |
74 } | |
75 | |
76 private: | |
77 const StreamType type_; | |
78 }; | |
79 | |
80 // HttpStreamFactoryImpl subclass that can wait until a preconnect is complete. | |
81 class MockHttpStreamFactoryImplForPreconnect : public HttpStreamFactoryImpl { | |
82 public: | |
83 MockHttpStreamFactoryImplForPreconnect(HttpNetworkSession* session, | |
84 bool for_websockets) | |
85 : HttpStreamFactoryImpl(session, for_websockets), | |
39 preconnect_done_(false), | 86 preconnect_done_(false), |
40 waiting_for_preconnect_(false) {} | 87 waiting_for_preconnect_(false) {} |
41 | 88 |
42 | 89 |
43 void WaitForPreconnects() { | 90 void WaitForPreconnects() { |
44 while (!preconnect_done_) { | 91 while (!preconnect_done_) { |
45 waiting_for_preconnect_ = true; | 92 waiting_for_preconnect_ = true; |
46 base::MessageLoop::current()->Run(); | 93 base::MessageLoop::current()->Run(); |
47 waiting_for_preconnect_ = false; | 94 waiting_for_preconnect_ = false; |
48 } | 95 } |
(...skipping 21 matching lines...) Expand all Loading... | |
70 | 117 |
71 virtual void OnStreamReady( | 118 virtual void OnStreamReady( |
72 const SSLConfig& used_ssl_config, | 119 const SSLConfig& used_ssl_config, |
73 const ProxyInfo& used_proxy_info, | 120 const ProxyInfo& used_proxy_info, |
74 HttpStreamBase* stream) OVERRIDE { | 121 HttpStreamBase* stream) OVERRIDE { |
75 stream_done_ = true; | 122 stream_done_ = true; |
76 if (waiting_for_stream_) | 123 if (waiting_for_stream_) |
77 base::MessageLoop::current()->Quit(); | 124 base::MessageLoop::current()->Quit(); |
78 stream_.reset(stream); | 125 stream_.reset(stream); |
79 used_ssl_config_ = used_ssl_config; | 126 used_ssl_config_ = used_ssl_config; |
127 used_proxy_info_ = used_proxy_info; | |
128 } | |
129 | |
130 virtual void OnWebSocketStreamReady( | |
131 const SSLConfig& used_ssl_config, | |
132 const ProxyInfo& used_proxy_info, | |
133 WebSocketStreamBase* stream) OVERRIDE { | |
134 stream_done_ = true; | |
135 if (waiting_for_stream_) | |
136 base::MessageLoop::current()->Quit(); | |
137 websocket_stream_.reset(stream); | |
138 used_ssl_config_ = used_ssl_config; | |
139 used_proxy_info_ = used_proxy_info; | |
80 } | 140 } |
81 | 141 |
82 virtual void OnStreamFailed( | 142 virtual void OnStreamFailed( |
83 int status, | 143 int status, |
84 const SSLConfig& used_ssl_config) OVERRIDE {} | 144 const SSLConfig& used_ssl_config) OVERRIDE {} |
85 | 145 |
86 virtual void OnCertificateError( | 146 virtual void OnCertificateError( |
87 int status, | 147 int status, |
88 const SSLConfig& used_ssl_config, | 148 const SSLConfig& used_ssl_config, |
89 const SSLInfo& ssl_info) OVERRIDE {} | 149 const SSLInfo& ssl_info) OVERRIDE {} |
(...skipping 16 matching lines...) Expand all Loading... | |
106 waiting_for_stream_ = true; | 166 waiting_for_stream_ = true; |
107 base::MessageLoop::current()->Run(); | 167 base::MessageLoop::current()->Run(); |
108 waiting_for_stream_ = false; | 168 waiting_for_stream_ = false; |
109 } | 169 } |
110 } | 170 } |
111 | 171 |
112 const SSLConfig& used_ssl_config() const { | 172 const SSLConfig& used_ssl_config() const { |
113 return used_ssl_config_; | 173 return used_ssl_config_; |
114 } | 174 } |
115 | 175 |
176 const ProxyInfo& used_proxy_info() const { | |
177 return used_proxy_info_; | |
178 } | |
179 | |
116 HttpStreamBase* stream() { | 180 HttpStreamBase* stream() { |
117 return stream_.get(); | 181 return stream_.get(); |
118 } | 182 } |
119 | 183 |
184 MockWebSocketStream* websocket_stream() { | |
185 return static_cast<MockWebSocketStream*>(websocket_stream_.get()); | |
186 } | |
187 | |
188 bool stream_done() const { return stream_done_; } | |
120 | 189 |
121 private: | 190 private: |
122 bool waiting_for_stream_; | 191 bool waiting_for_stream_; |
123 bool stream_done_; | 192 bool stream_done_; |
124 scoped_ptr<HttpStreamBase> stream_; | 193 scoped_ptr<HttpStreamBase> stream_; |
194 scoped_ptr<WebSocketStreamBase> websocket_stream_; | |
125 SSLConfig used_ssl_config_; | 195 SSLConfig used_ssl_config_; |
196 ProxyInfo used_proxy_info_; | |
126 | 197 |
127 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter); | 198 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter); |
128 }; | 199 }; |
129 | 200 |
201 class WebSocketSpdyStream : public MockWebSocketStream { | |
202 public: | |
203 explicit WebSocketSpdyStream(SpdySession* spdy_session) | |
204 : MockWebSocketStream(kStreamTypeSpdy), spdy_session_(spdy_session) {} | |
205 | |
206 virtual ~WebSocketSpdyStream() {} | |
207 | |
208 SpdySession* spdy_session() { return spdy_session_.get(); } | |
209 | |
210 private: | |
211 scoped_refptr<SpdySession> spdy_session_; | |
212 }; | |
213 | |
214 class WebSocketBasicStream : public MockWebSocketStream { | |
215 public: | |
216 explicit WebSocketBasicStream(ClientSocketHandle* connection) | |
217 : MockWebSocketStream(kStreamTypeBasic), connection_(connection) {} | |
218 | |
219 virtual ~WebSocketBasicStream() { | |
220 connection_->socket()->Disconnect(); | |
221 } | |
222 | |
223 ClientSocketHandle* connection() { return connection_.get(); } | |
224 | |
225 private: | |
226 scoped_ptr<ClientSocketHandle> connection_; | |
227 }; | |
228 | |
229 class WebSocketStreamFactory : public WebSocketStreamBase::Factory { | |
230 public: | |
231 virtual ~WebSocketStreamFactory() {} | |
232 | |
233 virtual WebSocketStreamBase* CreateBasicStream(ClientSocketHandle* connection, | |
234 bool using_proxy) OVERRIDE { | |
235 return new WebSocketBasicStream(connection); | |
236 } | |
237 | |
238 virtual WebSocketStreamBase* CreateSpdyStream( | |
239 SpdySession* spdy_session, | |
240 bool use_relative_url) OVERRIDE { | |
241 return new WebSocketSpdyStream(spdy_session); | |
242 } | |
243 }; | |
244 | |
245 class HttpStreamFactoryForDetectOrphanedJobComplete | |
246 : public HttpStreamFactoryImpl { | |
247 public: | |
248 HttpStreamFactoryForDetectOrphanedJobComplete(HttpNetworkSession* session, | |
249 bool for_websockets) | |
250 : HttpStreamFactoryImpl(session, for_websockets), counter_(0) {} | |
251 | |
252 size_t counter() const { return counter_; } | |
253 | |
254 private: | |
255 virtual void OnOrphanedJobComplete(const Job* job) OVERRIDE { | |
256 HttpStreamFactoryImpl::OnOrphanedJobComplete(job); | |
257 ++counter_; | |
258 base::MessageLoop::current()->Quit(); | |
259 } | |
260 | |
261 size_t counter_; | |
262 }; | |
263 | |
130 struct SessionDependencies { | 264 struct SessionDependencies { |
131 // Custom proxy service dependency. | 265 // Custom proxy service dependency. |
132 explicit SessionDependencies(ProxyService* proxy_service) | 266 explicit SessionDependencies(ProxyService* proxy_service) |
133 : host_resolver(new MockHostResolver), | 267 : host_resolver(new MockHostResolver), |
134 cert_verifier(new MockCertVerifier), | 268 cert_verifier(new MockCertVerifier), |
135 transport_security_state(new TransportSecurityState), | 269 transport_security_state(new TransportSecurityState), |
136 proxy_service(proxy_service), | 270 proxy_service(proxy_service), |
137 ssl_config_service(new SSLConfigServiceDefaults), | 271 ssl_config_service(new SSLConfigServiceDefaults), |
138 http_auth_handler_factory( | 272 http_auth_handler_factory( |
139 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), | 273 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), |
(...skipping 16 matching lines...) Expand all Loading... | |
156 params.cert_verifier = session_deps->cert_verifier.get(); | 290 params.cert_verifier = session_deps->cert_verifier.get(); |
157 params.transport_security_state = | 291 params.transport_security_state = |
158 session_deps->transport_security_state.get(); | 292 session_deps->transport_security_state.get(); |
159 params.proxy_service = session_deps->proxy_service.get(); | 293 params.proxy_service = session_deps->proxy_service.get(); |
160 params.ssl_config_service = session_deps->ssl_config_service.get(); | 294 params.ssl_config_service = session_deps->ssl_config_service.get(); |
161 params.client_socket_factory = &session_deps->socket_factory; | 295 params.client_socket_factory = &session_deps->socket_factory; |
162 params.http_auth_handler_factory = | 296 params.http_auth_handler_factory = |
163 session_deps->http_auth_handler_factory.get(); | 297 session_deps->http_auth_handler_factory.get(); |
164 params.net_log = session_deps->net_log; | 298 params.net_log = session_deps->net_log; |
165 params.http_server_properties = &session_deps->http_server_properties; | 299 params.http_server_properties = &session_deps->http_server_properties; |
300 | |
166 return new HttpNetworkSession(params); | 301 return new HttpNetworkSession(params); |
167 } | 302 } |
168 | 303 |
169 struct TestCase { | 304 struct TestCase { |
170 int num_streams; | 305 int num_streams; |
171 bool ssl; | 306 bool ssl; |
172 }; | 307 }; |
173 | 308 |
174 TestCase kTests[] = { | 309 TestCase kTests[] = { |
175 { 1, false }, | 310 { 1, false }, |
176 { 2, false }, | 311 { 2, false }, |
177 { 1, true}, | 312 { 1, true}, |
178 { 2, true}, | 313 { 2, true}, |
179 }; | 314 }; |
180 | 315 |
181 void PreconnectHelperForURL(int num_streams, | 316 void PreconnectHelperForURL(int num_streams, |
182 const GURL& url, | 317 const GURL& url, |
183 HttpNetworkSession* session) { | 318 HttpNetworkSession* session) { |
184 HttpNetworkSessionPeer peer(session); | 319 HttpNetworkSessionPeer peer(session); |
185 MockHttpStreamFactoryImpl* mock_factory = | 320 MockHttpStreamFactoryImplForPreconnect* mock_factory = |
186 new MockHttpStreamFactoryImpl(session); | 321 new MockHttpStreamFactoryImplForPreconnect(session, false); |
187 peer.SetHttpStreamFactory(mock_factory); | 322 peer.SetHttpStreamFactory(mock_factory); |
188 SSLConfig ssl_config; | 323 SSLConfig ssl_config; |
189 session->ssl_config_service()->GetSSLConfig(&ssl_config); | 324 session->ssl_config_service()->GetSSLConfig(&ssl_config); |
190 | 325 |
191 HttpRequestInfo request; | 326 HttpRequestInfo request; |
192 request.method = "GET"; | 327 request.method = "GET"; |
193 request.url = url; | 328 request.url = url; |
194 request.load_flags = 0; | 329 request.load_flags = 0; |
195 | 330 |
196 session->http_stream_factory()->PreconnectStreams( | 331 session->http_stream_factory()->PreconnectStreams( |
(...skipping 261 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
458 socket_data1.set_connect_data(MockConnect(ASYNC, ERR_ADDRESS_UNREACHABLE)); | 593 socket_data1.set_connect_data(MockConnect(ASYNC, ERR_ADDRESS_UNREACHABLE)); |
459 session_deps.socket_factory.AddSocketDataProvider(&socket_data1); | 594 session_deps.socket_factory.AddSocketDataProvider(&socket_data1); |
460 | 595 |
461 // Second connection attempt succeeds | 596 // Second connection attempt succeeds |
462 StaticSocketDataProvider socket_data2; | 597 StaticSocketDataProvider socket_data2; |
463 socket_data2.set_connect_data(MockConnect(ASYNC, OK)); | 598 socket_data2.set_connect_data(MockConnect(ASYNC, OK)); |
464 session_deps.socket_factory.AddSocketDataProvider(&socket_data2); | 599 session_deps.socket_factory.AddSocketDataProvider(&socket_data2); |
465 | 600 |
466 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | 601 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); |
467 | 602 |
468 // Now request a stream. It should succeed using the second proxy in the | 603 // Now request a stream. It should succeed using the second proxy in the |
469 // list. | 604 // list. |
470 HttpRequestInfo request_info; | 605 HttpRequestInfo request_info; |
471 request_info.method = "GET"; | 606 request_info.method = "GET"; |
472 request_info.url = GURL("http://www.google.com"); | 607 request_info.url = GURL("http://www.google.com"); |
473 | 608 |
474 SSLConfig ssl_config; | 609 SSLConfig ssl_config; |
475 StreamRequestWaiter waiter; | 610 StreamRequestWaiter waiter; |
476 scoped_ptr<HttpStreamRequest> request( | 611 scoped_ptr<HttpStreamRequest> request( |
477 session->http_stream_factory()->RequestStream( | 612 session->http_stream_factory()->RequestStream( |
478 request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, | 613 request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
530 int GetSocketPoolGroupCount(ClientSocketPool* pool) { | 665 int GetSocketPoolGroupCount(ClientSocketPool* pool) { |
531 int count = 0; | 666 int count = 0; |
532 scoped_ptr<base::DictionaryValue> dict(pool->GetInfoAsValue("", "", false)); | 667 scoped_ptr<base::DictionaryValue> dict(pool->GetInfoAsValue("", "", false)); |
533 EXPECT_TRUE(dict != NULL); | 668 EXPECT_TRUE(dict != NULL); |
534 base::DictionaryValue* groups = NULL; | 669 base::DictionaryValue* groups = NULL; |
535 if (dict->GetDictionary("groups", &groups) && (groups != NULL)) { | 670 if (dict->GetDictionary("groups", &groups) && (groups != NULL)) { |
536 count = static_cast<int>(groups->size()); | 671 count = static_cast<int>(groups->size()); |
537 } | 672 } |
538 return count; | 673 return count; |
539 } | 674 } |
540 }; | 675 } // namespace |
541 | 676 |
542 TEST(HttpStreamFactoryTest, PrivacyModeUsesDifferentSocketPoolGroup) { | 677 TEST(HttpStreamFactoryTest, PrivacyModeUsesDifferentSocketPoolGroup) { |
543 SessionDependencies session_deps(ProxyService::CreateDirect()); | 678 SessionDependencies session_deps(ProxyService::CreateDirect()); |
544 | 679 |
545 StaticSocketDataProvider socket_data; | 680 StaticSocketDataProvider socket_data; |
546 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | 681 socket_data.set_connect_data(MockConnect(ASYNC, OK)); |
547 session_deps.socket_factory.AddSocketDataProvider(&socket_data); | 682 session_deps.socket_factory.AddSocketDataProvider(&socket_data); |
548 | 683 |
549 SSLSocketDataProvider ssl(ASYNC, OK); | 684 SSLSocketDataProvider ssl(ASYNC, OK); |
550 session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); | 685 session_deps.socket_factory.AddSSLSocketDataProvider(&ssl); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
608 scoped_ptr<HttpStreamRequest> request( | 743 scoped_ptr<HttpStreamRequest> request( |
609 session->http_stream_factory()->RequestStream( | 744 session->http_stream_factory()->RequestStream( |
610 request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, | 745 request_info, DEFAULT_PRIORITY, ssl_config, ssl_config, |
611 &waiter, BoundNetLog())); | 746 &waiter, BoundNetLog())); |
612 | 747 |
613 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, request->GetLoadState()); | 748 EXPECT_EQ(LOAD_STATE_RESOLVING_HOST, request->GetLoadState()); |
614 | 749 |
615 waiter.WaitForStream(); | 750 waiter.WaitForStream(); |
616 } | 751 } |
617 | 752 |
753 TEST(HttpStreamFactoryTest, RequestHttpStream) { | |
754 SessionDependencies session_deps(ProxyService::CreateDirect()); | |
755 | |
756 StaticSocketDataProvider socket_data; | |
757 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
758 session_deps.socket_factory.AddSocketDataProvider(&socket_data); | |
759 | |
760 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | |
761 | |
762 // Now request a stream. It should succeed using the second proxy in the | |
763 // list. | |
764 HttpRequestInfo request_info; | |
765 request_info.method = "GET"; | |
766 request_info.url = GURL("http://www.google.com"); | |
767 request_info.load_flags = 0; | |
768 | |
769 SSLConfig ssl_config; | |
770 StreamRequestWaiter waiter; | |
771 scoped_ptr<HttpStreamRequest> request( | |
772 session->http_stream_factory()->RequestStream( | |
773 request_info, | |
774 DEFAULT_PRIORITY, | |
775 ssl_config, | |
776 ssl_config, | |
777 &waiter, | |
778 BoundNetLog())); | |
779 waiter.WaitForStream(); | |
780 EXPECT_TRUE(waiter.stream_done()); | |
781 ASSERT_TRUE(NULL != waiter.stream()); | |
782 EXPECT_TRUE(NULL == waiter.websocket_stream()); | |
783 EXPECT_FALSE(waiter.stream()->IsSpdyHttpStream()); | |
784 | |
785 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
786 session->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
787 EXPECT_EQ(0, GetSocketPoolGroupCount(session->GetSSLSocketPool( | |
788 HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
789 EXPECT_TRUE(waiter.used_proxy_info().is_direct()); | |
790 } | |
791 | |
792 TEST(HttpStreamFactoryTest, RequestHttpStreamOverSSL) { | |
793 SessionDependencies session_deps(ProxyService::CreateDirect()); | |
794 | |
795 MockRead mock_read(ASYNC, OK); | |
796 StaticSocketDataProvider socket_data(&mock_read, 1, NULL, 0); | |
797 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
798 session_deps.socket_factory.AddSocketDataProvider(&socket_data); | |
799 | |
800 SSLSocketDataProvider ssl_socket_data(ASYNC, OK); | |
801 session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_socket_data); | |
802 | |
803 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | |
804 | |
805 // Now request a stream. | |
806 HttpRequestInfo request_info; | |
807 request_info.method = "GET"; | |
808 request_info.url = GURL("https://www.google.com"); | |
809 request_info.load_flags = 0; | |
810 | |
811 SSLConfig ssl_config; | |
812 StreamRequestWaiter waiter; | |
813 scoped_ptr<HttpStreamRequest> request( | |
814 session->http_stream_factory()->RequestStream( | |
815 request_info, | |
816 DEFAULT_PRIORITY, | |
817 ssl_config, | |
818 ssl_config, | |
819 &waiter, | |
820 BoundNetLog())); | |
821 waiter.WaitForStream(); | |
822 EXPECT_TRUE(waiter.stream_done()); | |
823 ASSERT_TRUE(NULL != waiter.stream()); | |
824 EXPECT_TRUE(NULL == waiter.websocket_stream()); | |
825 EXPECT_FALSE(waiter.stream()->IsSpdyHttpStream()); | |
826 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
827 session->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
828 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
829 session->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
830 EXPECT_TRUE(waiter.used_proxy_info().is_direct()); | |
831 } | |
832 | |
833 TEST(HttpStreamFactoryTest, RequestHttpStreamOverProxy) { | |
834 SessionDependencies session_deps(ProxyService::CreateFixed("myproxy:8888")); | |
835 | |
836 StaticSocketDataProvider socket_data; | |
837 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
838 session_deps.socket_factory.AddSocketDataProvider(&socket_data); | |
839 | |
840 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | |
841 | |
842 // Now request a stream. It should succeed using the second proxy in the | |
843 // list. | |
844 HttpRequestInfo request_info; | |
845 request_info.method = "GET"; | |
846 request_info.url = GURL("http://www.google.com"); | |
847 request_info.load_flags = 0; | |
848 | |
849 SSLConfig ssl_config; | |
850 StreamRequestWaiter waiter; | |
851 scoped_ptr<HttpStreamRequest> request( | |
852 session->http_stream_factory()->RequestStream( | |
853 request_info, | |
854 DEFAULT_PRIORITY, | |
855 ssl_config, | |
856 ssl_config, | |
857 &waiter, | |
858 BoundNetLog())); | |
859 waiter.WaitForStream(); | |
860 EXPECT_TRUE(waiter.stream_done()); | |
861 ASSERT_TRUE(NULL != waiter.stream()); | |
862 EXPECT_TRUE(NULL == waiter.websocket_stream()); | |
863 EXPECT_FALSE(waiter.stream()->IsSpdyHttpStream()); | |
864 EXPECT_EQ(0, GetSocketPoolGroupCount( | |
865 session->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
866 EXPECT_EQ(0, GetSocketPoolGroupCount( | |
867 session->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
868 EXPECT_EQ(1, GetSocketPoolGroupCount(session->GetSocketPoolForHTTPProxy( | |
869 HttpNetworkSession::NORMAL_SOCKET_POOL, | |
870 HostPortPair("myproxy", 8888)))); | |
871 EXPECT_EQ(0, GetSocketPoolGroupCount(session->GetSocketPoolForSSLWithProxy( | |
872 HttpNetworkSession::NORMAL_SOCKET_POOL, | |
873 HostPortPair("myproxy", 8888)))); | |
874 EXPECT_FALSE(waiter.used_proxy_info().is_direct()); | |
875 } | |
876 | |
877 TEST(HttpStreamFactoryTest, RequestWebSocketBasicStream) { | |
878 SessionDependencies session_deps(ProxyService::CreateDirect()); | |
879 | |
880 StaticSocketDataProvider socket_data; | |
881 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
882 session_deps.socket_factory.AddSocketDataProvider(&socket_data); | |
883 | |
884 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | |
885 | |
886 // Now request a stream. | |
887 HttpRequestInfo request_info; | |
888 request_info.method = "GET"; | |
889 request_info.url = GURL("ws://www.google.com"); | |
890 request_info.load_flags = 0; | |
891 | |
892 SSLConfig ssl_config; | |
893 StreamRequestWaiter waiter; | |
894 WebSocketStreamFactory factory; | |
895 scoped_ptr<HttpStreamRequest> request( | |
896 session->websocket_stream_factory()->RequestWebSocketStream( | |
897 request_info, | |
898 DEFAULT_PRIORITY, | |
899 ssl_config, | |
900 ssl_config, | |
901 &waiter, | |
902 &factory, | |
903 BoundNetLog())); | |
904 waiter.WaitForStream(); | |
905 EXPECT_TRUE(waiter.stream_done()); | |
906 EXPECT_TRUE(NULL == waiter.stream()); | |
907 ASSERT_TRUE(NULL != waiter.websocket_stream()); | |
908 EXPECT_EQ(MockWebSocketStream::kStreamTypeBasic, | |
909 waiter.websocket_stream()->type()); | |
910 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
911 session->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
912 EXPECT_EQ(0, GetSocketPoolGroupCount( | |
913 session->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
914 EXPECT_TRUE(waiter.used_proxy_info().is_direct()); | |
915 } | |
916 | |
917 TEST(HttpStreamFactoryTest, RequestWebSocketBasicStreamOverSSL) { | |
918 SessionDependencies session_deps(ProxyService::CreateDirect()); | |
919 | |
920 MockRead mock_read(ASYNC, OK); | |
921 StaticSocketDataProvider socket_data(&mock_read, 1, NULL, 0); | |
922 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
923 session_deps.socket_factory.AddSocketDataProvider(&socket_data); | |
924 | |
925 SSLSocketDataProvider ssl_socket_data(ASYNC, OK); | |
926 session_deps.socket_factory.AddSSLSocketDataProvider(&ssl_socket_data); | |
927 | |
928 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps)); | |
929 | |
930 // Now request a stream. | |
931 HttpRequestInfo request_info; | |
932 request_info.method = "GET"; | |
933 request_info.url = GURL("wss://www.google.com"); | |
934 request_info.load_flags = 0; | |
935 | |
936 SSLConfig ssl_config; | |
937 StreamRequestWaiter waiter; | |
938 WebSocketStreamFactory factory; | |
939 scoped_ptr<HttpStreamRequest> request( | |
940 session->websocket_stream_factory()->RequestWebSocketStream( | |
941 request_info, | |
942 DEFAULT_PRIORITY, | |
943 ssl_config, | |
944 ssl_config, | |
945 &waiter, | |
946 &factory, | |
947 BoundNetLog())); | |
948 waiter.WaitForStream(); | |
949 EXPECT_TRUE(waiter.stream_done()); | |
950 EXPECT_TRUE(NULL == waiter.stream()); | |
951 ASSERT_TRUE(NULL != waiter.websocket_stream()); | |
952 EXPECT_EQ(MockWebSocketStream::kStreamTypeBasic, | |
953 waiter.websocket_stream()->type()); | |
954 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
955 session->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
956 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
957 session->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
958 EXPECT_TRUE(waiter.used_proxy_info().is_direct()); | |
959 } | |
960 | |
961 TEST(HttpStreamFactoryTest, RequestSpdyHttpStream) { | |
962 SpdySessionDependencies session_deps(kProtoSPDY3, | |
963 ProxyService::CreateDirect()); | |
964 | |
965 MockRead mock_read(ASYNC, OK); | |
966 StaticSocketDataProvider socket_data(&mock_read, 1, NULL, 0); | |
967 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
968 session_deps.socket_factory->AddSocketDataProvider(&socket_data); | |
969 | |
970 SSLSocketDataProvider ssl_socket_data(ASYNC, OK); | |
971 ssl_socket_data.SetNextProto(kProtoSPDY3); | |
972 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data); | |
973 | |
974 HostPortPair host_port_pair("www.google.com", 443); | |
975 scoped_refptr<HttpNetworkSession> | |
976 session(SpdySessionDependencies::SpdyCreateSession(&session_deps)); | |
977 | |
978 // Now request a stream. | |
979 HttpRequestInfo request_info; | |
980 request_info.method = "GET"; | |
981 request_info.url = GURL("https://www.google.com"); | |
982 request_info.load_flags = 0; | |
983 | |
984 SSLConfig ssl_config; | |
985 StreamRequestWaiter waiter; | |
986 scoped_ptr<HttpStreamRequest> request( | |
987 session->http_stream_factory()->RequestStream( | |
988 request_info, | |
989 DEFAULT_PRIORITY, | |
990 ssl_config, | |
991 ssl_config, | |
992 &waiter, | |
993 BoundNetLog())); | |
994 waiter.WaitForStream(); | |
995 EXPECT_TRUE(waiter.stream_done()); | |
996 EXPECT_TRUE(NULL == waiter.websocket_stream()); | |
997 ASSERT_TRUE(NULL != waiter.stream()); | |
998 EXPECT_TRUE(waiter.stream()->IsSpdyHttpStream()); | |
999 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
1000 session->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
1001 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
1002 session->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
1003 EXPECT_TRUE(waiter.used_proxy_info().is_direct()); | |
1004 } | |
1005 | |
1006 TEST(HttpStreamFactoryTest, RequestWebSocketSpdyStream) { | |
1007 SpdySessionDependencies session_deps(kProtoSPDY3, | |
1008 ProxyService::CreateDirect()); | |
1009 | |
1010 MockRead mock_read(SYNCHRONOUS, ERR_IO_PENDING); | |
1011 StaticSocketDataProvider socket_data(&mock_read, 1, NULL, 0); | |
1012 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
1013 session_deps.socket_factory->AddSocketDataProvider(&socket_data); | |
1014 | |
1015 SSLSocketDataProvider ssl_socket_data(ASYNC, OK); | |
1016 ssl_socket_data.SetNextProto(kProtoSPDY3); | |
1017 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data); | |
1018 | |
1019 HostPortPair host_port_pair("www.google.com", 80); | |
1020 scoped_refptr<HttpNetworkSession> | |
1021 session(SpdySessionDependencies::SpdyCreateSession(&session_deps)); | |
1022 | |
1023 // Now request a stream. | |
1024 HttpRequestInfo request_info; | |
1025 request_info.method = "GET"; | |
1026 request_info.url = GURL("wss://www.google.com"); | |
1027 request_info.load_flags = 0; | |
1028 | |
1029 SSLConfig ssl_config; | |
1030 StreamRequestWaiter waiter1; | |
1031 WebSocketStreamFactory factory; | |
1032 scoped_ptr<HttpStreamRequest> request1( | |
1033 session->websocket_stream_factory()->RequestWebSocketStream( | |
1034 request_info, | |
1035 DEFAULT_PRIORITY, | |
1036 ssl_config, | |
1037 ssl_config, | |
1038 &waiter1, | |
1039 &factory, | |
1040 BoundNetLog())); | |
1041 waiter1.WaitForStream(); | |
1042 EXPECT_TRUE(waiter1.stream_done()); | |
1043 ASSERT_TRUE(NULL != waiter1.websocket_stream()); | |
1044 EXPECT_EQ(MockWebSocketStream::kStreamTypeSpdy, | |
1045 waiter1.websocket_stream()->type()); | |
1046 EXPECT_TRUE(NULL == waiter1.stream()); | |
1047 | |
1048 StreamRequestWaiter waiter2; | |
1049 scoped_ptr<HttpStreamRequest> request2( | |
1050 session->websocket_stream_factory()->RequestWebSocketStream( | |
1051 request_info, | |
1052 DEFAULT_PRIORITY, | |
1053 ssl_config, | |
1054 ssl_config, | |
1055 &waiter2, | |
1056 &factory, | |
1057 BoundNetLog())); | |
1058 waiter2.WaitForStream(); | |
1059 EXPECT_TRUE(waiter2.stream_done()); | |
1060 ASSERT_TRUE(NULL != waiter2.websocket_stream()); | |
1061 EXPECT_EQ(MockWebSocketStream::kStreamTypeSpdy, | |
1062 waiter2.websocket_stream()->type()); | |
1063 EXPECT_TRUE(NULL == waiter2.stream()); | |
1064 EXPECT_NE(waiter2.websocket_stream(), waiter1.websocket_stream()); | |
1065 EXPECT_EQ(static_cast<WebSocketSpdyStream*>(waiter2.websocket_stream())-> | |
1066 spdy_session(), | |
1067 static_cast<WebSocketSpdyStream*>(waiter1.websocket_stream())-> | |
1068 spdy_session()); | |
1069 | |
1070 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
1071 session->GetTransportSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
1072 EXPECT_EQ(1, GetSocketPoolGroupCount( | |
1073 session->GetSSLSocketPool(HttpNetworkSession::NORMAL_SOCKET_POOL))); | |
1074 EXPECT_TRUE(waiter1.used_proxy_info().is_direct()); | |
1075 } | |
1076 | |
1077 TEST(HttpStreamFactoryTest, OrphanedWebSocketStream) { | |
1078 UseAlternateProtocolsScopedSetter use_alternate_protocols(true); | |
1079 SpdySessionDependencies session_deps(kProtoSPDY3, | |
1080 ProxyService::CreateDirect()); | |
1081 | |
1082 MockRead mock_read(ASYNC, OK); | |
1083 StaticSocketDataProvider socket_data(&mock_read, 1, NULL, 0); | |
1084 socket_data.set_connect_data(MockConnect(ASYNC, OK)); | |
1085 session_deps.socket_factory->AddSocketDataProvider(&socket_data); | |
1086 | |
1087 MockRead mock_read2(ASYNC, OK); | |
1088 StaticSocketDataProvider socket_data2(&mock_read2, 1, NULL, 0); | |
1089 socket_data2.set_connect_data(MockConnect(ASYNC, ERR_IO_PENDING)); | |
1090 session_deps.socket_factory->AddSocketDataProvider(&socket_data2); | |
1091 | |
1092 SSLSocketDataProvider ssl_socket_data(ASYNC, OK); | |
1093 ssl_socket_data.SetNextProto(kProtoSPDY3); | |
1094 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data); | |
1095 | |
1096 scoped_refptr<HttpNetworkSession> | |
1097 session(SpdySessionDependencies::SpdyCreateSession(&session_deps)); | |
1098 HttpStreamFactoryForDetectOrphanedJobComplete* | |
1099 http_websocket_stream_factory = | |
1100 new HttpStreamFactoryForDetectOrphanedJobComplete(session.get(), true); | |
1101 HttpNetworkSessionPeer peer(session); | |
1102 peer.SetWebSocketStreamFactory(http_websocket_stream_factory); | |
1103 | |
1104 // Now request a stream. | |
1105 HttpRequestInfo request_info; | |
1106 request_info.method = "GET"; | |
1107 request_info.url = GURL("ws://www.google.com:8888"); | |
1108 request_info.load_flags = 0; | |
1109 | |
1110 session->http_server_properties()->SetAlternateProtocol( | |
1111 HostPortPair("www.google.com", 8888), | |
1112 9999, | |
1113 NPN_SPDY_3); | |
1114 | |
1115 SSLConfig ssl_config; | |
1116 StreamRequestWaiter waiter; | |
1117 WebSocketStreamFactory factory; | |
1118 scoped_ptr<HttpStreamRequest> request( | |
1119 session->websocket_stream_factory()->RequestWebSocketStream( | |
1120 request_info, | |
1121 DEFAULT_PRIORITY, | |
1122 ssl_config, | |
1123 ssl_config, | |
1124 &waiter, | |
1125 &factory, | |
1126 BoundNetLog())); | |
1127 waiter.WaitForStream(); | |
1128 EXPECT_TRUE(waiter.stream_done()); | |
1129 EXPECT_TRUE(NULL == waiter.stream()); | |
1130 ASSERT_TRUE(NULL != waiter.websocket_stream()); | |
1131 EXPECT_EQ(MockWebSocketStream::kStreamTypeSpdy, | |
1132 waiter.websocket_stream()->type()); | |
1133 | |
1134 ASSERT_EQ(0, http_websocket_stream_factory->counter()); | |
1135 socket_data2.socket()->OnConnectComplete(MockConnect(SYNCHRONOUS, OK)); | |
1136 while (!http_websocket_stream_factory->counter()) | |
1137 base::MessageLoop::current()->Run(); | |
1138 ASSERT_EQ(1, http_websocket_stream_factory->counter()); | |
mmenke
2013/06/13 17:59:56
Aren't we cancelling orphaned jobs?
yhirano
2013/06/14 04:11:25
Done.
| |
1139 return; | |
1140 } | |
1141 | |
618 } // namespace | 1142 } // namespace |
619 | 1143 |
620 } // namespace net | 1144 } // namespace net |
OLD | NEW |