Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(260)

Side by Side Diff: net/http/http_network_transaction_unittest.cc

Issue 122453002: Allows deferral of a URLRequest just before talking to the network, at (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Formatting nits Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 BeforeNetworkStartHandler(bool defer)
mmenke 2014/01/07 15:26:23 explicit
jkarlin 2014/01/07 16:19:32 Done.
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 bool defer_on_before_network_start_;
mmenke 2014/01/07 15:26:23 const?
jkarlin 2014/01/07 16:19:32 Done.
424 bool observed_before_network_start_;
mmenke 2014/01/07 15:26:23 DISALLOW_COPY_AND_ASSIGN?
jkarlin 2014/01/07 16:19:32 Done.
425 };
426
407 // Fill |str| with a long header list that consumes >= |size| bytes. 427 // Fill |str| with a long header list that consumes >= |size| bytes.
408 void FillLargeHeadersString(std::string* str, int size) { 428 void FillLargeHeadersString(std::string* str, int size) {
409 const char* row = 429 const char* row =
410 "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n"; 430 "SomeHeaderName: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\r\n";
411 const int sizeof_row = strlen(row); 431 const int sizeof_row = strlen(row);
412 const int num_rows = static_cast<int>( 432 const int num_rows = static_cast<int>(
413 ceil(static_cast<float>(size) / sizeof_row)); 433 ceil(static_cast<float>(size) / sizeof_row));
414 const int sizeof_data = num_rows * sizeof_row; 434 const int sizeof_data = num_rows * sizeof_row;
415 DCHECK(sizeof_data >= size); 435 DCHECK(sizeof_data >= size);
416 str->reserve(sizeof_data); 436 str->reserve(sizeof_data);
(...skipping 919 matching lines...) Expand 10 before | Expand all | Expand 10 after
1336 MockRead(SYNCHRONOUS, OK), // EOF 1356 MockRead(SYNCHRONOUS, OK), // EOF
1337 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used 1357 MockRead("HTTP/1.0 200 OK\r\n\r\n"), // Should not be used
1338 MockRead("hello world"), 1358 MockRead("hello world"),
1339 MockRead(SYNCHRONOUS, OK), 1359 MockRead(SYNCHRONOUS, OK),
1340 }; 1360 };
1341 SimpleGetHelperResult out = SimpleGetHelper(data_reads, 1361 SimpleGetHelperResult out = SimpleGetHelper(data_reads,
1342 arraysize(data_reads)); 1362 arraysize(data_reads));
1343 EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv); 1363 EXPECT_EQ(ERR_EMPTY_RESPONSE, out.rv);
1344 } 1364 }
1345 1365
1366 // Test that network access can be deferred and resumed.
1367 TEST_P(HttpNetworkTransactionTest, ThrottleBeforeNetworkStart) {
1368 HttpRequestInfo request;
1369 request.method = "GET";
1370 request.url = GURL("http://www.google.com/");
1371 request.load_flags = 0;
1372
1373 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
1374 scoped_ptr<HttpTransaction> trans(
1375 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
1376
1377 // Defer on OnBeforeNetworkStart.
1378 BeforeNetworkStartHandler net_start_handler(true); // defer
1379 trans->SetBeforeNetworkStartCallback(
1380 base::Bind(&BeforeNetworkStartHandler::OnBeforeNetworkStart,
1381 base::Unretained(&net_start_handler)));
1382
1383 MockRead data_reads[] = {MockRead("HTTP/1.0 200 OK\r\n"),
1384 MockRead("Connection: keep-alive\r\n"),
1385 MockRead("Content-Length: 100\r\n\r\n"),
1386 MockRead("hello"),
1387 MockRead(SYNCHRONOUS, 0), };
1388 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0);
1389 session_deps_.socket_factory->AddSocketDataProvider(&data);
1390
1391 TestCompletionCallback callback;
1392
1393 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
1394 EXPECT_EQ(ERR_IO_PENDING, rv);
1395 base::MessageLoop::current()->RunUntilIdle();
1396
1397 // Should have deferred for network start.
1398 EXPECT_TRUE(net_start_handler.observed_before_network_start());
1399 EXPECT_EQ(LOAD_STATE_WAITING_FOR_DELEGATE, trans->GetLoadState());
1400 EXPECT_TRUE(trans->GetResponseInfo() == NULL);
1401
1402 trans->ResumeNetworkStart();
1403 rv = callback.WaitForResult();
1404 EXPECT_EQ(OK, rv);
1405 EXPECT_TRUE(trans->GetResponseInfo() != NULL);
1406
1407 scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(100));
1408 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback());
1409 if (rv == ERR_IO_PENDING)
1410 rv = callback.WaitForResult();
1411 EXPECT_EQ(5, rv);
1412 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback());
1413 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, rv);
1414
1415 trans.reset();
1416 base::MessageLoop::current()->RunUntilIdle();
1417 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get()));
1418 }
1419
1420 // Test that network use can be deferred and canceled.
1421 TEST_P(HttpNetworkTransactionTest, ThrottleAndCancelBeforeNetworkStart) {
1422 HttpRequestInfo request;
1423 request.method = "GET";
1424 request.url = GURL("http://www.google.com/");
1425 request.load_flags = 0;
1426
1427 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
1428 scoped_ptr<HttpTransaction> trans(
1429 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
1430
1431 // Defer on OnBeforeNetworkStart.
1432 BeforeNetworkStartHandler net_start_handler(true); // defer
1433 trans->SetBeforeNetworkStartCallback(
1434 base::Bind(&BeforeNetworkStartHandler::OnBeforeNetworkStart,
1435 base::Unretained(&net_start_handler)));
1436
1437 MockRead data_reads[] = {MockRead("HTTP/1.0 200 OK\r\n"),
1438 MockRead("Connection: keep-alive\r\n"),
1439 MockRead("Content-Length: 100\r\n\r\n"),
1440 MockRead("hello"),
1441 MockRead(SYNCHRONOUS, 0), };
1442 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0);
1443 session_deps_.socket_factory->AddSocketDataProvider(&data);
1444
1445 TestCompletionCallback callback;
1446
1447 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
1448 EXPECT_EQ(ERR_IO_PENDING, rv);
1449 base::MessageLoop::current()->RunUntilIdle();
1450
1451 // Should have deferred for network start.
1452 EXPECT_TRUE(net_start_handler.observed_before_network_start());
1453 EXPECT_EQ(LOAD_STATE_WAITING_FOR_DELEGATE, trans->GetLoadState());
1454 EXPECT_TRUE(trans->GetResponseInfo() == NULL);
1455
1456 trans.reset(); // Cancel the transaction here.
1457 base::MessageLoop::current()->RunUntilIdle();
mmenke 2014/01/07 15:26:23 Are these needed? TearDown() already does its fai
jkarlin 2014/01/07 16:19:32 Fair enough. Removed.
1458 }
1459
1346 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression 1460 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression
1347 // tests. There was a bug causing HttpNetworkTransaction to hang in the 1461 // tests. There was a bug causing HttpNetworkTransaction to hang in the
1348 // destructor in such situations. 1462 // destructor in such situations.
1349 // See http://crbug.com/154712 and http://crbug.com/156609. 1463 // See http://crbug.com/154712 and http://crbug.com/156609.
1350 TEST_P(HttpNetworkTransactionTest, KeepAliveEarlyClose) { 1464 TEST_P(HttpNetworkTransactionTest, KeepAliveEarlyClose) {
1351 HttpRequestInfo request; 1465 HttpRequestInfo request;
1352 request.method = "GET"; 1466 request.method = "GET";
1353 request.url = GURL("http://www.google.com/"); 1467 request.url = GURL("http://www.google.com/");
1354 request.load_flags = 0; 1468 request.load_flags = 0;
1355 1469
(...skipping 10977 matching lines...) Expand 10 before | Expand all | Expand 10 after
12333 // established, to let the HTTP request start. 12447 // established, to let the HTTP request start.
12334 ASSERT_EQ(OK, http_callback.WaitForResult()); 12448 ASSERT_EQ(OK, http_callback.WaitForResult());
12335 std::string response_data; 12449 std::string response_data;
12336 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); 12450 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data));
12337 EXPECT_EQ("falafel", response_data); 12451 EXPECT_EQ("falafel", response_data);
12338 12452
12339 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); 12453 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session));
12340 } 12454 }
12341 12455
12342 } // namespace net 12456 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698