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/spdy/spdy_http_stream.h" | 5 #include "net/spdy/spdy_http_stream.h" |
6 | 6 |
7 #include "base/threading/sequenced_worker_pool.h" | 7 #include "base/threading/sequenced_worker_pool.h" |
8 #include "crypto/ec_private_key.h" | 8 #include "crypto/ec_private_key.h" |
9 #include "crypto/ec_signature_creator.h" | 9 #include "crypto/ec_signature_creator.h" |
10 #include "crypto/signature_creator.h" | 10 #include "crypto/signature_creator.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 namespace net { | 22 namespace net { |
23 | 23 |
24 class SpdyHttpStreamSpdy3Test : public testing::Test { | 24 class SpdyHttpStreamSpdy3Test : public testing::Test { |
25 public: | 25 public: |
26 OrderedSocketData* data() { return data_.get(); } | 26 OrderedSocketData* data() { return data_.get(); } |
27 protected: | 27 protected: |
28 SpdyHttpStreamSpdy3Test() {} | 28 SpdyHttpStreamSpdy3Test() {} |
29 | 29 |
30 virtual void SetUp() { | 30 virtual void SetUp() { |
31 SpdySession::set_default_protocol(kProtoSPDY3); | 31 SpdySession::set_default_protocol(kProtoSPDY3); |
| 32 merge_chunks_ = UploadDataStream::merge_chunks(); |
32 } | 33 } |
33 | 34 |
34 virtual void TearDown() { | 35 virtual void TearDown() { |
35 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); | 36 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); |
| 37 UploadDataStream::set_merge_chunks(merge_chunks_); |
36 MessageLoop::current()->RunAllPending(); | 38 MessageLoop::current()->RunAllPending(); |
37 } | 39 } |
38 | 40 |
39 int InitSession(MockRead* reads, size_t reads_count, | 41 int InitSession(MockRead* reads, size_t reads_count, |
40 MockWrite* writes, size_t writes_count, | 42 MockWrite* writes, size_t writes_count, |
41 HostPortPair& host_port_pair) { | 43 HostPortPair& host_port_pair) { |
42 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 44 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
43 data_.reset(new OrderedSocketData(reads, reads_count, | 45 data_.reset(new OrderedSocketData(reads, reads_count, |
44 writes, writes_count)); | 46 writes, writes_count)); |
45 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); | 47 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); |
(...skipping 22 matching lines...) Expand all Loading... |
68 const std::string& proof); | 70 const std::string& proof); |
69 | 71 |
70 SpdySessionDependencies session_deps_; | 72 SpdySessionDependencies session_deps_; |
71 scoped_ptr<OrderedSocketData> data_; | 73 scoped_ptr<OrderedSocketData> data_; |
72 scoped_refptr<HttpNetworkSession> http_session_; | 74 scoped_refptr<HttpNetworkSession> http_session_; |
73 scoped_refptr<SpdySession> session_; | 75 scoped_refptr<SpdySession> session_; |
74 scoped_refptr<TransportSocketParams> transport_params_; | 76 scoped_refptr<TransportSocketParams> transport_params_; |
75 | 77 |
76 private: | 78 private: |
77 SpdyTestStateHelper spdy_state_; | 79 SpdyTestStateHelper spdy_state_; |
| 80 bool merge_chunks_; |
78 }; | 81 }; |
79 | 82 |
80 TEST_F(SpdyHttpStreamSpdy3Test, SendRequest) { | 83 TEST_F(SpdyHttpStreamSpdy3Test, SendRequest) { |
81 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); | 84 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(NULL, 0, false, 1, LOWEST)); |
82 MockWrite writes[] = { | 85 MockWrite writes[] = { |
83 CreateMockWrite(*req.get(), 1), | 86 CreateMockWrite(*req.get(), 1), |
84 }; | 87 }; |
85 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 88 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
86 MockRead reads[] = { | 89 MockRead reads[] = { |
87 CreateMockRead(*resp, 2), | 90 CreateMockRead(*resp, 2), |
(...skipping 30 matching lines...) Expand all Loading... |
118 data()->CompleteRead(); | 121 data()->CompleteRead(); |
119 | 122 |
120 // Because we abandoned the stream, we don't expect to find a session in the | 123 // Because we abandoned the stream, we don't expect to find a session in the |
121 // pool anymore. | 124 // pool anymore. |
122 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 125 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
123 EXPECT_TRUE(data()->at_read_eof()); | 126 EXPECT_TRUE(data()->at_read_eof()); |
124 EXPECT_TRUE(data()->at_write_eof()); | 127 EXPECT_TRUE(data()->at_write_eof()); |
125 } | 128 } |
126 | 129 |
127 TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) { | 130 TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) { |
| 131 // TODO(satish): Remove this once we have a better way to unit test POST |
| 132 // requests with chunked uploads. |
128 UploadDataStream::set_merge_chunks(false); | 133 UploadDataStream::set_merge_chunks(false); |
129 | 134 |
130 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); | 135 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
131 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); | 136 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
132 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); | 137 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); |
133 MockWrite writes[] = { | 138 MockWrite writes[] = { |
134 CreateMockWrite(*req.get(), 1), | 139 CreateMockWrite(*req.get(), 0), |
135 CreateMockWrite(*chunk1, 2), // POST upload frames | 140 CreateMockWrite(*chunk1, 1), // POST upload frames |
136 CreateMockWrite(*chunk2, 3), | 141 CreateMockWrite(*chunk2, 2), |
137 }; | 142 }; |
138 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); | 143 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
139 MockRead reads[] = { | 144 MockRead reads[] = { |
140 CreateMockRead(*resp, 4), | 145 CreateMockRead(*resp, 3), |
141 CreateMockRead(*chunk1, 5), | 146 CreateMockRead(*chunk1, 4), |
142 CreateMockRead(*chunk2, 5), | 147 CreateMockRead(*chunk2, 5), |
143 MockRead(SYNCHRONOUS, 0, 6) // EOF | 148 MockRead(SYNCHRONOUS, 0, 6) // EOF |
144 }; | 149 }; |
145 | 150 |
146 HostPortPair host_port_pair("www.google.com", 80); | 151 HostPortPair host_port_pair("www.google.com", 80); |
147 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 152 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
148 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), | 153 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), |
149 host_port_pair)); | 154 host_port_pair)); |
150 | 155 |
151 HttpRequestInfo request; | 156 HttpRequestInfo request; |
(...skipping 26 matching lines...) Expand all Loading... |
178 data()->CompleteRead(); | 183 data()->CompleteRead(); |
179 MessageLoop::current()->RunAllPending(); | 184 MessageLoop::current()->RunAllPending(); |
180 | 185 |
181 // Because we abandoned the stream, we don't expect to find a session in the | 186 // Because we abandoned the stream, we don't expect to find a session in the |
182 // pool anymore. | 187 // pool anymore. |
183 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 188 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
184 EXPECT_TRUE(data()->at_read_eof()); | 189 EXPECT_TRUE(data()->at_read_eof()); |
185 EXPECT_TRUE(data()->at_write_eof()); | 190 EXPECT_TRUE(data()->at_write_eof()); |
186 } | 191 } |
187 | 192 |
| 193 TEST_F(SpdyHttpStreamSpdy3Test, DelayedSendChunkedPost) { |
| 194 // TODO(satish): Remove this once we have a better way to unit test POST |
| 195 // requests with chunked uploads. |
| 196 UploadDataStream::set_merge_chunks(false); |
| 197 |
| 198 const char kUploadData1[] = "12345678"; |
| 199 const int kUploadData1Size = arraysize(kUploadData1)-1; |
| 200 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
| 201 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
| 202 scoped_ptr<SpdyFrame> chunk2( |
| 203 ConstructSpdyBodyFrame(1, kUploadData1, kUploadData1Size, false)); |
| 204 scoped_ptr<SpdyFrame> chunk3(ConstructSpdyBodyFrame(1, true)); |
| 205 MockWrite writes[] = { |
| 206 CreateMockWrite(*req.get(), 0), |
| 207 CreateMockWrite(*chunk1, 1), // POST upload frames |
| 208 CreateMockWrite(*chunk2, 2), |
| 209 CreateMockWrite(*chunk3, 3), |
| 210 }; |
| 211 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
| 212 MockRead reads[] = { |
| 213 CreateMockRead(*resp, 4), |
| 214 CreateMockRead(*chunk1, 5), |
| 215 CreateMockRead(*chunk2, 6), |
| 216 CreateMockRead(*chunk3, 7), |
| 217 MockRead(ASYNC, 0, 8) // EOF |
| 218 }; |
| 219 |
| 220 HostPortPair host_port_pair("www.google.com", 80); |
| 221 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
| 222 |
| 223 scoped_refptr<DeterministicSocketData> data( |
| 224 new DeterministicSocketData(reads, arraysize(reads), |
| 225 writes, arraysize(writes))); |
| 226 |
| 227 DeterministicMockClientSocketFactory* socket_factory = |
| 228 session_deps_.deterministic_socket_factory.get(); |
| 229 socket_factory->AddSocketDataProvider(data.get()); |
| 230 |
| 231 http_session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic( |
| 232 &session_deps_); |
| 233 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
| 234 transport_params_ = new TransportSocketParams(host_port_pair, |
| 235 MEDIUM, false, false, |
| 236 OnHostResolutionCallback()); |
| 237 |
| 238 TestCompletionCallback callback; |
| 239 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 240 |
| 241 EXPECT_EQ(ERR_IO_PENDING, |
| 242 connection->Init(host_port_pair.ToString(), |
| 243 transport_params_, |
| 244 MEDIUM, |
| 245 callback.callback(), |
| 246 http_session_->GetTransportSocketPool( |
| 247 HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 248 BoundNetLog())); |
| 249 |
| 250 callback.WaitForResult(); |
| 251 EXPECT_EQ(OK, |
| 252 session_->InitializeWithSocket(connection.release(), false, OK)); |
| 253 |
| 254 HttpRequestInfo request; |
| 255 request.method = "POST"; |
| 256 request.url = GURL("http://www.google.com/"); |
| 257 request.upload_data = new UploadData(); |
| 258 request.upload_data->set_is_chunked(true); |
| 259 |
| 260 BoundNetLog net_log; |
| 261 scoped_ptr<SpdyHttpStream> http_stream( |
| 262 new SpdyHttpStream(session_.get(), true)); |
| 263 ASSERT_EQ(OK, |
| 264 http_stream->InitializeStream(&request, |
| 265 net_log, |
| 266 CompletionCallback())); |
| 267 |
| 268 scoped_ptr<UploadDataStream> upload_stream( |
| 269 new UploadDataStream(request.upload_data)); |
| 270 ASSERT_EQ(OK, upload_stream->Init()); |
| 271 |
| 272 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, false); |
| 273 |
| 274 HttpRequestHeaders headers; |
| 275 HttpResponseInfo response; |
| 276 // This will attempt to Write() the initial request and headers, which will |
| 277 // complete asynchronously. |
| 278 EXPECT_EQ(ERR_IO_PENDING, |
| 279 http_stream->SendRequest(headers, |
| 280 upload_stream.Pass(), |
| 281 &response, |
| 282 callback.callback())); |
| 283 EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(pair)); |
| 284 |
| 285 // Complete the initial request write and the first chunk. |
| 286 data->RunFor(1); |
| 287 |
| 288 // Now append the second chunk. This will enqueue another write. |
| 289 request.upload_data->AppendChunk(kUploadData1, kUploadData1Size, false); |
| 290 // Now append final chunk. This will enqueue another write. |
| 291 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, true); |
| 292 |
| 293 data->RunFor(1); |
| 294 ASSERT_TRUE(callback.have_result()); |
| 295 |
| 296 // Finish writing all the chunks. |
| 297 data->RunFor(3); |
| 298 int rv = callback.WaitForResult(); |
| 299 EXPECT_GT(rv, 0); |
| 300 EXPECT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); |
| 301 |
| 302 // Finish reading |chunk1|, |chunk2| and |chunk3|. |
| 303 data->RunFor(4); |
| 304 |
| 305 // Check |chunk1| response. |
| 306 scoped_refptr<IOBuffer> buf1(new IOBuffer(kUploadDataSize)); |
| 307 http_stream->ReadResponseBody(buf1, kUploadDataSize, callback.callback()); |
| 308 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); |
| 309 |
| 310 // Check |chunk2| response. |
| 311 scoped_refptr<IOBuffer> buf2(new IOBuffer(kUploadData1Size)); |
| 312 http_stream->ReadResponseBody(buf2, kUploadData1Size, callback.callback()); |
| 313 EXPECT_EQ(kUploadData1, std::string(buf2->data(), kUploadData1Size)); |
| 314 |
| 315 // Check |chunk3| response. |
| 316 scoped_refptr<IOBuffer> buf3(new IOBuffer(kUploadDataSize)); |
| 317 http_stream->ReadResponseBody(buf3, kUploadDataSize, callback.callback()); |
| 318 EXPECT_EQ(kUploadData, std::string(buf3->data(), kUploadDataSize)); |
| 319 |
| 320 // Finish reading the |EOF|. |
| 321 ASSERT_TRUE(response.headers.get()); |
| 322 ASSERT_EQ(200, response.headers->response_code()); |
| 323 EXPECT_TRUE(data->at_read_eof()); |
| 324 EXPECT_TRUE(data->at_write_eof()); |
| 325 } |
| 326 |
188 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 | 327 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 |
189 TEST_F(SpdyHttpStreamSpdy3Test, SpdyURLTest) { | 328 TEST_F(SpdyHttpStreamSpdy3Test, SpdyURLTest) { |
190 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; | 329 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; |
191 const char * const base_url = "http://www.google.com/foo?query=what"; | 330 const char * const base_url = "http://www.google.com/foo?query=what"; |
192 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); | 331 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); |
193 MockWrite writes[] = { | 332 MockWrite writes[] = { |
194 CreateMockWrite(*req.get(), 1), | 333 CreateMockWrite(*req.get(), 1), |
195 }; | 334 }; |
196 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 335 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
197 MockRead reads[] = { | 336 MockRead reads[] = { |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 | 665 |
527 sequenced_worker_pool->Shutdown(); | 666 sequenced_worker_pool->Shutdown(); |
528 } | 667 } |
529 | 668 |
530 #endif // !defined(USE_OPENSSL) | 669 #endif // !defined(USE_OPENSSL) |
531 | 670 |
532 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 671 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
533 // methods. | 672 // methods. |
534 | 673 |
535 } // namespace net | 674 } // namespace net |
OLD | NEW |