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