| OLD | NEW |
| (Empty) |
| 1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #import "ios/web_view/test/chrome_web_view_test.h" | |
| 6 | |
| 7 #import <ChromeWebView/ChromeWebView.h> | |
| 8 #import <Foundation/Foundation.h> | |
| 9 | |
| 10 #include "base/base64.h" | |
| 11 #import "base/memory/ptr_util.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #import "ios/web_view/test/web_view_test_util.h" | |
| 14 #include "net/base/url_util.h" | |
| 15 #include "net/test/embedded_test_server/embedded_test_server.h" | |
| 16 #include "net/test/embedded_test_server/http_request.h" | |
| 17 #include "net/test/embedded_test_server/http_response.h" | |
| 18 #include "testing/gtest/include/gtest/gtest.h" | |
| 19 | |
| 20 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 21 #error "This file requires ARC support." | |
| 22 #endif | |
| 23 | |
| 24 namespace { | |
| 25 | |
| 26 // Test server path which renders a basic html page. | |
| 27 const char kPageHtmlPath[] = "/PageHtml?"; | |
| 28 // URL parameter for html body. Value must be base64 encoded. | |
| 29 const char kPageHtmlBodyParamName[] = "body"; | |
| 30 // URL parameter for page title. Value must be base64 encoded. | |
| 31 const char kPageHtmlTitleParamName[] = "title"; | |
| 32 | |
| 33 // Generates an html response. | |
| 34 std::unique_ptr<net::test_server::HttpResponse> CreatePageHTMLResponse( | |
| 35 const std::string& title, | |
| 36 const std::string& body) { | |
| 37 std::string html = base::StringPrintf( | |
| 38 "<html><head><title>%s</title></head><body>%s</body></html>", | |
| 39 title.c_str(), body.c_str()); | |
| 40 | |
| 41 auto http_response = base::MakeUnique<net::test_server::BasicHttpResponse>(); | |
| 42 http_response->set_content(html); | |
| 43 return std::move(http_response); | |
| 44 } | |
| 45 | |
| 46 // Returns true if |string| starts with |prefix|. String comparison is case | |
| 47 // insensitive. | |
| 48 bool StartsWith(std::string string, std::string prefix) { | |
| 49 return base::StartsWith(string, prefix, base::CompareCase::SENSITIVE); | |
| 50 } | |
| 51 | |
| 52 // Encodes the |string| for use as the value of a url parameter. | |
| 53 std::string EncodeQueryParamValue(std::string string) { | |
| 54 std::string encoded_string; | |
| 55 base::Base64Encode(string, &encoded_string); | |
| 56 return encoded_string; | |
| 57 } | |
| 58 | |
| 59 // Decodes the |encoded_string|. Undoes the encoding performed by | |
| 60 // |EncodeQueryParamValue|. | |
| 61 std::string DecodeQueryParamValue(std::string encoded_string) { | |
| 62 std::string decoded_string; | |
| 63 base::Base64Decode(encoded_string, &decoded_string); | |
| 64 return decoded_string; | |
| 65 } | |
| 66 | |
| 67 // Maps test server requests to responses. | |
| 68 std::unique_ptr<net::test_server::HttpResponse> TestRequestHandler( | |
| 69 const net::test_server::HttpRequest& request) { | |
| 70 if (StartsWith(request.relative_url, kPageHtmlPath)) { | |
| 71 std::string title; | |
| 72 std::string body; | |
| 73 | |
| 74 GURL request_url = request.GetURL(); | |
| 75 | |
| 76 std::string encoded_title; | |
| 77 bool title_found = net::GetValueForKeyInQuery( | |
| 78 request_url, kPageHtmlTitleParamName, &encoded_title); | |
| 79 if (title_found) { | |
| 80 title = DecodeQueryParamValue(encoded_title); | |
| 81 } | |
| 82 | |
| 83 std::string encoded_body; | |
| 84 bool body_found = net::GetValueForKeyInQuery( | |
| 85 request_url, kPageHtmlBodyParamName, &encoded_body); | |
| 86 if (body_found) { | |
| 87 body = DecodeQueryParamValue(encoded_body); | |
| 88 } | |
| 89 | |
| 90 return CreatePageHTMLResponse(title, body); | |
| 91 } | |
| 92 return nullptr; | |
| 93 } | |
| 94 | |
| 95 } // namespace | |
| 96 | |
| 97 namespace ios_web_view { | |
| 98 | |
| 99 ChromeWebViewTest::ChromeWebViewTest() | |
| 100 : web_view_(test::CreateWebView()), | |
| 101 test_server_(base::MakeUnique<net::EmbeddedTestServer>( | |
| 102 net::test_server::EmbeddedTestServer::TYPE_HTTP)) { | |
| 103 test_server_->RegisterRequestHandler(base::Bind(&TestRequestHandler)); | |
| 104 } | |
| 105 | |
| 106 ChromeWebViewTest::~ChromeWebViewTest() = default; | |
| 107 | |
| 108 void ChromeWebViewTest::SetUp() { | |
| 109 PlatformTest::SetUp(); | |
| 110 ASSERT_TRUE(test_server_->Start()); | |
| 111 } | |
| 112 | |
| 113 GURL ChromeWebViewTest::GetUrlForPageWithTitle(const std::string& title) { | |
| 114 return GetUrlForPageWithTitleAndBody(title, std::string()); | |
| 115 } | |
| 116 | |
| 117 GURL ChromeWebViewTest::GetUrlForPageWithHtmlBody(const std::string& html) { | |
| 118 return GetUrlForPageWithTitleAndBody(std::string(), html); | |
| 119 } | |
| 120 | |
| 121 GURL ChromeWebViewTest::GetUrlForPageWithTitleAndBody(const std::string& title, | |
| 122 const std::string& body) { | |
| 123 GURL url = test_server_->GetURL(kPageHtmlPath); | |
| 124 | |
| 125 // Encode |title| and |body| in url query in order to build the server | |
| 126 // response later in TestRequestHandler. | |
| 127 std::string encoded_title = EncodeQueryParamValue(title); | |
| 128 url = net::AppendQueryParameter(url, kPageHtmlTitleParamName, encoded_title); | |
| 129 std::string encoded_body = EncodeQueryParamValue(body); | |
| 130 url = net::AppendQueryParameter(url, kPageHtmlBodyParamName, encoded_body); | |
| 131 | |
| 132 return url; | |
| 133 } | |
| 134 | |
| 135 } // namespace ios_web_view | |
| OLD | NEW |