OLD | NEW |
1 // Copyright (c) 2006-2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2009 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> |
11 #include <shlobj.h> | 11 #include <shlobj.h> |
12 #elif defined(OS_LINUX) | 12 #elif defined(OS_LINUX) |
13 #include "base/nss_init.h" | 13 #include "base/nss_init.h" |
14 #endif | 14 #endif |
15 | 15 |
16 #include <algorithm> | 16 #include <algorithm> |
17 #include <string> | 17 #include <string> |
18 | 18 |
19 #include "base/file_util.h" | 19 #include "base/file_util.h" |
20 #include "base/message_loop.h" | 20 #include "base/message_loop.h" |
21 #include "base/path_service.h" | 21 #include "base/path_service.h" |
22 #include "base/process_util.h" | 22 #include "base/process_util.h" |
23 #include "base/string_piece.h" | 23 #include "base/string_piece.h" |
24 #include "base/string_util.h" | 24 #include "base/string_util.h" |
25 #include "net/base/cookie_monster.h" | 25 #include "net/base/cookie_monster.h" |
26 #include "net/base/load_flags.h" | 26 #include "net/base/load_flags.h" |
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/upload_data.h" |
30 #include "net/disk_cache/disk_cache.h" | 31 #include "net/disk_cache/disk_cache.h" |
31 #include "net/http/http_cache.h" | 32 #include "net/http/http_cache.h" |
32 #include "net/http/http_network_layer.h" | 33 #include "net/http/http_network_layer.h" |
33 #include "net/http/http_response_headers.h" | 34 #include "net/http/http_response_headers.h" |
34 #include "net/proxy/proxy_service.h" | 35 #include "net/proxy/proxy_service.h" |
35 #include "net/socket/ssl_test_util.h" | 36 #include "net/socket/ssl_test_util.h" |
36 #include "net/url_request/url_request.h" | 37 #include "net/url_request/url_request.h" |
37 #include "net/url_request/url_request_test_job.h" | 38 #include "net/url_request/url_request_test_job.h" |
38 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
39 #include "testing/platform_test.h" | 40 #include "testing/platform_test.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
93 srand(seed); | 94 srand(seed); |
94 } | 95 } |
95 | 96 |
96 for (size_t i = 0; i < len; i++) { | 97 for (size_t i = 0; i < len; i++) { |
97 buffer[i] = static_cast<char>(rand()); | 98 buffer[i] = static_cast<char>(rand()); |
98 if (!buffer[i]) | 99 if (!buffer[i]) |
99 buffer[i] = 'g'; | 100 buffer[i] = 'g'; |
100 } | 101 } |
101 } | 102 } |
102 | 103 |
| 104 scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) { |
| 105 scoped_refptr<net::UploadData> upload = new net::UploadData; |
| 106 upload->AppendBytes(data, strlen(data)); |
| 107 return upload; |
| 108 } |
| 109 |
103 } // namespace | 110 } // namespace |
104 | 111 |
105 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f | 112 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
106 class URLRequestTest : public PlatformTest { | 113 class URLRequestTest : public PlatformTest { |
107 }; | 114 }; |
108 | 115 |
109 TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { | 116 TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { |
110 // In this unit test, we're using the HTTPTestServer as a proxy server and | 117 // In this unit test, we're using the HTTPTestServer as a proxy server and |
111 // issuing a CONNECT request with the magic host name "www.redirect.com". | 118 // issuing a CONNECT request with the magic host name "www.redirect.com". |
112 // The HTTPTestServer will return a 302 response, which we should not | 119 // The HTTPTestServer will return a 302 response, which we should not |
(...skipping 951 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1064 req.Start(); | 1071 req.Start(); |
1065 MessageLoop::current()->Run(); | 1072 MessageLoop::current()->Run(); |
1066 | 1073 |
1067 EXPECT_EQ(1, d.response_started_count()); | 1074 EXPECT_EQ(1, d.response_started_count()); |
1068 EXPECT_EQ(0, d.bytes_received()); | 1075 EXPECT_EQ(0, d.bytes_received()); |
1069 EXPECT_FALSE(d.received_data_before_response()); | 1076 EXPECT_FALSE(d.received_data_before_response()); |
1070 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); | 1077 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
1071 } | 1078 } |
1072 } | 1079 } |
1073 | 1080 |
| 1081 TEST_F(URLRequestTest, DeferredRedirect) { |
| 1082 scoped_refptr<HTTPTestServer> server = |
| 1083 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
| 1084 ASSERT_TRUE(NULL != server.get()); |
| 1085 TestDelegate d; |
| 1086 { |
| 1087 d.set_quit_on_redirect(true); |
| 1088 TestURLRequest req(server->TestServerPage( |
| 1089 "files/redirect-test.html"), &d); |
| 1090 req.Start(); |
| 1091 MessageLoop::current()->Run(); |
| 1092 |
| 1093 EXPECT_EQ(1, d.received_redirect_count()); |
| 1094 |
| 1095 req.FollowDeferredRedirect(); |
| 1096 MessageLoop::current()->Run(); |
| 1097 |
| 1098 EXPECT_EQ(1, d.response_started_count()); |
| 1099 EXPECT_FALSE(d.received_data_before_response()); |
| 1100 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); |
| 1101 |
| 1102 FilePath path; |
| 1103 PathService::Get(base::DIR_SOURCE_ROOT, &path); |
| 1104 path = path.Append(FILE_PATH_LITERAL("net")); |
| 1105 path = path.Append(FILE_PATH_LITERAL("data")); |
| 1106 path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); |
| 1107 path = path.Append(FILE_PATH_LITERAL("with-headers.html")); |
| 1108 |
| 1109 std::string contents; |
| 1110 EXPECT_TRUE(file_util::ReadFileToString(path, &contents)); |
| 1111 EXPECT_EQ(contents, d.data_received()); |
| 1112 } |
| 1113 } |
| 1114 |
| 1115 TEST_F(URLRequestTest, CancelDeferredRedirect) { |
| 1116 scoped_refptr<HTTPTestServer> server = |
| 1117 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
| 1118 ASSERT_TRUE(NULL != server.get()); |
| 1119 TestDelegate d; |
| 1120 { |
| 1121 d.set_quit_on_redirect(true); |
| 1122 TestURLRequest req(server->TestServerPage( |
| 1123 "files/redirect-test.html"), &d); |
| 1124 req.Start(); |
| 1125 MessageLoop::current()->Run(); |
| 1126 |
| 1127 EXPECT_EQ(1, d.received_redirect_count()); |
| 1128 |
| 1129 req.Cancel(); |
| 1130 MessageLoop::current()->Run(); |
| 1131 |
| 1132 EXPECT_EQ(1, d.response_started_count()); |
| 1133 EXPECT_EQ(0, d.bytes_received()); |
| 1134 EXPECT_FALSE(d.received_data_before_response()); |
| 1135 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 1136 } |
| 1137 } |
| 1138 |
1074 TEST_F(URLRequestTest, VaryHeader) { | 1139 TEST_F(URLRequestTest, VaryHeader) { |
1075 scoped_refptr<HTTPTestServer> server = | 1140 scoped_refptr<HTTPTestServer> server = |
1076 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | 1141 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
1077 ASSERT_TRUE(NULL != server.get()); | 1142 ASSERT_TRUE(NULL != server.get()); |
1078 | 1143 |
1079 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); | 1144 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
1080 | 1145 |
1081 Time response_time; | 1146 Time response_time; |
1082 | 1147 |
1083 // populate the cache | 1148 // populate the cache |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1229 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") | 1294 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
1230 != std::string::npos); | 1295 != std::string::npos); |
1231 } | 1296 } |
1232 } | 1297 } |
1233 | 1298 |
1234 // In this test, we do a POST which the server will 302 redirect. | 1299 // In this test, we do a POST which the server will 302 redirect. |
1235 // The subsequent transaction should use GET, and should not send the | 1300 // The subsequent transaction should use GET, and should not send the |
1236 // Content-Type header. | 1301 // Content-Type header. |
1237 // http://code.google.com/p/chromium/issues/detail?id=843 | 1302 // http://code.google.com/p/chromium/issues/detail?id=843 |
1238 TEST_F(URLRequestTest, Post302RedirectGet) { | 1303 TEST_F(URLRequestTest, Post302RedirectGet) { |
| 1304 const char kData[] = "hello world"; |
1239 scoped_refptr<HTTPTestServer> server = | 1305 scoped_refptr<HTTPTestServer> server = |
1240 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | 1306 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
1241 ASSERT_TRUE(NULL != server.get()); | 1307 ASSERT_TRUE(NULL != server.get()); |
1242 TestDelegate d; | 1308 TestDelegate d; |
1243 TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d); | 1309 TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d); |
1244 req.set_method("POST"); | 1310 req.set_method("POST"); |
| 1311 req.set_upload(CreateSimpleUploadData(kData)); |
1245 | 1312 |
1246 // Set headers (some of which are specific to the POST). | 1313 // Set headers (some of which are specific to the POST). |
1247 // ("Content-Length: 10" is just a junk value to make sure it gets stripped). | |
1248 req.SetExtraRequestHeaders( | 1314 req.SetExtraRequestHeaders( |
1249 "Content-Type: multipart/form-data; " | 1315 "Content-Type: multipart/form-data; " |
1250 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" | 1316 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
1251 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," | 1317 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
1252 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" | 1318 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
1253 "Accept-Language: en-US,en\r\n" | 1319 "Accept-Language: en-US,en\r\n" |
1254 "Accept-Charset: ISO-8859-1,*,utf-8\r\n" | 1320 "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
1255 "Content-Length: 10\r\n" | 1321 "Content-Length: 11\r\n" |
1256 "Origin: http://localhost:1337/"); | 1322 "Origin: http://localhost:1337/"); |
1257 req.Start(); | 1323 req.Start(); |
1258 MessageLoop::current()->Run(); | 1324 MessageLoop::current()->Run(); |
1259 | 1325 |
1260 std::string mime_type; | 1326 std::string mime_type; |
1261 req.GetMimeType(&mime_type); | 1327 req.GetMimeType(&mime_type); |
1262 EXPECT_EQ("text/html", mime_type); | 1328 EXPECT_EQ("text/html", mime_type); |
1263 | 1329 |
1264 const std::string& data = d.data_received(); | 1330 const std::string& data = d.data_received(); |
1265 | 1331 |
1266 // Check that the post-specific headers were stripped: | 1332 // Check that the post-specific headers were stripped: |
1267 EXPECT_FALSE(ContainsString(data, "Content-Length:")); | 1333 EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
1268 EXPECT_FALSE(ContainsString(data, "Content-Type:")); | 1334 EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
1269 EXPECT_FALSE(ContainsString(data, "Origin:")); | 1335 EXPECT_FALSE(ContainsString(data, "Origin:")); |
1270 | 1336 |
1271 // These extra request headers should not have been stripped. | 1337 // These extra request headers should not have been stripped. |
1272 EXPECT_TRUE(ContainsString(data, "Accept:")); | 1338 EXPECT_TRUE(ContainsString(data, "Accept:")); |
1273 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); | 1339 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
1274 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); | 1340 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
1275 } | 1341 } |
1276 | 1342 |
1277 TEST_F(URLRequestTest, Post307RedirectPost) { | 1343 // TODO(darin): Re-enable this test once bug 16832 is fixed. |
| 1344 TEST_F(URLRequestTest, DISABLED_Post307RedirectPost) { |
| 1345 const char kData[] = "hello world"; |
1278 scoped_refptr<HTTPTestServer> server = | 1346 scoped_refptr<HTTPTestServer> server = |
1279 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | 1347 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
1280 ASSERT_TRUE(NULL != server.get()); | 1348 ASSERT_TRUE(NULL != server.get()); |
1281 TestDelegate d; | 1349 TestDelegate d; |
1282 TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"), | 1350 TestURLRequest req(server->TestServerPage("files/redirect307-to-echo"), |
1283 &d); | 1351 &d); |
1284 req.set_method("POST"); | 1352 req.set_method("POST"); |
| 1353 req.set_upload(CreateSimpleUploadData(kData).get()); |
| 1354 req.SetExtraRequestHeaders( |
| 1355 "Content-Length: " + UintToString(sizeof(kData) - 1)); |
1285 req.Start(); | 1356 req.Start(); |
1286 MessageLoop::current()->Run(); | 1357 MessageLoop::current()->Run(); |
1287 EXPECT_EQ(req.method(), "POST"); | 1358 EXPECT_EQ("POST", req.method()); |
| 1359 EXPECT_EQ(kData, d.data_received()); |
1288 } | 1360 } |
1289 | 1361 |
1290 // Custom URLRequestJobs for use with interceptor tests | 1362 // Custom URLRequestJobs for use with interceptor tests |
1291 class RestartTestJob : public URLRequestTestJob { | 1363 class RestartTestJob : public URLRequestTestJob { |
1292 public: | 1364 public: |
1293 explicit RestartTestJob(URLRequest* request) | 1365 explicit RestartTestJob(URLRequest* request) |
1294 : URLRequestTestJob(request, true) {} | 1366 : URLRequestTestJob(request, true) {} |
1295 protected: | 1367 protected: |
1296 virtual void StartAsync() { | 1368 virtual void StartAsync() { |
1297 this->NotifyRestartRequired(); | 1369 this->NotifyRestartRequired(); |
(...skipping 541 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1839 | 1911 |
1840 int64 file_size = 0; | 1912 int64 file_size = 0; |
1841 file_util::GetFileSize(app_path, &file_size); | 1913 file_util::GetFileSize(app_path, &file_size); |
1842 | 1914 |
1843 EXPECT_TRUE(!r.is_pending()); | 1915 EXPECT_TRUE(!r.is_pending()); |
1844 EXPECT_EQ(1, d.response_started_count()); | 1916 EXPECT_EQ(1, d.response_started_count()); |
1845 EXPECT_FALSE(d.received_data_before_response()); | 1917 EXPECT_FALSE(d.received_data_before_response()); |
1846 EXPECT_EQ(d.bytes_received(), 0); | 1918 EXPECT_EQ(d.bytes_received(), 0); |
1847 } | 1919 } |
1848 } | 1920 } |
OLD | NEW |