Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(279)

Side by Side Diff: net/http/http_stream_factory_impl_unittest.cc

Issue 14813024: Introduce RequestWebSocketStream into HttpStreamFactory (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix a comment. Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 8
9 #include "base/basictypes.h" 9 #include "base/basictypes.h"
10 #include "net/base/net_log.h" 10 #include "net/base/net_log.h"
11 #include "net/base/test_completion_callback.h" 11 #include "net/base/test_completion_callback.h"
12 #include "net/cert/mock_cert_verifier.h" 12 #include "net/cert/mock_cert_verifier.h"
13 #include "net/dns/mock_host_resolver.h" 13 #include "net/dns/mock_host_resolver.h"
14 #include "net/http/http_auth_handler_factory.h" 14 #include "net/http/http_auth_handler_factory.h"
15 #include "net/http/http_network_session.h" 15 #include "net/http/http_network_session.h"
16 #include "net/http/http_network_session_peer.h" 16 #include "net/http/http_network_session_peer.h"
17 #include "net/http/http_request_info.h" 17 #include "net/http/http_request_info.h"
18 #include "net/http/http_server_properties_impl.h" 18 #include "net/http/http_server_properties_impl.h"
19 #include "net/http/http_stream.h" 19 #include "net/http/http_stream.h"
20 #include "net/proxy/proxy_info.h" 20 #include "net/proxy/proxy_info.h"
21 #include "net/proxy/proxy_service.h" 21 #include "net/proxy/proxy_service.h"
22 #include "net/socket/client_socket_handle.h"
22 #include "net/socket/mock_client_socket_pool_manager.h" 23 #include "net/socket/mock_client_socket_pool_manager.h"
23 #include "net/socket/socket_test_util.h" 24 #include "net/socket/socket_test_util.h"
24 #include "net/spdy/spdy_session.h" 25 #include "net/spdy/spdy_session.h"
25 #include "net/spdy/spdy_session_pool.h" 26 #include "net/spdy/spdy_session_pool.h"
27 #include "net/spdy/spdy_stream.h"
28 #include "net/spdy/spdy_test_util_spdy3.h"
29 #include "net/ssl/ssl_config_service.h"
26 #include "net/ssl/ssl_config_service_defaults.h" 30 #include "net/ssl/ssl_config_service_defaults.h"
31 #include "testing/gmock/include/gmock/gmock.h"
27 #include "testing/gtest/include/gtest/gtest.h" 32 #include "testing/gtest/include/gtest/gtest.h"
28 33
29 namespace net { 34 namespace net {
30 35
31 namespace { 36 namespace {
32 37
38 class HttpStreamFactorySpdyForcer {
39 public:
40 HttpStreamFactorySpdyForcer() {
41 HttpStreamFactory::set_force_spdy_over_ssl(true);
42 HttpStreamFactory::set_force_spdy_always(true);
43 }
44 ~HttpStreamFactorySpdyForcer() {
45 HttpStreamFactory::set_force_spdy_over_ssl(false);
46 HttpStreamFactory::set_force_spdy_always(false);
47 }
48 };
49
50 class MockSSLConfigService : public SSLConfigService {
51 public:
52 explicit MockSSLConfigService(const SSLConfig& config) : config_(config) {}
53
54 // SSLConfigService implementation
55 virtual void GetSSLConfig(SSLConfig* config) OVERRIDE {
56 *config = config_;
57 }
58
59 // Sets the SSLConfig to be returned by GetSSLConfig and processes any
60 // updates.
61 void SetSSLConfig(const SSLConfig& config) {
62 SSLConfig old_config = config_;
63 config_ = config;
64 ProcessConfigUpdate(old_config, config_);
65 }
66
67 private:
68 virtual ~MockSSLConfigService() {}
69
70 SSLConfig config_;
71 };
72
73 class MockSSLConfigServiceObserver : public SSLConfigService::Observer {
74 public:
75 MockSSLConfigServiceObserver() {}
76 virtual ~MockSSLConfigServiceObserver() {}
77
78 MOCK_METHOD0(OnSSLConfigChanged, void());
79 };
80
33 class MockHttpStreamFactoryImpl : public HttpStreamFactoryImpl { 81 class MockHttpStreamFactoryImpl : public HttpStreamFactoryImpl {
34 public: 82 public:
35 MockHttpStreamFactoryImpl(HttpNetworkSession* session) 83 explicit MockHttpStreamFactoryImpl(HttpNetworkSession* session)
36 : HttpStreamFactoryImpl(session), 84 : HttpStreamFactoryImpl(session),
37 preconnect_done_(false), 85 preconnect_done_(false),
38 waiting_for_preconnect_(false) {} 86 waiting_for_preconnect_(false) {}
39 87
40 88
41 void WaitForPreconnects() { 89 void WaitForPreconnects() {
42 while (!preconnect_done_) { 90 while (!preconnect_done_) {
43 waiting_for_preconnect_ = true; 91 waiting_for_preconnect_ = true;
44 MessageLoop::current()->Run(); 92 MessageLoop::current()->Run();
45 waiting_for_preconnect_ = false; 93 waiting_for_preconnect_ = false;
(...skipping 23 matching lines...) Expand all
69 virtual void OnStreamReady( 117 virtual void OnStreamReady(
70 const SSLConfig& used_ssl_config, 118 const SSLConfig& used_ssl_config,
71 const ProxyInfo& used_proxy_info, 119 const ProxyInfo& used_proxy_info,
72 HttpStreamBase* stream) OVERRIDE { 120 HttpStreamBase* stream) OVERRIDE {
73 stream_done_ = true; 121 stream_done_ = true;
74 if (waiting_for_stream_) 122 if (waiting_for_stream_)
75 MessageLoop::current()->Quit(); 123 MessageLoop::current()->Quit();
76 stream_.reset(stream); 124 stream_.reset(stream);
77 } 125 }
78 126
127 virtual void OnSocketReady(
128 const SSLConfig& used_ssl_config,
129 const ProxyInfo& used_proxy_info,
130 ClientSocketHandle* connection) OVERRIDE {
131 stream_done_ = true;
132 if (waiting_for_stream_)
133 MessageLoop::current()->Quit();
134 connection_.reset(connection);
135 }
136
137 virtual void OnSpdySessionReady(
138 const SSLConfig& used_ssl_config,
139 const ProxyInfo& used_proxy_info,
140 SpdySession* session) OVERRIDE {
141 stream_done_ = true;
142 if (waiting_for_stream_)
143 MessageLoop::current()->Quit();
144 spdy_session_ = session;
145 }
146
79 virtual void OnStreamFailed( 147 virtual void OnStreamFailed(
80 int status, 148 int status,
81 const SSLConfig& used_ssl_config) OVERRIDE {} 149 const SSLConfig& used_ssl_config) OVERRIDE {}
82 150
83 virtual void OnCertificateError( 151 virtual void OnCertificateError(
84 int status, 152 int status,
85 const SSLConfig& used_ssl_config, 153 const SSLConfig& used_ssl_config,
86 const SSLInfo& ssl_info) OVERRIDE {} 154 const SSLInfo& ssl_info) OVERRIDE {}
87 155
88 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response, 156 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response,
(...skipping 10 matching lines...) Expand all
99 HttpStreamBase* stream) OVERRIDE {} 167 HttpStreamBase* stream) OVERRIDE {}
100 168
101 void WaitForStream() { 169 void WaitForStream() {
102 while (!stream_done_) { 170 while (!stream_done_) {
103 waiting_for_stream_ = true; 171 waiting_for_stream_ = true;
104 MessageLoop::current()->Run(); 172 MessageLoop::current()->Run();
105 waiting_for_stream_ = false; 173 waiting_for_stream_ = false;
106 } 174 }
107 } 175 }
108 176
177 public:
178 bool stream_done() const { return stream_done_; }
179 HttpStreamBase* stream() { return stream_.get(); }
180 SpdySession* spdy_session() {return spdy_session_.get(); }
181 ClientSocketHandle* connection() { return connection_.get(); }
182
109 private: 183 private:
110 bool waiting_for_stream_; 184 bool waiting_for_stream_;
111 bool stream_done_; 185 bool stream_done_;
112 scoped_ptr<HttpStreamBase> stream_; 186 scoped_ptr<HttpStreamBase> stream_;
187 scoped_ptr<ClientSocketHandle> connection_;
188 scoped_refptr<SpdySession> spdy_session_;
113 189
114 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter); 190 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter);
115 }; 191 };
116 192
117 struct SessionDependencies { 193 struct SessionDependencies {
118 // Custom proxy service dependency. 194 // Custom proxy service dependency.
119 explicit SessionDependencies(ProxyService* proxy_service) 195 explicit SessionDependencies(ProxyService* proxy_service)
120 : host_resolver(new MockHostResolver), 196 : host_resolver(new MockHostResolver),
121 cert_verifier(new MockCertVerifier), 197 cert_verifier(new MockCertVerifier),
122 proxy_service(proxy_service), 198 proxy_service(proxy_service),
123 ssl_config_service(new SSLConfigServiceDefaults),
124 http_auth_handler_factory( 199 http_auth_handler_factory(
125 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())), 200 HttpAuthHandlerFactory::CreateDefault(host_resolver.get())),
126 net_log(NULL) {} 201 net_log(NULL) {
202 SSLConfig initial_config;
203 initial_config.rev_checking_enabled = false;
204 initial_config.false_start_enabled = false;
205 initial_config.unrestricted_ssl3_fallback_enabled = false;
206 initial_config.version_min = SSL_PROTOCOL_VERSION_SSL3;
207 initial_config.version_max = SSL_PROTOCOL_VERSION_TLS1_1;
208 ssl_config_service = new MockSSLConfigService(initial_config);
209 ssl_config_service = new SSLConfigServiceDefaults();
Adam Rice 2013/05/13 13:03:03 This looks like the MockSSLConfigService is never
yhirano 2013/05/14 05:43:53 Thanks, done.
210 }
127 211
128 scoped_ptr<MockHostResolverBase> host_resolver; 212 scoped_ptr<MockHostResolverBase> host_resolver;
129 scoped_ptr<CertVerifier> cert_verifier; 213 scoped_ptr<CertVerifier> cert_verifier;
130 scoped_ptr<ProxyService> proxy_service; 214 scoped_ptr<ProxyService> proxy_service;
131 scoped_refptr<SSLConfigService> ssl_config_service; 215 scoped_refptr<SSLConfigService> ssl_config_service;
132 MockClientSocketFactory socket_factory; 216 MockClientSocketFactory socket_factory;
133 scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; 217 scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory;
134 HttpServerPropertiesImpl http_server_properties; 218 HttpServerPropertiesImpl http_server_properties;
135 NetLog* net_log; 219 NetLog* net_log;
136 }; 220 };
(...skipping 326 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 waiter.WaitForStream(); 547 waiter.WaitForStream();
464 548
465 // The proxy that failed should now be known to the proxy_service as bad. 549 // The proxy that failed should now be known to the proxy_service as bad.
466 const ProxyRetryInfoMap& retry_info = 550 const ProxyRetryInfoMap& retry_info =
467 session->proxy_service()->proxy_retry_info(); 551 session->proxy_service()->proxy_retry_info();
468 EXPECT_EQ(1u, retry_info.size()); 552 EXPECT_EQ(1u, retry_info.size());
469 ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); 553 ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99");
470 EXPECT_TRUE(iter != retry_info.end()); 554 EXPECT_TRUE(iter != retry_info.end());
471 } 555 }
472 556
557 TEST(HttpStreamFactoryTest, RequestStream) {
558 SessionDependencies session_deps(ProxyService::CreateDirect());
559
560 StaticSocketDataProvider socket_data;
561 socket_data.set_connect_data(MockConnect(ASYNC, OK));
562 session_deps.socket_factory.AddSocketDataProvider(&socket_data);
563
564 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
565
566 // Now request a stream. It should succeed using the second proxy in the
567 // list.
568 HttpRequestInfo request_info;
569 request_info.method = "GET";
570 request_info.url = GURL("http://www.google.com");
571 request_info.load_flags = 0;
572
573 SSLConfig ssl_config;
574 StreamRequestWaiter waiter;
575 scoped_ptr<HttpStreamRequest> request(
576 session->http_stream_factory()->RequestStream(
577 request_info,
578 DEFAULT_PRIORITY,
579 ssl_config,
580 ssl_config,
581 &waiter,
582 BoundNetLog()));
583 waiter.WaitForStream();
584 EXPECT_TRUE(waiter.stream_done());
585 EXPECT_TRUE(NULL == waiter.spdy_session());
586 EXPECT_TRUE(NULL == waiter.connection());
587 EXPECT_TRUE(NULL != waiter.stream());
588 }
589
590 TEST(HttpStreamFactoryTest, RequestSocket) {
591 SessionDependencies session_deps(ProxyService::CreateDirect());
592
593 StaticSocketDataProvider socket_data;
594 socket_data.set_connect_data(MockConnect(ASYNC, OK));
595 session_deps.socket_factory.AddSocketDataProvider(&socket_data);
596
597 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
598
599 // Now request a stream. It should succeed using the second proxy in the
Adam Rice 2013/05/13 13:03:03 Doesn't ProxyService::CreateDirect() above imply t
yhirano 2013/05/14 05:43:53 Thanks, I copied from the comment from the above t
600 // list.
601 HttpRequestInfo request_info;
602 request_info.method = "GET";
603 request_info.url = GURL("ws://www.google.com");
604 request_info.load_flags = 0;
605
606 SSLConfig ssl_config;
607 StreamRequestWaiter waiter;
608 scoped_ptr<HttpStreamRequest> request(
609 session->http_stream_factory()->RequestStreamForWebSocket(
610 request_info,
611 DEFAULT_PRIORITY,
612 ssl_config,
613 ssl_config,
614 &waiter,
615 BoundNetLog()));
616 waiter.WaitForStream();
617 EXPECT_TRUE(waiter.stream_done());
618 EXPECT_TRUE(NULL == waiter.stream());
619 EXPECT_TRUE(NULL == waiter.spdy_session());
620 EXPECT_TRUE(NULL != waiter.connection());
621 }
622
623 TEST(HttpStreamFactoryTest, RequestSpdyHttpStream) {
624 HttpStreamFactorySpdyForcer use_spdy;
625 SpdySessionDependencies session_deps(kProtoSPDY3,
626 ProxyService::CreateDirect());
627
628 MockRead r(ASYNC, OK);
629 StaticSocketDataProvider socket_data(&r, 1, NULL, 0);
630 socket_data.set_connect_data(MockConnect(ASYNC, OK));
631 session_deps.socket_factory->AddSocketDataProvider(&socket_data);
632
633 SSLSocketDataProvider ssl_socket_data(ASYNC, OK);
634 ssl_socket_data.protocol_negotiated = kProtoSPDY3;
635 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data);
636
637 HostPortPair host_port_pair("www.google.com", 80);
638 scoped_refptr<HttpNetworkSession>
639 session(SpdySessionDependencies::SpdyCreateSession(&session_deps));
640
641 // Now request a stream. It should succeed using the second proxy in the
642 // list.
643 HttpRequestInfo request_info;
644 request_info.method = "GET";
645 request_info.url = GURL("http://www.google.com");
646 request_info.load_flags = 0;
647
648 SSLConfig ssl_config;
649 StreamRequestWaiter waiter;
650 scoped_ptr<HttpStreamRequest> request(
651 session->http_stream_factory()->RequestStream(
652 request_info,
653 DEFAULT_PRIORITY,
654 ssl_config,
655 ssl_config,
656 &waiter,
657 BoundNetLog()));
658 waiter.WaitForStream();
659 EXPECT_TRUE(waiter.stream_done());
660 EXPECT_TRUE(NULL == waiter.spdy_session());
661 EXPECT_TRUE(NULL == waiter.connection());
662 EXPECT_TRUE(NULL != waiter.stream());
663 EXPECT_TRUE(waiter.stream()->IsSpdyHttpStream());
664 }
665
666 TEST(HttpStreamFactoryTest, RequestSpdySession) {
667 HttpStreamFactorySpdyForcer use_spdy;
668 SpdySessionDependencies session_deps(kProtoSPDY3,
669 ProxyService::CreateDirect());
670
671 MockRead r(ASYNC, OK);
672 StaticSocketDataProvider socket_data(&r, 1, NULL, 0);
673 socket_data.set_connect_data(MockConnect(ASYNC, OK));
674 session_deps.socket_factory->AddSocketDataProvider(&socket_data);
675
676 SSLSocketDataProvider ssl_socket_data(ASYNC, OK);
677 ssl_socket_data.protocol_negotiated = kProtoSPDY3;
678 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data);
679
680 HostPortPair host_port_pair("www.google.com", 80);
681 scoped_refptr<HttpNetworkSession>
682 session(SpdySessionDependencies::SpdyCreateSession(&session_deps));
683
684 // Now request a stream. It should succeed using the second proxy in the
685 // list.
686 HttpRequestInfo request_info;
687 request_info.method = "GET";
688 request_info.url = GURL("wss://www.google.com");
689 request_info.load_flags = 0;
690
691 SSLConfig ssl_config;
692 StreamRequestWaiter waiter;
693 scoped_ptr<HttpStreamRequest> request(
694 session->http_stream_factory()->RequestStreamForWebSocket(
695 request_info,
696 DEFAULT_PRIORITY,
697 ssl_config,
698 ssl_config,
699 &waiter,
700 BoundNetLog()));
701 waiter.WaitForStream();
702 EXPECT_TRUE(waiter.stream_done());
703 EXPECT_TRUE(NULL != waiter.spdy_session());
704 EXPECT_TRUE(NULL == waiter.connection());
705 EXPECT_TRUE(NULL == waiter.stream());
706 }
707
473 } // namespace 708 } // namespace
474 709
475 } // namespace net 710 } // namespace net
OLDNEW
« net/http/http_stream_factory_impl_job.cc ('K') | « net/http/http_stream_factory_impl_request.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698