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

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: Potential fix of a memory leak due to a reference cycle. 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 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
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[] = {MockRead("HTTP/1.0 200 OK\r\n"),
1386 MockRead("Connection: keep-alive\r\n"),
1387 MockRead("Content-Length: 100\r\n\r\n"),
1388 MockRead("hello"),
1389 MockRead(SYNCHRONOUS, 0), };
mmenke 2014/01/07 19:44:05 optional nit (x2): I don't think I've seen this d
jkarlin 2014/01/07 20:10:03 Huh, I wonder what's up with clang-format.
1390 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0);
1391 session_deps_.socket_factory->AddSocketDataProvider(&data);
1392
1393 TestCompletionCallback callback;
1394
1395 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
1396 EXPECT_EQ(ERR_IO_PENDING, rv);
1397 base::MessageLoop::current()->RunUntilIdle();
1398
1399 // Should have deferred for network start.
1400 EXPECT_TRUE(net_start_handler.observed_before_network_start());
1401 EXPECT_EQ(LOAD_STATE_WAITING_FOR_DELEGATE, trans->GetLoadState());
1402 EXPECT_TRUE(trans->GetResponseInfo() == NULL);
1403
1404 trans->ResumeNetworkStart();
1405 rv = callback.WaitForResult();
1406 EXPECT_EQ(OK, rv);
1407 EXPECT_TRUE(trans->GetResponseInfo() != NULL);
1408
1409 scoped_refptr<IOBufferWithSize> io_buf(new IOBufferWithSize(100));
1410 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback());
1411 if (rv == ERR_IO_PENDING)
1412 rv = callback.WaitForResult();
1413 EXPECT_EQ(5, rv);
1414 rv = trans->Read(io_buf.get(), io_buf->size(), callback.callback());
1415 EXPECT_EQ(ERR_CONTENT_LENGTH_MISMATCH, rv);
mmenke 2014/01/07 19:44:05 Any reason not to use a success case instead? (OR
jkarlin 2014/01/07 20:10:03 Done.
1416
1417 trans.reset();
1418 base::MessageLoop::current()->RunUntilIdle();
1419 EXPECT_EQ(0, GetIdleSocketCountInTransportSocketPool(session.get()));
mmenke 2014/01/07 19:44:05 Why are we checking this?
jkarlin 2014/01/07 20:10:03 Done.
jkarlin 2014/01/07 20:10:03 Done.
1420 }
1421
1422 // Test that network use can be deferred and canceled.
1423 TEST_P(HttpNetworkTransactionTest, ThrottleAndCancelBeforeNetworkStart) {
1424 HttpRequestInfo request;
1425 request.method = "GET";
1426 request.url = GURL("http://www.google.com/");
1427 request.load_flags = 0;
1428
1429 scoped_refptr<HttpNetworkSession> session(CreateSession(&session_deps_));
1430 scoped_ptr<HttpTransaction> trans(
1431 new HttpNetworkTransaction(DEFAULT_PRIORITY, session.get()));
1432
1433 // Defer on OnBeforeNetworkStart.
1434 BeforeNetworkStartHandler net_start_handler(true); // defer
1435 trans->SetBeforeNetworkStartCallback(
1436 base::Bind(&BeforeNetworkStartHandler::OnBeforeNetworkStart,
1437 base::Unretained(&net_start_handler)));
1438
1439 MockRead data_reads[] = {MockRead("HTTP/1.0 200 OK\r\n"),
1440 MockRead("Connection: keep-alive\r\n"),
1441 MockRead("Content-Length: 100\r\n\r\n"),
1442 MockRead("hello"),
1443 MockRead(SYNCHRONOUS, 0), };
1444 StaticSocketDataProvider data(data_reads, arraysize(data_reads), NULL, 0);
1445 session_deps_.socket_factory->AddSocketDataProvider(&data);
mmenke 2014/01/07 19:44:05 We don't need all this. In fact, it's better not
jkarlin 2014/01/07 20:10:03 Done.
1446
1447 TestCompletionCallback callback;
1448
1449 int rv = trans->Start(&request, callback.callback(), BoundNetLog());
1450 EXPECT_EQ(ERR_IO_PENDING, rv);
1451 base::MessageLoop::current()->RunUntilIdle();
1452
1453 // Should have deferred for network start.
1454 EXPECT_TRUE(net_start_handler.observed_before_network_start());
1455 EXPECT_EQ(LOAD_STATE_WAITING_FOR_DELEGATE, trans->GetLoadState());
1456 EXPECT_TRUE(trans->GetResponseInfo() == NULL);
1457 }
1458
1346 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression 1459 // Next 2 cases (KeepAliveEarlyClose and KeepAliveEarlyClose2) are regression
1347 // tests. There was a bug causing HttpNetworkTransaction to hang in the 1460 // tests. There was a bug causing HttpNetworkTransaction to hang in the
1348 // destructor in such situations. 1461 // destructor in such situations.
1349 // See http://crbug.com/154712 and http://crbug.com/156609. 1462 // See http://crbug.com/154712 and http://crbug.com/156609.
1350 TEST_P(HttpNetworkTransactionTest, KeepAliveEarlyClose) { 1463 TEST_P(HttpNetworkTransactionTest, KeepAliveEarlyClose) {
1351 HttpRequestInfo request; 1464 HttpRequestInfo request;
1352 request.method = "GET"; 1465 request.method = "GET";
1353 request.url = GURL("http://www.google.com/"); 1466 request.url = GURL("http://www.google.com/");
1354 request.load_flags = 0; 1467 request.load_flags = 0;
1355 1468
(...skipping 10977 matching lines...) Expand 10 before | Expand all | Expand 10 after
12333 // established, to let the HTTP request start. 12446 // established, to let the HTTP request start.
12334 ASSERT_EQ(OK, http_callback.WaitForResult()); 12447 ASSERT_EQ(OK, http_callback.WaitForResult());
12335 std::string response_data; 12448 std::string response_data;
12336 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data)); 12449 ASSERT_EQ(OK, ReadTransaction(http_trans.get(), &response_data));
12337 EXPECT_EQ("falafel", response_data); 12450 EXPECT_EQ("falafel", response_data);
12338 12451
12339 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session)); 12452 EXPECT_EQ(1, GetIdleSocketCountInTransportSocketPool(session));
12340 } 12453 }
12341 12454
12342 } // namespace net 12455 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698