OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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/spdy/spdy_http_stream.h" | 5 #include "net/spdy/spdy_http_stream.h" |
6 #include "base/ref_counted.h" | |
7 #include "base/time.h" | |
8 #include "net/base/mock_host_resolver.h" | |
9 #include "net/base/net_errors.h" | |
10 #include "net/base/net_log.h" | |
11 #include "net/base/ssl_config_service.h" | |
12 #include "net/base/ssl_config_service_defaults.h" | |
13 #include "net/base/test_completion_callback.h" | |
14 #include "net/http/http_auth_handler_factory.h" | |
15 #include "net/http/http_network_session.h" | |
16 #include "net/http/http_request_info.h" | |
17 #include "net/http/http_response_info.h" | |
18 #include "net/proxy/proxy_service.h" | |
19 #include "net/socket/socket_test_util.h" | |
20 #include "net/spdy/spdy_session.h" | 6 #include "net/spdy/spdy_session.h" |
21 #include "net/spdy/spdy_session_pool.h" | 7 #include "net/spdy/spdy_test_util.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
23 | 9 |
24 namespace net { | 10 namespace net { |
25 | 11 |
26 class SpdySessionPoolPeer { | |
27 public: | |
28 explicit SpdySessionPoolPeer(const scoped_refptr<SpdySessionPool>& pool) | |
29 : pool_(pool) {} | |
30 | |
31 void RemoveSpdySession(const scoped_refptr<SpdySession>& session) { | |
32 pool_->Remove(session); | |
33 } | |
34 | |
35 private: | |
36 const scoped_refptr<SpdySessionPool> pool_; | |
37 | |
38 DISALLOW_COPY_AND_ASSIGN(SpdySessionPoolPeer); | |
39 }; | |
40 | |
41 namespace { | |
42 | |
43 // Create a proxy service which fails on all requests (falls back to direct). | |
44 ProxyService* CreateNullProxyService() { | |
45 return ProxyService::CreateNull(); | |
46 } | |
47 | |
48 // Helper to manage the lifetimes of the dependencies for a | |
49 // SpdyNetworkTransaction. | |
50 class SessionDependencies { | |
51 public: | |
52 // Default set of dependencies -- "null" proxy service. | |
53 SessionDependencies() | |
54 : host_resolver(new MockHostResolver), | |
55 proxy_service(CreateNullProxyService()), | |
56 ssl_config_service(new SSLConfigServiceDefaults), | |
57 http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()), | |
58 spdy_session_pool(new SpdySessionPool()) {} | |
59 | |
60 // Custom proxy service dependency. | |
61 explicit SessionDependencies(ProxyService* proxy_service) | |
62 : host_resolver(new MockHostResolver), | |
63 proxy_service(proxy_service), | |
64 ssl_config_service(new SSLConfigServiceDefaults), | |
65 http_auth_handler_factory(HttpAuthHandlerFactory::CreateDefault()), | |
66 spdy_session_pool(new SpdySessionPool()) {} | |
67 | |
68 scoped_refptr<MockHostResolverBase> host_resolver; | |
69 scoped_refptr<ProxyService> proxy_service; | |
70 scoped_refptr<SSLConfigService> ssl_config_service; | |
71 MockClientSocketFactory socket_factory; | |
72 scoped_ptr<HttpAuthHandlerFactory> http_auth_handler_factory; | |
73 scoped_refptr<SpdySessionPool> spdy_session_pool; | |
74 }; | |
75 | |
76 HttpNetworkSession* CreateSession(SessionDependencies* session_deps) { | |
77 return new HttpNetworkSession(session_deps->host_resolver, | |
78 session_deps->proxy_service, | |
79 &session_deps->socket_factory, | |
80 session_deps->ssl_config_service, | |
81 session_deps->spdy_session_pool, | |
82 session_deps->http_auth_handler_factory.get(), | |
83 NULL, | |
84 NULL); | |
85 } | |
86 | |
87 class SpdyHttpStreamTest : public testing::Test { | 12 class SpdyHttpStreamTest : public testing::Test { |
88 protected: | 13 protected: |
89 SpdyHttpStreamTest() | 14 SpdyHttpStreamTest(){} |
90 : session_(CreateSession(&session_deps_)), | |
91 pool_peer_(session_->spdy_session_pool()) {} | |
92 | 15 |
93 scoped_refptr<SpdySession> CreateSpdySession() { | 16 void EnableCompression(bool enabled) { |
94 HostPortPair host_port_pair("www.google.com", 80); | 17 spdy::SpdyFramer::set_enable_compression_default(enabled); |
95 scoped_refptr<SpdySession> session( | |
96 session_->spdy_session_pool()->Get( | |
97 host_port_pair, session_, BoundNetLog())); | |
98 return session; | |
99 } | 18 } |
100 | 19 |
101 virtual void TearDown() { | 20 virtual void TearDown() { |
102 MessageLoop::current()->RunAllPending(); | 21 MessageLoop::current()->RunAllPending(); |
103 } | 22 } |
104 | |
105 SessionDependencies session_deps_; | |
106 scoped_refptr<HttpNetworkSession> session_; | |
107 SpdySessionPoolPeer pool_peer_; | |
108 }; | 23 }; |
109 | 24 |
110 // Needs fixing, see http://crbug.com/28622 | |
111 TEST_F(SpdyHttpStreamTest, SendRequest) { | 25 TEST_F(SpdyHttpStreamTest, SendRequest) { |
112 scoped_refptr<SpdySession> session(CreateSpdySession()); | 26 EnableCompression(false); |
| 27 SpdySession::SetSSLMode(false); |
| 28 |
| 29 SpdySessionDependencies session_deps; |
| 30 scoped_ptr<spdy::SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
| 31 MockWrite writes[] = { |
| 32 CreateMockWrite(*req.get(), 1), |
| 33 }; |
| 34 MockRead reads[] = { |
| 35 MockRead(false, 0, 2) // EOF |
| 36 }; |
| 37 scoped_refptr<OrderedSocketData> data( |
| 38 new OrderedSocketData(reads, arraysize(reads), |
| 39 writes, arraysize(writes))); |
| 40 session_deps.socket_factory.AddSocketDataProvider(data.get()); |
| 41 |
| 42 scoped_refptr<HttpNetworkSession> http_session( |
| 43 SpdySessionDependencies::SpdyCreateSession(&session_deps)); |
| 44 scoped_refptr<SpdySessionPool> spdy_session_pool( |
| 45 http_session->spdy_session_pool()); |
| 46 HostPortPair host_port_pair("www.google.com", 80); |
| 47 scoped_refptr<SpdySession> session = |
| 48 spdy_session_pool->Get( |
| 49 host_port_pair, http_session.get(), BoundNetLog()); |
| 50 scoped_refptr<TCPSocketParams> tcp_params = |
| 51 new TCPSocketParams(host_port_pair.host, host_port_pair.port, |
| 52 MEDIUM, GURL(), false); |
| 53 int rv = session->Connect(host_port_pair.host, tcp_params, MEDIUM); |
| 54 ASSERT_EQ(OK, rv); |
| 55 |
113 HttpRequestInfo request; | 56 HttpRequestInfo request; |
114 request.method = "GET"; | 57 request.method = "GET"; |
115 request.url = GURL("http://www.google.com/"); | 58 request.url = GURL("http://www.google.com/"); |
116 TestCompletionCallback callback; | 59 TestCompletionCallback callback; |
117 HttpResponseInfo response; | 60 HttpResponseInfo response; |
118 scoped_ptr<SpdyHttpStream> http_stream(new SpdyHttpStream()); | 61 scoped_ptr<SpdyHttpStream> http_stream(new SpdyHttpStream()); |
119 ASSERT_EQ( | 62 ASSERT_EQ( |
120 OK, | 63 OK, |
121 http_stream->InitializeStream(session, request, BoundNetLog(), NULL)); | 64 http_stream->InitializeStream(session, request, BoundNetLog(), NULL)); |
122 http_stream->InitializeRequest(base::Time::Now(), NULL); | 65 http_stream->InitializeRequest(base::Time::Now(), NULL); |
123 EXPECT_EQ(ERR_IO_PENDING, | 66 EXPECT_EQ(ERR_IO_PENDING, |
124 http_stream->SendRequest(&response, &callback)); | 67 http_stream->SendRequest(&response, &callback)); |
125 | 68 MessageLoop::current()->RunAllPending(); |
126 // Need to manually remove the spdy session since normally it gets removed on | 69 EXPECT_TRUE(spdy_session_pool->HasSession(host_port_pair)); |
127 // socket close/error, but we aren't communicating over a socket here. | 70 spdy_session_pool->Remove(session); |
128 pool_peer_.RemoveSpdySession(session); | |
129 } | 71 } |
130 | 72 |
131 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 73 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
132 // methods. | 74 // methods. |
133 | 75 |
134 } // namespace | |
135 | |
136 } // namespace net | 76 } // namespace net |
OLD | NEW |