Chromium Code Reviews| Index: ios/web_view/test/chrome_web_view_test.mm |
| diff --git a/ios/web_view/test/chrome_web_view_test.mm b/ios/web_view/test/chrome_web_view_test.mm |
| index 44942e51643501cb4c985b217550af168272c020..60c27dafa7e01d713d67a1ece13694678ea6892c 100644 |
| --- a/ios/web_view/test/chrome_web_view_test.mm |
| +++ b/ios/web_view/test/chrome_web_view_test.mm |
| @@ -9,8 +9,8 @@ |
| #include "base/base64.h" |
| #import "base/memory/ptr_util.h" |
| +#include "base/strings/stringprintf.h" |
| #import "ios/testing/wait_util.h" |
| -#include "net/test/embedded_test_server/default_handlers.h" |
| #include "net/test/embedded_test_server/embedded_test_server.h" |
| #include "net/test/embedded_test_server/http_request.h" |
| #include "net/test/embedded_test_server/http_response.h" |
| @@ -22,33 +22,75 @@ |
| namespace { |
| -// Test server path which echos the remainder of the url as html. The html |
| -// must be base64 encoded. |
| -const char kPageHtmlBodyPath[] = "/PageHtmlBody?"; |
| - |
| -// Generates an html response from a request to |kPageHtmlBodyPath|. |
| -std::unique_ptr<net::test_server::HttpResponse> EchoPageHTMLBodyInResponse( |
| - const net::test_server::HttpRequest& request) { |
| - DCHECK(base::StartsWith(request.relative_url, kPageHtmlBodyPath, |
| - base::CompareCase::INSENSITIVE_ASCII)); |
| - |
| - std::string body = request.relative_url.substr(strlen(kPageHtmlBodyPath)); |
| - |
| - std::string unescaped_body; |
| - base::Base64Decode(body, &unescaped_body); |
| - std::string html = "<html><body>" + unescaped_body + "</body></html>"; |
| +// Test server path which renders a basic html page. |
| +const char kPageHTMLPath[] = "/PageHtml?"; |
|
Eugene But (OOO till 7-30)
2017/05/31 22:10:01
s/HTML/Html
michaeldo
2017/06/01 15:45:53
Done.
|
| +// URL parameter for html body. Value must be base64 encoded. |
| +const char kPageHTMLBodyParamName[] = "body="; |
| +// URL parameter for page title. Value must be base64 encoded. |
| +const char kPageHTMLTitleParamName[] = "title="; |
| + |
| +// Generates an html response. |
| +std::unique_ptr<net::test_server::HttpResponse> CreatePageHTMLResponse( |
| + const std::string& title, |
| + const std::string& body) { |
| + std::string html = base::StringPrintf( |
| + "<html><head><title>%s</title></head><body>%s</body></html>", |
| + title.c_str(), body.c_str()); |
| auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); |
| http_response->set_content(html); |
| return std::move(http_response); |
| } |
| +// Splits a URL's |query_string| into separate key=value strings. |
| +std::vector<std::string> SplitQueryString(std::string query_string) { |
|
Eugene But (OOO till 7-30)
2017/05/31 22:10:01
How about using query_parser.h API?
michaeldo
2017/06/01 15:45:54
From components/query_parser/query_parser.h? Based
|
| + return base::SplitString(query_string, "&", base::KEEP_WHITESPACE, |
| + base::SPLIT_WANT_ALL); |
| +} |
| + |
| +// Returns true if |string| starts with |prefix|. String comparison is case |
| +// insensitive. |
| +bool StartsWith(std::string string, std::string prefix) { |
| + return base::StartsWith(string, prefix, base::CompareCase::INSENSITIVE_ASCII); |
|
Eugene But (OOO till 7-30)
2017/05/31 22:10:01
Why do we need case insensitive comparison?
michaeldo
2017/06/01 15:45:53
It doesn't need to be insensitive, changed to sens
|
| +} |
| + |
| +// Encodes the |string| for use as the value of a url parameter. |
| +std::string EncodeQueryParamValue(std::string string) { |
| + std::string encoded_string; |
| + base::Base64Encode(string, &encoded_string); |
| + return encoded_string; |
| +} |
| + |
| +// Decodes the |encoded_string|. Undoes the encoding performed by |
| +// |EncodeQueryParamValue|. |
| +std::string DecodeQueryParamValue(std::string encoded_string) { |
| + std::string decoded_string; |
| + base::Base64Decode(encoded_string, &decoded_string); |
| + return decoded_string; |
| +} |
| + |
| // Maps test server requests to responses. |
| std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( |
| const net::test_server::HttpRequest& request) { |
| - if (base::StartsWith(request.relative_url, kPageHtmlBodyPath, |
| - base::CompareCase::INSENSITIVE_ASCII)) { |
| - return EchoPageHTMLBodyInResponse(request); |
| + if (StartsWith(request.relative_url, kPageHTMLPath)) { |
| + std::string title; |
| + std::string body; |
| + |
| + GURL request_url = request.GetURL(); |
| + if (request_url.has_query()) { |
| + for (const auto& query : SplitQueryString(request_url.query())) { |
| + if (StartsWith(query, kPageHTMLTitleParamName)) { |
| + std::string encoded_title = |
| + query.substr(strlen(kPageHTMLTitleParamName)); |
| + title = DecodeQueryParamValue(encoded_title); |
| + } else if (StartsWith(query, kPageHTMLBodyParamName)) { |
| + std::string encoded_body = |
| + query.substr(strlen(kPageHTMLBodyParamName)); |
| + body = DecodeQueryParamValue(encoded_body); |
| + } |
| + } |
| + } |
| + return CreatePageHTMLResponse(title, body); |
| } |
| return nullptr; |
| } |
| @@ -60,7 +102,6 @@ namespace ios_web_view { |
| ChromeWebViewTest::ChromeWebViewTest() |
| : test_server_(base::MakeUnique<net::EmbeddedTestServer>( |
| net::test_server::EmbeddedTestServer::TYPE_HTTP)) { |
| - net::test_server::RegisterDefaultHandlers(test_server_.get()); |
| test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); |
| } |
| @@ -72,13 +113,29 @@ void ChromeWebViewTest::SetUp() { |
| } |
| GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) { |
| - return test_server_->GetURL("/echotitle/" + title); |
| + return GetUrlForPageWithTitleAndBody(title, std::string()); |
| } |
| GURL ChromeWebViewTest::GetUrlForPageWithHTMLBody(const std::string& html) { |
| - std::string base64_html; |
| - base::Base64Encode(html, &base64_html); |
| - return test_server_->GetURL(kPageHtmlBodyPath + base64_html); |
| + return GetUrlForPageWithTitleAndBody(std::string(), html); |
| +} |
| + |
| +GURL ChromeWebViewTest::GetUrlForPageWithTitleAndBody(const std::string& title, |
| + const std::string& body) { |
| + GURL url = test_server_->GetURL(kPageHTMLPath); |
| + |
| + // Encode |title| and |body| in url query in order to build the server |
| + // response later in TestRequestHandler. |
| + std::string url_params = std::string(); |
| + url_params.append(kPageHTMLTitleParamName); |
|
Eugene But (OOO till 7-30)
2017/05/31 22:10:01
How about using AppendQueryParameter?
michaeldo
2017/06/01 15:45:53
Yes! much cleaner. Done.
|
| + url_params.append(EncodeQueryParamValue(title)); |
| + url_params.append("&"); |
| + url_params.append(kPageHTMLBodyParamName); |
| + url_params.append(EncodeQueryParamValue(body)); |
| + |
| + GURL::Replacements replacements; |
| + replacements.SetQueryStr(url_params); |
| + return url.ReplaceComponents(replacements); |
| } |
| void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) { |