OLD | NEW |
1 // Copyright (c) 2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 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/proxy/proxy_script_fetcher.h" | 5 #include "net/proxy/proxy_script_fetcher.h" |
6 | 6 |
7 #include "base/file_path.h" | 7 #include "base/file_path.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/compiler_specific.h" |
9 #include "base/path_service.h" | 9 #include "base/path_service.h" |
10 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
174 { // Fetch a file that exists. | 174 { // Fetch a file that exists. |
175 FetchResult result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt")); | 175 FetchResult result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt")); |
176 EXPECT_EQ(net::OK, result.code); | 176 EXPECT_EQ(net::OK, result.code); |
177 EXPECT_EQ("-pac.txt-\n", result.bytes); | 177 EXPECT_EQ("-pac.txt-\n", result.bytes); |
178 } | 178 } |
179 } | 179 } |
180 | 180 |
181 // Note that all mime types are allowed for PAC file, to be consistent | 181 // Note that all mime types are allowed for PAC file, to be consistent |
182 // with other browsers. | 182 // with other browsers. |
183 TEST_F(ProxyScriptFetcherTest, HttpMimeType) { | 183 TEST_F(ProxyScriptFetcherTest, HttpMimeType) { |
184 TestServer server(kDocRoot); | 184 scoped_refptr<HTTPTestServer> server = |
| 185 HTTPTestServer::CreateServer(kDocRoot); |
| 186 ASSERT_TRUE(NULL != server.get()); |
185 SynchFetcher pac_fetcher; | 187 SynchFetcher pac_fetcher; |
186 | 188 |
187 { // Fetch a PAC with mime type "text/plain" | 189 { // Fetch a PAC with mime type "text/plain" |
188 GURL url = server.TestServerPage("files/pac.txt"); | 190 GURL url = server->TestServerPage("files/pac.txt"); |
189 FetchResult result = pac_fetcher.Fetch(url); | 191 FetchResult result = pac_fetcher.Fetch(url); |
190 EXPECT_EQ(net::OK, result.code); | 192 EXPECT_EQ(net::OK, result.code); |
191 EXPECT_EQ("-pac.txt-\n", result.bytes); | 193 EXPECT_EQ("-pac.txt-\n", result.bytes); |
192 } | 194 } |
193 { // Fetch a PAC with mime type "text/html" | 195 { // Fetch a PAC with mime type "text/html" |
194 GURL url = server.TestServerPage("files/pac.html"); | 196 GURL url = server->TestServerPage("files/pac.html"); |
195 FetchResult result = pac_fetcher.Fetch(url); | 197 FetchResult result = pac_fetcher.Fetch(url); |
196 EXPECT_EQ(net::OK, result.code); | 198 EXPECT_EQ(net::OK, result.code); |
197 EXPECT_EQ("-pac.html-\n", result.bytes); | 199 EXPECT_EQ("-pac.html-\n", result.bytes); |
198 } | 200 } |
199 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig" | 201 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig" |
200 GURL url = server.TestServerPage("files/pac.nsproxy"); | 202 GURL url = server->TestServerPage("files/pac.nsproxy"); |
201 FetchResult result = pac_fetcher.Fetch(url); | 203 FetchResult result = pac_fetcher.Fetch(url); |
202 EXPECT_EQ(net::OK, result.code); | 204 EXPECT_EQ(net::OK, result.code); |
203 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); | 205 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); |
204 } | 206 } |
205 } | 207 } |
206 | 208 |
207 TEST_F(ProxyScriptFetcherTest, HttpStatusCode) { | 209 TEST_F(ProxyScriptFetcherTest, HttpStatusCode) { |
208 TestServer server(kDocRoot); | 210 scoped_refptr<HTTPTestServer> server = |
| 211 HTTPTestServer::CreateServer(kDocRoot); |
| 212 ASSERT_TRUE(NULL != server.get()); |
209 SynchFetcher pac_fetcher; | 213 SynchFetcher pac_fetcher; |
210 | 214 |
211 { // Fetch a PAC which gives a 500 -- FAIL | 215 { // Fetch a PAC which gives a 500 -- FAIL |
212 GURL url = server.TestServerPage("files/500.pac"); | 216 GURL url = server->TestServerPage("files/500.pac"); |
213 FetchResult result = pac_fetcher.Fetch(url); | 217 FetchResult result = pac_fetcher.Fetch(url); |
214 EXPECT_EQ(net::ERR_PAC_STATUS_NOT_OK, result.code); | 218 EXPECT_EQ(net::ERR_PAC_STATUS_NOT_OK, result.code); |
215 EXPECT_TRUE(result.bytes.empty()); | 219 EXPECT_TRUE(result.bytes.empty()); |
216 } | 220 } |
217 { // Fetch a PAC which gives a 404 -- FAIL | 221 { // Fetch a PAC which gives a 404 -- FAIL |
218 GURL url = server.TestServerPage("files/404.pac"); | 222 GURL url = server->TestServerPage("files/404.pac"); |
219 FetchResult result = pac_fetcher.Fetch(url); | 223 FetchResult result = pac_fetcher.Fetch(url); |
220 EXPECT_EQ(net::ERR_PAC_STATUS_NOT_OK, result.code); | 224 EXPECT_EQ(net::ERR_PAC_STATUS_NOT_OK, result.code); |
221 EXPECT_TRUE(result.bytes.empty()); | 225 EXPECT_TRUE(result.bytes.empty()); |
222 } | 226 } |
223 } | 227 } |
224 | 228 |
225 TEST_F(ProxyScriptFetcherTest, ContentDisposition) { | 229 TEST_F(ProxyScriptFetcherTest, ContentDisposition) { |
226 TestServer server(kDocRoot); | 230 scoped_refptr<HTTPTestServer> server = |
| 231 HTTPTestServer::CreateServer(kDocRoot); |
| 232 ASSERT_TRUE(NULL != server.get()); |
227 SynchFetcher pac_fetcher; | 233 SynchFetcher pac_fetcher; |
228 | 234 |
229 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should | 235 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should |
230 // have no effect. | 236 // have no effect. |
231 GURL url = server.TestServerPage("files/downloadable.pac"); | 237 GURL url = server->TestServerPage("files/downloadable.pac"); |
232 FetchResult result = pac_fetcher.Fetch(url); | 238 FetchResult result = pac_fetcher.Fetch(url); |
233 EXPECT_EQ(net::OK, result.code); | 239 EXPECT_EQ(net::OK, result.code); |
234 EXPECT_EQ("-downloadable.pac-\n", result.bytes); | 240 EXPECT_EQ("-downloadable.pac-\n", result.bytes); |
235 } | 241 } |
236 | 242 |
237 TEST_F(ProxyScriptFetcherTest, TooLarge) { | 243 TEST_F(ProxyScriptFetcherTest, TooLarge) { |
238 TestServer server(kDocRoot); | 244 scoped_refptr<HTTPTestServer> server = |
| 245 HTTPTestServer::CreateServer(kDocRoot); |
| 246 ASSERT_TRUE(NULL != server.get()); |
239 SynchFetcher pac_fetcher; | 247 SynchFetcher pac_fetcher; |
240 | 248 |
241 // Set the maximum response size to 50 bytes. | 249 // Set the maximum response size to 50 bytes. |
242 int prev_size = net::ProxyScriptFetcher::SetSizeConstraintForUnittest(50); | 250 int prev_size = net::ProxyScriptFetcher::SetSizeConstraintForUnittest(50); |
243 | 251 |
244 // These two URLs are the same file, but are http:// vs file:// | 252 // These two URLs are the same file, but are http:// vs file:// |
245 GURL urls[] = { | 253 GURL urls[] = { |
246 server.TestServerPage("files/large-pac.nsproxy"), | 254 server->TestServerPage("files/large-pac.nsproxy"), |
247 GetTestFileUrl("large-pac.nsproxy") | 255 GetTestFileUrl("large-pac.nsproxy") |
248 }; | 256 }; |
249 | 257 |
250 // Try fetching URLs that are 101 bytes large. We should abort the request | 258 // Try fetching URLs that are 101 bytes large. We should abort the request |
251 // after 50 bytes have been read, and fail with a too large error. | 259 // after 50 bytes have been read, and fail with a too large error. |
252 for (size_t i = 0; i < arraysize(urls); ++i) { | 260 for (size_t i = 0; i < arraysize(urls); ++i) { |
253 const GURL& url = urls[i]; | 261 const GURL& url = urls[i]; |
254 FetchResult result = pac_fetcher.Fetch(url); | 262 FetchResult result = pac_fetcher.Fetch(url); |
255 EXPECT_EQ(net::ERR_FILE_TOO_BIG, result.code); | 263 EXPECT_EQ(net::ERR_FILE_TOO_BIG, result.code); |
256 EXPECT_TRUE(result.bytes.empty()); | 264 EXPECT_TRUE(result.bytes.empty()); |
257 } | 265 } |
258 | 266 |
259 // Restore the original size bound. | 267 // Restore the original size bound. |
260 net::ProxyScriptFetcher::SetSizeConstraintForUnittest(prev_size); | 268 net::ProxyScriptFetcher::SetSizeConstraintForUnittest(prev_size); |
261 | 269 |
262 { // Make sure we can still fetch regular URLs. | 270 { // Make sure we can still fetch regular URLs. |
263 GURL url = server.TestServerPage("files/pac.nsproxy"); | 271 GURL url = server->TestServerPage("files/pac.nsproxy"); |
264 FetchResult result = pac_fetcher.Fetch(url); | 272 FetchResult result = pac_fetcher.Fetch(url); |
265 EXPECT_EQ(net::OK, result.code); | 273 EXPECT_EQ(net::OK, result.code); |
266 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); | 274 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); |
267 } | 275 } |
268 } | 276 } |
269 | 277 |
270 TEST_F(ProxyScriptFetcherTest, Hang) { | 278 TEST_F(ProxyScriptFetcherTest, Hang) { |
271 TestServer server(kDocRoot); | 279 scoped_refptr<HTTPTestServer> server = |
| 280 HTTPTestServer::CreateServer(kDocRoot); |
| 281 ASSERT_TRUE(NULL != server.get()); |
272 SynchFetcher pac_fetcher; | 282 SynchFetcher pac_fetcher; |
273 | 283 |
274 // Set the timeout period to 0.5 seconds. | 284 // Set the timeout period to 0.5 seconds. |
275 int prev_timeout = | 285 int prev_timeout = |
276 net::ProxyScriptFetcher::SetTimeoutConstraintForUnittest(500); | 286 net::ProxyScriptFetcher::SetTimeoutConstraintForUnittest(500); |
277 | 287 |
278 // Try fetching a URL which takes 1.2 seconds. We should abort the request | 288 // Try fetching a URL which takes 1.2 seconds. We should abort the request |
279 // after 500 ms, and fail with a timeout error. | 289 // after 500 ms, and fail with a timeout error. |
280 { GURL url = server.TestServerPage("slow/proxy.pac?1.2"); | 290 { GURL url = server->TestServerPage("slow/proxy.pac?1.2"); |
281 FetchResult result = pac_fetcher.Fetch(url); | 291 FetchResult result = pac_fetcher.Fetch(url); |
282 EXPECT_EQ(net::ERR_TIMED_OUT, result.code); | 292 EXPECT_EQ(net::ERR_TIMED_OUT, result.code); |
283 EXPECT_TRUE(result.bytes.empty()); | 293 EXPECT_TRUE(result.bytes.empty()); |
284 } | 294 } |
285 | 295 |
286 // Restore the original timeout period. | 296 // Restore the original timeout period. |
287 net::ProxyScriptFetcher::SetTimeoutConstraintForUnittest(prev_timeout); | 297 net::ProxyScriptFetcher::SetTimeoutConstraintForUnittest(prev_timeout); |
288 | 298 |
289 { // Make sure we can still fetch regular URLs. | 299 { // Make sure we can still fetch regular URLs. |
290 GURL url = server.TestServerPage("files/pac.nsproxy"); | 300 GURL url = server->TestServerPage("files/pac.nsproxy"); |
291 FetchResult result = pac_fetcher.Fetch(url); | 301 FetchResult result = pac_fetcher.Fetch(url); |
292 EXPECT_EQ(net::OK, result.code); | 302 EXPECT_EQ(net::OK, result.code); |
293 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); | 303 EXPECT_EQ("-pac.nsproxy-\n", result.bytes); |
294 } | 304 } |
295 } | 305 } |
296 | 306 |
297 } // namespace net | 307 } // namespace net |
OLD | NEW |