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

Side by Side Diff: net/url_request/url_request_unittest.cc

Issue 732423002: Update from chromium https://crrev.com/304586 (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 6 years, 1 month 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
« no previous file with comments | « net/url_request/url_request_simple_job.cc ('k') | sandbox/linux/bpf_dsl/policy_compiler.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "build/build_config.h" 5 #include "build/build_config.h"
6 6
7 #if defined(OS_WIN) 7 #if defined(OS_WIN)
8 #include <windows.h> 8 #include <windows.h>
9 #include <shlobj.h> 9 #include <shlobj.h>
10 #endif 10 #endif
(...skipping 1140 matching lines...) Expand 10 before | Expand all | Expand 10 after
1151 } 1151 }
1152 protected: 1152 protected:
1153 void StartAsync() override { 1153 void StartAsync() override {
1154 request_->Cancel(); 1154 request_->Cancel();
1155 this->NotifyRestartRequired(); 1155 this->NotifyRestartRequired();
1156 } 1156 }
1157 private: 1157 private:
1158 ~CancelThenRestartTestJob() override {} 1158 ~CancelThenRestartTestJob() override {}
1159 }; 1159 };
1160 1160
1161 // An Interceptor for use with interceptor tests
1162 class TestInterceptor : URLRequest::Interceptor {
1163 public:
1164 TestInterceptor()
1165 : intercept_main_request_(false), restart_main_request_(false),
1166 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1167 simulate_main_network_error_(false),
1168 intercept_redirect_(false), cancel_redirect_request_(false),
1169 intercept_final_response_(false), cancel_final_request_(false),
1170 did_intercept_main_(false), did_restart_main_(false),
1171 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1172 did_simulate_error_main_(false),
1173 did_intercept_redirect_(false), did_cancel_redirect_(false),
1174 did_intercept_final_(false), did_cancel_final_(false) {
1175 URLRequest::Deprecated::RegisterRequestInterceptor(this);
1176 }
1177
1178 ~TestInterceptor() override {
1179 URLRequest::Deprecated::UnregisterRequestInterceptor(this);
1180 }
1181
1182 URLRequestJob* MaybeIntercept(URLRequest* request,
1183 NetworkDelegate* network_delegate) override {
1184 if (restart_main_request_) {
1185 restart_main_request_ = false;
1186 did_restart_main_ = true;
1187 return new RestartTestJob(request, network_delegate);
1188 }
1189 if (cancel_main_request_) {
1190 cancel_main_request_ = false;
1191 did_cancel_main_ = true;
1192 return new CancelTestJob(request, network_delegate);
1193 }
1194 if (cancel_then_restart_main_request_) {
1195 cancel_then_restart_main_request_ = false;
1196 did_cancel_then_restart_main_ = true;
1197 return new CancelThenRestartTestJob(request, network_delegate);
1198 }
1199 if (simulate_main_network_error_) {
1200 simulate_main_network_error_ = false;
1201 did_simulate_error_main_ = true;
1202 // will error since the requeted url is not one of its canned urls
1203 return new URLRequestTestJob(request, network_delegate, true);
1204 }
1205 if (!intercept_main_request_)
1206 return NULL;
1207 intercept_main_request_ = false;
1208 did_intercept_main_ = true;
1209 URLRequestTestJob* job = new URLRequestTestJob(request,
1210 network_delegate,
1211 main_headers_,
1212 main_data_,
1213 true);
1214 job->set_load_timing_info(main_request_load_timing_info_);
1215 return job;
1216 }
1217
1218 URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
1219 NetworkDelegate* network_delegate,
1220 const GURL& location) override {
1221 if (cancel_redirect_request_) {
1222 cancel_redirect_request_ = false;
1223 did_cancel_redirect_ = true;
1224 return new CancelTestJob(request, network_delegate);
1225 }
1226 if (!intercept_redirect_)
1227 return NULL;
1228 intercept_redirect_ = false;
1229 did_intercept_redirect_ = true;
1230 return new URLRequestTestJob(request,
1231 network_delegate,
1232 redirect_headers_,
1233 redirect_data_,
1234 true);
1235 }
1236
1237 URLRequestJob* MaybeInterceptResponse(
1238 URLRequest* request,
1239 NetworkDelegate* network_delegate) override {
1240 if (cancel_final_request_) {
1241 cancel_final_request_ = false;
1242 did_cancel_final_ = true;
1243 return new CancelTestJob(request, network_delegate);
1244 }
1245 if (!intercept_final_response_)
1246 return NULL;
1247 intercept_final_response_ = false;
1248 did_intercept_final_ = true;
1249 return new URLRequestTestJob(request,
1250 network_delegate,
1251 final_headers_,
1252 final_data_,
1253 true);
1254 }
1255
1256 // Whether to intercept the main request, and if so the response to return and
1257 // the LoadTimingInfo to use.
1258 bool intercept_main_request_;
1259 std::string main_headers_;
1260 std::string main_data_;
1261 LoadTimingInfo main_request_load_timing_info_;
1262
1263 // Other actions we take at MaybeIntercept time
1264 bool restart_main_request_;
1265 bool cancel_main_request_;
1266 bool cancel_then_restart_main_request_;
1267 bool simulate_main_network_error_;
1268
1269 // Whether to intercept redirects, and if so the response to return.
1270 bool intercept_redirect_;
1271 std::string redirect_headers_;
1272 std::string redirect_data_;
1273
1274 // Other actions we can take at MaybeInterceptRedirect time
1275 bool cancel_redirect_request_;
1276
1277 // Whether to intercept final response, and if so the response to return.
1278 bool intercept_final_response_;
1279 std::string final_headers_;
1280 std::string final_data_;
1281
1282 // Other actions we can take at MaybeInterceptResponse time
1283 bool cancel_final_request_;
1284
1285 // If we did something or not
1286 bool did_intercept_main_;
1287 bool did_restart_main_;
1288 bool did_cancel_main_;
1289 bool did_cancel_then_restart_main_;
1290 bool did_simulate_error_main_;
1291 bool did_intercept_redirect_;
1292 bool did_cancel_redirect_;
1293 bool did_intercept_final_;
1294 bool did_cancel_final_;
1295
1296 // Static getters for canned response header and data strings
1297
1298 static std::string ok_data() {
1299 return URLRequestTestJob::test_data_1();
1300 }
1301
1302 static std::string ok_headers() {
1303 return URLRequestTestJob::test_headers();
1304 }
1305
1306 static std::string redirect_data() {
1307 return std::string();
1308 }
1309
1310 static std::string redirect_headers() {
1311 return URLRequestTestJob::test_redirect_headers();
1312 }
1313
1314 static std::string error_data() {
1315 return std::string("ohhh nooooo mr. bill!");
1316 }
1317
1318 static std::string error_headers() {
1319 return URLRequestTestJob::test_error_headers();
1320 }
1321 };
1322
1323 TEST_F(URLRequestTest, Intercept) {
1324 TestInterceptor interceptor;
1325
1326 // intercept the main request and respond with a simple response
1327 interceptor.intercept_main_request_ = true;
1328 interceptor.main_headers_ = TestInterceptor::ok_headers();
1329 interceptor.main_data_ = TestInterceptor::ok_data();
1330
1331 TestDelegate d;
1332 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1333 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1334 base::SupportsUserData::Data* user_data0 = new base::SupportsUserData::Data();
1335 base::SupportsUserData::Data* user_data1 = new base::SupportsUserData::Data();
1336 base::SupportsUserData::Data* user_data2 = new base::SupportsUserData::Data();
1337 req->SetUserData(NULL, user_data0);
1338 req->SetUserData(&user_data1, user_data1);
1339 req->SetUserData(&user_data2, user_data2);
1340 req->set_method("GET");
1341 req->Start();
1342 base::RunLoop().Run();
1343
1344 // Make sure we can retrieve our specific user data
1345 EXPECT_EQ(user_data0, req->GetUserData(NULL));
1346 EXPECT_EQ(user_data1, req->GetUserData(&user_data1));
1347 EXPECT_EQ(user_data2, req->GetUserData(&user_data2));
1348
1349 // Check the interceptor got called as expected
1350 EXPECT_TRUE(interceptor.did_intercept_main_);
1351
1352 // Check we got one good response
1353 EXPECT_TRUE(req->status().is_success());
1354 EXPECT_EQ(200, req->response_headers()->response_code());
1355 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1356 EXPECT_EQ(1, d.response_started_count());
1357 EXPECT_EQ(0, d.received_redirect_count());
1358 }
1359
1360 TEST_F(URLRequestTest, InterceptRedirect) {
1361 TestInterceptor interceptor;
1362
1363 // intercept the main request and respond with a redirect
1364 interceptor.intercept_main_request_ = true;
1365 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1366 interceptor.main_data_ = TestInterceptor::redirect_data();
1367
1368 // intercept that redirect and respond a final OK response
1369 interceptor.intercept_redirect_ = true;
1370 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1371 interceptor.redirect_data_ = TestInterceptor::ok_data();
1372
1373 TestDelegate d;
1374 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1375 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1376 req->set_method("GET");
1377 req->Start();
1378 base::RunLoop().Run();
1379
1380 // Check the interceptor got called as expected
1381 EXPECT_TRUE(interceptor.did_intercept_main_);
1382 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1383
1384 // Check we got one good response
1385 EXPECT_TRUE(req->status().is_success());
1386 if (req->status().is_success()) {
1387 EXPECT_EQ(200, req->response_headers()->response_code());
1388 }
1389 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1390 EXPECT_EQ(1, d.response_started_count());
1391 EXPECT_EQ(0, d.received_redirect_count());
1392 }
1393
1394 TEST_F(URLRequestTest, InterceptServerError) {
1395 TestInterceptor interceptor;
1396
1397 // intercept the main request to generate a server error response
1398 interceptor.intercept_main_request_ = true;
1399 interceptor.main_headers_ = TestInterceptor::error_headers();
1400 interceptor.main_data_ = TestInterceptor::error_data();
1401
1402 // intercept that error and respond with an OK response
1403 interceptor.intercept_final_response_ = true;
1404 interceptor.final_headers_ = TestInterceptor::ok_headers();
1405 interceptor.final_data_ = TestInterceptor::ok_data();
1406
1407 TestDelegate d;
1408 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1409 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1410 req->set_method("GET");
1411 req->Start();
1412 base::RunLoop().Run();
1413
1414 // Check the interceptor got called as expected
1415 EXPECT_TRUE(interceptor.did_intercept_main_);
1416 EXPECT_TRUE(interceptor.did_intercept_final_);
1417
1418 // Check we got one good response
1419 EXPECT_TRUE(req->status().is_success());
1420 EXPECT_EQ(200, req->response_headers()->response_code());
1421 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1422 EXPECT_EQ(1, d.response_started_count());
1423 EXPECT_EQ(0, d.received_redirect_count());
1424 }
1425
1426 TEST_F(URLRequestTest, InterceptNetworkError) {
1427 TestInterceptor interceptor;
1428
1429 // intercept the main request to simulate a network error
1430 interceptor.simulate_main_network_error_ = true;
1431
1432 // intercept that error and respond with an OK response
1433 interceptor.intercept_final_response_ = true;
1434 interceptor.final_headers_ = TestInterceptor::ok_headers();
1435 interceptor.final_data_ = TestInterceptor::ok_data();
1436
1437 TestDelegate d;
1438 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1439 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1440 req->set_method("GET");
1441 req->Start();
1442 base::RunLoop().Run();
1443
1444 // Check the interceptor got called as expected
1445 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1446 EXPECT_TRUE(interceptor.did_intercept_final_);
1447
1448 // Check we received one good response
1449 EXPECT_TRUE(req->status().is_success());
1450 EXPECT_EQ(200, req->response_headers()->response_code());
1451 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1452 EXPECT_EQ(1, d.response_started_count());
1453 EXPECT_EQ(0, d.received_redirect_count());
1454 }
1455
1456 TEST_F(URLRequestTest, InterceptRestartRequired) {
1457 TestInterceptor interceptor;
1458
1459 // restart the main request
1460 interceptor.restart_main_request_ = true;
1461
1462 // then intercept the new main request and respond with an OK response
1463 interceptor.intercept_main_request_ = true;
1464 interceptor.main_headers_ = TestInterceptor::ok_headers();
1465 interceptor.main_data_ = TestInterceptor::ok_data();
1466
1467 TestDelegate d;
1468 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1469 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1470 req->set_method("GET");
1471 req->Start();
1472 base::RunLoop().Run();
1473
1474 // Check the interceptor got called as expected
1475 EXPECT_TRUE(interceptor.did_restart_main_);
1476 EXPECT_TRUE(interceptor.did_intercept_main_);
1477
1478 // Check we received one good response
1479 EXPECT_TRUE(req->status().is_success());
1480 if (req->status().is_success()) {
1481 EXPECT_EQ(200, req->response_headers()->response_code());
1482 }
1483 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1484 EXPECT_EQ(1, d.response_started_count());
1485 EXPECT_EQ(0, d.received_redirect_count());
1486 }
1487
1488 TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1489 TestInterceptor interceptor;
1490
1491 // intercept the main request and cancel from within the restarted job
1492 interceptor.cancel_main_request_ = true;
1493
1494 // setup to intercept final response and override it with an OK response
1495 interceptor.intercept_final_response_ = true;
1496 interceptor.final_headers_ = TestInterceptor::ok_headers();
1497 interceptor.final_data_ = TestInterceptor::ok_data();
1498
1499 TestDelegate d;
1500 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1501 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1502 req->set_method("GET");
1503 req->Start();
1504 base::RunLoop().Run();
1505
1506 // Check the interceptor got called as expected
1507 EXPECT_TRUE(interceptor.did_cancel_main_);
1508 EXPECT_FALSE(interceptor.did_intercept_final_);
1509
1510 // Check we see a canceled request
1511 EXPECT_FALSE(req->status().is_success());
1512 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1513 }
1514
1515 TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1516 TestInterceptor interceptor;
1517
1518 // intercept the main request and respond with a redirect
1519 interceptor.intercept_main_request_ = true;
1520 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1521 interceptor.main_data_ = TestInterceptor::redirect_data();
1522
1523 // intercept the redirect and cancel from within that job
1524 interceptor.cancel_redirect_request_ = true;
1525
1526 // setup to intercept final response and override it with an OK response
1527 interceptor.intercept_final_response_ = true;
1528 interceptor.final_headers_ = TestInterceptor::ok_headers();
1529 interceptor.final_data_ = TestInterceptor::ok_data();
1530
1531 TestDelegate d;
1532 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1533 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1534 req->set_method("GET");
1535 req->Start();
1536 base::RunLoop().Run();
1537
1538 // Check the interceptor got called as expected
1539 EXPECT_TRUE(interceptor.did_intercept_main_);
1540 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1541 EXPECT_FALSE(interceptor.did_intercept_final_);
1542
1543 // Check we see a canceled request
1544 EXPECT_FALSE(req->status().is_success());
1545 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1546 }
1547
1548 TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1549 TestInterceptor interceptor;
1550
1551 // intercept the main request to simulate a network error
1552 interceptor.simulate_main_network_error_ = true;
1553
1554 // setup to intercept final response and cancel from within that job
1555 interceptor.cancel_final_request_ = true;
1556
1557 TestDelegate d;
1558 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1559 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1560 req->set_method("GET");
1561 req->Start();
1562 base::RunLoop().Run();
1563
1564 // Check the interceptor got called as expected
1565 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1566 EXPECT_TRUE(interceptor.did_cancel_final_);
1567
1568 // Check we see a canceled request
1569 EXPECT_FALSE(req->status().is_success());
1570 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1571 }
1572
1573 TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1574 TestInterceptor interceptor;
1575
1576 // intercept the main request and cancel then restart from within that job
1577 interceptor.cancel_then_restart_main_request_ = true;
1578
1579 // setup to intercept final response and override it with an OK response
1580 interceptor.intercept_final_response_ = true;
1581 interceptor.final_headers_ = TestInterceptor::ok_headers();
1582 interceptor.final_data_ = TestInterceptor::ok_data();
1583
1584 TestDelegate d;
1585 scoped_ptr<URLRequest> req(default_context_.CreateRequest(
1586 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, NULL));
1587 req->set_method("GET");
1588 req->Start();
1589 base::RunLoop().Run();
1590
1591 // Check the interceptor got called as expected
1592 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1593 EXPECT_FALSE(interceptor.did_intercept_final_);
1594
1595 // Check we see a canceled request
1596 EXPECT_FALSE(req->status().is_success());
1597 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
1598 }
1599
1600 // An Interceptor for use with interceptor tests. 1161 // An Interceptor for use with interceptor tests.
1601 class MockURLRequestInterceptor : public URLRequestInterceptor { 1162 class MockURLRequestInterceptor : public URLRequestInterceptor {
1602 public: 1163 public:
1603 // Static getters for canned response header and data strings. 1164 // Static getters for canned response header and data strings.
1604 static std::string ok_data() { 1165 static std::string ok_data() {
1605 return URLRequestTestJob::test_data_1(); 1166 return URLRequestTestJob::test_data_1();
1606 } 1167 }
1607 1168
1608 static std::string ok_headers() { 1169 static std::string ok_headers() {
1609 return URLRequestTestJob::test_headers(); 1170 return URLRequestTestJob::test_headers();
(...skipping 527 matching lines...) Expand 10 before | Expand all | Expand 10 after
2137 EXPECT_FALSE(req->status().is_success()); 1698 EXPECT_FALSE(req->status().is_success());
2138 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status()); 1699 EXPECT_EQ(URLRequestStatus::CANCELED, req->status().status());
2139 } 1700 }
2140 1701
2141 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) { 1702 TEST_F(URLRequestInterceptorTest, InterceptRespectsCancelInRestart) {
2142 // Intercept the main request and cancel then restart from within that job. 1703 // Intercept the main request and cancel then restart from within that job.
2143 interceptor()->set_cancel_then_restart_main_request(true); 1704 interceptor()->set_cancel_then_restart_main_request(true);
2144 1705
2145 // Set up to intercept the final response and override it with an OK response. 1706 // Set up to intercept the final response and override it with an OK response.
2146 interceptor()->set_intercept_final_response(true); 1707 interceptor()->set_intercept_final_response(true);
2147 interceptor()->set_final_headers(TestInterceptor::ok_headers()); 1708 interceptor()->set_final_headers(MockURLRequestInterceptor::ok_headers());
2148 interceptor()->set_final_data(TestInterceptor::ok_data()); 1709 interceptor()->set_final_data(MockURLRequestInterceptor::ok_data());
2149 1710
2150 TestDelegate d; 1711 TestDelegate d;
2151 scoped_ptr<URLRequest> req(default_context().CreateRequest( 1712 scoped_ptr<URLRequest> req(default_context().CreateRequest(
2152 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, nullptr)); 1713 GURL("http://test_intercept/foo"), DEFAULT_PRIORITY, &d, nullptr));
2153 req->set_method("GET"); 1714 req->set_method("GET");
2154 req->Start(); 1715 req->Start();
2155 base::RunLoop().Run(); 1716 base::RunLoop().Run();
2156 1717
2157 // Check that the interceptor got called as expected. 1718 // Check that the interceptor got called as expected.
2158 EXPECT_TRUE(interceptor()->did_cancel_then_restart_main()); 1719 EXPECT_TRUE(interceptor()->did_cancel_then_restart_main());
(...skipping 6902 matching lines...) Expand 10 before | Expand all | Expand 10 after
9061 8622
9062 EXPECT_FALSE(r->is_pending()); 8623 EXPECT_FALSE(r->is_pending());
9063 EXPECT_EQ(1, d->response_started_count()); 8624 EXPECT_EQ(1, d->response_started_count());
9064 EXPECT_FALSE(d->received_data_before_response()); 8625 EXPECT_FALSE(d->received_data_before_response());
9065 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size)); 8626 EXPECT_EQ(d->bytes_received(), static_cast<int>(file_size));
9066 } 8627 }
9067 } 8628 }
9068 #endif // !defined(DISABLE_FTP_SUPPORT) 8629 #endif // !defined(DISABLE_FTP_SUPPORT)
9069 8630
9070 } // namespace net 8631 } // namespace net
OLDNEW
« no previous file with comments | « net/url_request/url_request_simple_job.cc ('k') | sandbox/linux/bpf_dsl/policy_compiler.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698