Skip to content

Commit

Permalink
Replace External URL testErrorPageRedirect with hermetic test.
Browse files Browse the repository at this point in the history
Old test was loading http://mock/bad and because this URL did not
exist the load was failing because of DNS resolution error.

New test uses EmbeddedTestServer, which closes the socket to simulate
ERR_INTERNET_DISCONNECTED error.

Bug: 694662
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: Iaca523e8153a2c4cca0e1f174652ed48f22cabd3
Reviewed-on: https://chromium-review.googlesource.com/1035610
Commit-Queue: Eugene But <eugenebut@chromium.org>
Reviewed-by: Danyao Wang <danyao@chromium.org>
Cr-Commit-Position: refs/heads/master@{#555316}
  • Loading branch information
Eugene But authored and Commit Bot committed May 2, 2018
1 parent 3c97d48 commit 7408a84
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 77 deletions.
25 changes: 0 additions & 25 deletions ios/chrome/browser/ui/external_url_error_page_egtest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -52,31 +52,6 @@ - (void)checkErrorPageIsNotVisible {

#pragma mark - tests

// Tests whether the error page is displayed if it is behind a redirect.
- (void)testErrorPageRedirect {
// TODO(crbug.com/694662): This test relies on external URL because of the bug.
// Re-enable this test on device once the bug is fixed.
#if !TARGET_IPHONE_SIMULATOR
EARL_GREY_TEST_DISABLED(@"Test disabled on device.");
#endif

std::unique_ptr<web::DataResponseProvider> provider(
new ErrorPageResponseProvider());
web::test::SetUpHttpServer(std::move(provider));

// Load a URL that redirects to the DNS-failing URL.
[ChromeEarlGrey
loadURL:ErrorPageResponseProvider::GetRedirectToDnsFailureUrl()];

// Verify that the redirect occurred before checking for the DNS error.
const std::string& redirectedURL =
ErrorPageResponseProvider::GetDnsFailureUrl().GetContent();
[[EarlGrey selectElementWithMatcher:OmniboxText(redirectedURL)]
assertWithMatcher:grey_notNil()];

[ChromeEarlGrey waitForErrorPage];
}

// Tests that the error page is not displayed if the bad URL is in a <iframe>
// tag.
- (void)testErrorPageInIFrame {
Expand Down
35 changes: 25 additions & 10 deletions ios/chrome/browser/web/error_page_egtest.mm
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include "base/strings/sys_string_conversions.h"
#import "ios/chrome/test/earl_grey/chrome_earl_grey.h"
#import "ios/chrome/test/earl_grey/chrome_test_case.h"
#include "net/test/embedded_test_server/default_handlers.h"
#include "net/test/embedded_test_server/http_request.h"
#include "net/test/embedded_test_server/http_response.h"

Expand Down Expand Up @@ -38,21 +39,26 @@ @implementation ErrorPageTestCase
- (void)setUp {
[super setUp];

// Tests handler which replies with URL query if serverRespondsWithContent set
// to YES. Otherwise the handler closes the socket.
// Tests handler which replies with URL query for /echo-query path if
// serverRespondsWithContent set to YES. Otherwise the handler closes the
// socket.
using net::test_server::HttpRequest;
using net::test_server::HttpResponse;
auto handler = ^std::unique_ptr<HttpResponse>(const HttpRequest& request) {
if (!self.serverRespondsWithContent) {
return std::make_unique<net::test_server::RawHttpResponse>(
/*headers=*/"", /*contents=*/"");
if (request.GetURL().path() == "/echo-query") {
if (!self.serverRespondsWithContent) {
return std::make_unique<net::test_server::RawHttpResponse>(
/*headers=*/"", /*contents=*/"");
}
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_content_type("text/html");
response->set_content(request.GetURL().query());
return std::move(response);
}
auto response = std::make_unique<net::test_server::BasicHttpResponse>();
response->set_content_type("text/html");
response->set_content(request.GetURL().query());
return std::move(response);
return nullptr;
};
self.testServer->RegisterDefaultHandler(base::BindBlockArc(handler));
RegisterDefaultHandlers(self.testServer);

GREYAssertTrue(self.testServer->Start(), @"Test server failed to start.");
}
Expand All @@ -61,7 +67,7 @@ - (void)setUp {
- (void)testReload {
// No response leads to ERR_INTERNET_DISCONNECTED error.
self.serverRespondsWithContent = NO;
[ChromeEarlGrey loadURL:self.testServer->GetURL("/?foo")];
[ChromeEarlGrey loadURL:self.testServer->GetURL("/echo-query?foo")];
[ChromeEarlGrey waitForStaticHTMLViewContainingText:GetErrorMessage()];

// Reload the page, which should load without errors.
Expand All @@ -70,4 +76,13 @@ - (void)testReload {
[ChromeEarlGrey waitForWebViewContainingText:"foo"];
}

// Loads the URL which redirects to unresponsive server.
- (void)testRedirectToFailingURL {
// No response leads to ERR_INTERNET_DISCONNECTED error.
self.serverRespondsWithContent = NO;
[ChromeEarlGrey
loadURL:self.testServer->GetURL("/server-redirect?echo-query")];
[ChromeEarlGrey waitForStaticHTMLViewContainingText:GetErrorMessage()];
}

@end
11 changes: 0 additions & 11 deletions ios/web/public/test/http_server/error_page_response_provider.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
#include "url/gurl.h"

// A HtmlResponseProvider that supports the following additional URLs:
// - GetRedirectToDnsFailureUrl - the response is a redirect to
// |GetDnsFailureUrl|.
// - GetDnsFailureUrl - triggers a DNS error.
class ErrorPageResponseProvider : public HtmlResponseProvider {
public:
Expand All @@ -23,15 +21,6 @@ class ErrorPageResponseProvider : public HtmlResponseProvider {
: HtmlResponseProvider(responses) {}
// Returns a URL that causes a DNS failure.
static GURL GetDnsFailureUrl();
// Returns a URL that redirects to a bad URL.
static GURL GetRedirectToDnsFailureUrl();

// HtmlResponseProvider implementation.
bool CanHandleRequest(const Request& request) override;
void GetResponseHeadersAndBody(
const Request& request,
scoped_refptr<net::HttpResponseHeaders>* headers,
std::string* response_body) override;
};

#endif // IOS_WEB_PUBLIC_TEST_HTTP_SERVER_ERROR_PAGE_RESPONSE_PROVIDER_H_
31 changes: 0 additions & 31 deletions ios/web/public/test/http_server/error_page_response_provider.mm
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@

#import "ios/web/public/test/http_server/error_page_response_provider.h"

#include "base/logging.h"
#import "ios/web/public/test/http_server/http_server.h"
#include "net/http/http_status_code.h"

#if !defined(__has_feature) || !__has_feature(objc_arc)
#error "This file requires ARC support."
#endif
Expand All @@ -16,30 +12,3 @@
GURL ErrorPageResponseProvider::GetDnsFailureUrl() {
return GURL("http://mock/bad");
}

// static
GURL ErrorPageResponseProvider::GetRedirectToDnsFailureUrl() {
return web::test::HttpServer::MakeUrl("http://mock/redirect");
}

// Returns true for |RedirectToDnsFailureUrl|.
bool ErrorPageResponseProvider::CanHandleRequest(const Request& request) {
return (HtmlResponseProvider::CanHandleRequest(request) ||
request.url ==
ErrorPageResponseProvider::GetRedirectToDnsFailureUrl());
}

void ErrorPageResponseProvider::GetResponseHeadersAndBody(
const Request& request,
scoped_refptr<net::HttpResponseHeaders>* headers,
std::string* response_body) {
if (HtmlResponseProvider::CanHandleRequest(request)) {
HtmlResponseProvider::GetResponseHeadersAndBody(request, headers,
response_body);
} else if (request.url == GetRedirectToDnsFailureUrl()) {
*headers = web::ResponseProvider::GetRedirectResponseHeaders(
ErrorPageResponseProvider::GetDnsFailureUrl().spec(), net::HTTP_FOUND);
} else {
NOTREACHED();
}
}

0 comments on commit 7408a84

Please sign in to comment.