| OLD | NEW |
| 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> |
| 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/message_loop.h" | 19 #include "base/message_loop.h" |
| 20 #include "base/path_service.h" | 20 #include "base/path_service.h" |
| 21 #include "base/process_util.h" | 21 #include "base/process_util.h" |
| 22 #include "base/string_piece.h" | 22 #include "base/string_piece.h" |
| 23 #include "base/string_util.h" | 23 #include "base/string_util.h" |
| 24 #include "net/base/cookie_monster.h" | 24 #include "net/base/cookie_monster.h" |
| 25 #include "net/base/load_flags.h" | 25 #include "net/base/load_flags.h" |
| 26 #include "net/base/net_errors.h" | 26 #include "net/base/net_errors.h" |
| 27 #include "net/base/net_module.h" | 27 #include "net/base/net_module.h" |
| 28 #include "net/base/net_util.h" | 28 #include "net/base/net_util.h" |
| 29 #include "net/base/ssl_test_util.h" | 29 #include "net/base/ssl_test_util.h" |
| 30 #include "net/base/upload_data.h" | |
| 31 #include "net/disk_cache/disk_cache.h" | 30 #include "net/disk_cache/disk_cache.h" |
| 32 #include "net/http/http_cache.h" | 31 #include "net/http/http_cache.h" |
| 33 #include "net/http/http_network_layer.h" | 32 #include "net/http/http_network_layer.h" |
| 34 #include "net/http/http_response_headers.h" | 33 #include "net/http/http_response_headers.h" |
| 35 #include "net/proxy/proxy_service.h" | 34 #include "net/proxy/proxy_service.h" |
| 36 #include "net/url_request/url_request.h" | 35 #include "net/url_request/url_request.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 36 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "testing/platform_test.h" | 37 #include "testing/platform_test.h" |
| 39 | 38 |
| 40 using base::Time; | 39 using base::Time; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 75 bool ContainsString(const std::string& haystack, const char* needle) { | 74 bool ContainsString(const std::string& haystack, const char* needle) { |
| 76 std::string::const_iterator it = | 75 std::string::const_iterator it = |
| 77 std::search(haystack.begin(), | 76 std::search(haystack.begin(), |
| 78 haystack.end(), | 77 haystack.end(), |
| 79 needle, | 78 needle, |
| 80 needle + strlen(needle), | 79 needle + strlen(needle), |
| 81 CaseInsensitiveCompare<char>()); | 80 CaseInsensitiveCompare<char>()); |
| 82 return it != haystack.end(); | 81 return it != haystack.end(); |
| 83 } | 82 } |
| 84 | 83 |
| 85 scoped_refptr<net::UploadData> CreateSimpleUploadData(const char* data) { | |
| 86 scoped_refptr<net::UploadData> upload = new net::UploadData; | |
| 87 upload->AppendBytes(data, strlen(data)); | |
| 88 return upload; | |
| 89 } | |
| 90 | |
| 91 } // namespace | 84 } // namespace |
| 92 | 85 |
| 93 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f | 86 // Inherit PlatformTest since we require the autorelease pool on Mac OS X.f |
| 94 class URLRequestTest : public PlatformTest { | 87 class URLRequestTest : public PlatformTest { |
| 95 }; | 88 }; |
| 96 | 89 |
| 97 TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { | 90 TEST_F(URLRequestTest, ProxyTunnelRedirectTest) { |
| 98 // In this unit test, we're using the HTTPTestServer as a proxy server and | 91 // In this unit test, we're using the HTTPTestServer as a proxy server and |
| 99 // issuing a CONNECT request with the magic host name "www.redirect.com". | 92 // issuing a CONNECT request with the magic host name "www.redirect.com". |
| 100 // The HTTPTestServer will return a 302 response, which we should not | 93 // The HTTPTestServer will return a 302 response, which we should not |
| (...skipping 794 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 895 req.Start(); | 888 req.Start(); |
| 896 MessageLoop::current()->Run(); | 889 MessageLoop::current()->Run(); |
| 897 | 890 |
| 898 EXPECT_EQ(1, d.response_started_count()); | 891 EXPECT_EQ(1, d.response_started_count()); |
| 899 EXPECT_EQ(0, d.bytes_received()); | 892 EXPECT_EQ(0, d.bytes_received()); |
| 900 EXPECT_FALSE(d.received_data_before_response()); | 893 EXPECT_FALSE(d.received_data_before_response()); |
| 901 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); | 894 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); |
| 902 } | 895 } |
| 903 } | 896 } |
| 904 | 897 |
| 905 TEST_F(URLRequestTest, DeferredRedirect) { | |
| 906 scoped_refptr<HTTPTestServer> server = | |
| 907 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | |
| 908 ASSERT_TRUE(NULL != server.get()); | |
| 909 TestDelegate d; | |
| 910 { | |
| 911 d.set_quit_on_redirect(true); | |
| 912 TestURLRequest req(server->TestServerPage( | |
| 913 "files/redirect-test.html"), &d); | |
| 914 req.Start(); | |
| 915 MessageLoop::current()->Run(); | |
| 916 | |
| 917 EXPECT_EQ(1, d.received_redirect_count()); | |
| 918 | |
| 919 req.FollowDeferredRedirect(); | |
| 920 MessageLoop::current()->Run(); | |
| 921 | |
| 922 EXPECT_EQ(1, d.response_started_count()); | |
| 923 EXPECT_FALSE(d.received_data_before_response()); | |
| 924 EXPECT_EQ(URLRequestStatus::SUCCESS, req.status().status()); | |
| 925 | |
| 926 FilePath path; | |
| 927 PathService::Get(base::DIR_SOURCE_ROOT, &path); | |
| 928 path = path.Append(FILE_PATH_LITERAL("net")); | |
| 929 path = path.Append(FILE_PATH_LITERAL("data")); | |
| 930 path = path.Append(FILE_PATH_LITERAL("url_request_unittest")); | |
| 931 path = path.Append(FILE_PATH_LITERAL("with-headers.html")); | |
| 932 | |
| 933 std::string contents; | |
| 934 EXPECT_TRUE(file_util::ReadFileToString(path, &contents)); | |
| 935 EXPECT_EQ(contents, d.data_received()); | |
| 936 } | |
| 937 } | |
| 938 | |
| 939 TEST_F(URLRequestTest, CancelDeferredRedirect) { | |
| 940 scoped_refptr<HTTPTestServer> server = | |
| 941 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | |
| 942 ASSERT_TRUE(NULL != server.get()); | |
| 943 TestDelegate d; | |
| 944 { | |
| 945 d.set_quit_on_redirect(true); | |
| 946 TestURLRequest req(server->TestServerPage( | |
| 947 "files/redirect-test.html"), &d); | |
| 948 req.Start(); | |
| 949 MessageLoop::current()->Run(); | |
| 950 | |
| 951 EXPECT_EQ(1, d.received_redirect_count()); | |
| 952 | |
| 953 req.Cancel(); | |
| 954 MessageLoop::current()->Run(); | |
| 955 | |
| 956 EXPECT_EQ(1, d.response_started_count()); | |
| 957 EXPECT_EQ(0, d.bytes_received()); | |
| 958 EXPECT_FALSE(d.received_data_before_response()); | |
| 959 EXPECT_EQ(URLRequestStatus::CANCELED, req.status().status()); | |
| 960 } | |
| 961 } | |
| 962 | |
| 963 TEST_F(URLRequestTest, VaryHeader) { | 898 TEST_F(URLRequestTest, VaryHeader) { |
| 964 scoped_refptr<HTTPTestServer> server = | 899 scoped_refptr<HTTPTestServer> server = |
| 965 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | 900 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
| 966 ASSERT_TRUE(NULL != server.get()); | 901 ASSERT_TRUE(NULL != server.get()); |
| 967 | 902 |
| 968 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); | 903 scoped_refptr<URLRequestContext> context = new URLRequestHttpCacheContext(); |
| 969 | 904 |
| 970 Time response_time; | 905 Time response_time; |
| 971 | 906 |
| 972 // populate the cache | 907 // populate the cache |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1118 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") | 1053 EXPECT_TRUE(d.data_received().find("Cookie: got_challenged=true") |
| 1119 != std::string::npos); | 1054 != std::string::npos); |
| 1120 } | 1055 } |
| 1121 } | 1056 } |
| 1122 | 1057 |
| 1123 // In this test, we do a POST which the server will 302 redirect. | 1058 // In this test, we do a POST which the server will 302 redirect. |
| 1124 // The subsequent transaction should use GET, and should not send the | 1059 // The subsequent transaction should use GET, and should not send the |
| 1125 // Content-Type header. | 1060 // Content-Type header. |
| 1126 // http://code.google.com/p/chromium/issues/detail?id=843 | 1061 // http://code.google.com/p/chromium/issues/detail?id=843 |
| 1127 TEST_F(URLRequestTest, Post302RedirectGet) { | 1062 TEST_F(URLRequestTest, Post302RedirectGet) { |
| 1128 const char kData[] = "hello world"; | |
| 1129 scoped_refptr<HTTPTestServer> server = | 1063 scoped_refptr<HTTPTestServer> server = |
| 1130 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | 1064 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
| 1131 ASSERT_TRUE(NULL != server.get()); | 1065 ASSERT_TRUE(NULL != server.get()); |
| 1132 TestDelegate d; | 1066 TestDelegate d; |
| 1133 TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d); | 1067 TestURLRequest req(server->TestServerPage("files/redirect-to-echoall"), &d); |
| 1134 req.set_method("POST"); | 1068 req.set_method("POST"); |
| 1135 req.set_upload(CreateSimpleUploadData(kData)); | |
| 1136 | 1069 |
| 1137 // Set headers (some of which are specific to the POST). | 1070 // Set headers (some of which are specific to the POST). |
| 1071 // ("Content-Length: 10" is just a junk value to make sure it gets stripped). |
| 1138 req.SetExtraRequestHeaders( | 1072 req.SetExtraRequestHeaders( |
| 1139 "Content-Type: multipart/form-data; " | 1073 "Content-Type: multipart/form-data; " |
| 1140 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" | 1074 "boundary=----WebKitFormBoundaryAADeAA+NAAWMAAwZ\r\n" |
| 1141 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," | 1075 "Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9," |
| 1142 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" | 1076 "text/plain;q=0.8,image/png,*/*;q=0.5\r\n" |
| 1143 "Accept-Language: en-US,en\r\n" | 1077 "Accept-Language: en-US,en\r\n" |
| 1144 "Accept-Charset: ISO-8859-1,*,utf-8\r\n" | 1078 "Accept-Charset: ISO-8859-1,*,utf-8\r\n" |
| 1145 "Content-Length: 11\r\n" | 1079 "Content-Length: 10\r\n" |
| 1146 "Origin: http://localhost:1337/"); | 1080 "Origin: http://localhost:1337/"); |
| 1147 req.Start(); | 1081 req.Start(); |
| 1148 MessageLoop::current()->Run(); | 1082 MessageLoop::current()->Run(); |
| 1149 | 1083 |
| 1150 std::string mime_type; | 1084 std::string mime_type; |
| 1151 req.GetMimeType(&mime_type); | 1085 req.GetMimeType(&mime_type); |
| 1152 EXPECT_EQ("text/html", mime_type); | 1086 EXPECT_EQ("text/html", mime_type); |
| 1153 | 1087 |
| 1154 const std::string& data = d.data_received(); | 1088 const std::string& data = d.data_received(); |
| 1155 | 1089 |
| 1156 // Check that the post-specific headers were stripped: | 1090 // Check that the post-specific headers were stripped: |
| 1157 EXPECT_FALSE(ContainsString(data, "Content-Length:")); | 1091 EXPECT_FALSE(ContainsString(data, "Content-Length:")); |
| 1158 EXPECT_FALSE(ContainsString(data, "Content-Type:")); | 1092 EXPECT_FALSE(ContainsString(data, "Content-Type:")); |
| 1159 EXPECT_FALSE(ContainsString(data, "Origin:")); | 1093 EXPECT_FALSE(ContainsString(data, "Origin:")); |
| 1160 | 1094 |
| 1161 // These extra request headers should not have been stripped. | 1095 // These extra request headers should not have been stripped. |
| 1162 EXPECT_TRUE(ContainsString(data, "Accept:")); | 1096 EXPECT_TRUE(ContainsString(data, "Accept:")); |
| 1163 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); | 1097 EXPECT_TRUE(ContainsString(data, "Accept-Language:")); |
| 1164 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); | 1098 EXPECT_TRUE(ContainsString(data, "Accept-Charset:")); |
| 1165 } | 1099 } |
| 1166 | 1100 |
| 1167 // TODO(darin): Re-enable this test once bug 16832 is fixed. | 1101 TEST_F(URLRequestTest, Post307RedirectPost) { |
| 1168 TEST_F(URLRequestTest, DISABLED_Post307RedirectPost) { | |
| 1169 const char kData[] = "hello world"; | |
| 1170 scoped_refptr<HTTPTestServer> server = | 1102 scoped_refptr<HTTPTestServer> server = |
| 1171 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); | 1103 HTTPTestServer::CreateServer(L"net/data/url_request_unittest", NULL); |
| 1172 ASSERT_TRUE(NULL != server.get()); | 1104 ASSERT_TRUE(NULL != server.get()); |
| 1173 TestDelegate d; | 1105 TestDelegate d; |
| 1174 TestURLRequest req(server->TestServerPage("files/redirect307-to-echo"), | 1106 TestURLRequest req(server->TestServerPage("files/redirect307-to-echoall"), |
| 1175 &d); | 1107 &d); |
| 1176 req.set_method("POST"); | 1108 req.set_method("POST"); |
| 1177 req.set_upload(CreateSimpleUploadData(kData).get()); | |
| 1178 req.SetExtraRequestHeaders( | |
| 1179 "Content-Length: " + UintToString(sizeof(kData) - 1)); | |
| 1180 req.Start(); | 1109 req.Start(); |
| 1181 MessageLoop::current()->Run(); | 1110 MessageLoop::current()->Run(); |
| 1182 EXPECT_EQ("POST", req.method()); | 1111 EXPECT_EQ(req.method(), "POST"); |
| 1183 EXPECT_EQ(kData, d.data_received()); | |
| 1184 } | 1112 } |
| 1185 | 1113 |
| 1186 // FTP tests appear to be hanging some of the time | 1114 // FTP tests appear to be hanging some of the time |
| 1187 #if 1 // !defined(OS_WIN) | 1115 #if 1 // !defined(OS_WIN) |
| 1188 #define MAYBE_FTPGetTestAnonymous DISABLED_FTPGetTestAnonymous | 1116 #define MAYBE_FTPGetTestAnonymous DISABLED_FTPGetTestAnonymous |
| 1189 #define MAYBE_FTPGetTest DISABLED_FTPGetTest | 1117 #define MAYBE_FTPGetTest DISABLED_FTPGetTest |
| 1190 #define MAYBE_FTPCheckWrongUser DISABLED_FTPCheckWrongUser | 1118 #define MAYBE_FTPCheckWrongUser DISABLED_FTPCheckWrongUser |
| 1191 #define MAYBE_FTPCheckWrongPassword DISABLED_FTPCheckWrongPassword | 1119 #define MAYBE_FTPCheckWrongPassword DISABLED_FTPCheckWrongPassword |
| 1192 #else | 1120 #else |
| 1193 #define MAYBE_FTPGetTestAnonymous FTPGetTestAnonymous | 1121 #define MAYBE_FTPGetTestAnonymous FTPGetTestAnonymous |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1287 | 1215 |
| 1288 int64 file_size = 0; | 1216 int64 file_size = 0; |
| 1289 file_util::GetFileSize(app_path, &file_size); | 1217 file_util::GetFileSize(app_path, &file_size); |
| 1290 | 1218 |
| 1291 EXPECT_TRUE(!r.is_pending()); | 1219 EXPECT_TRUE(!r.is_pending()); |
| 1292 EXPECT_EQ(1, d.response_started_count()); | 1220 EXPECT_EQ(1, d.response_started_count()); |
| 1293 EXPECT_FALSE(d.received_data_before_response()); | 1221 EXPECT_FALSE(d.received_data_before_response()); |
| 1294 EXPECT_EQ(d.bytes_received(), 0); | 1222 EXPECT_EQ(d.bytes_received(), 0); |
| 1295 } | 1223 } |
| 1296 } | 1224 } |
| OLD | NEW |