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