| 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, URLRequest* request) const OVERRIDE { |
| 389 return NULL; | 390 return NULL; |
| 390 } | 391 } |
| 391 | 392 |
| (...skipping 3147 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 3539 | 3540 |
| 3540 TestDelegate d; | 3541 TestDelegate d; |
| 3541 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); | 3542 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); |
| 3542 req.set_method("POST"); | 3543 req.set_method("POST"); |
| 3543 req.set_upload(CreateSimpleUploadData(kData).get()); | 3544 req.set_upload(CreateSimpleUploadData(kData).get()); |
| 3544 HttpRequestHeaders headers; | 3545 HttpRequestHeaders headers; |
| 3545 headers.SetHeader(HttpRequestHeaders::kContentLength, | 3546 headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3546 base::UintToString(arraysize(kData) - 1)); | 3547 base::UintToString(arraysize(kData) - 1)); |
| 3547 req.SetExtraRequestHeaders(headers); | 3548 req.SetExtraRequestHeaders(headers); |
| 3548 | 3549 |
| 3549 URLRequestRedirectJob* job = | 3550 URLRequestRedirectJob* job = new URLRequestRedirectJob( |
| 3550 new URLRequestRedirectJob(&req, test_server_.GetURL("echo")); | 3551 &req, default_context_.network_delegate(), test_server_.GetURL("echo")); |
| 3551 AddTestInterceptor()->set_main_intercept_job(job); | 3552 AddTestInterceptor()->set_main_intercept_job(job); |
| 3552 | 3553 |
| 3553 req.Start(); | 3554 req.Start(); |
| 3554 MessageLoop::current()->Run(); | 3555 MessageLoop::current()->Run(); |
| 3555 EXPECT_EQ("GET", req.method()); | 3556 EXPECT_EQ("GET", req.method()); |
| 3556 } | 3557 } |
| 3557 | 3558 |
| 3558 TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { | 3559 TEST_F(URLRequestTestHTTP, InterceptPost307RedirectPost) { |
| 3559 ASSERT_TRUE(test_server_.Start()); | 3560 ASSERT_TRUE(test_server_.Start()); |
| 3560 | 3561 |
| 3561 const char kData[] = "hello world"; | 3562 const char kData[] = "hello world"; |
| 3562 | 3563 |
| 3563 TestDelegate d; | 3564 TestDelegate d; |
| 3564 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); | 3565 URLRequest req(test_server_.GetURL("empty.html"), &d, &default_context_); |
| 3565 req.set_method("POST"); | 3566 req.set_method("POST"); |
| 3566 req.set_upload(CreateSimpleUploadData(kData).get()); | 3567 req.set_upload(CreateSimpleUploadData(kData).get()); |
| 3567 HttpRequestHeaders headers; | 3568 HttpRequestHeaders headers; |
| 3568 headers.SetHeader(HttpRequestHeaders::kContentLength, | 3569 headers.SetHeader(HttpRequestHeaders::kContentLength, |
| 3569 base::UintToString(arraysize(kData) - 1)); | 3570 base::UintToString(arraysize(kData) - 1)); |
| 3570 req.SetExtraRequestHeaders(headers); | 3571 req.SetExtraRequestHeaders(headers); |
| 3571 | 3572 |
| 3572 URLRequestRedirectJob* job = | 3573 URLRequestRedirectJob* job = new URLRequestRedirectJob( |
| 3573 new URLRequestRedirectJob(&req, test_server_.GetURL("echo")); | 3574 &req, default_context_.network_delegate(), test_server_.GetURL("echo")); |
| 3574 job->set_redirect_code( | 3575 job->set_redirect_code( |
| 3575 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); | 3576 URLRequestRedirectJob::REDIRECT_307_TEMPORARY_REDIRECT); |
| 3576 AddTestInterceptor()->set_main_intercept_job(job); | 3577 AddTestInterceptor()->set_main_intercept_job(job); |
| 3577 | 3578 |
| 3578 req.Start(); | 3579 req.Start(); |
| 3579 MessageLoop::current()->Run(); | 3580 MessageLoop::current()->Run(); |
| 3580 EXPECT_EQ("POST", req.method()); | 3581 EXPECT_EQ("POST", req.method()); |
| 3581 EXPECT_EQ(kData, d.data_received()); | 3582 EXPECT_EQ(kData, d.data_received()); |
| 3582 } | 3583 } |
| 3583 | 3584 |
| 3584 // Custom URLRequestJobs for use with interceptor tests | 3585 // Custom URLRequestJobs for use with interceptor tests |
| 3585 class RestartTestJob : public URLRequestTestJob { | 3586 class RestartTestJob : public URLRequestTestJob { |
| 3586 public: | 3587 public: |
| 3587 explicit RestartTestJob(URLRequest* request) | 3588 RestartTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 3588 : URLRequestTestJob(request, true) {} | 3589 : URLRequestTestJob(request, network_delegate, true) {} |
| 3589 protected: | 3590 protected: |
| 3590 virtual void StartAsync() { | 3591 virtual void StartAsync() { |
| 3591 this->NotifyRestartRequired(); | 3592 this->NotifyRestartRequired(); |
| 3592 } | 3593 } |
| 3593 private: | 3594 private: |
| 3594 ~RestartTestJob() {} | 3595 ~RestartTestJob() {} |
| 3595 }; | 3596 }; |
| 3596 | 3597 |
| 3597 class CancelTestJob : public URLRequestTestJob { | 3598 class CancelTestJob : public URLRequestTestJob { |
| 3598 public: | 3599 public: |
| 3599 explicit CancelTestJob(URLRequest* request) | 3600 explicit CancelTestJob(URLRequest* request, NetworkDelegate* network_delegate) |
| 3600 : URLRequestTestJob(request, true) {} | 3601 : URLRequestTestJob(request, network_delegate, true) {} |
| 3601 protected: | 3602 protected: |
| 3602 virtual void StartAsync() { | 3603 virtual void StartAsync() { |
| 3603 request_->Cancel(); | 3604 request_->Cancel(); |
| 3604 } | 3605 } |
| 3605 private: | 3606 private: |
| 3606 ~CancelTestJob() {} | 3607 ~CancelTestJob() {} |
| 3607 }; | 3608 }; |
| 3608 | 3609 |
| 3609 class CancelThenRestartTestJob : public URLRequestTestJob { | 3610 class CancelThenRestartTestJob : public URLRequestTestJob { |
| 3610 public: | 3611 public: |
| 3611 explicit CancelThenRestartTestJob(URLRequest* request) | 3612 explicit CancelThenRestartTestJob(URLRequest* request, |
| 3612 : URLRequestTestJob(request, true) { | 3613 NetworkDelegate* network_delegate) |
| 3614 : URLRequestTestJob(request, network_delegate, true) { |
| 3613 } | 3615 } |
| 3614 protected: | 3616 protected: |
| 3615 virtual void StartAsync() { | 3617 virtual void StartAsync() { |
| 3616 request_->Cancel(); | 3618 request_->Cancel(); |
| 3617 this->NotifyRestartRequired(); | 3619 this->NotifyRestartRequired(); |
| 3618 } | 3620 } |
| 3619 private: | 3621 private: |
| 3620 ~CancelThenRestartTestJob() {} | 3622 ~CancelThenRestartTestJob() {} |
| 3621 }; | 3623 }; |
| 3622 | 3624 |
| (...skipping 11 matching lines...) Expand all Loading... |
| 3634 did_simulate_error_main_(false), | 3636 did_simulate_error_main_(false), |
| 3635 did_intercept_redirect_(false), did_cancel_redirect_(false), | 3637 did_intercept_redirect_(false), did_cancel_redirect_(false), |
| 3636 did_intercept_final_(false), did_cancel_final_(false) { | 3638 did_intercept_final_(false), did_cancel_final_(false) { |
| 3637 URLRequest::Deprecated::RegisterRequestInterceptor(this); | 3639 URLRequest::Deprecated::RegisterRequestInterceptor(this); |
| 3638 } | 3640 } |
| 3639 | 3641 |
| 3640 ~TestInterceptor() { | 3642 ~TestInterceptor() { |
| 3641 URLRequest::Deprecated::UnregisterRequestInterceptor(this); | 3643 URLRequest::Deprecated::UnregisterRequestInterceptor(this); |
| 3642 } | 3644 } |
| 3643 | 3645 |
| 3644 virtual URLRequestJob* MaybeIntercept(URLRequest* request) { | 3646 virtual URLRequestJob* MaybeIntercept(URLRequest* request, |
| 3647 NetworkDelegate* network_delegate) { |
| 3645 if (restart_main_request_) { | 3648 if (restart_main_request_) { |
| 3646 restart_main_request_ = false; | 3649 restart_main_request_ = false; |
| 3647 did_restart_main_ = true; | 3650 did_restart_main_ = true; |
| 3648 return new RestartTestJob(request); | 3651 return new RestartTestJob(request, network_delegate); |
| 3649 } | 3652 } |
| 3650 if (cancel_main_request_) { | 3653 if (cancel_main_request_) { |
| 3651 cancel_main_request_ = false; | 3654 cancel_main_request_ = false; |
| 3652 did_cancel_main_ = true; | 3655 did_cancel_main_ = true; |
| 3653 return new CancelTestJob(request); | 3656 return new CancelTestJob(request, network_delegate); |
| 3654 } | 3657 } |
| 3655 if (cancel_then_restart_main_request_) { | 3658 if (cancel_then_restart_main_request_) { |
| 3656 cancel_then_restart_main_request_ = false; | 3659 cancel_then_restart_main_request_ = false; |
| 3657 did_cancel_then_restart_main_ = true; | 3660 did_cancel_then_restart_main_ = true; |
| 3658 return new CancelThenRestartTestJob(request); | 3661 return new CancelThenRestartTestJob(request, network_delegate); |
| 3659 } | 3662 } |
| 3660 if (simulate_main_network_error_) { | 3663 if (simulate_main_network_error_) { |
| 3661 simulate_main_network_error_ = false; | 3664 simulate_main_network_error_ = false; |
| 3662 did_simulate_error_main_ = true; | 3665 did_simulate_error_main_ = true; |
| 3663 // will error since the requeted url is not one of its canned urls | 3666 // will error since the requeted url is not one of its canned urls |
| 3664 return new URLRequestTestJob(request, true); | 3667 return new URLRequestTestJob(request, network_delegate, true); |
| 3665 } | 3668 } |
| 3666 if (!intercept_main_request_) | 3669 if (!intercept_main_request_) |
| 3667 return NULL; | 3670 return NULL; |
| 3668 intercept_main_request_ = false; | 3671 intercept_main_request_ = false; |
| 3669 did_intercept_main_ = true; | 3672 did_intercept_main_ = true; |
| 3670 return new URLRequestTestJob(request, | 3673 return new URLRequestTestJob(request, |
| 3671 main_headers_, | 3674 network_delegate, |
| 3672 main_data_, | 3675 main_headers_, |
| 3673 true); | 3676 main_data_, |
| 3677 true); |
| 3674 } | 3678 } |
| 3675 | 3679 |
| 3676 virtual URLRequestJob* MaybeInterceptRedirect(URLRequest* request, | 3680 virtual URLRequestJob* MaybeInterceptRedirect( |
| 3677 const GURL& location) { | 3681 URLRequest* request, |
| 3682 NetworkDelegate* network_delegate, |
| 3683 const GURL& location) { |
| 3678 if (cancel_redirect_request_) { | 3684 if (cancel_redirect_request_) { |
| 3679 cancel_redirect_request_ = false; | 3685 cancel_redirect_request_ = false; |
| 3680 did_cancel_redirect_ = true; | 3686 did_cancel_redirect_ = true; |
| 3681 return new CancelTestJob(request); | 3687 return new CancelTestJob(request, network_delegate); |
| 3682 } | 3688 } |
| 3683 if (!intercept_redirect_) | 3689 if (!intercept_redirect_) |
| 3684 return NULL; | 3690 return NULL; |
| 3685 intercept_redirect_ = false; | 3691 intercept_redirect_ = false; |
| 3686 did_intercept_redirect_ = true; | 3692 did_intercept_redirect_ = true; |
| 3687 return new URLRequestTestJob(request, | 3693 return new URLRequestTestJob(request, |
| 3688 redirect_headers_, | 3694 network_delegate, |
| 3689 redirect_data_, | 3695 redirect_headers_, |
| 3690 true); | 3696 redirect_data_, |
| 3697 true); |
| 3691 } | 3698 } |
| 3692 | 3699 |
| 3693 virtual URLRequestJob* MaybeInterceptResponse(URLRequest* request) { | 3700 virtual URLRequestJob* MaybeInterceptResponse( |
| 3701 URLRequest* request, NetworkDelegate* network_delegate) { |
| 3694 if (cancel_final_request_) { | 3702 if (cancel_final_request_) { |
| 3695 cancel_final_request_ = false; | 3703 cancel_final_request_ = false; |
| 3696 did_cancel_final_ = true; | 3704 did_cancel_final_ = true; |
| 3697 return new CancelTestJob(request); | 3705 return new CancelTestJob(request, network_delegate); |
| 3698 } | 3706 } |
| 3699 if (!intercept_final_response_) | 3707 if (!intercept_final_response_) |
| 3700 return NULL; | 3708 return NULL; |
| 3701 intercept_final_response_ = false; | 3709 intercept_final_response_ = false; |
| 3702 did_intercept_final_ = true; | 3710 did_intercept_final_ = true; |
| 3703 return new URLRequestTestJob(request, | 3711 return new URLRequestTestJob(request, |
| 3704 final_headers_, | 3712 network_delegate, |
| 3705 final_data_, | 3713 final_headers_, |
| 3706 true); | 3714 final_data_, |
| 3715 true); |
| 3707 } | 3716 } |
| 3708 | 3717 |
| 3709 // Whether to intercept the main request, and if so the response to return. | 3718 // Whether to intercept the main request, and if so the response to return. |
| 3710 bool intercept_main_request_; | 3719 bool intercept_main_request_; |
| 3711 std::string main_headers_; | 3720 std::string main_headers_; |
| 3712 std::string main_data_; | 3721 std::string main_data_; |
| 3713 | 3722 |
| 3714 // Other actions we take at MaybeIntercept time | 3723 // Other actions we take at MaybeIntercept time |
| 3715 bool restart_main_request_; | 3724 bool restart_main_request_; |
| 3716 bool cancel_main_request_; | 3725 bool cancel_main_request_; |
| (...skipping 423 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4140 }; | 4149 }; |
| 4141 | 4150 |
| 4142 // Make sure an FTP request using an unsafe ports fails. | 4151 // Make sure an FTP request using an unsafe ports fails. |
| 4143 TEST_F(URLRequestTestFTP, UnsafePort) { | 4152 TEST_F(URLRequestTestFTP, UnsafePort) { |
| 4144 ASSERT_TRUE(test_server_.Start()); | 4153 ASSERT_TRUE(test_server_.Start()); |
| 4145 | 4154 |
| 4146 URLRequestJobFactory job_factory; | 4155 URLRequestJobFactory job_factory; |
| 4147 | 4156 |
| 4148 GURL url("ftp://127.0.0.1:7"); | 4157 GURL url("ftp://127.0.0.1:7"); |
| 4149 FtpProtocolHandler ftp_protocol_handler( | 4158 FtpProtocolHandler ftp_protocol_handler( |
| 4150 default_context_.network_delegate(), | |
| 4151 default_context_.ftp_transaction_factory(), | 4159 default_context_.ftp_transaction_factory(), |
| 4152 default_context_.ftp_auth_cache()); | 4160 default_context_.ftp_auth_cache()); |
| 4153 job_factory.SetProtocolHandler( | 4161 job_factory.SetProtocolHandler( |
| 4154 "ftp", | 4162 "ftp", |
| 4155 new FtpProtocolHandler(default_context_.network_delegate(), | 4163 new FtpProtocolHandler(default_context_.ftp_transaction_factory(), |
| 4156 default_context_.ftp_transaction_factory(), | |
| 4157 default_context_.ftp_auth_cache())); | 4164 default_context_.ftp_auth_cache())); |
| 4158 default_context_.set_job_factory(&job_factory); | 4165 default_context_.set_job_factory(&job_factory); |
| 4159 | 4166 |
| 4160 TestDelegate d; | 4167 TestDelegate d; |
| 4161 { | 4168 { |
| 4162 URLRequest r(url, &d, &default_context_); | 4169 URLRequest r(url, &d, &default_context_); |
| 4163 r.Start(); | 4170 r.Start(); |
| 4164 EXPECT_TRUE(r.is_pending()); | 4171 EXPECT_TRUE(r.is_pending()); |
| 4165 | 4172 |
| 4166 MessageLoop::current()->Run(); | 4173 MessageLoop::current()->Run(); |
| (...skipping 491 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4658 req.SetExtraRequestHeaders(headers); | 4665 req.SetExtraRequestHeaders(headers); |
| 4659 req.Start(); | 4666 req.Start(); |
| 4660 MessageLoop::current()->Run(); | 4667 MessageLoop::current()->Run(); |
| 4661 // If the net tests are being run with ChromeFrame then we need to allow for | 4668 // If the net tests are being run with ChromeFrame then we need to allow for |
| 4662 // the 'chromeframe' suffix which is added to the user agent before the | 4669 // the 'chromeframe' suffix which is added to the user agent before the |
| 4663 // closing parentheses. | 4670 // closing parentheses. |
| 4664 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); | 4671 EXPECT_TRUE(StartsWithASCII(d.data_received(), "Lynx (textmode", true)); |
| 4665 } | 4672 } |
| 4666 | 4673 |
| 4667 } // namespace net | 4674 } // namespace net |
| OLD | NEW |