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 "crypto/ec_private_key.h" | 7 #include "crypto/ec_private_key.h" |
8 #include "crypto/ec_signature_creator.h" | 8 #include "crypto/ec_signature_creator.h" |
9 #include "crypto/signature_creator.h" | 9 #include "crypto/signature_creator.h" |
10 #include "net/base/asn1_util.h" | 10 #include "net/base/asn1_util.h" |
(...skipping 13 matching lines...) Expand all Loading... | |
24 OrderedSocketData* data() { return data_.get(); } | 24 OrderedSocketData* data() { return data_.get(); } |
25 protected: | 25 protected: |
26 SpdyHttpStreamSpdy2Test() {} | 26 SpdyHttpStreamSpdy2Test() {} |
27 | 27 |
28 virtual void SetUp() { | 28 virtual void SetUp() { |
29 SpdySession::set_default_protocol(kProtoSPDY2); | 29 SpdySession::set_default_protocol(kProtoSPDY2); |
30 } | 30 } |
31 | 31 |
32 virtual void TearDown() { | 32 virtual void TearDown() { |
33 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); | 33 crypto::ECSignatureCreator::SetFactoryForTesting(NULL); |
34 UploadDataStream::ResetMergeChunks(); | |
34 MessageLoop::current()->RunAllPending(); | 35 MessageLoop::current()->RunAllPending(); |
35 } | 36 } |
36 | 37 |
38 void set_merge_chunks(bool merge) { | |
39 UploadDataStream::set_merge_chunks(merge); | |
40 } | |
41 | |
37 int InitSession(MockRead* reads, size_t reads_count, | 42 int InitSession(MockRead* reads, size_t reads_count, |
38 MockWrite* writes, size_t writes_count, | 43 MockWrite* writes, size_t writes_count, |
39 HostPortPair& host_port_pair) { | 44 HostPortPair& host_port_pair) { |
40 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 45 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
41 data_.reset(new OrderedSocketData(reads, reads_count, | 46 data_.reset(new OrderedSocketData(reads, reads_count, |
42 writes, writes_count)); | 47 writes, writes_count)); |
43 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); | 48 session_deps_.socket_factory->AddSocketDataProvider(data_.get()); |
44 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); | 49 http_session_ = SpdySessionDependencies::SpdyCreateSession(&session_deps_); |
45 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); | 50 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
46 transport_params_ = new TransportSocketParams(host_port_pair, | 51 transport_params_ = new TransportSocketParams(host_port_pair, |
(...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
111 data()->CompleteRead(); | 116 data()->CompleteRead(); |
112 | 117 |
113 // Because we abandoned the stream, we don't expect to find a session in the | 118 // Because we abandoned the stream, we don't expect to find a session in the |
114 // pool anymore. | 119 // pool anymore. |
115 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 120 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
116 EXPECT_TRUE(data()->at_read_eof()); | 121 EXPECT_TRUE(data()->at_read_eof()); |
117 EXPECT_TRUE(data()->at_write_eof()); | 122 EXPECT_TRUE(data()->at_write_eof()); |
118 } | 123 } |
119 | 124 |
120 TEST_F(SpdyHttpStreamSpdy2Test, SendChunkedPost) { | 125 TEST_F(SpdyHttpStreamSpdy2Test, SendChunkedPost) { |
121 UploadDataStream::set_merge_chunks(false); | 126 // Disable merging of chunks until we have a better way to unit test POST |
127 // requests with chunked uploads. | |
Ryan Hamilton
2012/07/11 17:11:19
Please remove this comment. (If you really feel t
ramant (doing other things)
2012/07/11 19:40:43
Done.
| |
128 set_merge_chunks(false); | |
122 | 129 |
123 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); | 130 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
124 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); | 131 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
125 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); | 132 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); |
126 MockWrite writes[] = { | 133 MockWrite writes[] = { |
127 CreateMockWrite(*req.get(), 1), | 134 CreateMockWrite(*req.get(), 0), |
128 CreateMockWrite(*chunk1, 2), // POST upload frames | 135 CreateMockWrite(*chunk1, 1), // POST upload frames |
129 CreateMockWrite(*chunk2, 3), | 136 CreateMockWrite(*chunk2, 2), |
130 }; | 137 }; |
131 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); | 138 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
132 MockRead reads[] = { | 139 MockRead reads[] = { |
133 CreateMockRead(*resp, 4), | 140 CreateMockRead(*resp, 3), |
134 CreateMockRead(*chunk1, 5), | 141 CreateMockRead(*chunk1, 4), |
135 CreateMockRead(*chunk2, 5), | 142 CreateMockRead(*chunk2, 5), |
136 MockRead(SYNCHRONOUS, 0, 6) // EOF | 143 MockRead(SYNCHRONOUS, 0, 6) // EOF |
137 }; | 144 }; |
138 | 145 |
139 HostPortPair host_port_pair("www.google.com", 80); | 146 HostPortPair host_port_pair("www.google.com", 80); |
140 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 147 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
141 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), | 148 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), |
142 host_port_pair)); | 149 host_port_pair)); |
143 | 150 |
144 HttpRequestInfo request; | 151 HttpRequestInfo request; |
(...skipping 26 matching lines...) Expand all Loading... | |
171 data()->CompleteRead(); | 178 data()->CompleteRead(); |
172 MessageLoop::current()->RunAllPending(); | 179 MessageLoop::current()->RunAllPending(); |
173 | 180 |
174 // Because we abandoned the stream, we don't expect to find a session in the | 181 // Because we abandoned the stream, we don't expect to find a session in the |
175 // pool anymore. | 182 // pool anymore. |
176 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 183 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
177 EXPECT_TRUE(data()->at_read_eof()); | 184 EXPECT_TRUE(data()->at_read_eof()); |
178 EXPECT_TRUE(data()->at_write_eof()); | 185 EXPECT_TRUE(data()->at_write_eof()); |
179 } | 186 } |
180 | 187 |
188 TEST_F(SpdyHttpStreamSpdy2Test, DelayedSendChunkedPost) { | |
Ryan Hamilton
2012/07/11 17:11:19
Please add a comment before this tests that explai
ramant (doing other things)
2012/07/11 19:40:43
Done.
| |
189 // Disable merging of chunks until we have a better way to unit test POST | |
190 // requests with chunked uploads. | |
191 set_merge_chunks(false); | |
192 | |
193 const char kUploadData1[] = "12345678"; | |
194 const int kUploadData1Size = arraysize(kUploadData1)-1; | |
195 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); | |
196 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); | |
197 scoped_ptr<SpdyFrame> chunk2( | |
198 ConstructSpdyBodyFrame(1, kUploadData1, kUploadData1Size, false)); | |
199 scoped_ptr<SpdyFrame> chunk3(ConstructSpdyBodyFrame(1, true)); | |
200 MockWrite writes[] = { | |
201 CreateMockWrite(*req.get(), 0), | |
202 CreateMockWrite(*chunk1, 1), // POST upload frames | |
203 CreateMockWrite(*chunk2, 2), | |
204 CreateMockWrite(*chunk3, 3), | |
205 }; | |
206 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); | |
207 MockRead reads[] = { | |
208 CreateMockRead(*resp, 4), | |
209 CreateMockRead(*chunk1, 5), | |
210 CreateMockRead(*chunk2, 6), | |
211 CreateMockRead(*chunk3, 7), | |
212 MockRead(ASYNC, 0, 8) // EOF | |
213 }; | |
214 | |
215 HostPortPair host_port_pair("www.google.com", 80); | |
216 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | |
217 | |
218 scoped_ptr<DeterministicSocketData> data( | |
219 new DeterministicSocketData(reads, arraysize(reads), | |
220 writes, arraysize(writes))); | |
221 | |
222 DeterministicMockClientSocketFactory* socket_factory = | |
223 session_deps_.deterministic_socket_factory.get(); | |
224 socket_factory->AddSocketDataProvider(data.get()); | |
225 | |
226 http_session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic( | |
227 &session_deps_); | |
228 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); | |
229 transport_params_ = new TransportSocketParams(host_port_pair, | |
230 MEDIUM, false, false, | |
231 OnHostResolutionCallback()); | |
232 | |
233 TestCompletionCallback callback; | |
234 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); | |
235 | |
236 EXPECT_EQ(ERR_IO_PENDING, | |
237 connection->Init(host_port_pair.ToString(), | |
238 transport_params_, | |
239 MEDIUM, | |
240 callback.callback(), | |
241 http_session_->GetTransportSocketPool( | |
242 HttpNetworkSession::NORMAL_SOCKET_POOL), | |
243 BoundNetLog())); | |
244 | |
245 callback.WaitForResult(); | |
246 EXPECT_EQ(OK, | |
247 session_->InitializeWithSocket(connection.release(), false, OK)); | |
248 | |
249 HttpRequestInfo request; | |
250 request.method = "POST"; | |
251 request.url = GURL("http://www.google.com/"); | |
252 request.upload_data = new UploadData(); | |
253 request.upload_data->set_is_chunked(true); | |
254 | |
255 BoundNetLog net_log; | |
256 scoped_ptr<SpdyHttpStream> http_stream( | |
257 new SpdyHttpStream(session_.get(), true)); | |
258 ASSERT_EQ(OK, | |
259 http_stream->InitializeStream(&request, | |
260 net_log, | |
261 CompletionCallback())); | |
262 | |
263 scoped_ptr<UploadDataStream> upload_stream( | |
264 new UploadDataStream(request.upload_data)); | |
265 ASSERT_EQ(OK, upload_stream->Init()); | |
266 | |
267 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, false); | |
268 | |
269 HttpRequestHeaders headers; | |
270 HttpResponseInfo response; | |
271 // This will attempt to Write() the initial request and headers, which will | |
272 // complete asynchronously. | |
273 EXPECT_EQ(ERR_IO_PENDING, | |
274 http_stream->SendRequest(headers, | |
275 upload_stream.Pass(), | |
276 &response, | |
277 callback.callback())); | |
278 EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(pair)); | |
279 | |
280 // Complete the initial request write and the first chunk. | |
281 data->RunFor(2); | |
282 ASSERT_TRUE(callback.have_result()); | |
283 | |
284 // Now append the second chunk. This will enqueue another write. | |
285 request.upload_data->AppendChunk(kUploadData1, kUploadData1Size, false); | |
286 ASSERT_TRUE(callback.have_result()); | |
Ryan Sleevi
2012/07/11 17:43:25
drop this
ramant (doing other things)
2012/07/11 19:40:43
Done.
| |
287 | |
288 // Now append final chunk. This will enqueue another write. | |
289 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, true); | |
290 ASSERT_TRUE(callback.have_result()); | |
Ryan Sleevi
2012/07/11 17:43:25
drop this
ramant (doing other things)
2012/07/11 19:40:43
Done.
| |
291 | |
292 // Finish writing all the chunks. | |
293 data->RunFor(2); | |
294 ASSERT_TRUE(callback.have_result()); | |
Ryan Sleevi
2012/07/11 17:43:25
drop this
ramant (doing other things)
2012/07/11 19:40:43
Done.
| |
295 | |
296 // Read response headers. | |
297 data->RunFor(1); | |
298 int rv = callback.WaitForResult(); | |
299 EXPECT_GT(rv, 0); | |
Ryan Sleevi
2012/07/11 17:43:25
move 298+299 -> 283
ramant (doing other things)
2012/07/11 19:40:43
Done.
| |
300 EXPECT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); | |
Ryan Sleevi
2012/07/11 17:43:25
ASSERT_EQ
ramant (doing other things)
2012/07/11 19:40:43
Done.
| |
301 | |
302 // Read and check |chunk1| response. | |
303 data->RunFor(1); | |
304 scoped_refptr<IOBuffer> buf1(new IOBuffer(kUploadDataSize)); | |
305 rv = http_stream->ReadResponseBody(buf1, | |
306 kUploadDataSize, | |
307 callback.callback()); | |
308 ASSERT_EQ(kUploadDataSize, rv); | |
309 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); | |
310 | |
311 // Read and check |chunk2| response. | |
312 data->RunFor(1); | |
313 scoped_refptr<IOBuffer> buf2(new IOBuffer(kUploadData1Size)); | |
314 rv = http_stream->ReadResponseBody(buf2, | |
315 kUploadData1Size, | |
316 callback.callback()); | |
317 ASSERT_EQ(kUploadData1Size, rv); | |
318 EXPECT_EQ(kUploadData1, std::string(buf2->data(), kUploadData1Size)); | |
319 | |
320 // Read and check |chunk3| response. | |
321 data->RunFor(1); | |
322 scoped_refptr<IOBuffer> buf3(new IOBuffer(kUploadDataSize)); | |
323 rv = http_stream->ReadResponseBody(buf3, | |
324 kUploadDataSize, | |
325 callback.callback()); | |
326 ASSERT_EQ(kUploadDataSize, rv); | |
327 EXPECT_EQ(kUploadData, std::string(buf3->data(), kUploadDataSize)); | |
328 | |
329 // Finish reading the |EOF|. | |
330 data->RunFor(1); | |
331 ASSERT_TRUE(response.headers.get()); | |
332 ASSERT_EQ(200, response.headers->response_code()); | |
333 EXPECT_TRUE(data->at_read_eof()); | |
334 EXPECT_TRUE(data->at_write_eof()); | |
335 } | |
336 | |
181 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 | 337 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 |
182 TEST_F(SpdyHttpStreamSpdy2Test, SpdyURLTest) { | 338 TEST_F(SpdyHttpStreamSpdy2Test, SpdyURLTest) { |
183 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; | 339 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; |
184 const char * const base_url = "http://www.google.com/foo?query=what"; | 340 const char * const base_url = "http://www.google.com/foo?query=what"; |
185 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); | 341 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); |
186 MockWrite writes[] = { | 342 MockWrite writes[] = { |
187 CreateMockWrite(*req.get(), 1), | 343 CreateMockWrite(*req.get(), 1), |
188 }; | 344 }; |
189 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 345 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
190 MockRead reads[] = { | 346 MockRead reads[] = { |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
231 // pool anymore. | 387 // pool anymore. |
232 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 388 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
233 EXPECT_TRUE(data()->at_read_eof()); | 389 EXPECT_TRUE(data()->at_read_eof()); |
234 EXPECT_TRUE(data()->at_write_eof()); | 390 EXPECT_TRUE(data()->at_write_eof()); |
235 } | 391 } |
236 | 392 |
237 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 393 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
238 // methods. | 394 // methods. |
239 | 395 |
240 } // namespace net | 396 } // namespace net |
OLD | NEW |