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