diff options
author | mmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-19 16:44:10 +0000 |
---|---|---|
committer | mmenke@chromium.org <mmenke@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2012-03-19 16:44:10 +0000 |
commit | 72c8fe6115b6ccb8b0c815c587bab32aaf9a92fd (patch) | |
tree | ee383f7036830533479f41d5b7d53f1f7918fb8d | |
parent | 840512437ad6bab5d95c7910bacb1f641a368e0b (diff) | |
download | chromium_src-72c8fe6115b6ccb8b0c815c587bab32aaf9a92fd.zip chromium_src-72c8fe6115b6ccb8b0c815c587bab32aaf9a92fd.tar.gz chromium_src-72c8fe6115b6ccb8b0c815c587bab32aaf9a92fd.tar.bz2 |
Modify URLRequestFailedDnsJob to allow any error code to be
returned while "connecting" to either an fake HTTP or HTTPS
server, and rename it to URLRequestFailedJob.
This is needed for testing the (unlanded) captive portal
detection/response code.
BUG=87100
Review URL: https://chromiumcodereview.appspot.com/9704023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@127488 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/errorpage_browsertest.cc | 23 | ||||
-rw-r--r-- | chrome/browser/net/url_request_mock_util.cc | 6 | ||||
-rw-r--r-- | chrome/test/data/iframe_dns_error.html | 5 | ||||
-rw-r--r-- | content/browser/renderer_host/resource_dispatcher_host_uitest.cc | 7 | ||||
-rw-r--r-- | content/content_browser.gypi | 4 | ||||
-rw-r--r-- | content/test/net/url_request_failed_dns_job.cc | 47 | ||||
-rw-r--r-- | content/test/net/url_request_failed_dns_job.h | 38 | ||||
-rw-r--r-- | content/test/net/url_request_failed_job.cc | 87 | ||||
-rw-r--r-- | content/test/net/url_request_failed_job.h | 50 |
9 files changed, 162 insertions, 105 deletions
diff --git a/chrome/browser/errorpage_browsertest.cc b/chrome/browser/errorpage_browsertest.cc index 7cc72fd..e8f3ff8 100644 --- a/chrome/browser/errorpage_browsertest.cc +++ b/chrome/browser/errorpage_browsertest.cc @@ -10,9 +10,10 @@ #include "chrome/test/base/in_process_browser_test.h" #include "chrome/test/base/ui_test_utils.h" #include "content/public/browser/web_contents.h" -#include "content/test/net/url_request_failed_dns_job.h" +#include "content/test/net/url_request_failed_job.h" #include "content/test/net/url_request_mock_http_job.h" #include "content/test/test_navigation_observer.h" +#include "net/base/net_errors.h" using content::BrowserThread; using content::NavigationController; @@ -71,6 +72,11 @@ class ErrorPageTest : public InProcessBrowserTest { base::Bind(&chrome_browser_net::SetUrlRequestMocksEnabled, true)); } + // Returns a GURL that results in a DNS error. + GURL GetDnsErrorURL() const { + return URLRequestFailedJob::GetMockHttpUrl(net::ERR_NAME_NOT_RESOLVED); + } + private: // Navigates the browser the indicated direction in the history and waits for // |num_navigations| to occur and the title to change to |expected_title|. @@ -111,18 +117,16 @@ class ErrorPageTest : public InProcessBrowserTest { #endif // Test that a DNS error occuring in the main frame redirects to an error page. IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_Basic) { - GURL test_url(URLRequestFailedDnsJob::kTestUrl); // The first navigation should fail, and the second one should be the error // page. - NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); + NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); } // Test that a DNS error occuring in the main frame does not result in an // additional session history entry. IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack1) { - GURL test_url(URLRequestFailedDnsJob::kTestUrl); NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); - NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); + NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); GoBackAndWaitForTitle("Title Of Awesomeness", 1); } @@ -135,10 +139,9 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, DNSError_GoBack1) { // Test that a DNS error occuring in the main frame does not result in an // additional session history entry. IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2) { - GURL test_url(URLRequestFailedDnsJob::kTestUrl); NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); - NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); + NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); GoBackAndWaitForTitle("Mock Link Doctor", 2); @@ -154,10 +157,9 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2) { // Test that a DNS error occuring in the main frame does not result in an // additional session history entry. IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2AndForward) { - GURL test_url(URLRequestFailedDnsJob::kTestUrl); NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); - NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); + NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); GoBackAndWaitForTitle("Mock Link Doctor", 2); @@ -175,10 +177,9 @@ IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2AndForward) { // Test that a DNS error occuring in the main frame does not result in an // additional session history entry. IN_PROC_BROWSER_TEST_F(ErrorPageTest, MAYBE_DNSError_GoBack2Forward2) { - GURL test_url(URLRequestFailedDnsJob::kTestUrl); NavigateToFileURL(FILE_PATH_LITERAL("title3.html")); - NavigateToURLAndWaitForTitle(test_url, "Mock Link Doctor", 2); + NavigateToURLAndWaitForTitle(GetDnsErrorURL(), "Mock Link Doctor", 2); NavigateToFileURL(FILE_PATH_LITERAL("title2.html")); GoBackAndWaitForTitle("Mock Link Doctor", 2); diff --git a/chrome/browser/net/url_request_mock_util.cc b/chrome/browser/net/url_request_mock_util.cc index 23bd041..98219ad 100644 --- a/chrome/browser/net/url_request_mock_util.cc +++ b/chrome/browser/net/url_request_mock_util.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Copyright (c) 2012 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. @@ -11,7 +11,7 @@ #include "chrome/browser/net/url_request_mock_link_doctor_job.h" #include "chrome/common/chrome_paths.h" #include "content/public/browser/browser_thread.h" -#include "content/test/net/url_request_failed_dns_job.h" +#include "content/test/net/url_request_failed_job.h" #include "content/test/net/url_request_mock_http_job.h" #include "content/test/net/url_request_slow_download_job.h" #include "content/test/net/url_request_slow_http_job.h" @@ -34,7 +34,7 @@ void SetUrlRequestMocksEnabled(bool enabled) { net::URLRequestFilter::GetInstance()->ClearHandlers(); - URLRequestFailedDnsJob::AddUrlHandler(); + URLRequestFailedJob::AddUrlHandler(); URLRequestMockLinkDoctorJob::AddUrlHandler(); URLRequestSlowDownloadJob::AddUrlHandler(); diff --git a/chrome/test/data/iframe_dns_error.html b/chrome/test/data/iframe_dns_error.html index 8a68349..2bf6e25 100644 --- a/chrome/test/data/iframe_dns_error.html +++ b/chrome/test/data/iframe_dns_error.html @@ -3,7 +3,8 @@ <title>Blah</title> </head> <body> -This frame will fail to load with a DNS error: -<iframe src="http://url.handled.by.fake.dns/"></iframe> +This frame will fail to load with a DNS error. "mock.failed.request" is the +magic hostname used by URLRequestFailedJob, and -105 is ERR_NAME_NOT_RESOLVED. +<iframe src="http://mock.failed.request/-105"></iframe> </body> </html> diff --git a/content/browser/renderer_host/resource_dispatcher_host_uitest.cc b/content/browser/renderer_host/resource_dispatcher_host_uitest.cc index 6b55187..1ed043e 100644 --- a/content/browser/renderer_host/resource_dispatcher_host_uitest.cc +++ b/content/browser/renderer_host/resource_dispatcher_host_uitest.cc @@ -17,8 +17,9 @@ #include "chrome/test/ui/ui_test.h" #include "content/common/test_url_constants.h" #include "content/public/common/url_constants.h" -#include "content/test/net/url_request_failed_dns_job.h" +#include "content/test/net/url_request_failed_job.h" #include "content/test/net/url_request_mock_http_job.h" +#include "net/base/net_errors.h" #include "net/base/net_util.h" #include "net/test/test_server.h" @@ -351,7 +352,9 @@ TEST_F(ResourceDispatcherTest, DISABLED_CrossSiteNavigationErrorPage) { // http://crbug.com/22877. ASSERT_EQ(AUTOMATION_MSG_NAVIGATION_SUCCESS, tab->NavigateToURLBlockUntilNavigationsComplete( - GURL(URLRequestFailedDnsJob::kTestUrl), 2)); + GURL(URLRequestFailedJob::GetMockHttpUrl( + net::ERR_NAME_NOT_RESOLVED)), + 2)); EXPECT_NE(L"set cookie on unload", GetActiveTabTitle()); // Check that the cookie was set, meaning that the onunload handler ran. diff --git a/content/content_browser.gypi b/content/content_browser.gypi index 351ac2e..2169f63 100644 --- a/content/content_browser.gypi +++ b/content/content_browser.gypi @@ -702,8 +702,8 @@ 'browser/zygote_main_linux.cc', # TODO: These should be moved to test_support (see below), but # are currently used by production code in automation_provider.cc. - 'test/net/url_request_failed_dns_job.cc', - 'test/net/url_request_failed_dns_job.h', + 'test/net/url_request_failed_job.cc', + 'test/net/url_request_failed_job.h', 'test/net/url_request_mock_http_job.cc', 'test/net/url_request_mock_http_job.h', 'test/net/url_request_slow_download_job.cc', diff --git a/content/test/net/url_request_failed_dns_job.cc b/content/test/net/url_request_failed_dns_job.cc deleted file mode 100644 index 2ac30d5..0000000 --- a/content/test/net/url_request_failed_dns_job.cc +++ /dev/null @@ -1,47 +0,0 @@ -// Copyright (c) 2011 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 "content/test/net/url_request_failed_dns_job.h" - -#include "base/bind.h" -#include "base/compiler_specific.h" -#include "base/message_loop.h" -#include "googleurl/src/gurl.h" -#include "net/base/net_errors.h" -#include "net/url_request/url_request.h" -#include "net/url_request/url_request_filter.h" - -const char URLRequestFailedDnsJob::kTestUrl[] = - "http://url.handled.by.fake.dns/"; - -URLRequestFailedDnsJob::URLRequestFailedDnsJob(net::URLRequest* request) - : net::URLRequestJob(request), - ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} - -URLRequestFailedDnsJob::~URLRequestFailedDnsJob() {} - -void URLRequestFailedDnsJob::Start() { - MessageLoop::current()->PostTask( - FROM_HERE, - base::Bind(&URLRequestFailedDnsJob::StartAsync, - weak_factory_.GetWeakPtr())); -} - -// static -void URLRequestFailedDnsJob::AddUrlHandler() { - net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); - filter->AddUrlHandler(GURL(kTestUrl), - &URLRequestFailedDnsJob::Factory); -} - -/*static */ -net::URLRequestJob* URLRequestFailedDnsJob::Factory(net::URLRequest* request, - const std::string& scheme) { - return new URLRequestFailedDnsJob(request); -} - -void URLRequestFailedDnsJob::StartAsync() { - NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, - net::ERR_NAME_NOT_RESOLVED)); -} diff --git a/content/test/net/url_request_failed_dns_job.h b/content/test/net/url_request_failed_dns_job.h deleted file mode 100644 index 9d03ae2..0000000 --- a/content/test/net/url_request_failed_dns_job.h +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright (c) 2011 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. -// This class simulates what wininet does when a dns lookup fails. - -#ifndef CONTENT_TEST_NET_URL_REQUEST_FAILED_DNS_JOB_H_ -#define CONTENT_TEST_NET_URL_REQUEST_FAILED_DNS_JOB_H_ -#pragma once - -#include "base/memory/weak_ptr.h" -#include "content/common/content_export.h" -#include "net/url_request/url_request_job.h" - -class URLRequestFailedDnsJob : public net::URLRequestJob { - public: - explicit URLRequestFailedDnsJob(net::URLRequest* request); - - virtual void Start() OVERRIDE; - - static net::URLRequestJob* Factory(net::URLRequest* request, - const std::string& scheme); - - // A test URL that can be used in UI tests. - CONTENT_EXPORT static const char kTestUrl[]; - - // Adds the testing URLs to the net::URLRequestFilter. - CONTENT_EXPORT static void AddUrlHandler(); - - private: - virtual ~URLRequestFailedDnsJob(); - - // Simulate a DNS failure. - void StartAsync(); - - base::WeakPtrFactory<URLRequestFailedDnsJob> weak_factory_; -}; - -#endif // CONTENT_TEST_NET_URL_REQUEST_FAILED_DNS_JOB_H_ diff --git a/content/test/net/url_request_failed_job.cc b/content/test/net/url_request_failed_job.cc new file mode 100644 index 0000000..9ea08b8 --- /dev/null +++ b/content/test/net/url_request_failed_job.cc @@ -0,0 +1,87 @@ +// Copyright (c) 2012 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 "content/test/net/url_request_failed_job.h" + +#include "base/bind.h" +#include "base/logging.h" +#include "base/message_loop.h" +#include "base/string_number_conversions.h" +#include "net/base/net_errors.h" +#include "net/url_request/url_request.h" +#include "net/url_request/url_request_filter.h" + +namespace { + +const char kMockHostname[] = "mock.failed.request"; + +// Gets the numeric net error code from URL of the form: +// scheme://kMockHostname/error_code. The error code must be a valid +// net error code, and not net::OK or net::ERR_IO_PENDING. +int GetErrorCode(net::URLRequest* request) { + int net_error; + std::string path = request->url().path(); + if (path[0] == '/' && base::StringToInt(path.c_str() + 1, &net_error)) { + CHECK_LT(net_error, 0); + CHECK_NE(net_error, net::ERR_IO_PENDING); + return net_error; + } + NOTREACHED(); + return net::ERR_UNEXPECTED; +} + +GURL GetMockUrl(const std::string& scheme, int net_error) { + CHECK_LT(net_error, 0); + CHECK_NE(net_error, net::ERR_IO_PENDING); + return GURL(scheme + "://" + kMockHostname + "/" + + base::IntToString(net_error)); +} + +} // namespace + +URLRequestFailedJob::URLRequestFailedJob(net::URLRequest* request, + int net_error) + : net::URLRequestJob(request), + net_error_(net_error), + ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} + +URLRequestFailedJob::~URLRequestFailedJob() {} + +void URLRequestFailedJob::Start() { + MessageLoop::current()->PostTask( + FROM_HERE, + base::Bind(&URLRequestFailedJob::StartAsync, + weak_factory_.GetWeakPtr())); +} + +// static +void URLRequestFailedJob::AddUrlHandler() { + // Add kMockHostname to net::URLRequestFilter for HTTP and HTTPS. + net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); + filter->AddHostnameHandler("http", kMockHostname, + URLRequestFailedJob::Factory); + filter->AddHostnameHandler("https", kMockHostname, + URLRequestFailedJob::Factory); +} + +// static +GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) { + return GetMockUrl("http", net_error); +} + +// static +GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) { + return GetMockUrl("https", net_error); +} + +// static +net::URLRequestJob* URLRequestFailedJob::Factory(net::URLRequest* request, + const std::string& scheme) { + return new URLRequestFailedJob(request, GetErrorCode(request)); +} + +void URLRequestFailedJob::StartAsync() { + NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, + net_error_)); +} diff --git a/content/test/net/url_request_failed_job.h b/content/test/net/url_request_failed_job.h new file mode 100644 index 0000000..b09edb7 --- /dev/null +++ b/content/test/net/url_request_failed_job.h @@ -0,0 +1,50 @@ +// Copyright (c) 2012 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. + +#ifndef CONTENT_TEST_NET_URL_REQUEST_FAILED_JOB_H_ +#define CONTENT_TEST_NET_URL_REQUEST_FAILED_JOB_H_ +#pragma once + +#include <string> + +#include "base/compiler_specific.h" +#include "base/memory/weak_ptr.h" +#include "content/common/content_export.h" +#include "googleurl/src/gurl.h" +#include "net/url_request/url_request_job.h" + +// This class simulates a URLRequestJob failing with a given error code while +// trying to connect. +class URLRequestFailedJob : public net::URLRequestJob { + public: + URLRequestFailedJob(net::URLRequest* request, int net_error); + + virtual void Start() OVERRIDE; + + // Adds the testing URLs to the net::URLRequestFilter. + CONTENT_EXPORT static void AddUrlHandler(); + + // Given a net error code, constructs a mock URL that will return that error + // asynchronously when started. |net_error| must be a valid net error code + // other than net::OK and net::ERR_IO_PENDING. + CONTENT_EXPORT static GURL GetMockHttpUrl(int net_error); + CONTENT_EXPORT static GURL GetMockHttpsUrl(int net_error); + + private: + static net::URLRequestJob* Factory(net::URLRequest* request, + const std::string& scheme); + + virtual ~URLRequestFailedJob(); + + // Simulate a failure. + void StartAsync(); + + const int net_error_; + + base::WeakPtrFactory<URLRequestFailedJob> weak_factory_; + + DISALLOW_COPY_AND_ASSIGN(URLRequestFailedJob); +}; + +#endif // CONTENT_TEST_NET_URL_REQUEST_FAILED_JOB_H_ |