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