1 // Copyright 2014 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.
5 #include "native_test_server.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
10 #include "base/files/file_path.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/path_service.h"
13 #include "base/strings/string_util.h"
14 #include "jni/NativeTestServer_jni.h"
15 #include "net/http/http_status_code.h"
16 #include "net/test/embedded_test_server/embedded_test_server.h"
17 #include "net/test/embedded_test_server/http_request.h"
18 #include "net/test/embedded_test_server/http_response.h"
25 const char echo_body_path
[] = "/echo_body";
26 const char echo_header_path
[] = "/echo_header";
27 const char echo_all_headers_path
[] = "/echo_all_headers";
28 const char echo_method_path
[] = "/echo_method";
29 const char redirect_to_echo_body_path
[] = "/redirect_to_echo_body";
31 net::test_server::EmbeddedTestServer
* g_test_server
= nullptr;
33 scoped_ptr
<net::test_server::HttpResponse
> UploadServerRequestHandler(
34 const net::test_server::HttpRequest
& request
) {
35 DCHECK(g_test_server
);
36 scoped_ptr
<net::test_server::BasicHttpResponse
> response(
37 new net::test_server::BasicHttpResponse());
38 response
->set_content_type("text/plain");
40 if (request
.relative_url
== echo_body_path
) {
41 if (request
.has_content
) {
42 response
->set_content(request
.content
);
44 response
->set_content("Request has no body. :(");
46 return response
.Pass();
49 if (StartsWithASCII(request
.relative_url
, echo_header_path
, true)) {
50 GURL url
= g_test_server
->GetURL(request
.relative_url
);
51 auto it
= request
.headers
.find(url
.query());
52 if (it
!= request
.headers
.end()) {
53 response
->set_content(it
->second
);
55 response
->set_content("Header not found. :(");
57 return response
.Pass();
60 if (request
.relative_url
== echo_all_headers_path
) {
61 response
->set_content(request
.all_headers
);
62 return response
.Pass();
65 if (request
.relative_url
== echo_method_path
) {
66 response
->set_content(request
.method_string
);
67 return response
.Pass();
70 if (request
.relative_url
== redirect_to_echo_body_path
) {
71 response
->set_code(net::HTTP_TEMPORARY_REDIRECT
);
72 response
->AddCustomHeader("Location", echo_body_path
);
73 return response
.Pass();
76 // Unhandled requests result in the Embedded test server sending a 404.
77 return scoped_ptr
<net::test_server::BasicHttpResponse
>();
82 jboolean
StartNativeTestServer(JNIEnv
* env
,
84 jstring jtest_files_root
) {
88 g_test_server
= new net::test_server::EmbeddedTestServer();
89 g_test_server
->RegisterRequestHandler(
90 base::Bind(&UploadServerRequestHandler
));
91 // Add a second handler for paths that UploadServerRequestHandler does not
93 base::FilePath
test_files_root(
94 base::android::ConvertJavaStringToUTF8(env
, jtest_files_root
));
95 g_test_server
->ServeFilesFromDirectory(test_files_root
);
96 return g_test_server
->InitializeAndWaitUntilReady();
99 void ShutdownNativeTestServer(JNIEnv
* env
, jclass jcaller
) {
102 delete g_test_server
;
103 g_test_server
= NULL
;
106 jstring
GetEchoBodyURL(JNIEnv
* env
, jclass jcaller
) {
107 DCHECK(g_test_server
);
108 GURL url
= g_test_server
->GetURL(echo_body_path
);
109 return base::android::ConvertUTF8ToJavaString(env
, url
.spec()).Release();
112 jstring
GetEchoHeaderURL(JNIEnv
* env
, jclass jcaller
, jstring jheader
) {
113 DCHECK(g_test_server
);
114 GURL url
= g_test_server
->GetURL(echo_header_path
);
115 GURL::Replacements replacements
;
116 std::string header
= base::android::ConvertJavaStringToUTF8(env
, jheader
);
117 replacements
.SetQueryStr(header
.c_str());
118 url
= url
.ReplaceComponents(replacements
);
119 return base::android::ConvertUTF8ToJavaString(env
, url
.spec()).Release();
122 jstring
GetEchoAllHeadersURL(JNIEnv
* env
, jclass jcaller
) {
123 DCHECK(g_test_server
);
124 GURL url
= g_test_server
->GetURL(echo_all_headers_path
);
125 return base::android::ConvertUTF8ToJavaString(env
, url
.spec()).Release();
128 jstring
GetEchoMethodURL(JNIEnv
* env
, jclass jcaller
) {
129 DCHECK(g_test_server
);
130 GURL url
= g_test_server
->GetURL(echo_method_path
);
131 return base::android::ConvertUTF8ToJavaString(env
, url
.spec()).Release();
134 jstring
GetRedirectToEchoBody(JNIEnv
* env
, jclass jcaller
) {
135 DCHECK(g_test_server
);
136 GURL url
= g_test_server
->GetURL(redirect_to_echo_body_path
);
137 return base::android::ConvertUTF8ToJavaString(env
, url
.spec()).Release();
140 jstring
GetFileURL(JNIEnv
* env
, jclass jcaller
, jstring jfile_path
) {
141 DCHECK(g_test_server
);
142 std::string file
= base::android::ConvertJavaStringToUTF8(env
, jfile_path
);
143 GURL url
= g_test_server
->GetURL(file
);
144 return base::android::ConvertUTF8ToJavaString(env
, url
.spec()).Release();
147 bool RegisterNativeTestServer(JNIEnv
* env
) {
148 return RegisterNativesImpl(env
);
151 } // namespace cronet