OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/cronet/android/test/native_test_server.h" | 5 #include "components/cronet/android/test/native_test_server.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <string> | 8 #include <string> |
9 #include <utility> | 9 #include <utility> |
10 | 10 |
11 #include "base/android/jni_android.h" | 11 #include "base/android/jni_android.h" |
12 #include "base/android/jni_string.h" | 12 #include "base/android/jni_string.h" |
13 #include "base/android/path_utils.h" | 13 #include "base/android/path_utils.h" |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/files/file_path.h" | 15 #include "base/files/file_path.h" |
16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ptr_util.h" | |
18 #include "base/path_service.h" | 19 #include "base/path_service.h" |
19 #include "base/strings/string_util.h" | 20 #include "base/strings/string_util.h" |
20 #include "base/strings/stringprintf.h" | 21 #include "base/strings/stringprintf.h" |
21 #include "base/test/test_support_android.h" | 22 #include "base/test/test_support_android.h" |
23 #include "base/threading/thread_task_runner_handle.h" | |
22 #include "components/cronet/android/test/cronet_test_util.h" | 24 #include "components/cronet/android/test/cronet_test_util.h" |
23 #include "jni/NativeTestServer_jni.h" | 25 #include "jni/NativeTestServer_jni.h" |
24 #include "net/base/host_port_pair.h" | 26 #include "net/base/host_port_pair.h" |
25 #include "net/base/url_util.h" | 27 #include "net/base/url_util.h" |
26 #include "net/http/http_status_code.h" | 28 #include "net/http/http_status_code.h" |
27 #include "net/test/embedded_test_server/embedded_test_server.h" | 29 #include "net/test/embedded_test_server/embedded_test_server.h" |
28 #include "net/test/embedded_test_server/http_request.h" | 30 #include "net/test/embedded_test_server/http_request.h" |
29 #include "net/test/embedded_test_server/http_response.h" | 31 #include "net/test/embedded_test_server/http_response.h" |
32 #include "net/test/embedded_test_server/request_handler_util.h" | |
30 #include "url/gurl.h" | 33 #include "url/gurl.h" |
31 | 34 |
32 namespace cronet { | 35 namespace cronet { |
33 | 36 |
34 namespace { | 37 namespace { |
35 | 38 |
36 const char kEchoBodyPath[] = "/echo_body"; | 39 const char kEchoBodyPath[] = "/echo_body"; |
37 const char kEchoHeaderPath[] = "/echo_header"; | 40 const char kEchoHeaderPath[] = "/echo_header"; |
38 const char kEchoAllHeadersPath[] = "/echo_all_headers"; | 41 const char kEchoAllHeadersPath[] = "/echo_all_headers"; |
39 const char kEchoMethodPath[] = "/echo_method"; | 42 const char kEchoMethodPath[] = "/echo_method"; |
40 const char kRedirectToEchoBodyPath[] = "/redirect_to_echo_body"; | 43 const char kRedirectToEchoBodyPath[] = "/redirect_to_echo_body"; |
44 const char kInfiniteResponsePath[] = "/infinite_response"; | |
41 // Path that advertises the dictionary passed in query params if client | 45 // Path that advertises the dictionary passed in query params if client |
42 // supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server | 46 // supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server |
43 // responds with "Get-Dictionary: /sdch/dict/LeQxM80O". | 47 // responds with "Get-Dictionary: /sdch/dict/LeQxM80O". |
44 const char kSdchPath[] = "/sdch/index"; | 48 const char kSdchPath[] = "/sdch/index"; |
45 // Path that returns encoded response if client has the right dictionary. | 49 // Path that returns encoded response if client has the right dictionary. |
46 const char kSdchTestPath[] = "/sdch/test"; | 50 const char kSdchTestPath[] = "/sdch/test"; |
47 // Path where dictionaries are stored. | 51 // Path where dictionaries are stored. |
48 const char kSdchDictPath[] = "/sdch/dict/"; | 52 const char kSdchDictPath[] = "/sdch/dict/"; |
49 | 53 |
50 net::EmbeddedTestServer* g_test_server = nullptr; | 54 net::EmbeddedTestServer* g_test_server = nullptr; |
51 | 55 |
56 // A HttpResponse that is never ending. | |
57 class InfiniteResponse : public net::test_server::BasicHttpResponse { | |
58 public: | |
59 InfiniteResponse() {} | |
60 | |
61 void SendResponse( | |
62 const net::test_server::SendBytesCallback& send, | |
63 const net::test_server::SendCompleteCallback& done) override { | |
64 // Use am arbitrarily large content length so that the client will be | |
65 // expecting data. | |
66 send.Run("HTTP/1.1 200 OK\r\nContent-Length:100000000\r\n\r\n", | |
pauljensen
2016/07/20 17:40:23
Is there any way we can avoid this arbitrary numbe
xunjieli
2016/07/20 17:46:18
If we don't specify the Content-Length, client wil
pauljensen
2016/07/20 17:55:55
10^8 isn't really that big relative to the size of
xunjieli
2016/07/20 18:42:02
Done. Good point!
| |
67 base::Bind(&InfiniteResponse::SendInfinite, send)); | |
68 } | |
69 | |
70 private: | |
71 // Keeps sending the word "echo" over and over again. | |
72 static void SendInfinite(const net::test_server::SendBytesCallback& send) { | |
73 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
74 FROM_HERE, | |
75 base::Bind(send, "echo", | |
76 base::Bind(&InfiniteResponse::SendInfinite, send))); | |
77 } | |
78 | |
79 DISALLOW_COPY_AND_ASSIGN(InfiniteResponse); | |
80 }; | |
81 | |
52 std::unique_ptr<net::test_server::RawHttpResponse> ConstructResponseBasedOnFile( | 82 std::unique_ptr<net::test_server::RawHttpResponse> ConstructResponseBasedOnFile( |
53 const base::FilePath& file_path) { | 83 const base::FilePath& file_path) { |
54 std::string file_contents; | 84 std::string file_contents; |
55 bool read_file = base::ReadFileToString(file_path, &file_contents); | 85 bool read_file = base::ReadFileToString(file_path, &file_contents); |
56 DCHECK(read_file); | 86 DCHECK(read_file); |
57 base::FilePath headers_path( | 87 base::FilePath headers_path( |
58 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); | 88 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
59 std::string headers_contents; | 89 std::string headers_contents; |
60 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); | 90 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); |
61 DCHECK(read_headers); | 91 DCHECK(read_headers); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 new net::test_server::BasicHttpResponse()); | 184 new net::test_server::BasicHttpResponse()); |
155 response->set_content_type("text/plain"); | 185 response->set_content_type("text/plain"); |
156 response->set_content("Sdch is not used.\n"); | 186 response->set_content("Sdch is not used.\n"); |
157 return std::move(response); | 187 return std::move(response); |
158 } | 188 } |
159 | 189 |
160 // Unhandled requests result in the Embedded test server sending a 404. | 190 // Unhandled requests result in the Embedded test server sending a 404. |
161 return std::unique_ptr<net::test_server::BasicHttpResponse>(); | 191 return std::unique_ptr<net::test_server::BasicHttpResponse>(); |
162 } | 192 } |
163 | 193 |
194 std::unique_ptr<net::test_server::HttpResponse> HandleInfiniteRequest( | |
195 const net::test_server::HttpRequest& request) { | |
196 return base::WrapUnique(new InfiniteResponse); | |
197 } | |
198 | |
164 } // namespace | 199 } // namespace |
165 | 200 |
166 jboolean StartNativeTestServer(JNIEnv* env, | 201 jboolean StartNativeTestServer(JNIEnv* env, |
167 const JavaParamRef<jclass>& jcaller, | 202 const JavaParamRef<jclass>& jcaller, |
168 const JavaParamRef<jstring>& jtest_files_root, | 203 const JavaParamRef<jstring>& jtest_files_root, |
169 const JavaParamRef<jstring>& jtest_data_dir) { | 204 const JavaParamRef<jstring>& jtest_data_dir) { |
170 // Shouldn't happen. | 205 // Shouldn't happen. |
171 if (g_test_server) | 206 if (g_test_server) |
172 return false; | 207 return false; |
173 | 208 |
174 base::FilePath test_data_dir( | 209 base::FilePath test_data_dir( |
175 base::android::ConvertJavaStringToUTF8(env, jtest_data_dir)); | 210 base::android::ConvertJavaStringToUTF8(env, jtest_data_dir)); |
176 base::InitAndroidTestPaths(test_data_dir); | 211 base::InitAndroidTestPaths(test_data_dir); |
177 | 212 |
178 g_test_server = new net::EmbeddedTestServer(); | 213 g_test_server = new net::EmbeddedTestServer(); |
179 g_test_server->RegisterRequestHandler( | 214 g_test_server->RegisterRequestHandler( |
180 base::Bind(&NativeTestServerRequestHandler)); | 215 base::Bind(&NativeTestServerRequestHandler)); |
216 g_test_server->RegisterDefaultHandler( | |
217 base::Bind(&net::test_server::HandlePrefixedRequest, | |
218 kInfiniteResponsePath, base::Bind(&HandleInfiniteRequest))); | |
181 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); | 219 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); |
182 base::FilePath test_files_root( | 220 base::FilePath test_files_root( |
183 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 221 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); |
184 | 222 |
185 // Add a third handler for paths that NativeTestServerRequestHandler does not | 223 // Add a third handler for paths that NativeTestServerRequestHandler does not |
186 // handle. | 224 // handle. |
187 g_test_server->ServeFilesFromDirectory(test_files_root); | 225 g_test_server->ServeFilesFromDirectory(test_files_root); |
188 return g_test_server->Start(); | 226 return g_test_server->Start(); |
189 } | 227 } |
190 | 228 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 } | 290 } |
253 | 291 |
254 ScopedJavaLocalRef<jstring> GetSdchURL(JNIEnv* env, | 292 ScopedJavaLocalRef<jstring> GetSdchURL(JNIEnv* env, |
255 const JavaParamRef<jclass>& jcaller) { | 293 const JavaParamRef<jclass>& jcaller) { |
256 DCHECK(g_test_server); | 294 DCHECK(g_test_server); |
257 std::string url(base::StringPrintf("http://%s:%d", kFakeSdchDomain, | 295 std::string url(base::StringPrintf("http://%s:%d", kFakeSdchDomain, |
258 g_test_server->port())); | 296 g_test_server->port())); |
259 return base::android::ConvertUTF8ToJavaString(env, url); | 297 return base::android::ConvertUTF8ToJavaString(env, url); |
260 } | 298 } |
261 | 299 |
300 ScopedJavaLocalRef<jstring> GetInfiniteResponseURL( | |
301 JNIEnv* env, | |
302 const JavaParamRef<jclass>& jcaller) { | |
303 DCHECK(g_test_server); | |
304 GURL url = g_test_server->GetURL(kInfiniteResponsePath); | |
305 return base::android::ConvertUTF8ToJavaString(env, url.spec()); | |
306 } | |
307 | |
262 ScopedJavaLocalRef<jstring> GetHostPort(JNIEnv* env, | 308 ScopedJavaLocalRef<jstring> GetHostPort(JNIEnv* env, |
263 const JavaParamRef<jclass>& jcaller) { | 309 const JavaParamRef<jclass>& jcaller) { |
264 DCHECK(g_test_server); | 310 DCHECK(g_test_server); |
265 std::string host_port = | 311 std::string host_port = |
266 net::HostPortPair::FromURL(g_test_server->base_url()).ToString(); | 312 net::HostPortPair::FromURL(g_test_server->base_url()).ToString(); |
267 return base::android::ConvertUTF8ToJavaString(env, host_port); | 313 return base::android::ConvertUTF8ToJavaString(env, host_port); |
268 } | 314 } |
269 | 315 |
270 jboolean IsDataReductionProxySupported(JNIEnv* env, | 316 jboolean IsDataReductionProxySupported(JNIEnv* env, |
271 const JavaParamRef<jclass>& jcaller) { | 317 const JavaParamRef<jclass>& jcaller) { |
272 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 318 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
273 return JNI_TRUE; | 319 return JNI_TRUE; |
274 #else | 320 #else |
275 return JNI_FALSE; | 321 return JNI_FALSE; |
276 #endif | 322 #endif |
277 } | 323 } |
278 | 324 |
279 bool RegisterNativeTestServer(JNIEnv* env) { | 325 bool RegisterNativeTestServer(JNIEnv* env) { |
280 return RegisterNativesImpl(env); | 326 return RegisterNativesImpl(env); |
281 } | 327 } |
282 | 328 |
283 } // namespace cronet | 329 } // namespace cronet |
OLD | NEW |