OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 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/http/http_network_transaction.h" | 5 #include "net/http/http_network_transaction.h" |
6 | 6 |
7 #include <math.h> // ceil | 7 #include <math.h> // ceil |
8 #include <stdarg.h> | 8 #include <stdarg.h> |
9 #include <string> | 9 #include <string> |
10 #include <vector> | 10 #include <vector> |
(...skipping 386 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 | 397 |
398 INSTANTIATE_TEST_CASE_P( | 398 INSTANTIATE_TEST_CASE_P( |
399 NextProto, | 399 NextProto, |
400 HttpNetworkTransactionTest, | 400 HttpNetworkTransactionTest, |
401 testing::Values(kProtoDeprecatedSPDY2, | 401 testing::Values(kProtoDeprecatedSPDY2, |
402 kProtoSPDY3, kProtoSPDY31, kProtoSPDY4a2, | 402 kProtoSPDY3, kProtoSPDY31, kProtoSPDY4a2, |
403 kProtoHTTP2Draft04)); | 403 kProtoHTTP2Draft04)); |
404 | 404 |
405 namespace { | 405 namespace { |
406 | 406 |
| 407 class BeforeNetworkStartHandler { |
| 408 public: |
| 409 explicit BeforeNetworkStartHandler(bool defer) |
| 410 : defer_on_before_network_start_(defer), |
| 411 observed_before_network_start_(false) {} |
| 412 |
| 413 void OnBeforeNetworkStart(bool* defer) { |
| 414 *defer = defer_on_before_network_start_; |
| 415 observed_before_network_start_ = true; |
| 416 } |
| 417 |
| 418 bool observed_before_network_start() const { |
| 419 return observed_before_network_start_; |
| 420 } |
| 421 |
| 422 private: |
| 423 const bool defer_on_before_network_start_; |
| 424 bool observed_before_network_start_; |
| 425 |
| 426 DISALLOW_COPY_AND_ASSIGN(BeforeNetworkStartHandler); |
| 427 }; |
| 428 |
407 // Fill |str| with a long header list that consumes >= |size| bytes. | 429 // Fill |str| with a long header list that consumes >= |size| bytes. |
408 void FillLargeHeadersString(std::string* str, int size) { | 430 void FillLargeHeadersString(std::string* str, int size) { |
409 const char* row = | 431 const char* row = |
410 "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; | 432 "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; |
411 const int sizeof_row = strlen(row); | 433 const int sizeof_row = strlen(row); |
412 const int num_rows = static_cast<int>( | 434 const int num_rows = static_cast<int>( |
413 ceil(static_cast<float>(size) / sizeof_row)); | 435 ceil(static_cast<float>(size) / sizeof_row)); |
414 const int sizeof_data = num_rows * sizeof_row; | 436 const int sizeof_data = num_rows * sizeof_row; |
415 DCHECK(sizeof_data >= size); | 437 DCHECK(sizeof_data >= size); |
416 str->reserve(sizeof_data); | 438 str->reserve(sizeof_data); |
(...skipping 919 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1336 MockRead(SYNCHRONOUS, OK), // EOF | 1358 MockRead(SYNCHRONOUS, OK), // EOF |
1337 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used | 1359 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used |
1338 MockRead("hello world"), | 1360 MockRead("hello world"), |
1339 MockRead(SYNCHRONOUS, OK), | 1361 MockRead(SYNCHRONOUS, OK), |
1340 }; | 1362 }; |
1341 SimpleGetHelperResult out = SimpleGetHelper(data_reads, | 1363 SimpleGetHelperResult out = SimpleGetHelper(data_reads, |
1342 arraysize(data_reads)); | 1364 arraysize(data_reads)); |
1343 EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); | 1365 EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); |
1344 } | 1366 } |
1345 | 1367 |
| 1368 // Test that network access can be deferred and resumed. |
| 1369 TEST_P(HttpNetworkTransactionTest, ThrottleBeforeNetworkStart) { |
| 1370 HttpRequestInfo request; |
| 1371 request.method = "GET"; |
| 1372 request.url = GURL("http://www.google.com/"); |
| 1373 request.load_flags = 0; |
| 1374 |
| 1375 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1376 scoped_ptr<HttpTransaction> trans( |
| 1377 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 1378 |
| 1379 // Defer on OnBeforeNetworkStart. |
| 1380 BeforeNetworkStartHandler net_start_handler(true); // defer |
| 1381 trans->SetBeforeNetworkStartCallback( |
| 1382 base::Bind(&BeforeNetworkStartHandler::OnBeforeNetworkStart, |
| 1383 base::Unretained(&net_start_handler))); |
| 1384 |
| 1385 MockRead data_reads[] = { |
| 1386 MockRead("HTTP/1.0 200 OK\r\n"), |
| 1387 MockRead("Content-Length: 5\r\n\r\n"), |
| 1388 MockRead("hello"), |
| 1389 MockRead(SYNCHRONOUS, 0), |
| 1390 }; |
| 1391 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0); |
| 1392 session_deps_.socket_factory->AddSocketDataProvider(&data); |
| 1393 |
| 1394 TestCompletionCallback callback; |
| 1395 |
| 1396 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 1397 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1398 base::MessageLoop::current()->RunUntilIdle(); |
| 1399 |
| 1400 // Should have deferred for network start. |
| 1401 EXPECT_TRUE(net_start_handler.observed_before_network_start()); |
| 1402 EXPECT_EQ(LOAD_STATE_WAITING_FOR_DELEGATE, trans->GetLoadState()); |
| 1403 EXPECT_TRUE(trans->GetResponseInfo() == NULL); |
| 1404 |
| 1405 trans->ResumeNetworkStart(); |
| 1406 rv = callback.WaitForResult(); |
| 1407 EXPECT_EQ(OK, rv); |
| 1408 EXPECT_TRUE(trans->GetResponseInfo() != NULL); |
| 1409 |
| 1410 scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(100)); |
| 1411 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback()); |
| 1412 if (rv == ERR_IO_PENDING) |
| 1413 rv = callback.WaitForResult(); |
| 1414 EXPECT_EQ(5, rv); |
| 1415 trans.reset(); |
| 1416 } |
| 1417 |
| 1418 // Test that network use can be deferred and canceled. |
| 1419 TEST_P(HttpNetworkTransactionTest, ThrottleAndCancelBeforeNetworkStart) { |
| 1420 HttpRequestInfo request; |
| 1421 request.method = "GET"; |
| 1422 request.url = GURL("http://www.google.com/"); |
| 1423 request.load_flags = 0; |
| 1424 |
| 1425 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_)); |
| 1426 scoped_ptr<HttpTransaction> trans( |
| 1427 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get())); |
| 1428 |
| 1429 // Defer on OnBeforeNetworkStart. |
| 1430 BeforeNetworkStartHandler net_start_handler(true); // defer |
| 1431 trans->SetBeforeNetworkStartCallback( |
| 1432 base::Bind(&BeforeNetworkStartHandler::OnBeforeNetworkStart, |
| 1433 base::Unretained(&net_start_handler))); |
| 1434 |
| 1435 TestCompletionCallback callback; |
| 1436 |
| 1437 int rv = trans->Start(&request, callback.callback(), BoundNetLog()); |
| 1438 EXPECT_EQ(ERR_IO_PENDING, rv); |
| 1439 base::MessageLoop::current()->RunUntilIdle(); |
| 1440 |
| 1441 // Should have deferred for network start. |
| 1442 EXPECT_TRUE(net_start_handler.observed_before_network_start()); |
| 1443 EXPECT_EQ(LOAD_STATE_WAITING_FOR_DELEGATE, trans->GetLoadState()); |
| 1444 EXPECT_TRUE(trans->GetResponseInfo() == NULL); |
| 1445 } |
| 1446 |
1346 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression | 1447 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression |
1347 // tests. There was a bug causing HttpNetworkTransaction to hang in the | 1448 // tests. There was a bug causing HttpNetworkTransaction to hang in the |
1348 // destructor in such situations. | 1449 // destructor in such situations. |
1349 // See http://crbug.com/154712 and http://crbug.com/156609. | 1450 // See http://crbug.com/154712 and http://crbug.com/156609. |
1350 TEST_P(HttpNetworkTransactionTest, KeepAliveEarlyClose) { | 1451 TEST_P(HttpNetworkTransactionTest, KeepAliveEarlyClose) { |
1351 HttpRequestInfo request; | 1452 HttpRequestInfo request; |
1352 request.method = "GET"; | 1453 request.method = "GET"; |
1353 request.url = GURL("http://www.google.com/"); | 1454 request.url = GURL("http://www.google.com/"); |
1354 request.load_flags = 0; | 1455 request.load_flags = 0; |
1355 | 1456 |
(...skipping 10977 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
12333 // established, to let the HTTP request start. | 12434 // established, to let the HTTP request start. |
12334 ASSERT_EQ(OK, http_callback.WaitForResult()); | 12435 ASSERT_EQ(OK, http_callback.WaitForResult()); |
12335 std::string response_data; | 12436 std::string response_data; |
12336 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); | 12437 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); |
12337 EXPECT_EQ("falafel", response_data); | 12438 EXPECT_EQ("falafel", response_data); |
12338 | 12439 |
12339 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); | 12440 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); |
12340 } | 12441 } |
12341 | 12442 |
12342 } // namespace net | 12443 } // namespace net |
OLD | NEW |