Commit dbed3ee0 authored by Gauthier Ambard's avatar Gauthier Ambard Committed by Commit Bot

[iOS] Fix error page detection in helper

This CL fixes the ErrorPageHelper detection of the original URL in an
error page URL.
It used to not used the same keyword as the creation method.

Bug: 991608
Change-Id: Ib826b879e539b5d21ccde2d6e92dbba7adccff49
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2027334Reviewed-by: default avatarEugene But <eugenebut@chromium.org>
Commit-Queue: Gauthier Ambard <gambard@chromium.org>
Auto-Submit: Gauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#737837}
parent be5efb0b
...@@ -16,6 +16,8 @@ ...@@ -16,6 +16,8 @@
namespace { namespace {
const char kOriginalUrlKey[] = "url";
// Escapes HTML characters in |text|. // Escapes HTML characters in |text|.
NSString* EscapeHTMLCharacters(NSString* text) { NSString* EscapeHTMLCharacters(NSString* text) {
return base::SysUTF8ToNSString( return base::SysUTF8ToNSString(
...@@ -76,9 +78,9 @@ NSString* InjectedErrorPageFilePath() { ...@@ -76,9 +78,9 @@ NSString* InjectedErrorPageFilePath() {
- (NSURL*)errorPageFileURL { - (NSURL*)errorPageFileURL {
if (!_errorPageFileURL) { if (!_errorPageFileURL) {
NSURLQueryItem* itemURL = NSURLQueryItem* itemURL = [NSURLQueryItem
[NSURLQueryItem queryItemWithName:@"url" queryItemWithName:base::SysUTF8ToNSString(kOriginalUrlKey)
value:self.failedNavigationURLString]; value:self.failedNavigationURLString];
NSURLQueryItem* itemError = NSURLQueryItem* itemError =
[NSURLQueryItem queryItemWithName:@"error" [NSURLQueryItem queryItemWithName:@"error"
value:_error.localizedDescription]; value:_error.localizedDescription];
...@@ -137,10 +139,9 @@ NSString* InjectedErrorPageFilePath() { ...@@ -137,10 +139,9 @@ NSString* InjectedErrorPageFilePath() {
if (URL.SchemeIsFile() && if (URL.SchemeIsFile() &&
URL.path() == base::SysNSStringToUTF8(LoadedErrorPageFilePath())) { URL.path() == base::SysNSStringToUTF8(LoadedErrorPageFilePath())) {
for (net::QueryIterator it(URL); !it.IsAtEnd(); it.Advance()) { std::string value;
if (it.GetKey() == "file") { if (net::GetValueForKeyInQuery(URL, kOriginalUrlKey, &value)) {
return GURL(it.GetValue()); return GURL(value);
}
} }
} }
...@@ -157,7 +158,7 @@ NSString* InjectedErrorPageFilePath() { ...@@ -157,7 +158,7 @@ NSString* InjectedErrorPageFilePath() {
resolvingAgainstBaseURL:NO]; resolvingAgainstBaseURL:NO];
NSURL* failedNavigationURL = nil; NSURL* failedNavigationURL = nil;
for (NSURLQueryItem* item in URLComponents.queryItems) { for (NSURLQueryItem* item in URLComponents.queryItems) {
if ([item.name isEqualToString:@"url"]) { if ([item.name isEqualToString:base::SysUTF8ToNSString(kOriginalUrlKey)]) {
failedNavigationURL = [NSURL URLWithString:item.value]; failedNavigationURL = [NSURL URLWithString:item.value];
break; break;
} }
......
...@@ -5,6 +5,7 @@ ...@@ -5,6 +5,7 @@
#import "ios/web/navigation/error_page_helper.h" #import "ios/web/navigation/error_page_helper.h"
#include "base/strings/sys_string_conversions.h" #include "base/strings/sys_string_conversions.h"
#import "net/base/mac/url_conversions.h"
#include "testing/gtest/include/gtest/gtest.h" #include "testing/gtest/include/gtest/gtest.h"
#import "testing/gtest_mac.h" #import "testing/gtest_mac.h"
#include "testing/platform_test.h" #include "testing/platform_test.h"
...@@ -28,6 +29,48 @@ TEST_F(ErrorPageHelperTest, FailedNavigationURL) { ...@@ -28,6 +29,48 @@ TEST_F(ErrorPageHelperTest, FailedNavigationURL) {
EXPECT_NSEQ(url, helper.failedNavigationURL); EXPECT_NSEQ(url, helper.failedNavigationURL);
} }
// Tests that the original URL is correctly extracted from the file error URL
// created by the helper.
TEST_F(ErrorPageHelperTest, ExtractOriginalURLFromErrorPageURL) {
NSString* url_string = @"https://test-error-page.com";
NSError* error = [NSError
errorWithDomain:NSURLErrorDomain
code:NSURLErrorBadURL
userInfo:@{NSURLErrorFailingURLStringErrorKey : url_string}];
ErrorPageHelper* helper = [[ErrorPageHelper alloc] initWithError:error];
GURL result_original_url = [ErrorPageHelper
failedNavigationURLFromErrorPageFileURL:net::GURLWithNSURL(
helper.errorPageFileURL)];
EXPECT_EQ(GURL(base::SysNSStringToUTF8(url_string)), result_original_url);
}
// Tests that the error page is correctly identified as error page.
TEST_F(ErrorPageHelperTest, IsErrorPageFileURL) {
NSString* url_string = @"https://test-error-page.com";
NSError* error = [NSError
errorWithDomain:NSURLErrorDomain
code:NSURLErrorBadURL
userInfo:@{NSURLErrorFailingURLStringErrorKey : url_string}];
ErrorPageHelper* helper = [[ErrorPageHelper alloc] initWithError:error];
EXPECT_TRUE([helper
isErrorPageFileURLForFailedNavigationURL:helper.errorPageFileURL]);
}
// Tests that a normal URL isn't identified as error page.
TEST_F(ErrorPageHelperTest, IsErrorPageFileURLWrong) {
NSString* url_string = @"file://test-error-page.com";
NSError* error =
[NSError errorWithDomain:NSURLErrorDomain
code:NSURLErrorBadURL
userInfo:@{
NSURLErrorFailingURLStringErrorKey : @"http://fake.com"
}];
ErrorPageHelper* helper = [[ErrorPageHelper alloc] initWithError:error];
EXPECT_FALSE([helper
isErrorPageFileURLForFailedNavigationURL:[NSURL
URLWithString:url_string]]);
}
// Tests that the failed navigation URL is correctly extracted from the page // Tests that the failed navigation URL is correctly extracted from the page
// URL. // URL.
TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLCorrect) { TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLCorrect) {
...@@ -37,14 +80,14 @@ TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLCorrect) { ...@@ -37,14 +80,14 @@ TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLCorrect) {
ofType:@"html"]); ofType:@"html"]);
std::string url_string = "file://" + path + std::string url_string = "file://" + path +
"?url=http://not-that-url.com&file=" + expected_url + "?file=http://not-that-url.com&url=" + expected_url +
"&garbage=http://still-not-that-one.com"; "&garbage=http://still-not-that-one.com";
GURL result_url = [ErrorPageHelper GURL result_url = [ErrorPageHelper
failedNavigationURLFromErrorPageFileURL:GURL(url_string)]; failedNavigationURLFromErrorPageFileURL:GURL(url_string)];
EXPECT_EQ(GURL(expected_url), result_url); EXPECT_EQ(GURL(expected_url), result_url);
} }
// Tests that the extract failed navigation URL is empty if the |file| query // Tests that the extract failed navigation URL is empty if the |url| query
// isn't present in the page URL. // isn't present in the page URL.
TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLNoQuery) { TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLNoQuery) {
std::string expected_url = "http://expected-url.com"; std::string expected_url = "http://expected-url.com";
...@@ -52,7 +95,7 @@ TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLNoQuery) { ...@@ -52,7 +95,7 @@ TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLNoQuery) {
pathForResource:@"error_page_loaded" pathForResource:@"error_page_loaded"
ofType:@"html"]); ofType:@"html"]);
std::string url_string = "file://" + path + "?url=" + expected_url + std::string url_string = "file://" + path + "?file=" + expected_url +
"&garbage=http://still-not-that-one.com"; "&garbage=http://still-not-that-one.com";
GURL result_url = [ErrorPageHelper GURL result_url = [ErrorPageHelper
failedNavigationURLFromErrorPageFileURL:GURL(url_string)]; failedNavigationURLFromErrorPageFileURL:GURL(url_string)];
...@@ -63,7 +106,7 @@ TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLNoQuery) { ...@@ -63,7 +106,7 @@ TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLNoQuery) {
// current page isn't correct. // current page isn't correct.
TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLWrongPath) { TEST_F(ErrorPageHelperTest, FailedNavigationURLFromErrorPageFileURLWrongPath) {
std::string url_string = std::string url_string =
"file://not-the-correct-path.com?file=http://potential-url.com"; "file://not-the-correct-path.com?url=http://potential-url.com";
GURL result_url = [ErrorPageHelper GURL result_url = [ErrorPageHelper
failedNavigationURLFromErrorPageFileURL:GURL(url_string)]; failedNavigationURLFromErrorPageFileURL:GURL(url_string)];
EXPECT_TRUE(result_url.is_empty()); EXPECT_TRUE(result_url.is_empty());
...@@ -77,7 +120,7 @@ TEST_F(ErrorPageHelperTest, ...@@ -77,7 +120,7 @@ TEST_F(ErrorPageHelperTest,
pathForResource:@"error_page_loaded" pathForResource:@"error_page_loaded"
ofType:@"html"]); ofType:@"html"]);
std::string url_string = "http://" + path + "?file=http://potential-url.com"; std::string url_string = "http://" + path + "?url=http://potential-url.com";
GURL result_url = [ErrorPageHelper GURL result_url = [ErrorPageHelper
failedNavigationURLFromErrorPageFileURL:GURL(url_string)]; failedNavigationURLFromErrorPageFileURL:GURL(url_string)];
EXPECT_TRUE(result_url.is_empty()); EXPECT_TRUE(result_url.is_empty());
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment