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

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: 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"
24 #include "net/socket/next_proto.h"
23 #include "net/socket/socket_test_util.h" 25 #include "net/socket/socket_test_util.h"
24 #include "net/spdy/spdy_session.h" 26 #include "net/spdy/spdy_session.h"
25 #include "net/spdy/spdy_session_pool.h" 27 #include "net/spdy/spdy_session_pool.h"
28 #include "net/spdy/spdy_test_util_common.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"
27 #include "testing/gtest/include/gtest/gtest.h" 31 #include "testing/gtest/include/gtest/gtest.h"
28 32
29 namespace net { 33 namespace net {
30 34
31 namespace { 35 namespace {
32 36
37 class HttpStreamFactorySpdyForcer {
mmenke 2013/05/15 15:38:20 Suggest ScopedForceSpdyOverSsl.
yhirano 2013/05/16 05:02:35 Done.
38 public:
39 HttpStreamFactorySpdyForcer():
40 over_ssl_(HttpStreamFactory::force_spdy_over_ssl()),
41 always_(HttpStreamFactory::force_spdy_always()) {
42 HttpStreamFactory::set_force_spdy_over_ssl(true);
43 HttpStreamFactory::set_force_spdy_always(true);
44 }
45 ~HttpStreamFactorySpdyForcer() {
46 HttpStreamFactory::set_force_spdy_over_ssl(over_ssl_);
47 HttpStreamFactory::set_force_spdy_always(always_);
48 }
49 private:
mmenke 2013/05/15 15:38:20 nit: Line break before private
yhirano 2013/05/16 05:02:35 Done.
50 const bool over_ssl_;
51 const bool always_;
mmenke 2013/05/15 15:38:20 I suggest orig_force_spdy_over_ssl_ / orig_force_s
yhirano 2013/05/16 05:02:35 Done.
52 };
53
33 class MockHttpStreamFactoryImpl : public HttpStreamFactoryImpl { 54 class MockHttpStreamFactoryImpl : public HttpStreamFactoryImpl {
34 public: 55 public:
35 MockHttpStreamFactoryImpl(HttpNetworkSession* session) 56 explicit MockHttpStreamFactoryImpl(HttpNetworkSession* session)
36 : HttpStreamFactoryImpl(session), 57 : HttpStreamFactoryImpl(session),
37 preconnect_done_(false), 58 preconnect_done_(false),
38 waiting_for_preconnect_(false) {} 59 waiting_for_preconnect_(false) {}
39 60
40 61
41 void WaitForPreconnects() { 62 void WaitForPreconnects() {
42 while (!preconnect_done_) { 63 while (!preconnect_done_) {
43 waiting_for_preconnect_ = true; 64 waiting_for_preconnect_ = true;
44 MessageLoop::current()->Run(); 65 MessageLoop::current()->Run();
45 waiting_for_preconnect_ = false; 66 waiting_for_preconnect_ = false;
(...skipping 23 matching lines...) Expand all
69 virtual void OnStreamReady( 90 virtual void OnStreamReady(
70 const SSLConfig& used_ssl_config, 91 const SSLConfig& used_ssl_config,
71 const ProxyInfo& used_proxy_info, 92 const ProxyInfo& used_proxy_info,
72 HttpStreamBase* stream) OVERRIDE { 93 HttpStreamBase* stream) OVERRIDE {
73 stream_done_ = true; 94 stream_done_ = true;
74 if (waiting_for_stream_) 95 if (waiting_for_stream_)
75 MessageLoop::current()->Quit(); 96 MessageLoop::current()->Quit();
76 stream_.reset(stream); 97 stream_.reset(stream);
77 } 98 }
78 99
100 virtual void OnSocketReady(
101 const SSLConfig& used_ssl_config,
102 const ProxyInfo& used_proxy_info,
103 ClientSocketHandle* connection) OVERRIDE {
104 stream_done_ = true;
105 if (waiting_for_stream_)
106 MessageLoop::current()->Quit();
107 connection_.reset(connection);
108 }
109
110 virtual void OnSpdySessionReady(
111 const SSLConfig& used_ssl_config,
112 const ProxyInfo& used_proxy_info,
113 SpdySession* session) OVERRIDE {
114 stream_done_ = true;
115 if (waiting_for_stream_)
116 MessageLoop::current()->Quit();
117 spdy_session_ = session;
118 }
119
79 virtual void OnStreamFailed( 120 virtual void OnStreamFailed(
80 int status, 121 int status,
81 const SSLConfig& used_ssl_config) OVERRIDE {} 122 const SSLConfig& used_ssl_config) OVERRIDE {}
82 123
83 virtual void OnCertificateError( 124 virtual void OnCertificateError(
84 int status, 125 int status,
85 const SSLConfig& used_ssl_config, 126 const SSLConfig& used_ssl_config,
86 const SSLInfo& ssl_info) OVERRIDE {} 127 const SSLInfo& ssl_info) OVERRIDE {}
87 128
88 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response, 129 virtual void OnNeedsProxyAuth(const HttpResponseInfo& proxy_response,
(...skipping 10 matching lines...) Expand all
99 HttpStreamBase* stream) OVERRIDE {} 140 HttpStreamBase* stream) OVERRIDE {}
100 141
101 void WaitForStream() { 142 void WaitForStream() {
102 while (!stream_done_) { 143 while (!stream_done_) {
103 waiting_for_stream_ = true; 144 waiting_for_stream_ = true;
104 MessageLoop::current()->Run(); 145 MessageLoop::current()->Run();
105 waiting_for_stream_ = false; 146 waiting_for_stream_ = false;
106 } 147 }
107 } 148 }
108 149
150 public:
151 bool stream_done() const { return stream_done_; }
152 HttpStreamBase* stream() { return stream_.get(); }
153 SpdySession* spdy_session() {return spdy_session_.get(); }
154 ClientSocketHandle* connection() { return connection_.get(); }
155
109 private: 156 private:
110 bool waiting_for_stream_; 157 bool waiting_for_stream_;
111 bool stream_done_; 158 bool stream_done_;
112 scoped_ptr<HttpStreamBase> stream_; 159 scoped_ptr<HttpStreamBase> stream_;
160 scoped_ptr<ClientSocketHandle> connection_;
161 scoped_refptr<SpdySession> spdy_session_;
113 162
114 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter); 163 DISALLOW_COPY_AND_ASSIGN(StreamRequestWaiter);
115 }; 164 };
116 165
117 struct SessionDependencies { 166 struct SessionDependencies {
118 // Custom proxy service dependency. 167 // Custom proxy service dependency.
119 explicit SessionDependencies(ProxyService* proxy_service) 168 explicit SessionDependencies(ProxyService* proxy_service)
120 : host_resolver(new MockHostResolver), 169 : host_resolver(new MockHostResolver),
121 cert_verifier(new MockCertVerifier), 170 cert_verifier(new MockCertVerifier),
122 proxy_service(proxy_service), 171 proxy_service(proxy_service),
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
463 waiter.WaitForStream(); 512 waiter.WaitForStream();
464 513
465 // The proxy that failed should now be known to the proxy_service as bad. 514 // The proxy that failed should now be known to the proxy_service as bad.
466 const ProxyRetryInfoMap& retry_info = 515 const ProxyRetryInfoMap& retry_info =
467 session->proxy_service()->proxy_retry_info(); 516 session->proxy_service()->proxy_retry_info();
468 EXPECT_EQ(1u, retry_info.size()); 517 EXPECT_EQ(1u, retry_info.size());
469 ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99"); 518 ProxyRetryInfoMap::const_iterator iter = retry_info.find("bad:99");
470 EXPECT_TRUE(iter != retry_info.end()); 519 EXPECT_TRUE(iter != retry_info.end());
471 } 520 }
472 521
522 TEST(HttpStreamFactoryTest, RequestStream) {
523 SessionDependencies session_deps(ProxyService::CreateDirect());
524
525 StaticSocketDataProvider socket_data;
526 socket_data.set_connect_data(MockConnect(ASYNC, OK));
527 session_deps.socket_factory.AddSocketDataProvider(&socket_data);
528
529 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
530
531 // Now request a stream.
532 HttpRequestInfo request_info;
533 request_info.method = "GET";
534 request_info.url = GURL("http://www.google.com");
535 request_info.load_flags = 0;
536
537 SSLConfig ssl_config;
538 StreamRequestWaiter waiter;
539 scoped_ptr<HttpStreamRequest> request(
540 session->http_stream_factory()->RequestStream(
541 request_info,
542 DEFAULT_PRIORITY,
543 ssl_config,
544 ssl_config,
545 &waiter,
546 BoundNetLog()));
547 waiter.WaitForStream();
548 EXPECT_TRUE(waiter.stream_done());
549 EXPECT_TRUE(NULL == waiter.spdy_session());
550 EXPECT_TRUE(NULL == waiter.connection());
551 EXPECT_TRUE(NULL != waiter.stream());
552 }
553
554 TEST(HttpStreamFactoryTest, RequestSocket) {
555 SessionDependencies session_deps(ProxyService::CreateDirect());
556
557 StaticSocketDataProvider socket_data;
558 socket_data.set_connect_data(MockConnect(ASYNC, OK));
559 session_deps.socket_factory.AddSocketDataProvider(&socket_data);
560
561 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps));
562
563 // Now request a stream.
564 HttpRequestInfo request_info;
565 request_info.method = "GET";
566 request_info.url = GURL("ws://www.google.com");
567 request_info.load_flags = 0;
568
569 SSLConfig ssl_config;
570 StreamRequestWaiter waiter;
571 scoped_ptr<HttpStreamRequest> request(
572 session->http_stream_factory()->RequestStreamForWebSocket(
573 request_info,
574 DEFAULT_PRIORITY,
575 ssl_config,
576 ssl_config,
577 &waiter,
578 BoundNetLog()));
579 waiter.WaitForStream();
580 EXPECT_TRUE(waiter.stream_done());
581 EXPECT_TRUE(NULL == waiter.stream());
582 EXPECT_TRUE(NULL == waiter.spdy_session());
583 EXPECT_TRUE(NULL != waiter.connection());
584 }
585
586 TEST(HttpStreamFactoryTest, RequestSpdyHttpStream) {
587 HttpStreamFactorySpdyForcer use_spdy;
588 SpdySessionDependencies session_deps(kProtoSPDY3,
589 ProxyService::CreateDirect());
590
591 MockRead mock_read(ASYNC, OK);
592 StaticSocketDataProvider socket_data(&mock_read, 1, NULL, 0);
593 socket_data.set_connect_data(MockConnect(ASYNC, OK));
594 session_deps.socket_factory->AddSocketDataProvider(&socket_data);
595
596 SSLSocketDataProvider ssl_socket_data(ASYNC, OK);
597 ssl_socket_data.protocol_negotiated = kProtoSPDY3;
598 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data);
599
600 HostPortPair host_port_pair("www.google.com", 80);
601 scoped_refptr<HttpNetworkSession>
602 session(SpdySessionDependencies::SpdyCreateSession(&session_deps));
603
604 // Now request a stream.
605 HttpRequestInfo request_info;
606 request_info.method = "GET";
607 request_info.url = GURL("http://www.google.com");
608 request_info.load_flags = 0;
609
610 SSLConfig ssl_config;
611 StreamRequestWaiter waiter;
612 scoped_ptr<HttpStreamRequest> request(
613 session->http_stream_factory()->RequestStream(
614 request_info,
615 DEFAULT_PRIORITY,
616 ssl_config,
617 ssl_config,
618 &waiter,
619 BoundNetLog()));
620 waiter.WaitForStream();
621 EXPECT_TRUE(waiter.stream_done());
622 EXPECT_TRUE(NULL == waiter.spdy_session());
623 EXPECT_TRUE(NULL == waiter.connection());
624 EXPECT_TRUE(NULL != waiter.stream());
625 EXPECT_TRUE(waiter.stream()->IsSpdyHttpStream());
626 }
627
628 TEST(HttpStreamFactoryTest, RequestSpdySession) {
mmenke 2013/05/15 15:38:20 Maybe a test where there's a pre-existing SPDY ses
yhirano 2013/05/16 05:02:35 Done.
629 HttpStreamFactorySpdyForcer use_spdy;
630 SpdySessionDependencies session_deps(kProtoSPDY3,
631 ProxyService::CreateDirect());
632
633 MockRead mock_read(ASYNC, OK);
634 StaticSocketDataProvider socket_data(&mock_read, 1, NULL, 0);
635 socket_data.set_connect_data(MockConnect(ASYNC, OK));
636 session_deps.socket_factory->AddSocketDataProvider(&socket_data);
637
638 SSLSocketDataProvider ssl_socket_data(ASYNC, OK);
639 ssl_socket_data.protocol_negotiated = kProtoSPDY3;
640 session_deps.socket_factory->AddSSLSocketDataProvider(&ssl_socket_data);
641
642 HostPortPair host_port_pair("www.google.com", 80);
643 scoped_refptr<HttpNetworkSession>
644 session(SpdySessionDependencies::SpdyCreateSession(&session_deps));
645
646 // Now request a stream.
647 HttpRequestInfo request_info;
648 request_info.method = "GET";
649 request_info.url = GURL("wss://www.google.com");
650 request_info.load_flags = 0;
651
652 SSLConfig ssl_config;
653 StreamRequestWaiter waiter;
654 scoped_ptr<HttpStreamRequest> request(
655 session->http_stream_factory()->RequestStreamForWebSocket(
656 request_info,
657 DEFAULT_PRIORITY,
658 ssl_config,
659 ssl_config,
660 &waiter,
661 BoundNetLog()));
662 waiter.WaitForStream();
663 EXPECT_TRUE(waiter.stream_done());
664 EXPECT_TRUE(NULL != waiter.spdy_session());
665 EXPECT_TRUE(NULL == waiter.connection());
666 EXPECT_TRUE(NULL == waiter.stream());
667 }
668
473 } // namespace 669 } // namespace
474 670
475 } // namespace net 671 } // namespace net
OLDNEW
« net/http/http_stream_factory_impl_request.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