OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 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/url_request/url_request_http_job.h" | 5 #include "net/url_request/url_request_http_job.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <cstddef> | 9 #include <cstddef> |
10 #include <memory> | 10 #include <memory> |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 using ::testing::Return; | 60 using ::testing::Return; |
61 | 61 |
62 const char kSimpleGetMockWrite[] = | 62 const char kSimpleGetMockWrite[] = |
63 "GET / HTTP/1.1\r\n" | 63 "GET / HTTP/1.1\r\n" |
64 "Host: www.example.com\r\n" | 64 "Host: www.example.com\r\n" |
65 "Connection: keep-alive\r\n" | 65 "Connection: keep-alive\r\n" |
66 "User-Agent:\r\n" | 66 "User-Agent:\r\n" |
67 "Accept-Encoding: gzip, deflate\r\n" | 67 "Accept-Encoding: gzip, deflate\r\n" |
68 "Accept-Language: en-us,fr\r\n\r\n"; | 68 "Accept-Language: en-us,fr\r\n\r\n"; |
69 | 69 |
| 70 const char kSimpleHeadMockWrite[] = |
| 71 "HEAD / HTTP/1.1\r\n" |
| 72 "Host: www.example.com\r\n" |
| 73 "Connection: keep-alive\r\n" |
| 74 "User-Agent:\r\n" |
| 75 "Accept-Encoding: gzip, deflate\r\n" |
| 76 "Accept-Language: en-us,fr\r\n\r\n"; |
| 77 |
70 // Inherit from URLRequestHttpJob to expose the priority and some | 78 // Inherit from URLRequestHttpJob to expose the priority and some |
71 // other hidden functions. | 79 // other hidden functions. |
72 class TestURLRequestHttpJob : public URLRequestHttpJob { | 80 class TestURLRequestHttpJob : public URLRequestHttpJob { |
73 public: | 81 public: |
74 explicit TestURLRequestHttpJob(URLRequest* request) | 82 explicit TestURLRequestHttpJob(URLRequest* request) |
75 : URLRequestHttpJob(request, | 83 : URLRequestHttpJob(request, |
76 request->context()->network_delegate(), | 84 request->context()->network_delegate(), |
77 request->context()->http_user_agent_settings()), | 85 request->context()->http_user_agent_settings()), |
78 use_null_source_stream_(false) {} | 86 use_null_source_stream_(false) {} |
79 | 87 |
(...skipping 216 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), | 304 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
297 request->GetTotalSentBytes()); | 305 request->GetTotalSentBytes()); |
298 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), | 306 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), |
299 request->GetTotalReceivedBytes()); | 307 request->GetTotalReceivedBytes()); |
300 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), | 308 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
301 network_delegate_.total_network_bytes_sent()); | 309 network_delegate_.total_network_bytes_sent()); |
302 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), | 310 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), |
303 network_delegate_.total_network_bytes_received()); | 311 network_delegate_.total_network_bytes_received()); |
304 } | 312 } |
305 | 313 |
| 314 // Tests a successful HEAD request. |
| 315 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulHead) { |
| 316 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)}; |
| 317 MockRead reads[] = { |
| 318 MockRead("HTTP/1.1 200 OK\r\n" |
| 319 "Content-Length: 0\r\n\r\n")}; |
| 320 |
| 321 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 322 arraysize(writes)); |
| 323 socket_factory_.AddSocketDataProvider(&socket_data); |
| 324 |
| 325 TestDelegate delegate; |
| 326 std::unique_ptr<URLRequest> request = context_->CreateRequest( |
| 327 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate); |
| 328 |
| 329 request->set_method("HEAD"); |
| 330 request->Start(); |
| 331 ASSERT_TRUE(request->is_pending()); |
| 332 base::RunLoop().Run(); |
| 333 |
| 334 EXPECT_THAT(delegate.request_status(), IsOk()); |
| 335 EXPECT_EQ(0, request->received_response_content_length()); |
| 336 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 337 request->GetTotalSentBytes()); |
| 338 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), |
| 339 request->GetTotalReceivedBytes()); |
| 340 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 341 network_delegate_.total_network_bytes_sent()); |
| 342 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), |
| 343 network_delegate_.total_network_bytes_received()); |
| 344 } |
| 345 |
| 346 // Similar to above test but tests that even if response body is there in the |
| 347 // HEAD response stream, it should not be read due to HttpStreamParser's logic. |
| 348 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulHeadWithContent) { |
| 349 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)}; |
| 350 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 351 "Content-Length: 12\r\n\r\n"), |
| 352 MockRead("Test Content")}; |
| 353 |
| 354 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 355 arraysize(writes)); |
| 356 socket_factory_.AddSocketDataProvider(&socket_data); |
| 357 |
| 358 TestDelegate delegate; |
| 359 std::unique_ptr<URLRequest> request = context_->CreateRequest( |
| 360 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate); |
| 361 |
| 362 request->set_method("HEAD"); |
| 363 request->Start(); |
| 364 ASSERT_TRUE(request->is_pending()); |
| 365 base::RunLoop().Run(); |
| 366 |
| 367 EXPECT_THAT(delegate.request_status(), IsOk()); |
| 368 EXPECT_EQ(0, request->received_response_content_length()); |
| 369 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 370 request->GetTotalSentBytes()); |
| 371 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)) - 12, |
| 372 request->GetTotalReceivedBytes()); |
| 373 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 374 network_delegate_.total_network_bytes_sent()); |
| 375 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)) - 12, |
| 376 network_delegate_.total_network_bytes_received()); |
| 377 } |
| 378 |
| 379 TEST_F(URLRequestHttpJobWithMockSocketsTest, TestSuccessfulCachedHeadRequest) { |
| 380 // Cache the response. |
| 381 { |
| 382 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
| 383 MockRead reads[] = {MockRead("HTTP/1.1 200 OK\r\n" |
| 384 "Content-Length: 12\r\n\r\n"), |
| 385 MockRead("Test Content")}; |
| 386 |
| 387 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 388 arraysize(writes)); |
| 389 socket_factory_.AddSocketDataProvider(&socket_data); |
| 390 |
| 391 TestDelegate delegate; |
| 392 std::unique_ptr<URLRequest> request = context_->CreateRequest( |
| 393 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate); |
| 394 |
| 395 request->Start(); |
| 396 ASSERT_TRUE(request->is_pending()); |
| 397 base::RunLoop().Run(); |
| 398 |
| 399 EXPECT_THAT(delegate.request_status(), IsOk()); |
| 400 EXPECT_EQ(12, request->received_response_content_length()); |
| 401 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 402 request->GetTotalSentBytes()); |
| 403 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), |
| 404 request->GetTotalReceivedBytes()); |
| 405 EXPECT_EQ(CountWriteBytes(writes, arraysize(writes)), |
| 406 network_delegate_.total_network_bytes_sent()); |
| 407 EXPECT_EQ(CountReadBytes(reads, arraysize(reads)), |
| 408 network_delegate_.total_network_bytes_received()); |
| 409 } |
| 410 |
| 411 // Send a HEAD request for the cached response. |
| 412 { |
| 413 MockWrite writes[] = {MockWrite(kSimpleHeadMockWrite)}; |
| 414 MockRead reads[] = { |
| 415 MockRead("HTTP/1.1 200 OK\r\n" |
| 416 "Content-Length: 0\r\n\r\n")}; |
| 417 |
| 418 StaticSocketDataProvider socket_data(reads, arraysize(reads), writes, |
| 419 arraysize(writes)); |
| 420 socket_factory_.AddSocketDataProvider(&socket_data); |
| 421 |
| 422 TestDelegate delegate; |
| 423 std::unique_ptr<URLRequest> request = context_->CreateRequest( |
| 424 GURL("http://www.example.com"), DEFAULT_PRIORITY, &delegate); |
| 425 |
| 426 // Use the cached version. |
| 427 request->SetLoadFlags(LOAD_SKIP_CACHE_VALIDATION); |
| 428 request->set_method("HEAD"); |
| 429 request->Start(); |
| 430 ASSERT_TRUE(request->is_pending()); |
| 431 base::RunLoop().Run(); |
| 432 |
| 433 EXPECT_THAT(delegate.request_status(), IsOk()); |
| 434 EXPECT_EQ(0, request->received_response_content_length()); |
| 435 EXPECT_EQ(0, request->GetTotalSentBytes()); |
| 436 EXPECT_EQ(0, request->GetTotalReceivedBytes()); |
| 437 } |
| 438 } |
| 439 |
306 TEST_F(URLRequestHttpJobWithMockSocketsTest, | 440 TEST_F(URLRequestHttpJobWithMockSocketsTest, |
307 TestContentLengthSuccessfulHttp09Request) { | 441 TestContentLengthSuccessfulHttp09Request) { |
308 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; | 442 MockWrite writes[] = {MockWrite(kSimpleGetMockWrite)}; |
309 MockRead reads[] = {MockRead("Test Content"), | 443 MockRead reads[] = {MockRead("Test Content"), |
310 MockRead(net::SYNCHRONOUS, net::OK)}; | 444 MockRead(net::SYNCHRONOUS, net::OK)}; |
311 | 445 |
312 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); | 446 StaticSocketDataProvider socket_data(reads, arraysize(reads), nullptr, 0); |
313 socket_factory_.AddSocketDataProvider(&socket_data); | 447 socket_factory_.AddSocketDataProvider(&socket_data); |
314 | 448 |
315 TestDelegate delegate; | 449 TestDelegate delegate; |
(...skipping 892 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1208 base::RunLoop().RunUntilIdle(); | 1342 base::RunLoop().RunUntilIdle(); |
1209 EXPECT_THAT(delegate_.request_status(), IsError(ERR_IO_PENDING)); | 1343 EXPECT_THAT(delegate_.request_status(), IsError(ERR_IO_PENDING)); |
1210 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); | 1344 EXPECT_TRUE(fake_handshake_stream->initialize_stream_was_called()); |
1211 } | 1345 } |
1212 | 1346 |
1213 #endif // BUILDFLAG(ENABLE_WEBSOCKETS) | 1347 #endif // BUILDFLAG(ENABLE_WEBSOCKETS) |
1214 | 1348 |
1215 } // namespace | 1349 } // namespace |
1216 | 1350 |
1217 } // namespace net | 1351 } // namespace net |
OLD | NEW |