| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_impl.h" | 5 #include "net/proxy/proxy_script_fetcher_impl.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/compiler_specific.h" | 10 #include "base/compiler_specific.h" |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 #include "net/cert/mock_cert_verifier.h" | 23 #include "net/cert/mock_cert_verifier.h" |
| 24 #include "net/cert/multi_log_ct_verifier.h" | 24 #include "net/cert/multi_log_ct_verifier.h" |
| 25 #include "net/disk_cache/disk_cache.h" | 25 #include "net/disk_cache/disk_cache.h" |
| 26 #include "net/dns/mock_host_resolver.h" | 26 #include "net/dns/mock_host_resolver.h" |
| 27 #include "net/http/http_cache.h" | 27 #include "net/http/http_cache.h" |
| 28 #include "net/http/http_network_session.h" | 28 #include "net/http/http_network_session.h" |
| 29 #include "net/http/http_server_properties_impl.h" | 29 #include "net/http/http_server_properties_impl.h" |
| 30 #include "net/http/transport_security_state.h" | 30 #include "net/http/transport_security_state.h" |
| 31 #include "net/ssl/ssl_config_service_defaults.h" | 31 #include "net/ssl/ssl_config_service_defaults.h" |
| 32 #include "net/test/embedded_test_server/embedded_test_server.h" | 32 #include "net/test/embedded_test_server/embedded_test_server.h" |
| 33 #include "net/test/gtest_util.h" |
| 33 #include "net/url_request/url_request_context_storage.h" | 34 #include "net/url_request/url_request_context_storage.h" |
| 34 #include "net/url_request/url_request_file_job.h" | 35 #include "net/url_request/url_request_file_job.h" |
| 35 #include "net/url_request/url_request_job_factory_impl.h" | 36 #include "net/url_request/url_request_job_factory_impl.h" |
| 36 #include "net/url_request/url_request_test_util.h" | 37 #include "net/url_request/url_request_test_util.h" |
| 38 #include "testing/gmock/include/gmock/gmock.h" |
| 37 #include "testing/gtest/include/gtest/gtest.h" | 39 #include "testing/gtest/include/gtest/gtest.h" |
| 38 #include "testing/platform_test.h" | 40 #include "testing/platform_test.h" |
| 39 | 41 |
| 40 #if !defined(DISABLE_FILE_SUPPORT) | 42 #if !defined(DISABLE_FILE_SUPPORT) |
| 41 #include "net/url_request/file_protocol_handler.h" | 43 #include "net/url_request/file_protocol_handler.h" |
| 42 #endif | 44 #endif |
| 43 | 45 |
| 46 using net::test::IsError; |
| 47 using net::test::IsOk; |
| 48 |
| 44 using base::ASCIIToUTF16; | 49 using base::ASCIIToUTF16; |
| 45 | 50 |
| 46 // TODO(eroman): | 51 // TODO(eroman): |
| 47 // - Test canceling an outstanding request. | 52 // - Test canceling an outstanding request. |
| 48 // - Test deleting ProxyScriptFetcher while a request is in progress. | 53 // - Test deleting ProxyScriptFetcher while a request is in progress. |
| 49 | 54 |
| 50 namespace net { | 55 namespace net { |
| 51 | 56 |
| 52 namespace { | 57 namespace { |
| 53 | 58 |
| (...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 | 215 |
| 211 #if !defined(DISABLE_FILE_SUPPORT) | 216 #if !defined(DISABLE_FILE_SUPPORT) |
| 212 TEST_F(ProxyScriptFetcherImplTest, FileUrl) { | 217 TEST_F(ProxyScriptFetcherImplTest, FileUrl) { |
| 213 ProxyScriptFetcherImpl pac_fetcher(&context_); | 218 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 214 | 219 |
| 215 { // Fetch a non-existent file. | 220 { // Fetch a non-existent file. |
| 216 base::string16 text; | 221 base::string16 text; |
| 217 TestCompletionCallback callback; | 222 TestCompletionCallback callback; |
| 218 int result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"), | 223 int result = pac_fetcher.Fetch(GetTestFileUrl("does-not-exist"), |
| 219 &text, callback.callback()); | 224 &text, callback.callback()); |
| 220 EXPECT_EQ(ERR_IO_PENDING, result); | 225 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 221 EXPECT_EQ(ERR_FILE_NOT_FOUND, callback.WaitForResult()); | 226 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_FILE_NOT_FOUND)); |
| 222 EXPECT_TRUE(text.empty()); | 227 EXPECT_TRUE(text.empty()); |
| 223 } | 228 } |
| 224 { // Fetch a file that exists. | 229 { // Fetch a file that exists. |
| 225 base::string16 text; | 230 base::string16 text; |
| 226 TestCompletionCallback callback; | 231 TestCompletionCallback callback; |
| 227 int result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"), | 232 int result = pac_fetcher.Fetch(GetTestFileUrl("pac.txt"), |
| 228 &text, callback.callback()); | 233 &text, callback.callback()); |
| 229 EXPECT_EQ(ERR_IO_PENDING, result); | 234 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 230 EXPECT_EQ(OK, callback.WaitForResult()); | 235 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 231 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); | 236 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); |
| 232 } | 237 } |
| 233 } | 238 } |
| 234 #endif // !defined(DISABLE_FILE_SUPPORT) | 239 #endif // !defined(DISABLE_FILE_SUPPORT) |
| 235 | 240 |
| 236 // Note that all mime types are allowed for PAC file, to be consistent | 241 // Note that all mime types are allowed for PAC file, to be consistent |
| 237 // with other browsers. | 242 // with other browsers. |
| 238 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) { | 243 TEST_F(ProxyScriptFetcherImplTest, HttpMimeType) { |
| 239 ASSERT_TRUE(test_server_.Start()); | 244 ASSERT_TRUE(test_server_.Start()); |
| 240 | 245 |
| 241 ProxyScriptFetcherImpl pac_fetcher(&context_); | 246 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 242 | 247 |
| 243 { // Fetch a PAC with mime type "text/plain" | 248 { // Fetch a PAC with mime type "text/plain" |
| 244 GURL url(test_server_.GetURL("/pac.txt")); | 249 GURL url(test_server_.GetURL("/pac.txt")); |
| 245 base::string16 text; | 250 base::string16 text; |
| 246 TestCompletionCallback callback; | 251 TestCompletionCallback callback; |
| 247 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 252 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 248 EXPECT_EQ(ERR_IO_PENDING, result); | 253 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 249 EXPECT_EQ(OK, callback.WaitForResult()); | 254 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 250 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); | 255 EXPECT_EQ(ASCIIToUTF16("-pac.txt-\n"), text); |
| 251 } | 256 } |
| 252 { // Fetch a PAC with mime type "text/html" | 257 { // Fetch a PAC with mime type "text/html" |
| 253 GURL url(test_server_.GetURL("/pac.html")); | 258 GURL url(test_server_.GetURL("/pac.html")); |
| 254 base::string16 text; | 259 base::string16 text; |
| 255 TestCompletionCallback callback; | 260 TestCompletionCallback callback; |
| 256 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 261 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 257 EXPECT_EQ(ERR_IO_PENDING, result); | 262 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 258 EXPECT_EQ(OK, callback.WaitForResult()); | 263 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 259 EXPECT_EQ(ASCIIToUTF16("-pac.html-\n"), text); | 264 EXPECT_EQ(ASCIIToUTF16("-pac.html-\n"), text); |
| 260 } | 265 } |
| 261 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig" | 266 { // Fetch a PAC with mime type "application/x-ns-proxy-autoconfig" |
| 262 GURL url(test_server_.GetURL("/pac.nsproxy")); | 267 GURL url(test_server_.GetURL("/pac.nsproxy")); |
| 263 base::string16 text; | 268 base::string16 text; |
| 264 TestCompletionCallback callback; | 269 TestCompletionCallback callback; |
| 265 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 270 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 266 EXPECT_EQ(ERR_IO_PENDING, result); | 271 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 267 EXPECT_EQ(OK, callback.WaitForResult()); | 272 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 268 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 273 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
| 269 } | 274 } |
| 270 } | 275 } |
| 271 | 276 |
| 272 TEST_F(ProxyScriptFetcherImplTest, HttpStatusCode) { | 277 TEST_F(ProxyScriptFetcherImplTest, HttpStatusCode) { |
| 273 ASSERT_TRUE(test_server_.Start()); | 278 ASSERT_TRUE(test_server_.Start()); |
| 274 | 279 |
| 275 ProxyScriptFetcherImpl pac_fetcher(&context_); | 280 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 276 | 281 |
| 277 { // Fetch a PAC which gives a 500 -- FAIL | 282 { // Fetch a PAC which gives a 500 -- FAIL |
| 278 GURL url(test_server_.GetURL("/500.pac")); | 283 GURL url(test_server_.GetURL("/500.pac")); |
| 279 base::string16 text; | 284 base::string16 text; |
| 280 TestCompletionCallback callback; | 285 TestCompletionCallback callback; |
| 281 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 286 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 282 EXPECT_EQ(ERR_IO_PENDING, result); | 287 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 283 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); | 288 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PAC_STATUS_NOT_OK)); |
| 284 EXPECT_TRUE(text.empty()); | 289 EXPECT_TRUE(text.empty()); |
| 285 } | 290 } |
| 286 { // Fetch a PAC which gives a 404 -- FAIL | 291 { // Fetch a PAC which gives a 404 -- FAIL |
| 287 GURL url(test_server_.GetURL("/404.pac")); | 292 GURL url(test_server_.GetURL("/404.pac")); |
| 288 base::string16 text; | 293 base::string16 text; |
| 289 TestCompletionCallback callback; | 294 TestCompletionCallback callback; |
| 290 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 295 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 291 EXPECT_EQ(ERR_IO_PENDING, result); | 296 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 292 EXPECT_EQ(ERR_PAC_STATUS_NOT_OK, callback.WaitForResult()); | 297 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_PAC_STATUS_NOT_OK)); |
| 293 EXPECT_TRUE(text.empty()); | 298 EXPECT_TRUE(text.empty()); |
| 294 } | 299 } |
| 295 } | 300 } |
| 296 | 301 |
| 297 TEST_F(ProxyScriptFetcherImplTest, ContentDisposition) { | 302 TEST_F(ProxyScriptFetcherImplTest, ContentDisposition) { |
| 298 ASSERT_TRUE(test_server_.Start()); | 303 ASSERT_TRUE(test_server_.Start()); |
| 299 | 304 |
| 300 ProxyScriptFetcherImpl pac_fetcher(&context_); | 305 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 301 | 306 |
| 302 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should | 307 // Fetch PAC scripts via HTTP with a Content-Disposition header -- should |
| 303 // have no effect. | 308 // have no effect. |
| 304 GURL url(test_server_.GetURL("/downloadable.pac")); | 309 GURL url(test_server_.GetURL("/downloadable.pac")); |
| 305 base::string16 text; | 310 base::string16 text; |
| 306 TestCompletionCallback callback; | 311 TestCompletionCallback callback; |
| 307 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 312 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 308 EXPECT_EQ(ERR_IO_PENDING, result); | 313 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 309 EXPECT_EQ(OK, callback.WaitForResult()); | 314 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 310 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text); | 315 EXPECT_EQ(ASCIIToUTF16("-downloadable.pac-\n"), text); |
| 311 } | 316 } |
| 312 | 317 |
| 313 // Verifies that PAC scripts are not being cached. | 318 // Verifies that PAC scripts are not being cached. |
| 314 TEST_F(ProxyScriptFetcherImplTest, NoCache) { | 319 TEST_F(ProxyScriptFetcherImplTest, NoCache) { |
| 315 ASSERT_TRUE(test_server_.Start()); | 320 ASSERT_TRUE(test_server_.Start()); |
| 316 | 321 |
| 317 ProxyScriptFetcherImpl pac_fetcher(&context_); | 322 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 318 | 323 |
| 319 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. | 324 // Fetch a PAC script whose HTTP headers make it cacheable for 1 hour. |
| 320 GURL url(test_server_.GetURL("/cacheable_1hr.pac")); | 325 GURL url(test_server_.GetURL("/cacheable_1hr.pac")); |
| 321 { | 326 { |
| 322 base::string16 text; | 327 base::string16 text; |
| 323 TestCompletionCallback callback; | 328 TestCompletionCallback callback; |
| 324 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 329 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 325 EXPECT_EQ(ERR_IO_PENDING, result); | 330 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 326 EXPECT_EQ(OK, callback.WaitForResult()); | 331 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 327 EXPECT_EQ(ASCIIToUTF16("-cacheable_1hr.pac-\n"), text); | 332 EXPECT_EQ(ASCIIToUTF16("-cacheable_1hr.pac-\n"), text); |
| 328 } | 333 } |
| 329 | 334 |
| 330 // Kill the HTTP server. | 335 // Kill the HTTP server. |
| 331 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); | 336 ASSERT_TRUE(test_server_.ShutdownAndWaitUntilComplete()); |
| 332 | 337 |
| 333 // Try to fetch the file again. Since the server is not running anymore, the | 338 // Try to fetch the file again. Since the server is not running anymore, the |
| 334 // call should fail, thus indicating that the file was not fetched from the | 339 // call should fail, thus indicating that the file was not fetched from the |
| 335 // local cache. | 340 // local cache. |
| 336 { | 341 { |
| 337 base::string16 text; | 342 base::string16 text; |
| 338 TestCompletionCallback callback; | 343 TestCompletionCallback callback; |
| 339 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 344 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 340 EXPECT_EQ(ERR_IO_PENDING, result); | 345 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 341 | 346 |
| 342 // Expect any error. The exact error varies by platform. | 347 // Expect any error. The exact error varies by platform. |
| 343 EXPECT_NE(OK, callback.WaitForResult()); | 348 EXPECT_NE(OK, callback.WaitForResult()); |
| 344 } | 349 } |
| 345 } | 350 } |
| 346 | 351 |
| 347 TEST_F(ProxyScriptFetcherImplTest, TooLarge) { | 352 TEST_F(ProxyScriptFetcherImplTest, TooLarge) { |
| 348 ASSERT_TRUE(test_server_.Start()); | 353 ASSERT_TRUE(test_server_.Start()); |
| 349 | 354 |
| 350 ProxyScriptFetcherImpl pac_fetcher(&context_); | 355 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 351 | 356 |
| 352 // Set the maximum response size to 50 bytes. | 357 // Set the maximum response size to 50 bytes. |
| 353 int prev_size = pac_fetcher.SetSizeConstraint(50); | 358 int prev_size = pac_fetcher.SetSizeConstraint(50); |
| 354 | 359 |
| 355 // These two URLs are the same file, but are http:// vs file:// | 360 // These two URLs are the same file, but are http:// vs file:// |
| 356 GURL urls[] = { | 361 GURL urls[] = { |
| 357 test_server_.GetURL("/large-pac.nsproxy"), | 362 test_server_.GetURL("/large-pac.nsproxy"), |
| 358 #if !defined(DISABLE_FILE_SUPPORT) | 363 #if !defined(DISABLE_FILE_SUPPORT) |
| 359 GetTestFileUrl("large-pac.nsproxy") | 364 GetTestFileUrl("large-pac.nsproxy") |
| 360 #endif | 365 #endif |
| 361 }; | 366 }; |
| 362 | 367 |
| 363 // Try fetching URLs that are 101 bytes large. We should abort the request | 368 // Try fetching URLs that are 101 bytes large. We should abort the request |
| 364 // after 50 bytes have been read, and fail with a too large error. | 369 // after 50 bytes have been read, and fail with a too large error. |
| 365 for (size_t i = 0; i < arraysize(urls); ++i) { | 370 for (size_t i = 0; i < arraysize(urls); ++i) { |
| 366 const GURL& url = urls[i]; | 371 const GURL& url = urls[i]; |
| 367 base::string16 text; | 372 base::string16 text; |
| 368 TestCompletionCallback callback; | 373 TestCompletionCallback callback; |
| 369 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 374 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 370 EXPECT_EQ(ERR_IO_PENDING, result); | 375 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 371 EXPECT_EQ(ERR_FILE_TOO_BIG, callback.WaitForResult()); | 376 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_FILE_TOO_BIG)); |
| 372 EXPECT_TRUE(text.empty()); | 377 EXPECT_TRUE(text.empty()); |
| 373 } | 378 } |
| 374 | 379 |
| 375 // Restore the original size bound. | 380 // Restore the original size bound. |
| 376 pac_fetcher.SetSizeConstraint(prev_size); | 381 pac_fetcher.SetSizeConstraint(prev_size); |
| 377 | 382 |
| 378 { // Make sure we can still fetch regular URLs. | 383 { // Make sure we can still fetch regular URLs. |
| 379 GURL url(test_server_.GetURL("/pac.nsproxy")); | 384 GURL url(test_server_.GetURL("/pac.nsproxy")); |
| 380 base::string16 text; | 385 base::string16 text; |
| 381 TestCompletionCallback callback; | 386 TestCompletionCallback callback; |
| 382 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 387 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 383 EXPECT_EQ(ERR_IO_PENDING, result); | 388 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 384 EXPECT_EQ(OK, callback.WaitForResult()); | 389 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 385 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 390 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
| 386 } | 391 } |
| 387 } | 392 } |
| 388 | 393 |
| 389 TEST_F(ProxyScriptFetcherImplTest, Hang) { | 394 TEST_F(ProxyScriptFetcherImplTest, Hang) { |
| 390 ASSERT_TRUE(test_server_.Start()); | 395 ASSERT_TRUE(test_server_.Start()); |
| 391 | 396 |
| 392 ProxyScriptFetcherImpl pac_fetcher(&context_); | 397 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 393 | 398 |
| 394 // Set the timeout period to 0.5 seconds. | 399 // Set the timeout period to 0.5 seconds. |
| 395 base::TimeDelta prev_timeout = pac_fetcher.SetTimeoutConstraint( | 400 base::TimeDelta prev_timeout = pac_fetcher.SetTimeoutConstraint( |
| 396 base::TimeDelta::FromMilliseconds(500)); | 401 base::TimeDelta::FromMilliseconds(500)); |
| 397 | 402 |
| 398 // Try fetching a URL which takes 1.2 seconds. We should abort the request | 403 // Try fetching a URL which takes 1.2 seconds. We should abort the request |
| 399 // after 500 ms, and fail with a timeout error. | 404 // after 500 ms, and fail with a timeout error. |
| 400 { | 405 { |
| 401 GURL url(test_server_.GetURL("/slow/proxy.pac?1.2")); | 406 GURL url(test_server_.GetURL("/slow/proxy.pac?1.2")); |
| 402 base::string16 text; | 407 base::string16 text; |
| 403 TestCompletionCallback callback; | 408 TestCompletionCallback callback; |
| 404 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 409 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 405 EXPECT_EQ(ERR_IO_PENDING, result); | 410 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 406 EXPECT_EQ(ERR_TIMED_OUT, callback.WaitForResult()); | 411 EXPECT_THAT(callback.WaitForResult(), IsError(ERR_TIMED_OUT)); |
| 407 EXPECT_TRUE(text.empty()); | 412 EXPECT_TRUE(text.empty()); |
| 408 } | 413 } |
| 409 | 414 |
| 410 // Restore the original timeout period. | 415 // Restore the original timeout period. |
| 411 pac_fetcher.SetTimeoutConstraint(prev_timeout); | 416 pac_fetcher.SetTimeoutConstraint(prev_timeout); |
| 412 | 417 |
| 413 { // Make sure we can still fetch regular URLs. | 418 { // Make sure we can still fetch regular URLs. |
| 414 GURL url(test_server_.GetURL("/pac.nsproxy")); | 419 GURL url(test_server_.GetURL("/pac.nsproxy")); |
| 415 base::string16 text; | 420 base::string16 text; |
| 416 TestCompletionCallback callback; | 421 TestCompletionCallback callback; |
| 417 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 422 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 418 EXPECT_EQ(ERR_IO_PENDING, result); | 423 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 419 EXPECT_EQ(OK, callback.WaitForResult()); | 424 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 420 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); | 425 EXPECT_EQ(ASCIIToUTF16("-pac.nsproxy-\n"), text); |
| 421 } | 426 } |
| 422 } | 427 } |
| 423 | 428 |
| 424 // The ProxyScriptFetcher should decode any content-codings | 429 // The ProxyScriptFetcher should decode any content-codings |
| 425 // (like gzip, bzip, etc.), and apply any charset conversions to yield | 430 // (like gzip, bzip, etc.), and apply any charset conversions to yield |
| 426 // UTF8. | 431 // UTF8. |
| 427 TEST_F(ProxyScriptFetcherImplTest, Encodings) { | 432 TEST_F(ProxyScriptFetcherImplTest, Encodings) { |
| 428 ASSERT_TRUE(test_server_.Start()); | 433 ASSERT_TRUE(test_server_.Start()); |
| 429 | 434 |
| 430 ProxyScriptFetcherImpl pac_fetcher(&context_); | 435 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 431 | 436 |
| 432 // Test a response that is gzip-encoded -- should get inflated. | 437 // Test a response that is gzip-encoded -- should get inflated. |
| 433 { | 438 { |
| 434 GURL url(test_server_.GetURL("/gzipped_pac")); | 439 GURL url(test_server_.GetURL("/gzipped_pac")); |
| 435 base::string16 text; | 440 base::string16 text; |
| 436 TestCompletionCallback callback; | 441 TestCompletionCallback callback; |
| 437 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 442 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 438 EXPECT_EQ(ERR_IO_PENDING, result); | 443 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 439 EXPECT_EQ(OK, callback.WaitForResult()); | 444 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 440 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text); | 445 EXPECT_EQ(ASCIIToUTF16("This data was gzipped.\n"), text); |
| 441 } | 446 } |
| 442 | 447 |
| 443 // Test a response that was served as UTF-16 (BE). It should | 448 // Test a response that was served as UTF-16 (BE). It should |
| 444 // be converted to UTF8. | 449 // be converted to UTF8. |
| 445 { | 450 { |
| 446 GURL url(test_server_.GetURL("/utf16be_pac")); | 451 GURL url(test_server_.GetURL("/utf16be_pac")); |
| 447 base::string16 text; | 452 base::string16 text; |
| 448 TestCompletionCallback callback; | 453 TestCompletionCallback callback; |
| 449 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 454 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 450 EXPECT_EQ(ERR_IO_PENDING, result); | 455 EXPECT_THAT(result, IsError(ERR_IO_PENDING)); |
| 451 EXPECT_EQ(OK, callback.WaitForResult()); | 456 EXPECT_THAT(callback.WaitForResult(), IsOk()); |
| 452 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text); | 457 EXPECT_EQ(ASCIIToUTF16("This was encoded as UTF-16BE.\n"), text); |
| 453 } | 458 } |
| 454 } | 459 } |
| 455 | 460 |
| 456 TEST_F(ProxyScriptFetcherImplTest, DataURLs) { | 461 TEST_F(ProxyScriptFetcherImplTest, DataURLs) { |
| 457 ProxyScriptFetcherImpl pac_fetcher(&context_); | 462 ProxyScriptFetcherImpl pac_fetcher(&context_); |
| 458 | 463 |
| 459 const char kEncodedUrl[] = | 464 const char kEncodedUrl[] = |
| 460 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R" | 465 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R" |
| 461 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl" | 466 "m9yVVJMKHVybCwgaG9zdCkgewogIGlmIChob3N0ID09ICdmb29iYXIuY29tJykKICAgIHJl" |
| 462 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0="; | 467 "dHVybiAnUFJPWFkgYmxhY2tob2xlOjgwJzsKICByZXR1cm4gJ0RJUkVDVCc7Cn0="; |
| 463 const char kPacScript[] = | 468 const char kPacScript[] = |
| 464 "function FindProxyForURL(url, host) {\n" | 469 "function FindProxyForURL(url, host) {\n" |
| 465 " if (host == 'foobar.com')\n" | 470 " if (host == 'foobar.com')\n" |
| 466 " return 'PROXY blackhole:80';\n" | 471 " return 'PROXY blackhole:80';\n" |
| 467 " return 'DIRECT';\n" | 472 " return 'DIRECT';\n" |
| 468 "}"; | 473 "}"; |
| 469 | 474 |
| 470 // Test fetching a "data:"-url containing a base64 encoded PAC script. | 475 // Test fetching a "data:"-url containing a base64 encoded PAC script. |
| 471 { | 476 { |
| 472 GURL url(kEncodedUrl); | 477 GURL url(kEncodedUrl); |
| 473 base::string16 text; | 478 base::string16 text; |
| 474 TestCompletionCallback callback; | 479 TestCompletionCallback callback; |
| 475 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 480 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 476 EXPECT_EQ(OK, result); | 481 EXPECT_THAT(result, IsOk()); |
| 477 EXPECT_EQ(ASCIIToUTF16(kPacScript), text); | 482 EXPECT_EQ(ASCIIToUTF16(kPacScript), text); |
| 478 } | 483 } |
| 479 | 484 |
| 480 const char kEncodedUrlBroken[] = | 485 const char kEncodedUrlBroken[] = |
| 481 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"; | 486 "data:application/x-ns-proxy-autoconfig;base64,ZnVuY3Rpb24gRmluZFByb3h5R"; |
| 482 | 487 |
| 483 // Test a broken "data:"-url containing a base64 encoded PAC script. | 488 // Test a broken "data:"-url containing a base64 encoded PAC script. |
| 484 { | 489 { |
| 485 GURL url(kEncodedUrlBroken); | 490 GURL url(kEncodedUrlBroken); |
| 486 base::string16 text; | 491 base::string16 text; |
| 487 TestCompletionCallback callback; | 492 TestCompletionCallback callback; |
| 488 int result = pac_fetcher.Fetch(url, &text, callback.callback()); | 493 int result = pac_fetcher.Fetch(url, &text, callback.callback()); |
| 489 EXPECT_EQ(ERR_FAILED, result); | 494 EXPECT_THAT(result, IsError(ERR_FAILED)); |
| 490 } | 495 } |
| 491 } | 496 } |
| 492 | 497 |
| 493 } // namespace | 498 } // namespace |
| 494 | 499 |
| 495 } // namespace net | 500 } // namespace net |
| OLD | NEW |