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_parser.h" | 5 #include "net/http/http_stream_parser.h" |
6 | 6 |
7 #include "base/file_util.h" | 7 #include "base/file_util.h" |
8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
9 #include "base/files/scoped_temp_dir.h" | 9 #include "base/files/scoped_temp_dir.h" |
10 #include "base/memory/ref_counted.h" | 10 #include "base/memory/ref_counted.h" |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
218 scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); | 218 scoped_ptr<ClientSocketHandle> socket_handle(new ClientSocketHandle); |
219 socket_handle->set_socket(transport.release()); | 219 socket_handle->set_socket(transport.release()); |
220 | 220 |
221 HttpRequestInfo request_info; | 221 HttpRequestInfo request_info; |
222 request_info.method = "GET"; | 222 request_info.method = "GET"; |
223 request_info.url = GURL("http://localhost"); | 223 request_info.url = GURL("http://localhost"); |
224 request_info.load_flags = LOAD_NORMAL; | 224 request_info.load_flags = LOAD_NORMAL; |
225 request_info.upload_data_stream = &upload_stream; | 225 request_info.upload_data_stream = &upload_stream; |
226 | 226 |
227 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 227 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
228 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer, | 228 HttpStreamParser parser( |
229 BoundNetLog()); | 229 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog()); |
230 | 230 |
231 HttpRequestHeaders request_headers; | 231 HttpRequestHeaders request_headers; |
232 request_headers.SetHeader("Host", "localhost"); | 232 request_headers.SetHeader("Host", "localhost"); |
233 request_headers.SetHeader("Transfer-Encoding", "chunked"); | 233 request_headers.SetHeader("Transfer-Encoding", "chunked"); |
234 request_headers.SetHeader("Connection", "keep-alive"); | 234 request_headers.SetHeader("Connection", "keep-alive"); |
235 | 235 |
236 HttpResponseInfo response_info; | 236 HttpResponseInfo response_info; |
237 // This will attempt to Write() the initial request and headers, which will | 237 // This will attempt to Write() the initial request and headers, which will |
238 // complete asynchronously. | 238 // complete asynchronously. |
239 rv = parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, | 239 rv = parser.SendRequest("GET /one.html HTTP/1.1\r\n", request_headers, |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
285 rv = parser.ReadResponseHeaders(callback.callback()); | 285 rv = parser.ReadResponseHeaders(callback.callback()); |
286 ASSERT_EQ(ERR_IO_PENDING, rv); | 286 ASSERT_EQ(ERR_IO_PENDING, rv); |
287 data.RunFor(2); | 287 data.RunFor(2); |
288 | 288 |
289 ASSERT_TRUE(callback.have_result()); | 289 ASSERT_TRUE(callback.have_result()); |
290 rv = callback.WaitForResult(); | 290 rv = callback.WaitForResult(); |
291 ASSERT_GT(rv, 0); | 291 ASSERT_GT(rv, 0); |
292 | 292 |
293 // Finally, attempt to read the response body. | 293 // Finally, attempt to read the response body. |
294 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); | 294 scoped_refptr<IOBuffer> body_buffer(new IOBuffer(kBodySize)); |
295 rv = parser.ReadResponseBody(body_buffer, kBodySize, callback.callback()); | 295 rv = parser.ReadResponseBody( |
| 296 body_buffer.get(), kBodySize, callback.callback()); |
296 ASSERT_EQ(ERR_IO_PENDING, rv); | 297 ASSERT_EQ(ERR_IO_PENDING, rv); |
297 data.RunFor(1); | 298 data.RunFor(1); |
298 | 299 |
299 ASSERT_TRUE(callback.have_result()); | 300 ASSERT_TRUE(callback.have_result()); |
300 rv = callback.WaitForResult(); | 301 rv = callback.WaitForResult(); |
301 ASSERT_EQ(kBodySize, rv); | 302 ASSERT_EQ(kBodySize, rv); |
302 } | 303 } |
303 | 304 |
304 TEST(HttpStreamParser, TruncatedHeaders) { | 305 TEST(HttpStreamParser, TruncatedHeaders) { |
305 MockRead truncated_status_reads[] = { | 306 MockRead truncated_status_reads[] = { |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
375 HttpRequestInfo request_info; | 376 HttpRequestInfo request_info; |
376 request_info.method = "GET"; | 377 request_info.method = "GET"; |
377 if (protocol == HTTP) { | 378 if (protocol == HTTP) { |
378 request_info.url = GURL("http://localhost"); | 379 request_info.url = GURL("http://localhost"); |
379 } else { | 380 } else { |
380 request_info.url = GURL("https://localhost"); | 381 request_info.url = GURL("https://localhost"); |
381 } | 382 } |
382 request_info.load_flags = LOAD_NORMAL; | 383 request_info.load_flags = LOAD_NORMAL; |
383 | 384 |
384 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); | 385 scoped_refptr<GrowableIOBuffer> read_buffer(new GrowableIOBuffer); |
385 HttpStreamParser parser(socket_handle.get(), &request_info, read_buffer, | 386 HttpStreamParser parser( |
386 BoundNetLog()); | 387 socket_handle.get(), &request_info, read_buffer.get(), BoundNetLog()); |
387 | 388 |
388 HttpRequestHeaders request_headers; | 389 HttpRequestHeaders request_headers; |
389 HttpResponseInfo response_info; | 390 HttpResponseInfo response_info; |
390 rv = parser.SendRequest("GET / HTTP/1.1\r\n", request_headers, | 391 rv = parser.SendRequest("GET / HTTP/1.1\r\n", request_headers, |
391 &response_info, callback.callback()); | 392 &response_info, callback.callback()); |
392 ASSERT_EQ(OK, rv); | 393 ASSERT_EQ(OK, rv); |
393 | 394 |
394 rv = parser.ReadResponseHeaders(callback.callback()); | 395 rv = parser.ReadResponseHeaders(callback.callback()); |
395 if (i == arraysize(reads) - 1) { | 396 if (i == arraysize(reads) - 1) { |
396 EXPECT_EQ(OK, rv); | 397 EXPECT_EQ(OK, rv); |
397 EXPECT_TRUE(response_info.headers.get()); | 398 EXPECT_TRUE(response_info.headers.get()); |
398 } else { | 399 } else { |
399 if (protocol == HTTP) { | 400 if (protocol == HTTP) { |
400 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); | 401 EXPECT_EQ(ERR_CONNECTION_CLOSED, rv); |
401 EXPECT_TRUE(response_info.headers.get()); | 402 EXPECT_TRUE(response_info.headers.get()); |
402 } else { | 403 } else { |
403 EXPECT_EQ(ERR_HEADERS_TRUNCATED, rv); | 404 EXPECT_EQ(ERR_HEADERS_TRUNCATED, rv); |
404 EXPECT_FALSE(response_info.headers.get()); | 405 EXPECT_FALSE(response_info.headers.get()); |
405 } | 406 } |
406 } | 407 } |
407 } | 408 } |
408 } | 409 } |
409 } | 410 } |
410 | 411 |
411 } // namespace net | 412 } // namespace net |
OLD | NEW |