Chromium Code Reviews| 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 #include "android_webview/test/embedded_test_server/aw_embedded_test_server.h" | |
| 6 | |
| 7 #include <jni.h> | |
| 8 | |
| 9 #include "android_webview/test/jni/AwEmbeddedTestServerImpl_jni.h" | |
| 10 #include "base/android/jni_array.h" | |
| 11 #include "base/base64.h" | |
| 12 #include "base/strings/stringprintf.h" | |
| 13 #include "net/test/embedded_test_server/http_request.h" | |
| 14 #include "net/test/embedded_test_server/http_response.h" | |
| 15 #include "net/test/embedded_test_server/request_handler_util.h" | |
| 16 | |
| 17 using base::android::JavaParamRef; | |
| 18 using base::android::JavaRef; | |
| 19 using base::android::ScopedJavaLocalRef; | |
| 20 using net::test_server::BasicHttpResponse; | |
| 21 using net::test_server::HttpRequest; | |
| 22 using net::test_server::HttpResponse; | |
| 23 using net::test_server::ParseQuery; | |
| 24 using net::test_server::RequestQuery; | |
| 25 | |
| 26 namespace android_webview { | |
| 27 namespace test { | |
| 28 | |
|
sgurun-gerrit only
2017/03/29 18:19:27
put the handlers in a anonymous namespace for inte
shenghuazhang
2017/04/11 01:38:27
Done.
| |
| 29 // /click-redirect?url=URL&echoheader=HEADER | |
|
sgurun-gerrit only
2017/03/29 18:19:27
s/echoheader/header/
shenghuazhang
2017/04/11 01:38:27
Done.
| |
| 30 // Returns a href redirect to URL. | |
| 31 // Responds in the message body with the headers echoed on the current request. | |
| 32 std::unique_ptr<HttpResponse> HandleClickRedirect(const HttpRequest& request) { | |
| 33 if (!ShouldHandle(request, "/click-redirect")) | |
| 34 return nullptr; | |
| 35 GURL request_url = request.GetURL(); | |
| 36 RequestQuery query = ParseQuery(request_url); | |
| 37 | |
| 38 std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | |
| 39 http_response->set_content_type("text/html"); | |
| 40 http_response->AddCustomHeader("Cache-Control", "no-cache, no-store"); | |
| 41 | |
| 42 std::string url; | |
| 43 if (query.find("url") != query.end()) { | |
| 44 url = query.at("url").front(); | |
| 45 } | |
| 46 | |
| 47 std::string content; | |
| 48 if (query.find("echoheader") != query.end()) { | |
| 49 for (const auto& header : query.at("echoheader")) { | |
| 50 std::string header_value = "None"; | |
| 51 if (request.headers.find(header) != request.headers.end()) | |
| 52 header_value = request.headers.at(header); | |
| 53 content += header_value + "\n"; | |
| 54 } | |
| 55 } | |
| 56 | |
| 57 http_response->set_content(base::StringPrintf( | |
| 58 "<html><body><div>%s</div>" | |
| 59 "<a id=\"click\" href=\"%s\">Click to redirect to %s</a></body></html>", | |
| 60 content.c_str(), url.c_str(), url.c_str())); | |
| 61 | |
| 62 return std::move(http_response); | |
| 63 } | |
| 64 | |
| 65 // /echoheader-and-set-data?header=HEADER1&data=DATA | |
| 66 // Responds as json in the message body with the headers echoed on the current | |
| 67 // request and a content that matches DATA. | |
| 68 std::unique_ptr<HttpResponse> HandleEchoHeaderAndSetData( | |
| 69 const HttpRequest& request) { | |
| 70 if (!ShouldHandle(request, "/echoheader-and-set-data")) | |
| 71 return nullptr; | |
| 72 GURL request_url = request.GetURL(); | |
| 73 RequestQuery query = ParseQuery(request_url); | |
| 74 | |
| 75 std::string header_content; | |
| 76 if (query.find("header") != query.end()) { | |
| 77 for (const auto& header : query.at("header")) { | |
| 78 std::string header_value = "None"; | |
| 79 if (request.headers.find(header) != request.headers.end()) | |
| 80 header_value = request.headers.at(header); | |
| 81 if (!header_content.empty()) | |
| 82 header_content += ","; | |
| 83 header_content += "\"" + header_value + "\""; | |
| 84 } | |
| 85 } | |
| 86 | |
| 87 std::string data_content; | |
| 88 if (query.find("data") != query.end()) { | |
| 89 for (const auto& data : query.at("data")) { | |
| 90 if (!data_content.empty()) | |
| 91 data_content += ", "; | |
| 92 data_content += "\"" + data + "\""; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | |
| 97 http_response->set_content_type("application/json"); | |
| 98 http_response->AddCustomHeader("Cache-Control", "no-cache, no-store"); | |
| 99 http_response->set_content( | |
| 100 base::StringPrintf("{" | |
| 101 " \"echo header\": [%s]," | |
| 102 " \"data content\": [%s]" | |
| 103 "}", | |
| 104 header_content.c_str(), data_content.c_str())); | |
| 105 | |
| 106 return std::move(http_response); | |
| 107 } | |
| 108 | |
| 109 // /server-redirect-echoheader?url=URL&header=HEADER | |
| 110 // Returns a server-redirect (301) to URL. Pass the headers echoed on the | |
| 111 // current request into the URL request. | |
| 112 std::unique_ptr<HttpResponse> HandleServerRedirectEchoHeader( | |
| 113 const HttpRequest& request) { | |
| 114 if (!ShouldHandle(request, "/server-redirect-echoheader")) | |
| 115 return nullptr; | |
| 116 GURL request_url = request.GetURL(); | |
| 117 RequestQuery query = ParseQuery(request_url); | |
| 118 | |
| 119 std::string url; | |
| 120 if (query.find("url") != query.end()) { | |
| 121 url = query.at("url").front(); | |
| 122 } | |
| 123 | |
| 124 std::string url_suffix; | |
| 125 if (query.find("header") != query.end()) { | |
| 126 for (const auto& header : query.at("header")) { | |
| 127 std::string header_value = "None"; | |
| 128 if (request.headers.find(header) != request.headers.end()) | |
| 129 header_value = request.headers.at(header); | |
| 130 url_suffix += "&data=" + header_value; | |
| 131 } | |
| 132 } | |
| 133 url.append(url_suffix); | |
| 134 | |
| 135 std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | |
| 136 http_response->set_code(net::HTTP_MOVED_PERMANENTLY); | |
| 137 http_response->AddCustomHeader("Location", url); | |
| 138 http_response->set_content_type("text/html"); | |
| 139 http_response->set_content(base::StringPrintf( | |
| 140 "<html><body>Redirecting to %s</body></html>", url.c_str())); | |
| 141 return std::move(http_response); | |
| 142 } | |
| 143 | |
| 144 // /set-image-response?resource=RESOURCE&header=HEADER | |
|
sgurun-gerrit only
2017/03/29 18:19:27
rename to image-response-if-header-not-exists
shenghuazhang
2017/04/11 01:38:27
Done.
| |
| 145 // Returns the response with the base64 encoded image resource in the request. | |
| 146 std::unique_ptr<HttpResponse> HandleSetImageResponse( | |
| 147 const HttpRequest& request) { | |
| 148 if (!ShouldHandle(request, "/set-image-response")) | |
| 149 return nullptr; | |
| 150 GURL request_url = request.GetURL(); | |
| 151 RequestQuery query = ParseQuery(request_url); | |
| 152 | |
| 153 std::string resource; | |
| 154 if (query.find("resource") != query.end()) { | |
| 155 resource = query.at("resource").front(); | |
| 156 } | |
| 157 | |
| 158 bool header_exist = false; | |
| 159 if (query.find("header") != query.end()) { | |
|
shenghuazhang
2017/03/23 01:26:16
The test 'testLoadUrlWithExtraHeaders' uses this h
| |
| 160 for (const auto& header : query.at("header")) { | |
| 161 if (request.headers.find(header) != request.headers.end()) | |
| 162 header_exist = true; | |
| 163 break; | |
| 164 } | |
| 165 } | |
| 166 | |
| 167 std::string decoded_resource; | |
| 168 if (!base::Base64Decode(resource, &decoded_resource)) | |
| 169 return nullptr; | |
| 170 | |
| 171 std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | |
| 172 http_response->set_content_type("image/png"); | |
| 173 http_response->AddCustomHeader("Cache-Control", "no-store"); | |
| 174 if (!header_exist) | |
| 175 http_response->set_content(decoded_resource); | |
| 176 else { | |
|
shenghuazhang
2017/03/23 01:26:16
if the extra header exist, set '404' status code t
| |
| 177 http_response->set_code(net::HTTP_NOT_FOUND); | |
| 178 http_response->set_content("Found Extra Header. Validation Failed."); | |
| 179 } | |
| 180 return std::move(http_response); | |
| 181 } | |
| 182 | |
| 183 // /image-onload-html?imagesrc=URL&header=HEADER | |
| 184 // Returns the response with the base64 encoded image resource in the request. | |
| 185 std::unique_ptr<HttpResponse> HandleImageOnloadHtml( | |
| 186 const HttpRequest& request) { | |
| 187 if (!ShouldHandle(request, "/image-onload-html")) | |
| 188 return nullptr; | |
| 189 GURL request_url = request.GetURL(); | |
| 190 RequestQuery query = ParseQuery(request_url); | |
| 191 | |
| 192 std::string image_url; | |
| 193 if (query.find("imagesrc") != query.end()) { | |
| 194 image_url = query.at("imagesrc").front(); | |
| 195 } | |
| 196 | |
| 197 std::string content; | |
| 198 if (query.find("header") != query.end()) { | |
| 199 for (const auto& header : query.at("header")) { | |
| 200 std::string header_value = "None"; | |
| 201 if (request.headers.find(header) != request.headers.end()) | |
| 202 header_value = request.headers.at(header); | |
| 203 content += header_value + "\n"; | |
| 204 } | |
| 205 } | |
| 206 | |
| 207 std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse); | |
| 208 http_response->set_content_type("text/html"); | |
| 209 http_response->set_content(base::StringPrintf( | |
| 210 "<html><head><script>" | |
| 211 "function updateTitle() " | |
| 212 "{ document.title=document.getElementById('img').naturalHeight } " | |
|
sgurun-gerrit only
2017/03/29 18:19:27
where do you use the title?
| |
| 213 "function setContent() " | |
| 214 "{ document.getElementById('img').textContent=" | |
| 215 "'Found Extra Header on Image URL.' }</script></head><body><div>%s</div>" | |
| 216 "<div onload='updateTitle();'><img id='img' onload='updateTitle();' " | |
| 217 "src='%s' onError='setContent()'></div></body></html>", | |
|
shenghuazhang
2017/03/23 01:26:16
This way is a little hacky. Since we expect the 'F
shenghuazhang
2017/03/23 01:26:16
If the image src url sent an error in response, ca
| |
| 218 content.c_str(), image_url.c_str())); | |
| 219 return std::move(http_response); | |
| 220 } | |
| 221 | |
| 222 // static | |
| 223 ScopedJavaLocalRef<jlongArray> GetHandlers(JNIEnv* env, | |
| 224 const JavaParamRef<jclass>&) { | |
| 225 std::vector<jlong> handlers = { | |
| 226 reinterpret_cast<jlong>(&HandleClickRedirect), | |
| 227 reinterpret_cast<jlong>(&HandleEchoHeaderAndSetData), | |
| 228 reinterpret_cast<jlong>(&HandleServerRedirectEchoHeader), | |
| 229 reinterpret_cast<jlong>(&HandleSetImageResponse), | |
| 230 reinterpret_cast<jlong>(&HandleImageOnloadHtml)}; | |
| 231 return base::android::ToJavaLongArray(env, handlers); | |
| 232 } | |
| 233 | |
| 234 // static | |
| 235 bool RegisterCustomHandlers(JNIEnv* env) { | |
| 236 return RegisterNativesImpl(env); | |
| 237 } | |
| 238 | |
| 239 } // namespace test | |
| 240 } // namespace android_webview | |
| OLD | NEW |