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 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 EXPECT_TRUE(data()->at_write_eof()); | 124 EXPECT_TRUE(data()->at_write_eof()); |
125 } | 125 } |
126 | 126 |
127 TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) { | 127 TEST_F(SpdyHttpStreamSpdy3Test, SendChunkedPost) { |
128 UploadDataStream::set_merge_chunks(false); | 128 UploadDataStream::set_merge_chunks(false); |
129 | 129 |
130 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); | 130 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
131 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); | 131 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
132 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); | 132 scoped_ptr<SpdyFrame> chunk2(ConstructSpdyBodyFrame(1, true)); |
133 MockWrite writes[] = { | 133 MockWrite writes[] = { |
134 CreateMockWrite(*req.get(), 1), | 134 CreateMockWrite(*req.get(), 0), |
135 CreateMockWrite(*chunk1, 2), // POST upload frames | 135 CreateMockWrite(*chunk1, 1), // POST upload frames |
136 CreateMockWrite(*chunk2, 3), | 136 CreateMockWrite(*chunk2, 2), |
137 }; | 137 }; |
138 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); | 138 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
139 MockRead reads[] = { | 139 MockRead reads[] = { |
140 CreateMockRead(*resp, 4), | 140 CreateMockRead(*resp, 3), |
141 CreateMockRead(*chunk1, 5), | 141 CreateMockRead(*chunk1, 4), |
142 CreateMockRead(*chunk2, 5), | 142 CreateMockRead(*chunk2, 5), |
143 MockRead(SYNCHRONOUS, 0, 6) // EOF | 143 MockRead(SYNCHRONOUS, 0, 6) // EOF |
144 }; | 144 }; |
145 | 145 |
146 HostPortPair host_port_pair("www.google.com", 80); | 146 HostPortPair host_port_pair("www.google.com", 80); |
147 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); | 147 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
148 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), | 148 EXPECT_EQ(OK, InitSession(reads, arraysize(reads), writes, arraysize(writes), |
149 host_port_pair)); | 149 host_port_pair)); |
150 | 150 |
151 HttpRequestInfo request; | 151 HttpRequestInfo request; |
(...skipping 26 matching lines...) Expand all Loading... |
178 data()->CompleteRead(); | 178 data()->CompleteRead(); |
179 MessageLoop::current()->RunAllPending(); | 179 MessageLoop::current()->RunAllPending(); |
180 | 180 |
181 // 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 |
182 // pool anymore. | 182 // pool anymore. |
183 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); | 183 EXPECT_FALSE(http_session_->spdy_session_pool()->HasSession(pair)); |
184 EXPECT_TRUE(data()->at_read_eof()); | 184 EXPECT_TRUE(data()->at_read_eof()); |
185 EXPECT_TRUE(data()->at_write_eof()); | 185 EXPECT_TRUE(data()->at_write_eof()); |
186 } | 186 } |
187 | 187 |
| 188 TEST_F(SpdyHttpStreamSpdy3Test, DelayedSendChunkedPost) { |
| 189 UploadDataStream::set_merge_chunks(false); |
| 190 |
| 191 const char kUploadData1[] = "12345678"; |
| 192 const int kUploadData1Size = arraysize(kUploadData1)-1; |
| 193 scoped_ptr<SpdyFrame> req(ConstructChunkedSpdyPost(NULL, 0)); |
| 194 scoped_ptr<SpdyFrame> chunk1(ConstructSpdyBodyFrame(1, false)); |
| 195 scoped_ptr<SpdyFrame> chunk2( |
| 196 ConstructSpdyBodyFrame(1, kUploadData1, kUploadData1Size, false)); |
| 197 scoped_ptr<SpdyFrame> chunk3(ConstructSpdyBodyFrame(1, true)); |
| 198 MockWrite writes[] = { |
| 199 CreateMockWrite(*req.get(), 0), |
| 200 CreateMockWrite(*chunk1, 1), // POST upload frames |
| 201 CreateMockWrite(*chunk2, 2), |
| 202 CreateMockWrite(*chunk3, 3), |
| 203 }; |
| 204 scoped_ptr<SpdyFrame> resp(ConstructSpdyPostSynReply(NULL, 0)); |
| 205 MockRead reads[] = { |
| 206 CreateMockRead(*resp, 4), |
| 207 CreateMockRead(*chunk1, 5), |
| 208 CreateMockRead(*chunk2, 6), |
| 209 CreateMockRead(*chunk3, 7), |
| 210 MockRead(ASYNC, 0, 8) // EOF |
| 211 }; |
| 212 |
| 213 HostPortPair host_port_pair("www.google.com", 80); |
| 214 HostPortProxyPair pair(host_port_pair, ProxyServer::Direct()); |
| 215 |
| 216 scoped_refptr<DeterministicSocketData> data( |
| 217 new DeterministicSocketData(reads, arraysize(reads), |
| 218 writes, arraysize(writes))); |
| 219 |
| 220 DeterministicMockClientSocketFactory* socket_factory = |
| 221 session_deps_.deterministic_socket_factory.get(); |
| 222 socket_factory->AddSocketDataProvider(data.get()); |
| 223 |
| 224 http_session_ = SpdySessionDependencies::SpdyCreateSessionDeterministic( |
| 225 &session_deps_); |
| 226 session_ = http_session_->spdy_session_pool()->Get(pair, BoundNetLog()); |
| 227 transport_params_ = new TransportSocketParams(host_port_pair, |
| 228 MEDIUM, false, false, |
| 229 OnHostResolutionCallback()); |
| 230 |
| 231 TestCompletionCallback callback; |
| 232 scoped_ptr<ClientSocketHandle> connection(new ClientSocketHandle); |
| 233 |
| 234 EXPECT_EQ(ERR_IO_PENDING, |
| 235 connection->Init(host_port_pair.ToString(), |
| 236 transport_params_, |
| 237 MEDIUM, |
| 238 callback.callback(), |
| 239 http_session_->GetTransportSocketPool( |
| 240 HttpNetworkSession::NORMAL_SOCKET_POOL), |
| 241 BoundNetLog())); |
| 242 |
| 243 callback.WaitForResult(); |
| 244 EXPECT_EQ(OK, |
| 245 session_->InitializeWithSocket(connection.release(), false, OK)); |
| 246 |
| 247 HttpRequestInfo request; |
| 248 request.method = "POST"; |
| 249 request.url = GURL("http://www.google.com/"); |
| 250 request.upload_data = new UploadData(); |
| 251 request.upload_data->set_is_chunked(true); |
| 252 |
| 253 BoundNetLog net_log; |
| 254 scoped_ptr<SpdyHttpStream> http_stream( |
| 255 new SpdyHttpStream(session_.get(), true)); |
| 256 ASSERT_EQ(OK, |
| 257 http_stream->InitializeStream(&request, |
| 258 net_log, |
| 259 CompletionCallback())); |
| 260 |
| 261 scoped_ptr<UploadDataStream> upload_stream( |
| 262 new UploadDataStream(request.upload_data)); |
| 263 ASSERT_EQ(OK, upload_stream->Init()); |
| 264 |
| 265 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, false); |
| 266 |
| 267 HttpRequestHeaders headers; |
| 268 HttpResponseInfo response; |
| 269 // This will attempt to Write() the initial request and headers, which will |
| 270 // complete asynchronously. |
| 271 EXPECT_EQ(ERR_IO_PENDING, |
| 272 http_stream->SendRequest(headers, |
| 273 upload_stream.Pass(), |
| 274 &response, |
| 275 callback.callback())); |
| 276 EXPECT_TRUE(http_session_->spdy_session_pool()->HasSession(pair)); |
| 277 |
| 278 // Complete the initial request write and the first chunk. |
| 279 data->RunFor(1); |
| 280 |
| 281 // Now append the second chunk. This will enqueue another write. |
| 282 request.upload_data->AppendChunk(kUploadData1, kUploadData1Size, false); |
| 283 |
| 284 // Complete the second chunk. |
| 285 data->RunFor(1); |
| 286 |
| 287 // Now append final chunk. This will enqueue another write. |
| 288 request.upload_data->AppendChunk(kUploadData, kUploadDataSize, true); |
| 289 |
| 290 // Finalize writing the last chunk, which will enqueue the trailer. |
| 291 data->RunFor(3); |
| 292 int rv = callback.WaitForResult(); |
| 293 EXPECT_GT(rv, 0); |
| 294 EXPECT_EQ(OK, http_stream->ReadResponseHeaders(callback.callback())); |
| 295 |
| 296 // Finish reading |chunk1|, |chunk2| and |chunk3|. |
| 297 data->RunFor(1); |
| 298 scoped_refptr<IOBuffer> buf1(new IOBuffer(kUploadDataSize)); |
| 299 http_stream->ReadResponseBody(buf1, kUploadDataSize, callback.callback()); |
| 300 EXPECT_EQ(kUploadData, std::string(buf1->data(), kUploadDataSize)); |
| 301 |
| 302 data->RunFor(1); |
| 303 scoped_refptr<IOBuffer> buf2(new IOBuffer(kUploadData1Size)); |
| 304 http_stream->ReadResponseBody(buf2, kUploadData1Size, callback.callback()); |
| 305 EXPECT_EQ(kUploadData1, std::string(buf2->data(), kUploadData1Size)); |
| 306 |
| 307 data->RunFor(1); |
| 308 scoped_refptr<IOBuffer> buf3(new IOBuffer(kUploadDataSize)); |
| 309 http_stream->ReadResponseBody(buf3, kUploadDataSize, callback.callback()); |
| 310 EXPECT_EQ(kUploadData, std::string(buf3->data(), kUploadDataSize)); |
| 311 |
| 312 // Finish reading the |EOF|. |
| 313 data->RunFor(1); |
| 314 ASSERT_TRUE(response.headers.get()); |
| 315 ASSERT_EQ(200, response.headers->response_code()); |
| 316 EXPECT_TRUE(data->at_read_eof()); |
| 317 EXPECT_TRUE(data->at_write_eof()); |
| 318 } |
| 319 |
188 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 | 320 // Test case for bug: http://code.google.com/p/chromium/issues/detail?id=50058 |
189 TEST_F(SpdyHttpStreamSpdy3Test, SpdyURLTest) { | 321 TEST_F(SpdyHttpStreamSpdy3Test, SpdyURLTest) { |
190 const char * const full_url = "http://www.google.com/foo?query=what#anchor"; | 322 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"; | 323 const char * const base_url = "http://www.google.com/foo?query=what"; |
192 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); | 324 scoped_ptr<SpdyFrame> req(ConstructSpdyGet(base_url, false, 1, LOWEST)); |
193 MockWrite writes[] = { | 325 MockWrite writes[] = { |
194 CreateMockWrite(*req.get(), 1), | 326 CreateMockWrite(*req.get(), 1), |
195 }; | 327 }; |
196 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); | 328 scoped_ptr<SpdyFrame> resp(ConstructSpdyGetSynReply(NULL, 0, 1)); |
197 MockRead reads[] = { | 329 MockRead reads[] = { |
(...skipping 328 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
526 | 658 |
527 sequenced_worker_pool->Shutdown(); | 659 sequenced_worker_pool->Shutdown(); |
528 } | 660 } |
529 | 661 |
530 #endif // !defined(USE_OPENSSL) | 662 #endif // !defined(USE_OPENSSL) |
531 | 663 |
532 // TODO(willchan): Write a longer test for SpdyStream that exercises all | 664 // TODO(willchan): Write a longer test for SpdyStream that exercises all |
533 // methods. | 665 // methods. |
534 | 666 |
535 } // namespace net | 667 } // namespace net |
OLD | NEW |