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