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..e8d64d6cd2b36b7467ae47f7d84d817e2e1cc6b2 100644 |
| --- a/ios/web_view/test/chrome_web_view_test.mm |
| +++ b/ios/web_view/test/chrome_web_view_test.mm |
| @@ -10,7 +10,6 @@ |
| #include "base/base64.h" |
| #import "base/memory/ptr_util.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,21 +21,19 @@ |
| namespace { |
| -// Test server path which echos the remainder of the url as html. The html |
| -// must be base64 encoded. |
| -const char kPageHtmlBodyPath[] = "/PageHtmlBody?"; |
| +// Test server path which renders a basic html page. |
| +const char kPageHTMLPath[] = "/PageHtml?"; |
| +// 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 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>"; |
| +// Generates an html response. |
| +std::unique_ptr<net::test_server::HttpResponse> CreatePageHTMLResponse( |
| + const std::string& title, |
| + const std::string& body) { |
| + std::string html = "<html><head><title>" + title + "</title></head><body>" + |
|
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
Please use StringPrint instead of +
michaeldo
2017/05/26 14:59:02
Done.
|
| + body + "</body></html>"; |
| auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); |
| http_response->set_content(html); |
| @@ -46,9 +43,36 @@ std::unique_ptr<net::test_server::HttpResponse> EchoPageHTMLBodyInResponse( |
| // 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, |
| + if (base::StartsWith(request.relative_url, kPageHTMLPath, |
| base::CompareCase::INSENSITIVE_ASCII)) { |
| - return EchoPageHTMLBodyInResponse(request); |
| + std::string title = std::string(); |
|
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
It's quite hard to tell what this method does. Do
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
s/std::string title = std::string();/std::string t
michaeldo
2017/05/26 14:59:01
Done.
michaeldo
2017/05/26 14:59:01
I agree, this method went through many local revis
michaeldo
2017/05/26 14:59:02
I agree, this method went through many local revis
|
| + std::string body = std::string(); |
| + |
| + GURL request_url = request.GetURL(); |
| + if (request_url.has_query()) { |
| + std::vector<std::string> query_list = |
| + base::SplitString(request_url.query(), "&", base::KEEP_WHITESPACE, |
| + base::SPLIT_WANT_ALL); |
| + |
| + for (const auto& query : query_list) { |
| + if (base::StartsWith(query, kPageHTMLTitleParamName, |
| + base::CompareCase::INSENSITIVE_ASCII)) { |
| + std::string encoded_title = |
| + query.substr(strlen(kPageHTMLTitleParamName)); |
| + std::string decoded_title; |
| + base::Base64Decode(encoded_title, &decoded_title); |
| + title = decoded_title; |
| + } else if (base::StartsWith(query, kPageHTMLBodyParamName, |
| + base::CompareCase::INSENSITIVE_ASCII)) { |
| + std::string encoded_body = |
| + query.substr(strlen(kPageHTMLBodyParamName)); |
| + std::string decoded_body; |
| + base::Base64Decode(encoded_body, &decoded_body); |
| + body = decoded_body; |
| + } |
| + } |
| + } |
| + return CreatePageHTMLResponse(title, body); |
| } |
| return nullptr; |
| } |
| @@ -60,7 +84,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 +95,27 @@ 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) { |
| + std::string url_params = std::string(); |
|
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
Please describe in comments how query is used.
michaeldo
2017/05/26 14:59:02
Done.
|
| + std::string base64_title; |
| + base::Base64Encode(title, &base64_title); |
| + url_params += kPageHTMLTitleParamName + base64_title; |
|
Eugene But (OOO till 7-30)
2017/05/25 17:43:03
Please use GURL API to construct the url.
michaeldo
2017/05/26 14:59:02
Done.
|
| + |
| + url_params += "&"; |
| + |
| + std::string base64_body; |
| + base::Base64Encode(body, &base64_body); |
| + url_params += kPageHTMLBodyParamName + base64_body; |
| + |
| + return test_server_->GetURL(kPageHTMLPath + url_params); |
| } |
| void ChromeWebViewTest::LoadUrl(CWVWebView* web_view, NSURL* url) { |