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. It can go further to |
| 72 // limit the response to exactly an exabyte, but it shouldn't be necessary |
| 73 // for the purpose of testing. |
| 74 static void SendExabyte(const net::test_server::SendBytesCallback& send) { |
| 75 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 76 FROM_HERE, base::Bind(send, "echo", |
| 77 base::Bind(&ExabyteResponse::SendExabyte, send))); |
| 78 } |
| 79 |
| 80 DISALLOW_COPY_AND_ASSIGN(ExabyteResponse); |
| 81 }; |
| 82 |
52 std::unique_ptr<net::test_server::RawHttpResponse> ConstructResponseBasedOnFile( | 83 std::unique_ptr<net::test_server::RawHttpResponse> ConstructResponseBasedOnFile( |
53 const base::FilePath& file_path) { | 84 const base::FilePath& file_path) { |
54 std::string file_contents; | 85 std::string file_contents; |
55 bool read_file = base::ReadFileToString(file_path, &file_contents); | 86 bool read_file = base::ReadFileToString(file_path, &file_contents); |
56 DCHECK(read_file); | 87 DCHECK(read_file); |
57 base::FilePath headers_path( | 88 base::FilePath headers_path( |
58 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); | 89 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
59 std::string headers_contents; | 90 std::string headers_contents; |
60 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); | 91 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); |
61 DCHECK(read_headers); | 92 DCHECK(read_headers); |
(...skipping 92 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 new net::test_server::BasicHttpResponse()); | 185 new net::test_server::BasicHttpResponse()); |
155 response->set_content_type("text/plain"); | 186 response->set_content_type("text/plain"); |
156 response->set_content("Sdch is not used.\n"); | 187 response->set_content("Sdch is not used.\n"); |
157 return std::move(response); | 188 return std::move(response); |
158 } | 189 } |
159 | 190 |
160 // Unhandled requests result in the Embedded test server sending a 404. | 191 // Unhandled requests result in the Embedded test server sending a 404. |
161 return std::unique_ptr<net::test_server::BasicHttpResponse>(); | 192 return std::unique_ptr<net::test_server::BasicHttpResponse>(); |
162 } | 193 } |
163 | 194 |
| 195 std::unique_ptr<net::test_server::HttpResponse> HandleExabyteRequest( |
| 196 const net::test_server::HttpRequest& request) { |
| 197 return base::WrapUnique(new ExabyteResponse); |
| 198 } |
| 199 |
164 } // namespace | 200 } // namespace |
165 | 201 |
166 jboolean StartNativeTestServer(JNIEnv* env, | 202 jboolean StartNativeTestServer(JNIEnv* env, |
167 const JavaParamRef<jclass>& jcaller, | 203 const JavaParamRef<jclass>& jcaller, |
168 const JavaParamRef<jstring>& jtest_files_root, | 204 const JavaParamRef<jstring>& jtest_files_root, |
169 const JavaParamRef<jstring>& jtest_data_dir) { | 205 const JavaParamRef<jstring>& jtest_data_dir) { |
170 // Shouldn't happen. | 206 // Shouldn't happen. |
171 if (g_test_server) | 207 if (g_test_server) |
172 return false; | 208 return false; |
173 | 209 |
174 base::FilePath test_data_dir( | 210 base::FilePath test_data_dir( |
175 base::android::ConvertJavaStringToUTF8(env, jtest_data_dir)); | 211 base::android::ConvertJavaStringToUTF8(env, jtest_data_dir)); |
176 base::InitAndroidTestPaths(test_data_dir); | 212 base::InitAndroidTestPaths(test_data_dir); |
177 | 213 |
178 g_test_server = new net::EmbeddedTestServer(); | 214 g_test_server = new net::EmbeddedTestServer(); |
179 g_test_server->RegisterRequestHandler( | 215 g_test_server->RegisterRequestHandler( |
180 base::Bind(&NativeTestServerRequestHandler)); | 216 base::Bind(&NativeTestServerRequestHandler)); |
| 217 g_test_server->RegisterDefaultHandler( |
| 218 base::Bind(&net::test_server::HandlePrefixedRequest, kExabyteResponsePath, |
| 219 base::Bind(&HandleExabyteRequest))); |
181 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); | 220 g_test_server->RegisterRequestHandler(base::Bind(&SdchRequestHandler)); |
182 base::FilePath test_files_root( | 221 base::FilePath test_files_root( |
183 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); | 222 base::android::ConvertJavaStringToUTF8(env, jtest_files_root)); |
184 | 223 |
185 // Add a third handler for paths that NativeTestServerRequestHandler does not | 224 // Add a third handler for paths that NativeTestServerRequestHandler does not |
186 // handle. | 225 // handle. |
187 g_test_server->ServeFilesFromDirectory(test_files_root); | 226 g_test_server->ServeFilesFromDirectory(test_files_root); |
188 return g_test_server->Start(); | 227 return g_test_server->Start(); |
189 } | 228 } |
190 | 229 |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 } | 291 } |
253 | 292 |
254 ScopedJavaLocalRef<jstring> GetSdchURL(JNIEnv* env, | 293 ScopedJavaLocalRef<jstring> GetSdchURL(JNIEnv* env, |
255 const JavaParamRef<jclass>& jcaller) { | 294 const JavaParamRef<jclass>& jcaller) { |
256 DCHECK(g_test_server); | 295 DCHECK(g_test_server); |
257 std::string url(base::StringPrintf("http://%s:%d", kFakeSdchDomain, | 296 std::string url(base::StringPrintf("http://%s:%d", kFakeSdchDomain, |
258 g_test_server->port())); | 297 g_test_server->port())); |
259 return base::android::ConvertUTF8ToJavaString(env, url); | 298 return base::android::ConvertUTF8ToJavaString(env, url); |
260 } | 299 } |
261 | 300 |
| 301 ScopedJavaLocalRef<jstring> GetExabyteResponseURL( |
| 302 JNIEnv* env, |
| 303 const JavaParamRef<jclass>& jcaller) { |
| 304 DCHECK(g_test_server); |
| 305 GURL url = g_test_server->GetURL(kExabyteResponsePath); |
| 306 return base::android::ConvertUTF8ToJavaString(env, url.spec()); |
| 307 } |
| 308 |
262 ScopedJavaLocalRef<jstring> GetHostPort(JNIEnv* env, | 309 ScopedJavaLocalRef<jstring> GetHostPort(JNIEnv* env, |
263 const JavaParamRef<jclass>& jcaller) { | 310 const JavaParamRef<jclass>& jcaller) { |
264 DCHECK(g_test_server); | 311 DCHECK(g_test_server); |
265 std::string host_port = | 312 std::string host_port = |
266 net::HostPortPair::FromURL(g_test_server->base_url()).ToString(); | 313 net::HostPortPair::FromURL(g_test_server->base_url()).ToString(); |
267 return base::android::ConvertUTF8ToJavaString(env, host_port); | 314 return base::android::ConvertUTF8ToJavaString(env, host_port); |
268 } | 315 } |
269 | 316 |
270 jboolean IsDataReductionProxySupported(JNIEnv* env, | 317 jboolean IsDataReductionProxySupported(JNIEnv* env, |
271 const JavaParamRef<jclass>& jcaller) { | 318 const JavaParamRef<jclass>& jcaller) { |
272 #if defined(DATA_REDUCTION_PROXY_SUPPORT) | 319 #if defined(DATA_REDUCTION_PROXY_SUPPORT) |
273 return JNI_TRUE; | 320 return JNI_TRUE; |
274 #else | 321 #else |
275 return JNI_FALSE; | 322 return JNI_FALSE; |
276 #endif | 323 #endif |
277 } | 324 } |
278 | 325 |
279 bool RegisterNativeTestServer(JNIEnv* env) { | 326 bool RegisterNativeTestServer(JNIEnv* env) { |
280 return RegisterNativesImpl(env); | 327 return RegisterNativesImpl(env); |
281 } | 328 } |
282 | 329 |
283 } // namespace cronet | 330 } // namespace cronet |
OLD | NEW |