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

Unified Diff: android_webview/embeddedtestserver/custom_handlers.cc

Issue 2687573002: [Android Webview] Refactor LoadUrlTest and work on embedded_test_server custom handler (Closed)
Patch Set: ready 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 side-by-side diff with in-line comments
Download patch
Index: android_webview/embeddedtestserver/custom_handlers.cc
diff --git a/android_webview/embeddedtestserver/custom_handlers.cc b/android_webview/embeddedtestserver/custom_handlers.cc
new file mode 100644
index 0000000000000000000000000000000000000000..6506ac3bcd557eecf27427221a9a221ed441ebca
--- /dev/null
+++ b/android_webview/embeddedtestserver/custom_handlers.cc
@@ -0,0 +1,86 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "android_webview/embeddedtestserver/custom_handlers.h"
+
+#include <jni.h>
+
+#include "base/android/jni_array.h"
+#include "base/strings/stringprintf.h"
+#include "net/test/embedded_test_server/http_request.h"
+#include "net/test/embedded_test_server/http_response.h"
+#include "net/test/embedded_test_server/request_handler_util.h"
+#include "net/test/jni/CustomHandlers_jni.h"
+
+using base::android::JavaParamRef;
+using base::android::JavaRef;
+using base::android::ScopedJavaLocalRef;
+using net::test_server::BasicHttpResponse;
+using net::test_server::HttpRequest;
+using net::test_server::HttpResponse;
+
+namespace net {
+namespace test {
+
+// /click-redirect?url=URL&&echoheaders=HEADERS
shenghuazhang 2017/03/13 18:48:15 Should be '/click-redirect?url=URL&echoheaders=HEA
+// Returns a href redirect to URL.
+// Responds in the message body with the headers echoed on the current request.
+std::unique_ptr<HttpResponse> HandleClickRedirect(const HttpRequest& request) {
+ if (!ShouldHandle(request, "/click-redirect"))
+ return nullptr;
+ GURL request_url = request.GetURL();
+
+ std::unique_ptr<BasicHttpResponse> http_response(new BasicHttpResponse);
+ http_response->set_content_type("text/html");
+ http_response->AddCustomHeader("Cache-Control", "no-cache, no-store");
+
+ if (request_url.has_query()) {
+ std::vector<std::string> query_list = base::SplitString(
+ request_url.query(), "|", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
+ std::string pre_url("url=");
+ std::string url;
+ std::string pre_header("echoheaders=");
+ std::string header_query;
+ for (const auto& query : query_list) {
+ if (query.compare(0, pre_url.length(), pre_url) == 0) {
+ url = query.substr(pre_url.length());
+ }
+ if (query.compare(0, pre_header.length(), pre_header) == 0) {
+ header_query = query.substr(pre_header.length());
+ }
+ }
+
+ std::string content;
+ std::vector<std::string> headers = base::SplitString(
+ header_query, "&", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL);
+ for (const auto& header : headers) {
+ std::string header_name = header;
+ std::string header_value = "None";
+ if (request.headers.find(header_name) != request.headers.end())
+ header_value = request.headers.at(header_name);
+ content += header_value + "\n";
+ }
+
+ http_response->set_content(base::StringPrintf(
+ "<html><body><b>%s</b>"
+ "<a id=\"click\" href=\"%s\">Click to redirect to %s</a></body></html>",
+ content.c_str(), url.c_str(), url.c_str()));
+ }
+ return std::move(http_response);
+}
+
+static ScopedJavaLocalRef<jlongArray> GetHandlers(
+ JNIEnv* env,
+ const JavaParamRef<jobject>& jobj) {
+ std::vector<jlong> handlers = {reinterpret_cast<jlong>(&HandleClickRedirect)};
+ return base::android::ToJavaLongArray(env, handlers);
+}
+
+// static
+bool RegisterCustomHandlers(JNIEnv* env) {
+ return RegisterNativesImpl(env);
+}
+
+} // namespace test
+} // namespace net

Powered by Google App Engine
This is Rietveld 408576698