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

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

Issue 67019: URLRequest::Interceptor enhancements... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: '' Created 11 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « net/url_request/url_request_test_job.cc ('k') | no next file » | 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) 2006-2008 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2006-2008 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_unittest.h" 5 #include "net/url_request/url_request_unittest.h"
6 6
7 #include "build/build_config.h" 7 #include "build/build_config.h"
8 8
9 #if defined(OS_WIN) 9 #if defined(OS_WIN)
10 #include <windows.h> 10 #include <windows.h>
(...skipping 16 matching lines...) Expand all
27 #include "net/base/net_errors.h" 27 #include "net/base/net_errors.h"
28 #include "net/base/net_module.h" 28 #include "net/base/net_module.h"
29 #include "net/base/net_util.h" 29 #include "net/base/net_util.h"
30 #include "net/base/ssl_test_util.h" 30 #include "net/base/ssl_test_util.h"
31 #include "net/disk_cache/disk_cache.h" 31 #include "net/disk_cache/disk_cache.h"
32 #include "net/http/http_cache.h" 32 #include "net/http/http_cache.h"
33 #include "net/http/http_network_layer.h" 33 #include "net/http/http_network_layer.h"
34 #include "net/http/http_response_headers.h" 34 #include "net/http/http_response_headers.h"
35 #include "net/proxy/proxy_service.h" 35 #include "net/proxy/proxy_service.h"
36 #include "net/url_request/url_request.h" 36 #include "net/url_request/url_request.h"
37 #include "net/url_request/url_request_test_job.h"
37 #include "testing/gtest/include/gtest/gtest.h" 38 #include "testing/gtest/include/gtest/gtest.h"
38 #include "testing/platform_test.h" 39 #include "testing/platform_test.h"
39 40
40 using base::Time; 41 using base::Time;
41 42
42 namespace { 43 namespace {
43 44
44 class URLRequestHttpCacheContext : public URLRequestContext { 45 class URLRequestHttpCacheContext : public URLRequestContext {
45 public: 46 public:
46 URLRequestHttpCacheContext() { 47 URLRequestHttpCacheContext() {
(...skipping 1057 matching lines...) Expand 10 before | Expand all | Expand 10 after
1104 ASSERT_TRUE(NULL != server.get()); 1105 ASSERT_TRUE(NULL != server.get());
1105 TestDelegate d; 1106 TestDelegate d;
1106 TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"), 1107 TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"),
1107 &d); 1108 &d);
1108 req.set_method("POST"); 1109 req.set_method("POST");
1109 req.Start(); 1110 req.Start();
1110 MessageLoop::current()->Run(); 1111 MessageLoop::current()->Run();
1111 EXPECT_EQ(req.method(), "POST"); 1112 EXPECT_EQ(req.method(), "POST");
1112 } 1113 }
1113 1114
1115 // Custom URLRequestJobs for use with interceptor tests
1116 class RestartTestJob : public URLRequestTestJob {
1117 public:
1118 RestartTestJob(URLRequest* request) : URLRequestTestJob(request, true) {}
1119 protected:
1120 virtual void StartAsync() {
1121 this->NotifyRestartRequired();
1122 }
1123 };
1124
1125 class CancelTestJob : public URLRequestTestJob {
1126 public:
1127 CancelTestJob(URLRequest* request) : URLRequestTestJob(request, true) {}
1128 protected:
1129 virtual void StartAsync() {
1130 request_->Cancel();
1131 }
1132 };
1133
1134 class CancelThenRestartTestJob : public URLRequestTestJob {
1135 public:
1136 CancelThenRestartTestJob(URLRequest* request)
1137 : URLRequestTestJob(request, true) {
1138 }
1139 protected:
1140 virtual void StartAsync() {
1141 request_->Cancel();
1142 this->NotifyRestartRequired();
1143 }
1144 };
1145
1146 // An Interceptor for use with interceptor tests
1147 class TestInterceptor : URLRequest::Interceptor {
1148 public:
1149 TestInterceptor()
1150 : intercept_main_request_(false), restart_main_request_(false),
1151 cancel_main_request_(false), cancel_then_restart_main_request_(false),
1152 simulate_main_network_error_(false),
1153 intercept_redirect_(false), cancel_redirect_request_(false),
1154 intercept_final_response_(false), cancel_final_request_(false),
1155 did_intercept_main_(false), did_restart_main_(false),
1156 did_cancel_main_(false), did_cancel_then_restart_main_(false),
1157 did_simulate_error_main_(false),
1158 did_intercept_redirect_(false), did_cancel_redirect_(false),
1159 did_intercept_final_(false), did_cancel_final_(false) {
1160 URLRequest::RegisterRequestInterceptor(this);
1161 }
1162
1163 ~TestInterceptor() {
1164 URLRequest::UnregisterRequestInterceptor(this);
1165 }
1166
1167 virtual URLRequestJob* MaybeIntercept(URLRequest* request) {
1168 if (restart_main_request_) {
1169 restart_main_request_ = false;
1170 did_restart_main_ = true;
1171 return new RestartTestJob(request);
1172 }
1173 if (cancel_main_request_) {
1174 cancel_main_request_ = false;
1175 did_cancel_main_ = true;
1176 return new CancelTestJob(request);
1177 }
1178 if (cancel_then_restart_main_request_) {
1179 cancel_then_restart_main_request_ = false;
1180 did_cancel_then_restart_main_ = true;
1181 return new CancelThenRestartTestJob(request);
1182 }
1183 if (simulate_main_network_error_) {
1184 simulate_main_network_error_ = false;
1185 did_simulate_error_main_ = true;
1186 // will error since the requeted url is not one of its canned urls
1187 return new URLRequestTestJob(request, true);
1188 }
1189 if (!intercept_main_request_)
1190 return NULL;
1191 intercept_main_request_ = false;
1192 did_intercept_main_ = true;
1193 return new URLRequestTestJob(request,
1194 main_headers_,
1195 main_data_,
1196 true);
1197 }
1198
1199 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request,
1200 const GURL& location) {
1201 if (cancel_redirect_request_) {
1202 cancel_redirect_request_ = false;
1203 did_cancel_redirect_ = true;
1204 return new CancelTestJob(request);
1205 }
1206 if (!intercept_redirect_)
1207 return NULL;
1208 intercept_redirect_ = false;
1209 did_intercept_redirect_ = true;
1210 return new URLRequestTestJob(request,
1211 redirect_headers_,
1212 redirect_data_,
1213 true);
1214 }
1215
1216 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) {
1217 if (cancel_final_request_) {
1218 cancel_final_request_ = false;
1219 did_cancel_final_ = true;
1220 return new CancelTestJob(request);
1221 }
1222 if (!intercept_final_response_)
1223 return NULL;
1224 intercept_final_response_ = false;
1225 did_intercept_final_ = true;
1226 return new URLRequestTestJob(request,
1227 final_headers_,
1228 final_data_,
1229 true);
1230 }
1231
1232 // Whether to intercept the main request, and if so the response to return.
1233 bool intercept_main_request_;
1234 std::string main_headers_;
1235 std::string main_data_;
1236
1237 // Other actions we take at MaybeIntercept time
1238 bool restart_main_request_;
1239 bool cancel_main_request_;
1240 bool cancel_then_restart_main_request_;
1241 bool simulate_main_network_error_;
1242
1243 // Whether to intercept redirects, and if so the response to return.
1244 bool intercept_redirect_;
1245 std::string redirect_headers_;
1246 std::string redirect_data_;
1247
1248 // Other actions we can take at MaybeInterceptRedirect time
1249 bool cancel_redirect_request_;
1250
1251 // Whether to intercept final response, and if so the response to return.
1252 bool intercept_final_response_;
1253 std::string final_headers_;
1254 std::string final_data_;
1255
1256 // Other actions we can take at MaybeInterceptResponse time
1257 bool cancel_final_request_;
1258
1259 // If we did something or not
1260 bool did_intercept_main_;
1261 bool did_restart_main_;
1262 bool did_cancel_main_;
1263 bool did_cancel_then_restart_main_;
1264 bool did_simulate_error_main_;
1265 bool did_intercept_redirect_;
1266 bool did_cancel_redirect_;
1267 bool did_intercept_final_;
1268 bool did_cancel_final_;
1269
1270 // Static getters for canned response header and data strings
1271
1272 static std::string ok_data() {
1273 return URLRequestTestJob::test_data_1();
1274 }
1275
1276 static std::string ok_headers() {
1277 return URLRequestTestJob::test_headers();
1278 }
1279
1280 static std::string redirect_data() {
1281 return std::string();
1282 }
1283
1284 static std::string redirect_headers() {
1285 return URLRequestTestJob::test_redirect_headers();
1286 }
1287
1288 static std::string error_data() {
1289 return std::string("ohhh nooooo mr. bill!");
1290 }
1291
1292 static std::string error_headers() {
1293 return URLRequestTestJob::test_error_headers();
1294 }
1295 };
1296
1297 TEST_F(URLRequestTest, Intercept) {
1298 TestInterceptor interceptor;
1299
1300 // intercept the main request and respond with a simple response
1301 interceptor.intercept_main_request_ = true;
1302 interceptor.main_headers_ = TestInterceptor::ok_headers();
1303 interceptor.main_data_ = TestInterceptor::ok_data();
1304
1305 TestDelegate d;
1306 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1307 URLRequest::UserData* user_data0 = new URLRequest::UserData();
1308 URLRequest::UserData* user_data1 = new URLRequest::UserData();
1309 URLRequest::UserData* user_data2 = new URLRequest::UserData();
1310 req.SetUserData(NULL, user_data0);
1311 req.SetUserData(&user_data1, user_data1);
1312 req.SetUserData(&user_data2, user_data2);
1313 req.set_method("GET");
1314 req.Start();
1315 MessageLoop::current()->Run();
1316
1317 // Make sure we can retrieve our specific user data
1318 EXPECT_EQ(user_data0, req.GetUserData(NULL));
1319 EXPECT_EQ(user_data1, req.GetUserData(&user_data1));
1320 EXPECT_EQ(user_data2, req.GetUserData(&user_data2));
1321
1322 // Check the interceptor got called as expected
1323 EXPECT_TRUE(interceptor.did_intercept_main_);
1324
1325 // Check we got one good response
1326 EXPECT_TRUE(req.status().is_success());
1327 EXPECT_EQ(200, req.response_headers()->response_code());
1328 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1329 EXPECT_EQ(1, d.response_started_count());
1330 EXPECT_EQ(0, d.received_redirect_count());
1331 }
1332
1333 TEST_F(URLRequestTest, InterceptRedirect) {
1334 TestInterceptor interceptor;
1335
1336 // intercept the main request and respond with a redirect
1337 interceptor.intercept_main_request_ = true;
1338 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1339 interceptor.main_data_ = TestInterceptor::redirect_data();
1340
1341 // intercept that redirect and respond a final OK response
1342 interceptor.intercept_redirect_ = true;
1343 interceptor.redirect_headers_ = TestInterceptor::ok_headers();
1344 interceptor.redirect_data_ = TestInterceptor::ok_data();
1345
1346 TestDelegate d;
1347 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1348 req.set_method("GET");
1349 req.Start();
1350 MessageLoop::current()->Run();
1351
1352 // Check the interceptor got called as expected
1353 EXPECT_TRUE(interceptor.did_intercept_main_);
1354 EXPECT_TRUE(interceptor.did_intercept_redirect_);
1355
1356 // Check we got one good response
1357 EXPECT_TRUE(req.status().is_success());
1358 EXPECT_EQ(200, req.response_headers()->response_code());
1359 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1360 EXPECT_EQ(1, d.response_started_count());
1361 EXPECT_EQ(0, d.received_redirect_count());
1362 }
1363
1364 TEST_F(URLRequestTest, InterceptServerError) {
1365 TestInterceptor interceptor;
1366
1367 // intercept the main request to generate a server error response
1368 interceptor.intercept_main_request_ = true;
1369 interceptor.main_headers_ = TestInterceptor::error_headers();
1370 interceptor.main_data_ = TestInterceptor::error_data();
1371
1372 // intercept that error and respond with an OK response
1373 interceptor.intercept_final_response_ = true;
1374 interceptor.final_headers_ = TestInterceptor::ok_headers();
1375 interceptor.final_data_ = TestInterceptor::ok_data();
1376
1377 TestDelegate d;
1378 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1379 req.set_method("GET");
1380 req.Start();
1381 MessageLoop::current()->Run();
1382
1383 // Check the interceptor got called as expected
1384 EXPECT_TRUE(interceptor.did_intercept_main_);
1385 EXPECT_TRUE(interceptor.did_intercept_final_);
1386
1387 // Check we got one good response
1388 EXPECT_TRUE(req.status().is_success());
1389 EXPECT_EQ(200, req.response_headers()->response_code());
1390 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1391 EXPECT_EQ(1, d.response_started_count());
1392 EXPECT_EQ(0, d.received_redirect_count());
1393 }
1394
1395 TEST_F(URLRequestTest, InterceptNetworkError) {
1396 TestInterceptor interceptor;
1397
1398 // intercept the main request to simulate a network error
1399 interceptor.simulate_main_network_error_ = true;
1400
1401 // intercept that error and respond with an OK response
1402 interceptor.intercept_final_response_ = true;
1403 interceptor.final_headers_ = TestInterceptor::ok_headers();
1404 interceptor.final_data_ = TestInterceptor::ok_data();
1405
1406 TestDelegate d;
1407 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1408 req.set_method("GET");
1409 req.Start();
1410 MessageLoop::current()->Run();
1411
1412 // Check the interceptor got called as expected
1413 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1414 EXPECT_TRUE(interceptor.did_intercept_final_);
1415
1416 // Check we received one good response
1417 EXPECT_TRUE(req.status().is_success());
1418 EXPECT_EQ(200, req.response_headers()->response_code());
1419 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1420 EXPECT_EQ(1, d.response_started_count());
1421 EXPECT_EQ(0, d.received_redirect_count());
1422 }
1423
1424 TEST_F(URLRequestTest, InterceptRestartRequired) {
1425 TestInterceptor interceptor;
1426
1427 // restart the main request
1428 interceptor.restart_main_request_ = true;
1429
1430 // then intercept the new main request and respond with an OK response
1431 interceptor.intercept_main_request_ = true;
1432 interceptor.main_headers_ = TestInterceptor::ok_headers();
1433 interceptor.main_data_ = TestInterceptor::ok_data();
1434
1435 TestDelegate d;
1436 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1437 req.set_method("GET");
1438 req.Start();
1439 MessageLoop::current()->Run();
1440
1441 // Check the interceptor got called as expected
1442 EXPECT_TRUE(interceptor.did_restart_main_);
1443 EXPECT_TRUE(interceptor.did_intercept_main_);
1444
1445 // Check we received one good response
1446 EXPECT_TRUE(req.status().is_success());
1447 EXPECT_EQ(200, req.response_headers()->response_code());
1448 EXPECT_EQ(TestInterceptor::ok_data(), d.data_received());
1449 EXPECT_EQ(1, d.response_started_count());
1450 EXPECT_EQ(0, d.received_redirect_count());
1451 }
1452
1453 TEST_F(URLRequestTest, InterceptRespectsCancelMain) {
1454 TestInterceptor interceptor;
1455
1456 // intercept the main request and cancel from within the restarted job
1457 interceptor.cancel_main_request_ = true;
1458
1459 // setup to intercept final response and override it with an OK response
1460 interceptor.intercept_final_response_ = true;
1461 interceptor.final_headers_ = TestInterceptor::ok_headers();
1462 interceptor.final_data_ = TestInterceptor::ok_data();
1463
1464 TestDelegate d;
1465 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1466 req.set_method("GET");
1467 req.Start();
1468 MessageLoop::current()->Run();
1469
1470 // Check the interceptor got called as expected
1471 EXPECT_TRUE(interceptor.did_cancel_main_);
1472 EXPECT_FALSE(interceptor.did_intercept_final_);
1473
1474 // Check we see a canceled request
1475 EXPECT_FALSE(req.status().is_success());
1476 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1477 }
1478
1479 TEST_F(URLRequestTest, InterceptRespectsCancelRedirect) {
1480 TestInterceptor interceptor;
1481
1482 // intercept the main request and respond with a redirect
1483 interceptor.intercept_main_request_ = true;
1484 interceptor.main_headers_ = TestInterceptor::redirect_headers();
1485 interceptor.main_data_ = TestInterceptor::redirect_data();
1486
1487 // intercept the redirect and cancel from within that job
1488 interceptor.cancel_redirect_request_ = true;
1489
1490 // setup to intercept final response and override it with an OK response
1491 interceptor.intercept_final_response_ = true;
1492 interceptor.final_headers_ = TestInterceptor::ok_headers();
1493 interceptor.final_data_ = TestInterceptor::ok_data();
1494
1495 TestDelegate d;
1496 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1497 req.set_method("GET");
1498 req.Start();
1499 MessageLoop::current()->Run();
1500
1501 // Check the interceptor got called as expected
1502 EXPECT_TRUE(interceptor.did_intercept_main_);
1503 EXPECT_TRUE(interceptor.did_cancel_redirect_);
1504 EXPECT_FALSE(interceptor.did_intercept_final_);
1505
1506 // Check we see a canceled request
1507 EXPECT_FALSE(req.status().is_success());
1508 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1509 }
1510
1511 TEST_F(URLRequestTest, InterceptRespectsCancelFinal) {
1512 TestInterceptor interceptor;
1513
1514 // intercept the main request to simulate a network error
1515 interceptor.simulate_main_network_error_ = true;
1516
1517 // setup to intercept final response and cancel from within that job
1518 interceptor.cancel_final_request_ = true;
1519
1520 TestDelegate d;
1521 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1522 req.set_method("GET");
1523 req.Start();
1524 MessageLoop::current()->Run();
1525
1526 // Check the interceptor got called as expected
1527 EXPECT_TRUE(interceptor.did_simulate_error_main_);
1528 EXPECT_TRUE(interceptor.did_cancel_final_);
1529
1530 // Check we see a canceled request
1531 EXPECT_FALSE(req.status().is_success());
1532 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1533 }
1534
1535 TEST_F(URLRequestTest, InterceptRespectsCancelInRestart) {
1536 TestInterceptor interceptor;
1537
1538 // intercept the main request and cancel then restart from within that job
1539 interceptor.cancel_then_restart_main_request_ = true;
1540
1541 // setup to intercept final response and override it with an OK response
1542 interceptor.intercept_final_response_ = true;
1543 interceptor.final_headers_ = TestInterceptor::ok_headers();
1544 interceptor.final_data_ = TestInterceptor::ok_data();
1545
1546 TestDelegate d;
1547 TestURLRequest req(GURL("http://test_intercept/foo"), &d);
1548 req.set_method("GET");
1549 req.Start();
1550 MessageLoop::current()->Run();
1551
1552 // Check the interceptor got called as expected
1553 EXPECT_TRUE(interceptor.did_cancel_then_restart_main_);
1554 EXPECT_FALSE(interceptor.did_intercept_final_);
1555
1556 // Check we see a canceled request
1557 EXPECT_FALSE(req.status().is_success());
1558 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status());
1559 }
1560
1114 // FTP tests appear to be hanging some of the time 1561 // FTP tests appear to be hanging some of the time
1115 #if 1 // !defined(OS_WIN) 1562 #if 1 // !defined(OS_WIN)
1116 #define MAYBE_FTPGetTestAnonymous DISABLED_FTPGetTestAnonymous 1563 #define MAYBE_FTPGetTestAnonymous DISABLED_FTPGetTestAnonymous
1117 #define MAYBE_FTPGetTest DISABLED_FTPGetTest 1564 #define MAYBE_FTPGetTest DISABLED_FTPGetTest
1118 #define MAYBE_FTPCheckWrongUser DISABLED_FTPCheckWrongUser 1565 #define MAYBE_FTPCheckWrongUser DISABLED_FTPCheckWrongUser
1119 #define MAYBE_FTPCheckWrongPassword DISABLED_FTPCheckWrongPassword 1566 #define MAYBE_FTPCheckWrongPassword DISABLED_FTPCheckWrongPassword
1120 #else 1567 #else
1121 #define MAYBE_FTPGetTestAnonymous FTPGetTestAnonymous 1568 #define MAYBE_FTPGetTestAnonymous FTPGetTestAnonymous
1122 #define MAYBE_FTPGetTest FTPGetTest 1569 #define MAYBE_FTPGetTest FTPGetTest
1123 #define MAYBE_FTPCheckWrongUser FTPCheckWrongUser 1570 #define MAYBE_FTPCheckWrongUser FTPCheckWrongUser
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after
1215 1662
1216 int64 file_size = 0; 1663 int64 file_size = 0;
1217 file_util::GetFileSize(app_path, &file_size); 1664 file_util::GetFileSize(app_path, &file_size);
1218 1665
1219 EXPECT_TRUE(!r.is_pending()); 1666 EXPECT_TRUE(!r.is_pending());
1220 EXPECT_EQ(1, d.response_started_count()); 1667 EXPECT_EQ(1, d.response_started_count());
1221 EXPECT_FALSE(d.received_data_before_response()); 1668 EXPECT_FALSE(d.received_data_before_response());
1222 EXPECT_EQ(d.bytes_received(), 0); 1669 EXPECT_EQ(d.bytes_received(), 0);
1223 } 1670 }
1224 } 1671 }
OLDNEW
« no previous file with comments | « net/url_request/url_request_test_job.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698