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 kExabyteResponsePath[] = "/exabyte_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 almost never ending (with an Extabyte content-length). | |
57 class ExabyteResponse : public net::test_server::BasicHttpResponse { | |
58 public: | |
59 ExabyteResponse() {} | |
60 | |
61 void SendResponse( | |
62 const net::test_server::SendBytesCallback& send, | |
63 const net::test_server::SendCompleteCallback& done) override { | |
64 // Use 10^18 bytes (exabyte) as the content length so that the client will | |
65 // be expecting data. | |
66 send.Run("HTTP/1.1 200 OK\r\nContent-Length:1000000000000000000\r\n\r\n", | |
67 base::Bind(&ExabyteResponse::SendExabyte, send)); | |
68 } | |
69 | |
70 private: | |
71 // Keeps sending the word "echo" over and over again. | |
72 static void SendExabyte(const net::test_server::SendBytesCallback& send) { | |
73 base::ThreadTaskRunnerHandle::Get()->PostTask( | |
pauljensen
2016/07/21 11:30:54
I think we should make this stop after sending an
xunjieli
2016/07/21 13:05:05
Done.
| |
74 FROM_HERE, base::Bind(send, "echo", | |
75 base::Bind(&ExabyteResponse::SendExabyte, send))); | |
76 } | |
77 | |
78 DISALLOW_COPY_AND_ASSIGN(ExabyteResponse); | |
79 }; | |
80 | |
52 std::unique_ptr<net::test_server::RawHttpResponse> ConstructResponseBasedOnFile( | 81 std::unique_ptr<net::test_server::RawHttpResponse> ConstructResponseBasedOnFile( |
53 const base::FilePath& file_path) { | 82 const base::FilePath& file_path) { |
54 std::string file_contents; | 83 std::string file_contents; |
55 bool read_file = base::ReadFileToString(file_path, &file_contents); | 84 bool read_file = base::ReadFileToString(file_path, &file_contents); |
56 DCHECK(read_file); | 85 DCHECK(read_file); |
57 base::FilePath headers_path( | 86 base::FilePath headers_path( |
58 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); | 87 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
59 std::string headers_contents; | 88 std::string headers_contents; |
60 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); | 89 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); |
61 DCHECK(read_headers); | 90 DCHECK(read_headers); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
154 new net::test_server::BasicHttpResponse()); | 183 new net::test_server::BasicHttpResponse()); |
155 response->set_content_type("text/plain"); | 184 response->set_content_type("text/plain"); |
156 response->set_content("Sdch is not used.\n"); | 185 response->set_content("Sdch is not used.\n"); |
157 return std::move(response); | 186 return std::move(response); |
158 } | 187 } |
159 | 188 |
160 // Unhandled requests result in the Embedded test server sending a 404. | 189 // Unhandled requests result in the Embedded test server sending a 404. |
161 return std::unique_ptr<net::test_server::BasicHttpResponse>(); | 190 return std::unique_ptr<net::test_server::BasicHttpResponse>(); |
162 } | 191 } |
163 | 192 |
193 std::unique_ptr<net::test_server::HttpResponse> HandleExabyteRequest( | |
194 const net::test_server::HttpRequest& request) { | |
195 return base::WrapUnique(new ExabyteResponse); | |
196 } | |
197 | |
164 } // namespace | 198 } // namespace |
165 | 199 |
166 jboolean StartNativeTestServer(JNIEnv* env, | 200 jboolean StartNativeTestServer(JNIEnv* env, |
167 const JavaParamRef<jclass>& jcaller, | 201 const JavaParamRef<jclass>& jcaller, |
168 const JavaParamRef<jstring>& jtest_files_root, | 202 const JavaParamRef<jstring>& jtest_files_root, |
169 const JavaParamRef<jstring>& jtest_data_dir) { | 203 const JavaParamRef<jstring>& jtest_data_dir) { |
170 // Shouldn't happen. | 204 // Shouldn't happen. |
171 if (g_test_server) | 205 if (g_test_server) |
172 return false; | 206 return false; |
173 | 207 |
174 base::FilePath test_data_dir( | 208 base::FilePath test_data_dir( |
175 base::android::ConvertJavaStringToUTF8(env, jtest_data_dir)); | 209 base::android::ConvertJavaStringToUTF8(env, jtest_data_dir)); |
176 base::InitAndroidTestPaths(test_data_dir); | 210 base::InitAndroidTestPaths(test_data_dir); |
177 | 211 |
178 g_test_server = new net::EmbeddedTestServer(); | 212 g_test_server = new net::EmbeddedTestServer(); |
179 g_test_server->RegisterRequestHandler( | 213 g_test_server->RegisterRequestHandler( |
180 base::Bind(&NativeTestServerRequestHandler)); | 214 base::Bind(&NativeTestServerRequestHandler)); |
215 g_test_server->RegisterDefaultHandler( | |
216 base::Bind(&net::test_server::HandlePrefixedRequest, kExabyteResponsePath, | |
217 base::Bind(&HandleExabyteRequest))); | |
181 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); | 218 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); |
182 base::FilePath test_files_root( | 219 base::FilePath test_files_root( |
183 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 220 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); |
184 | 221 |
185 // Add a third handler for paths that NativeTestServerRequestHandler does not | 222 // Add a third handler for paths that NativeTestServerRequestHandler does not |
186 // handle. | 223 // handle. |
187 g_test_server->ServeFilesFromDirectory(test_files_root); | 224 g_test_server->ServeFilesFromDirectory(test_files_root); |
188 return g_test_server->Start(); | 225 return g_test_server->Start(); |
189 } | 226 } |
190 | 227 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
252 } | 289 } |
253 | 290 |
254 ScopedJavaLocalRef<jstring> GetSdchURL(JNIEnv* env, | 291 ScopedJavaLocalRef<jstring> GetSdchURL(JNIEnv* env, |
255 const JavaParamRef<jclass>& jcaller) { | 292 const JavaParamRef<jclass>& jcaller) { |
256 DCHECK(g_test_server); | 293 DCHECK(g_test_server); |
257 std::string url(base::StringPrintf("http://%s:%d", kFakeSdchDomain, | 294 std::string url(base::StringPrintf("http://%s:%d", kFakeSdchDomain, |
258 g_test_server->port())); | 295 g_test_server->port())); |
259 return base::android::ConvertUTF8ToJavaString(env, url); | 296 return base::android::ConvertUTF8ToJavaString(env, url); |
260 } | 297 } |
261 | 298 |
299 ScopedJavaLocalRef<jstring> GetExabyteResponseURL( | |
300 JNIEnv* env, | |
301 const JavaParamRef<jclass>& jcaller) { | |
302 DCHECK(g_test_server); | |
303 GURL url = g_test_server->GetURL(kExabyteResponsePath); | |
304 return base::android::ConvertUTF8ToJavaString(env, url.spec()); | |
305 } | |
306 | |
262 ScopedJavaLocalRef<jstring> GetHostPort(JNIEnv* env, | 307 ScopedJavaLocalRef<jstring> GetHostPort(JNIEnv* env, |
263 const JavaParamRef<jclass>& jcaller) { | 308 const JavaParamRef<jclass>& jcaller) { |
264 DCHECK(g_test_server); | 309 DCHECK(g_test_server); |
265 std::string host_port = | 310 std::string host_port = |
266 net::HostPortPair::FromURL(g_test_server->base_url()).ToString(); | 311 net::HostPortPair::FromURL(g_test_server->base_url()).ToString(); |
267 return base::android::ConvertUTF8ToJavaString(env, host_port); | 312 return base::android::ConvertUTF8ToJavaString(env, host_port); |
268 } | 313 } |
269 | 314 |
270 jboolean IsDataReductionProxySupported(JNIEnv* env, | 315 jboolean IsDataReductionProxySupported(JNIEnv* env, |
271 const JavaParamRef<jclass>& jcaller) { | 316 const JavaParamRef<jclass>& jcaller) { |
272 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 317 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
273 return JNI_TRUE; | 318 return JNI_TRUE; |
274 #else | 319 #else |
275 return JNI_FALSE; | 320 return JNI_FALSE; |
276 #endif | 321 #endif |
277 } | 322 } |
278 | 323 |
279 bool RegisterNativeTestServer(JNIEnv* env) { | 324 bool RegisterNativeTestServer(JNIEnv* env) { |
280 return RegisterNativesImpl(env); | 325 return RegisterNativesImpl(env); |
281 } | 326 } |
282 | 327 |
283 } // namespace cronet | 328 } // namespace cronet |
OLD | NEW |