Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(265)

Side by Side Diff: android_webview/test/embeddedtestserver/custom_handlers.cc

Issue 2687573002: [Android Webview] Refactor LoadUrlTest and work on embedded_test_server custom handler (Closed)
Patch Set: John comment & url encode Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(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/embeddedtestserver/custom_handlers.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/base64url.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
29 // /click-redirect?url=URL&echoheaders=HEADERS
jbudorick 2017/03/13 23:32:54 Right now, you've got HEADERS as a URL-encoded lis
shenghuazhang 2017/03/17 01:07:15 Good suggestion! Also, now user need to encode the
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("echoheaders") != query.end()) {
49 std::string header_list = query.at("echoheaders").front();
jbudorick 2017/03/13 23:32:54 w/ the above change, this code would just be itera
50 std::vector<std::string> headers = base::SplitString(
51 header_list, "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
52 for (const auto& header : headers) {
53 std::string header_name = header;
54 std::string header_value = "None";
55 if (request.headers.find(header_name) != request.headers.end())
56 header_value = request.headers.at(header_name);
57 content += header_value + "\n";
58 }
59 }
60
61 http_response->set_content(base::StringPrintf(
62 "<html><body><b>%s</b>"
63 "<a id=\"click\" href=\"%s\">Click to redirect to %s</a></body></html>",
64 content.c_str(), url.c_str(), url.c_str()));
65
66 return std::move(http_response);
67 }
68
69 // static
70 ScopedJavaLocalRef<jlongArray> GetHandlers(JNIEnv* env,
71 const JavaParamRef<jobject>& jobj) {
72 std::vector<jlong> handlers = {reinterpret_cast<jlong>(&HandleClickRedirect)};
73 return base::android::ToJavaLongArray(env, handlers);
74 }
75
76 // static
77 bool RegisterCustomHandlers(JNIEnv* env) {
78 return RegisterNativesImpl(env);
79 }
80
81 } // namespace test
82 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698