Chromium Code Reviews| Index: content/browser/loader/navigation_url_loader_unittest.cc |
| diff --git a/content/browser/loader/navigation_url_loader_unittest.cc b/content/browser/loader/navigation_url_loader_unittest.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..041b9692ae4a945c53d63966374b123b8fe15ebf |
| --- /dev/null |
| +++ b/content/browser/loader/navigation_url_loader_unittest.cc |
| @@ -0,0 +1,300 @@ |
| +// Copyright 2014 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "base/memory/ref_counted.h" |
| +#include "base/memory/scoped_ptr.h" |
| +#include "base/run_loop.h" |
| +#include "content/browser/frame_host/navigation_request_info.h" |
| +#include "content/browser/loader/navigation_url_loader_impl.h" |
| +#include "content/browser/loader/resource_dispatcher_host_impl.h" |
| +#include "content/browser/streams/stream.h" |
| +#include "content/browser/streams/stream_context.h" |
| +#include "content/browser/streams/stream_registry.h" |
| +#include "content/browser/streams/stream_url_request_job.h" |
| +#include "content/public/browser/browser_context.h" |
| +#include "content/public/browser/resource_context.h" |
| +#include "content/public/browser/stream_handle.h" |
| +#include "content/public/common/resource_response.h" |
| +#include "content/public/test/test_browser_context.h" |
| +#include "content/public/test/test_browser_thread_bundle.h" |
| +#include "net/base/net_errors.h" |
| +#include "net/http/http_response_headers.h" |
| +#include "net/url_request/redirect_info.h" |
| +#include "net/url_request/url_request.h" |
| +#include "net/url_request/url_request_context.h" |
| +#include "net/url_request/url_request_job_factory_impl.h" |
| +#include "net/url_request/url_request_test_job.h" |
| +#include "net/url_request/url_request_test_util.h" |
| +#include "testing/gtest/include/gtest/gtest.h" |
| + |
| +namespace content { |
| + |
| +namespace { |
| + |
| +class StreamProtocolHandler |
| + : public net::URLRequestJobFactory::ProtocolHandler { |
| + public: |
| + StreamProtocolHandler(StreamRegistry* registry) : registry_(registry) {} |
| + |
| + // net::URLRequestJobFactory::ProtocolHandler implementation. |
| + virtual net::URLRequestJob* MaybeCreateJob( |
| + net::URLRequest* request, |
| + net::NetworkDelegate* network_delegate) const OVERRIDE { |
| + scoped_refptr<Stream> stream = registry_->GetStream(request->url()); |
| + if (stream.get()) |
| + return new StreamURLRequestJob(request, network_delegate, stream); |
| + return NULL; |
| + } |
| + private: |
| + StreamRegistry* registry_; |
| +}; |
| + |
| +class TestNavigationURLLoaderDelegate : public NavigationURLLoader::Delegate { |
| + public: |
| + TestNavigationURLLoaderDelegate() |
| + : net_error_(0), |
| + request_redirected_(new base::RunLoop), |
| + response_started_(new base::RunLoop), |
| + request_failed_(new base::RunLoop) { |
| + } |
| + |
| + const net::RedirectInfo& redirect_info() const { return redirect_info_; } |
| + ResourceResponse* response() const { return response_.get(); } |
| + StreamHandle* body() const { return body_.get(); } |
| + int net_error() const { return net_error_; } |
| + |
| + void WaitForRequestRedirected() { |
| + request_redirected_->Run(); |
| + request_redirected_.reset(new base::RunLoop); |
| + } |
| + |
| + void WaitForResponseStarted() { |
| + response_started_->Run(); |
| + response_started_.reset(new base::RunLoop); |
| + } |
| + |
| + void WaitForRequestFailed() { |
| + request_failed_->Run(); |
| + request_failed_.reset(new base::RunLoop); |
| + } |
| + |
| + // NavigationURLLoader::Delegate implementation. |
| + virtual void OnRequestRedirected(const net::RedirectInfo& redirect_info, |
| + ResourceResponse* response) OVERRIDE { |
| + redirect_info_ = redirect_info; |
| + response_ = response; |
| + request_redirected_->Quit(); |
| + } |
| + |
| + virtual void OnResponseStarted(ResourceResponse* response, |
| + scoped_ptr<StreamHandle> body) OVERRIDE { |
| + response_ = response; |
| + body_ = body.Pass(); |
| + response_started_->Quit(); |
| + } |
| + |
| + virtual void OnRequestFailed(int net_error) OVERRIDE { |
| + net_error_ = net_error; |
| + request_failed_->Quit(); |
| + } |
| + |
| + private: |
| + net::RedirectInfo redirect_info_; |
| + scoped_refptr<ResourceResponse> response_; |
| + scoped_ptr<StreamHandle> body_; |
| + int net_error_; |
| + |
| + scoped_ptr<base::RunLoop> request_redirected_; |
| + scoped_ptr<base::RunLoop> response_started_; |
| + scoped_ptr<base::RunLoop> request_failed_; |
| +}; |
| + |
| +} // namespace |
| + |
| +class NavigationURLLoaderTest : public testing::Test { |
| + public: |
| + NavigationURLLoaderTest() |
| + : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
| + browser_context_(new TestBrowserContext) { |
| + BrowserContext::EnsureResourceContextInitialized(browser_context_.get()); |
| + base::RunLoop().RunUntilIdle(); |
| + net::URLRequestContext* request_context = |
| + browser_context_->GetResourceContext()->GetRequestContext(); |
| + // Attach URLRequestTestJob and make streams work. |
| + job_factory_.SetProtocolHandler( |
| + "test", net::URLRequestTestJob::CreateProtocolHandler()); |
| + job_factory_.SetProtocolHandler( |
| + "blob", new StreamProtocolHandler( |
| + StreamContext::GetFor(browser_context_.get())->registry())); |
| + request_context->set_job_factory(&job_factory_); |
| + } |
| + |
| + // Helper function for fetching the body of a URL to a string. |
| + std::string FetchURL(const GURL& url) { |
| + net::TestDelegate delegate; |
| + net::URLRequestContext* request_context = |
| + browser_context_->GetResourceContext()->GetRequestContext(); |
| + scoped_ptr<net::URLRequest> request(request_context->CreateRequest( |
| + url, net::DEFAULT_PRIORITY, &delegate, NULL)); |
| + request->Start(); |
| + base::RunLoop().Run(); |
| + |
| + EXPECT_TRUE(request->status().is_success()); |
| + EXPECT_EQ(200, request->response_headers()->response_code()); |
| + return delegate.data_received(); |
| + } |
| + |
| + protected: |
| + TestBrowserThreadBundle thread_bundle_; |
| + net::URLRequestJobFactoryImpl job_factory_; |
| + scoped_ptr<TestBrowserContext> browser_context_; |
| + ResourceDispatcherHostImpl host_; |
| +}; |
| + |
| +// Tests that a basic request works. |
| +TEST_F(NavigationURLLoaderTest, Basic) { |
| + TestNavigationURLLoaderDelegate delegate; |
| + |
| + // Fake a top-level request. |
| + FrameHostMsg_BeginNavigation_Params params; |
|
clamy
2014/09/12 20:51:25
TestRenderFrameHost::SendBeginNavigationWithURL ha
davidben
2014/09/19 18:30:50
Hrm. Maybe, although layering-wise loader/ shouldn
clamy
2014/09/22 21:11:01
Acknowledged.
|
| + params.method = "GET"; |
| + params.url = net::URLRequestTestJob::test_url_1(); |
| + NavigationRequestInfo request_info(params); |
| + request_info.first_party_for_cookies = params.url; |
| + request_info.is_main_frame = true; |
| + |
| + scoped_ptr<NavigationURLLoader> loader( |
| + NavigationURLLoader::Create(browser_context_.get(), |
| + 0, request_info, NULL, &delegate)); |
| + |
| + // Wait for the response to come back. |
| + delegate.WaitForResponseStarted(); |
| + |
| + // Check the response is correct. |
| + EXPECT_EQ("text/html", delegate.response()->head.mime_type); |
| + EXPECT_EQ("HTTP/1.1 200 OK", |
| + delegate.response()->head.headers->GetStatusLine()); |
| + |
| + // Check the body is correct. |
| + EXPECT_EQ(net::URLRequestTestJob::test_data_1(), |
| + FetchURL(delegate.body()->GetURL())); |
| +} |
| + |
| +// Tests that request failures are propogated correctly. |
| +TEST_F(NavigationURLLoaderTest, RequestFailed) { |
| + TestNavigationURLLoaderDelegate delegate; |
| + |
| + // Fake a top-level request. |
| + FrameHostMsg_BeginNavigation_Params params; |
| + params.method = "GET"; |
| + params.url = GURL("bogus:bogus"); |
| + NavigationRequestInfo request_info(params); |
| + request_info.first_party_for_cookies = params.url; |
| + request_info.is_main_frame = true; |
| + |
| + scoped_ptr<NavigationURLLoader> loader( |
| + NavigationURLLoader::Create(browser_context_.get(), |
| + 0, request_info, NULL, &delegate)); |
| + |
| + // Wait for the request to fail as expected. |
| + delegate.WaitForRequestFailed(); |
| + EXPECT_EQ(net::ERR_UNKNOWN_URL_SCHEME, delegate.net_error()); |
| +} |
| + |
| +// Test that redirects are sent to the delegate. |
| +TEST_F(NavigationURLLoaderTest, RequestRedirected) { |
| + TestNavigationURLLoaderDelegate delegate; |
| + |
| + // Fake a top-level request. |
| + FrameHostMsg_BeginNavigation_Params params; |
| + params.method = "GET"; |
| + params.url = net::URLRequestTestJob::test_url_redirect_to_url_2(); |
| + NavigationRequestInfo request_info(params); |
| + request_info.first_party_for_cookies = params.url; |
| + request_info.is_main_frame = true; |
| + |
| + scoped_ptr<NavigationURLLoader> loader( |
| + NavigationURLLoader::Create(browser_context_.get(), |
| + 0, request_info, NULL, &delegate)); |
| + |
| + // Wait for the request to redirect. |
| + delegate.WaitForRequestRedirected(); |
| + EXPECT_EQ(net::URLRequestTestJob::test_url_2(), |
| + delegate.redirect_info().new_url); |
| + EXPECT_EQ("GET", delegate.redirect_info().new_method); |
| + EXPECT_EQ(net::URLRequestTestJob::test_url_2(), |
| + delegate.redirect_info().new_first_party_for_cookies); |
| + EXPECT_EQ("HTTP/1.1 302 MOVED", |
| + delegate.response()->head.headers->GetStatusLine()); |
| + |
| + // Wait for the response to complete. |
| + loader->FollowRedirect(); |
| + base::RunLoop().RunUntilIdle(); |
| + net::URLRequestTestJob::ProcessOnePendingMessage(); |
| + delegate.WaitForResponseStarted(); |
| + |
| + // Check the response is correct. |
| + EXPECT_EQ("text/html", delegate.response()->head.mime_type); |
| + EXPECT_EQ("HTTP/1.1 200 OK", |
| + delegate.response()->head.headers->GetStatusLine()); |
| + |
| + // Check the body is correct. |
| + EXPECT_EQ(net::URLRequestTestJob::test_data_2(), |
| + FetchURL(delegate.body()->GetURL())); |
| +} |
| + |
| +// Tests that the destroying the loader cancels the request. |
| +TEST_F(NavigationURLLoaderTest, CancelOnDestruct) { |
|
clamy
2014/09/12 20:51:25
Should we also add a test for the case where the l
davidben
2014/09/19 18:30:50
Added a couple of tests in this vein.
|
| + TestNavigationURLLoaderDelegate delegate; |
| + |
| + // Fake a top-level request. |
| + FrameHostMsg_BeginNavigation_Params params; |
| + params.method = "GET"; |
| + params.url = net::URLRequestTestJob::test_url_redirect_to_url_2(); |
| + NavigationRequestInfo request_info(params); |
| + request_info.first_party_for_cookies = params.url; |
| + request_info.is_main_frame = true; |
| + |
| + scoped_ptr<NavigationURLLoader> loader( |
| + NavigationURLLoader::Create(browser_context_.get(), |
| + 0, request_info, NULL, &delegate)); |
| + |
| + // Wait for the request to redirect. |
| + delegate.WaitForRequestRedirected(); |
| + |
| + // Destroy the loader and verify that URLRequestTestJob no longer has anything |
| + // paused. |
| + loader.reset(); |
| + base::RunLoop().RunUntilIdle(); |
| + EXPECT_FALSE(net::URLRequestTestJob::ProcessOnePendingMessage()); |
| +} |
| + |
| +// Tests that the loader may be canceled by context. |
| +TEST_F(NavigationURLLoaderTest, CancelByContext) { |
| + TestNavigationURLLoaderDelegate delegate; |
| + |
| + // Fake a top-level request. |
| + FrameHostMsg_BeginNavigation_Params params; |
| + params.method = "GET"; |
| + params.url = net::URLRequestTestJob::test_url_redirect_to_url_2(); |
| + NavigationRequestInfo request_info(params); |
| + request_info.first_party_for_cookies = params.url; |
| + request_info.is_main_frame = true; |
| + |
| + scoped_ptr<NavigationURLLoader> loader( |
| + NavigationURLLoader::Create(browser_context_.get(), |
| + 0, request_info, NULL, &delegate)); |
| + |
| + // Wait for the request to redirect. |
| + delegate.WaitForRequestRedirected(); |
| + |
| + // Cancel all requests. |
| + host_.CancelRequestsForContext(browser_context_->GetResourceContext()); |
| + |
| + // Wait for the request to now be aborted. |
| + delegate.WaitForRequestFailed(); |
| + EXPECT_EQ(net::ERR_ABORTED, delegate.net_error()); |
| +} |
| + |
| +} // namespace content |