| 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 "build/build_config.h" | 5 #include "build/build_config.h" |
| 6 | 6 |
| 7 #if defined(OS_WIN) | 7 #if defined(OS_WIN) |
| 8 #include <windows.h> | 8 #include <windows.h> |
| 9 #include <shlobj.h> | 9 #include <shlobj.h> |
| 10 #endif | 10 #endif |
| (...skipping 360 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 371 }; | 371 }; |
| 372 | 372 |
| 373 | 373 |
| 374 // A simple Interceptor that returns a pre-built URLRequestJob one time. | 374 // A simple Interceptor that returns a pre-built URLRequestJob one time. |
| 375 class TestJobInterceptor : public URLRequestJobFactory::Interceptor { | 375 class TestJobInterceptor : public URLRequestJobFactory::Interceptor { |
| 376 public: | 376 public: |
| 377 TestJobInterceptor() | 377 TestJobInterceptor() |
| 378 : main_intercept_job_(NULL) { | 378 : main_intercept_job_(NULL) { |
| 379 } | 379 } |
| 380 | 380 |
| 381 virtual URLRequestJob* MaybeIntercept(URLRequest* request) const OVERRIDE { | 381 virtual URLRequestJob* MaybeIntercept( |
| 382 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE { |
| 382 URLRequestJob* job = main_intercept_job_; | 383 URLRequestJob* job = main_intercept_job_; |
| 383 main_intercept_job_ = NULL; | 384 main_intercept_job_ = NULL; |
| 384 return job; | 385 return job; |
| 385 } | 386 } |
| 386 | 387 |
| 387 virtual URLRequestJob* MaybeInterceptRedirect( | 388 virtual URLRequestJob* MaybeInterceptRedirect( |
| 388 const GURL& location, URLRequest* request) const OVERRIDE { | 389 const GURL& location, |
| 390 URLRequest* request, |
| 391 NetworkDelegate* network_delegate) const OVERRIDE { |
| 389 return NULL; | 392 return NULL; |
| 390 } | 393 } |
| 391 | 394 |
| 392 virtual URLRequestJob* MaybeInterceptResponse( | 395 virtual URLRequestJob* MaybeInterceptResponse( |
| 393 URLRequest* request) const OVERRIDE { | 396 URLRequest* request, NetworkDelegate* network_delegate) const OVERRIDE { |
| 394 return NULL; | 397 return NULL; |
| 395 } | 398 } |
| 396 | 399 |
| 397 void set_main_intercept_job(URLRequestJob* job) { | 400 void set_main_intercept_job(URLRequestJob* job) { |
| 398 main_intercept_job_ = job; | 401 main_intercept_job_ = job; |
| 399 } | 402 } |
| 400 | 403 |
| 401 private: | 404 private: |
| 402 mutable URLRequestJob* main_intercept_job_; | 405 mutable URLRequestJob* main_intercept_job_; |
| 403 }; | 406 }; |
| (...skipping 3149 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3553 | 3556 |
| 3554 TestDelegate d; | 3557 TestDelegate d; |
| 3555 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); | 3558 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); |
| 3556 req.set_method("POST"); | 3559 req.set_method("POST"); |
| 3557 req.set_upload(CreateSimpleUploadData(kData).get()); | 3560 req.set_upload(CreateSimpleUploadData(kData).get()); |
| 3558 HttpRequestHeaders headers; | 3561 HttpRequestHeaders headers; |
| 3559 headers.SetHeader(HttpRequestHeaders::kContentLength, | 3562 headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3560 base::UintToString(arraysize(kData) - 1)); | 3563 base::UintToString(arraysize(kData) - 1)); |
| 3561 req.SetExtraRequestHeaders(headers); | 3564 req.SetExtraRequestHeaders(headers); |
| 3562 | 3565 |
| 3563 URLRequestRedirectJob* job = | 3566 URLRequestRedirectJob* job = new URLRequestRedirectJob( |
| 3564 new URLRequestRedirectJob(&req, test_server_.GetURL("echo")); | 3567 &req, default_context_.network_delegate(), test_server_.GetURL("echo")); |
| 3565 AddTestInterceptor()->set_main_intercept_job(job); | 3568 AddTestInterceptor()->set_main_intercept_job(job); |
| 3566 | 3569 |
| 3567 req.Start(); | 3570 req.Start(); |
| 3568 MessageLoop::current()->Run(); | 3571 MessageLoop::current()->Run(); |
| 3569 EXPECT_EQ("GET", req.method()); | 3572 EXPECT_EQ("GET", req.method()); |
| 3570 } | 3573 } |
| 3571 | 3574 |
| 3572 TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { | 3575 TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 3573 ASSERT_TRUE(test_server_.Start()); | 3576 ASSERT_TRUE(test_server_.Start()); |
| 3574 | 3577 |
| 3575 const char kData[] = "hello world"; | 3578 const char kData[] = "hello world"; |
| 3576 | 3579 |
| 3577 TestDelegate d; | 3580 TestDelegate d; |
| 3578 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); | 3581 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); |
| 3579 req.set_method("POST"); | 3582 req.set_method("POST"); |
| 3580 req.set_upload(CreateSimpleUploadData(kData).get()); | 3583 req.set_upload(CreateSimpleUploadData(kData).get()); |
| 3581 HttpRequestHeaders headers; | 3584 HttpRequestHeaders headers; |
| 3582 headers.SetHeader(HttpRequestHeaders::kContentLength, | 3585 headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3583 base::UintToString(arraysize(kData) - 1)); | 3586 base::UintToString(arraysize(kData) - 1)); |
| 3584 req.SetExtraRequestHeaders(headers); | 3587 req.SetExtraRequestHeaders(headers); |
| 3585 | 3588 |
| 3586 URLRequestRedirectJob* job = | 3589 URLRequestRedirectJob* job = new URLRequestRedirectJob( |
| 3587 new URLRequestRedirectJob(&req, test_server_.GetURL("echo")); | 3590 &req, default_context_.network_delegate(), test_server_.GetURL("echo")); |
| 3588 job->set_redirect_code( | 3591 job->set_redirect_code( |
| 3589 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); | 3592 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); |
| 3590 AddTestInterceptor()->set_main_intercept_job(job); | 3593 AddTestInterceptor()->set_main_intercept_job(job); |
| 3591 | 3594 |
| 3592 req.Start(); | 3595 req.Start(); |
| 3593 MessageLoop::current()->Run(); | 3596 MessageLoop::current()->Run(); |
| 3594 EXPECT_EQ("POST", req.method()); | 3597 EXPECT_EQ("POST", req.method()); |
| 3595 EXPECT_EQ(kData, d.data_received()); | 3598 EXPECT_EQ(kData, d.data_received()); |
| 3596 } | 3599 } |
| 3597 | 3600 |
| 3598 // Custom URLRequestJobs for use with interceptor tests | 3601 // Custom URLRequestJobs for use with interceptor tests |
| 3599 class RestartTestJob : public URLRequestTestJob { | 3602 class RestartTestJob : public URLRequestTestJob { |
| 3600 public: | 3603 public: |
| 3601 explicit RestartTestJob(URLRequest* request) | 3604 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 3602 : URLRequestTestJob(request, true) {} | 3605 : URLRequestTestJob(request, network_delegate, true) {} |
| 3603 protected: | 3606 protected: |
| 3604 virtual void StartAsync() { | 3607 virtual void StartAsync() { |
| 3605 this->NotifyRestartRequired(); | 3608 this->NotifyRestartRequired(); |
| 3606 } | 3609 } |
| 3607 private: | 3610 private: |
| 3608 ~RestartTestJob() {} | 3611 ~RestartTestJob() {} |
| 3609 }; | 3612 }; |
| 3610 | 3613 |
| 3611 class CancelTestJob : public URLRequestTestJob { | 3614 class CancelTestJob : public URLRequestTestJob { |
| 3612 public: | 3615 public: |
| 3613 explicit CancelTestJob(URLRequest* request) | 3616 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 3614 : URLRequestTestJob(request, true) {} | 3617 : URLRequestTestJob(request, network_delegate, true) {} |
| 3615 protected: | 3618 protected: |
| 3616 virtual void StartAsync() { | 3619 virtual void StartAsync() { |
| 3617 request_->Cancel(); | 3620 request_->Cancel(); |
| 3618 } | 3621 } |
| 3619 private: | 3622 private: |
| 3620 ~CancelTestJob() {} | 3623 ~CancelTestJob() {} |
| 3621 }; | 3624 }; |
| 3622 | 3625 |
| 3623 class CancelThenRestartTestJob : public URLRequestTestJob { | 3626 class CancelThenRestartTestJob : public URLRequestTestJob { |
| 3624 public: | 3627 public: |
| 3625 explicit CancelThenRestartTestJob(URLRequest* request) | 3628 explicit CancelThenRestartTestJob(URLRequest* request, |
| 3626 : URLRequestTestJob(request, true) { | 3629 NetworkDelegate* network_delegate) |
| 3630 : URLRequestTestJob(request, network_delegate, true) { |
| 3627 } | 3631 } |
| 3628 protected: | 3632 protected: |
| 3629 virtual void StartAsync() { | 3633 virtual void StartAsync() { |
| 3630 request_->Cancel(); | 3634 request_->Cancel(); |
| 3631 this->NotifyRestartRequired(); | 3635 this->NotifyRestartRequired(); |
| 3632 } | 3636 } |
| 3633 private: | 3637 private: |
| 3634 ~CancelThenRestartTestJob() {} | 3638 ~CancelThenRestartTestJob() {} |
| 3635 }; | 3639 }; |
| 3636 | 3640 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 3648 did_simulate_error_main_(false), | 3652 did_simulate_error_main_(false), |
| 3649 did_intercept_redirect_(false), did_cancel_redirect_(false), | 3653 did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 3650 did_intercept_final_(false), did_cancel_final_(false) { | 3654 did_intercept_final_(false), did_cancel_final_(false) { |
| 3651 URLRequest::Deprecated::RegisterRequestInterceptor(this); | 3655 URLRequest::Deprecated::RegisterRequestInterceptor(this); |
| 3652 } | 3656 } |
| 3653 | 3657 |
| 3654 ~TestInterceptor() { | 3658 ~TestInterceptor() { |
| 3655 URLRequest::Deprecated::UnregisterRequestInterceptor(this); | 3659 URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
| 3656 } | 3660 } |
| 3657 | 3661 |
| 3658 virtual URLRequestJob* MaybeIntercept(URLRequest* request) { | 3662 virtual URLRequestJob* MaybeIntercept(URLRequest* request, |
| 3663 NetworkDelegate* network_delegate) { |
| 3659 if (restart_main_request_) { | 3664 if (restart_main_request_) { |
| 3660 restart_main_request_ = false; | 3665 restart_main_request_ = false; |
| 3661 did_restart_main_ = true; | 3666 did_restart_main_ = true; |
| 3662 return new RestartTestJob(request); | 3667 return new RestartTestJob(request, network_delegate); |
| 3663 } | 3668 } |
| 3664 if (cancel_main_request_) { | 3669 if (cancel_main_request_) { |
| 3665 cancel_main_request_ = false; | 3670 cancel_main_request_ = false; |
| 3666 did_cancel_main_ = true; | 3671 did_cancel_main_ = true; |
| 3667 return new CancelTestJob(request); | 3672 return new CancelTestJob(request, network_delegate); |
| 3668 } | 3673 } |
| 3669 if (cancel_then_restart_main_request_) { | 3674 if (cancel_then_restart_main_request_) { |
| 3670 cancel_then_restart_main_request_ = false; | 3675 cancel_then_restart_main_request_ = false; |
| 3671 did_cancel_then_restart_main_ = true; | 3676 did_cancel_then_restart_main_ = true; |
| 3672 return new CancelThenRestartTestJob(request); | 3677 return new CancelThenRestartTestJob(request, network_delegate); |
| 3673 } | 3678 } |
| 3674 if (simulate_main_network_error_) { | 3679 if (simulate_main_network_error_) { |
| 3675 simulate_main_network_error_ = false; | 3680 simulate_main_network_error_ = false; |
| 3676 did_simulate_error_main_ = true; | 3681 did_simulate_error_main_ = true; |
| 3677 // will error since the requeted url is not one of its canned urls | 3682 // will error since the requeted url is not one of its canned urls |
| 3678 return new URLRequestTestJob(request, true); | 3683 return new URLRequestTestJob(request, network_delegate, true); |
| 3679 } | 3684 } |
| 3680 if (!intercept_main_request_) | 3685 if (!intercept_main_request_) |
| 3681 return NULL; | 3686 return NULL; |
| 3682 intercept_main_request_ = false; | 3687 intercept_main_request_ = false; |
| 3683 did_intercept_main_ = true; | 3688 did_intercept_main_ = true; |
| 3684 return new URLRequestTestJob(request, | 3689 return new URLRequestTestJob(request, |
| 3685 main_headers_, | 3690 network_delegate, |
| 3686 main_data_, | 3691 main_headers_, |
| 3687 true); | 3692 main_data_, |
| 3693 true); |
| 3688 } | 3694 } |
| 3689 | 3695 |
| 3690 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request, | 3696 virtual URLRequestJob* MaybeInterceptRedirect( |
| 3691 const GURL& location) { | 3697 URLRequest* request, |
| 3698 NetworkDelegate* network_delegate, |
| 3699 const GURL& location) { |
| 3692 if (cancel_redirect_request_) { | 3700 if (cancel_redirect_request_) { |
| 3693 cancel_redirect_request_ = false; | 3701 cancel_redirect_request_ = false; |
| 3694 did_cancel_redirect_ = true; | 3702 did_cancel_redirect_ = true; |
| 3695 return new CancelTestJob(request); | 3703 return new CancelTestJob(request, network_delegate); |
| 3696 } | 3704 } |
| 3697 if (!intercept_redirect_) | 3705 if (!intercept_redirect_) |
| 3698 return NULL; | 3706 return NULL; |
| 3699 intercept_redirect_ = false; | 3707 intercept_redirect_ = false; |
| 3700 did_intercept_redirect_ = true; | 3708 did_intercept_redirect_ = true; |
| 3701 return new URLRequestTestJob(request, | 3709 return new URLRequestTestJob(request, |
| 3702 redirect_headers_, | 3710 network_delegate, |
| 3703 redirect_data_, | 3711 redirect_headers_, |
| 3704 true); | 3712 redirect_data_, |
| 3713 true); |
| 3705 } | 3714 } |
| 3706 | 3715 |
| 3707 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) { | 3716 virtual URLRequestJob* MaybeInterceptResponse( |
| 3717 URLRequest* request, NetworkDelegate* network_delegate) { |
| 3708 if (cancel_final_request_) { | 3718 if (cancel_final_request_) { |
| 3709 cancel_final_request_ = false; | 3719 cancel_final_request_ = false; |
| 3710 did_cancel_final_ = true; | 3720 did_cancel_final_ = true; |
| 3711 return new CancelTestJob(request); | 3721 return new CancelTestJob(request, network_delegate); |
| 3712 } | 3722 } |
| 3713 if (!intercept_final_response_) | 3723 if (!intercept_final_response_) |
| 3714 return NULL; | 3724 return NULL; |
| 3715 intercept_final_response_ = false; | 3725 intercept_final_response_ = false; |
| 3716 did_intercept_final_ = true; | 3726 did_intercept_final_ = true; |
| 3717 return new URLRequestTestJob(request, | 3727 return new URLRequestTestJob(request, |
| 3718 final_headers_, | 3728 network_delegate, |
| 3719 final_data_, | 3729 final_headers_, |
| 3720 true); | 3730 final_data_, |
| 3731 true); |
| 3721 } | 3732 } |
| 3722 | 3733 |
| 3723 // Whether to intercept the main request, and if so the response to return. | 3734 // Whether to intercept the main request, and if so the response to return. |
| 3724 bool intercept_main_request_; | 3735 bool intercept_main_request_; |
| 3725 std::string main_headers_; | 3736 std::string main_headers_; |
| 3726 std::string main_data_; | 3737 std::string main_data_; |
| 3727 | 3738 |
| 3728 // Other actions we take at MaybeIntercept time | 3739 // Other actions we take at MaybeIntercept time |
| 3729 bool restart_main_request_; | 3740 bool restart_main_request_; |
| 3730 bool cancel_main_request_; | 3741 bool cancel_main_request_; |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4154 }; | 4165 }; |
| 4155 | 4166 |
| 4156 // Make sure an FTP request using an unsafe ports fails. | 4167 // Make sure an FTP request using an unsafe ports fails. |
| 4157 TEST_F(URLRequestTestFTP, UnsafePort) { | 4168 TEST_F(URLRequestTestFTP, UnsafePort) { |
| 4158 ASSERT_TRUE(test_server_.Start()); | 4169 ASSERT_TRUE(test_server_.Start()); |
| 4159 | 4170 |
| 4160 URLRequestJobFactory job_factory; | 4171 URLRequestJobFactory job_factory; |
| 4161 | 4172 |
| 4162 GURL url("ftp://127.0.0.1:7"); | 4173 GURL url("ftp://127.0.0.1:7"); |
| 4163 FtpProtocolHandler ftp_protocol_handler( | 4174 FtpProtocolHandler ftp_protocol_handler( |
| 4164 default_context_.network_delegate(), | |
| 4165 default_context_.ftp_transaction_factory(), | 4175 default_context_.ftp_transaction_factory(), |
| 4166 default_context_.ftp_auth_cache()); | 4176 default_context_.ftp_auth_cache()); |
| 4167 job_factory.SetProtocolHandler( | 4177 job_factory.SetProtocolHandler( |
| 4168 "ftp", | 4178 "ftp", |
| 4169 new FtpProtocolHandler(default_context_.network_delegate(), | 4179 new FtpProtocolHandler(default_context_.ftp_transaction_factory(), |
| 4170 default_context_.ftp_transaction_factory(), | |
| 4171 default_context_.ftp_auth_cache())); | 4180 default_context_.ftp_auth_cache())); |
| 4172 default_context_.set_job_factory(&job_factory); | 4181 default_context_.set_job_factory(&job_factory); |
| 4173 | 4182 |
| 4174 TestDelegate d; | 4183 TestDelegate d; |
| 4175 { | 4184 { |
| 4176 URLRequest r(url, &d, &default_context_); | 4185 URLRequest r(url, &d, &default_context_); |
| 4177 r.Start(); | 4186 r.Start(); |
| 4178 EXPECT_TRUE(r.is_pending()); | 4187 EXPECT_TRUE(r.is_pending()); |
| 4179 | 4188 |
| 4180 MessageLoop::current()->Run(); | 4189 MessageLoop::current()->Run(); |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4672 req.SetExtraRequestHeaders(headers); | 4681 req.SetExtraRequestHeaders(headers); |
| 4673 req.Start(); | 4682 req.Start(); |
| 4674 MessageLoop::current()->Run(); | 4683 MessageLoop::current()->Run(); |
| 4675 // If the net tests are being run with ChromeFrame then we need to allow for | 4684 // If the net tests are being run with ChromeFrame then we need to allow for |
| 4676 // the 'chromeframe' suffix which is added to the user agent before the | 4685 // the 'chromeframe' suffix which is added to the user agent before the |
| 4677 // closing parentheses. | 4686 // closing parentheses. |
| 4678 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); | 4687 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); |
| 4679 } | 4688 } |
| 4680 | 4689 |
| 4681 } // namespace net | 4690 } // namespace net |
| OLD | NEW |