| 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 "native_test_server.h" | 5 #include "native_test_server.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/android/jni_android.h" | 9 #include "base/android/jni_android.h" |
| 10 #include "base/android/jni_string.h" | 10 #include "base/android/jni_string.h" |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 44 // supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server | 44 // supports Sdch encoding. E.g. /sdch/index?q=LeQxM80O will make the server |
| 45 // responds with "Get-Dictionary: /sdch/dict/LeQxM80O". | 45 // responds with "Get-Dictionary: /sdch/dict/LeQxM80O". |
| 46 const char kSdchPath[] = "/sdch/index"; | 46 const char kSdchPath[] = "/sdch/index"; |
| 47 // Path that returns encoded response if client has the right dictionary. | 47 // Path that returns encoded response if client has the right dictionary. |
| 48 const char kSdchTestPath[] = "/sdch/test"; | 48 const char kSdchTestPath[] = "/sdch/test"; |
| 49 // Path where dictionaries are stored. | 49 // Path where dictionaries are stored. |
| 50 const char kSdchDictPath[] = "/sdch/dict/"; | 50 const char kSdchDictPath[] = "/sdch/dict/"; |
| 51 | 51 |
| 52 net::test_server::EmbeddedTestServer* g_test_server = nullptr; | 52 net::test_server::EmbeddedTestServer* g_test_server = nullptr; |
| 53 | 53 |
| 54 class CustomHttpResponse : public net::test_server::HttpResponse { | 54 scoped_ptr<net::test_server::RawHttpResponse> ConstructResponseBasedOnFile( |
| 55 public: | |
| 56 CustomHttpResponse(const std::string& headers, const std::string& contents) | |
| 57 : headers_(headers), contents_(contents) {} | |
| 58 | |
| 59 std::string ToResponseString() const override { | |
| 60 return headers_ + "\r\n" + contents_; | |
| 61 } | |
| 62 | |
| 63 void AddHeader(const std::string& key_value_pair) { | |
| 64 headers_.append(base::StringPrintf("%s\r\n", key_value_pair.c_str())); | |
| 65 } | |
| 66 | |
| 67 private: | |
| 68 std::string headers_; | |
| 69 std::string contents_; | |
| 70 | |
| 71 DISALLOW_COPY_AND_ASSIGN(CustomHttpResponse); | |
| 72 }; | |
| 73 | |
| 74 scoped_ptr<CustomHttpResponse> ConstructResponseBasedOnFile( | |
| 75 const base::FilePath& file_path) { | 55 const base::FilePath& file_path) { |
| 76 std::string file_contents; | 56 std::string file_contents; |
| 77 bool read_file = base::ReadFileToString(file_path, &file_contents); | 57 bool read_file = base::ReadFileToString(file_path, &file_contents); |
| 78 DCHECK(read_file); | 58 DCHECK(read_file); |
| 79 base::FilePath headers_path( | 59 base::FilePath headers_path( |
| 80 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); | 60 file_path.AddExtension(FILE_PATH_LITERAL("mock-http-headers"))); |
| 81 std::string headers_contents; | 61 std::string headers_contents; |
| 82 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); | 62 bool read_headers = base::ReadFileToString(headers_path, &headers_contents); |
| 83 DCHECK(read_headers); | 63 DCHECK(read_headers); |
| 84 scoped_ptr<CustomHttpResponse> http_response( | 64 scoped_ptr<net::test_server::RawHttpResponse> http_response( |
| 85 new CustomHttpResponse(headers_contents, file_contents)); | 65 new net::test_server::RawHttpResponse(headers_contents, file_contents)); |
| 86 return http_response.Pass(); | 66 return http_response.Pass(); |
| 87 } | 67 } |
| 88 | 68 |
| 89 scoped_ptr<net::test_server::HttpResponse> NativeTestServerRequestHandler( | 69 scoped_ptr<net::test_server::HttpResponse> NativeTestServerRequestHandler( |
| 90 const net::test_server::HttpRequest& request) { | 70 const net::test_server::HttpRequest& request) { |
| 91 DCHECK(g_test_server); | 71 DCHECK(g_test_server); |
| 92 scoped_ptr<net::test_server::BasicHttpResponse> response( | 72 scoped_ptr<net::test_server::BasicHttpResponse> response( |
| 93 new net::test_server::BasicHttpResponse()); | 73 new net::test_server::BasicHttpResponse()); |
| 94 response->set_content_type("text/plain"); | 74 response->set_content_type("text/plain"); |
| 95 | 75 |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 138 const net::test_server::HttpRequest& request) { | 118 const net::test_server::HttpRequest& request) { |
| 139 DCHECK(g_test_server); | 119 DCHECK(g_test_server); |
| 140 base::FilePath dir_path; | 120 base::FilePath dir_path; |
| 141 bool get_data_dir = base::android::GetDataDirectory(&dir_path); | 121 bool get_data_dir = base::android::GetDataDirectory(&dir_path); |
| 142 DCHECK(get_data_dir); | 122 DCHECK(get_data_dir); |
| 143 dir_path = dir_path.Append(FILE_PATH_LITERAL("test")); | 123 dir_path = dir_path.Append(FILE_PATH_LITERAL("test")); |
| 144 | 124 |
| 145 if (base::StartsWith(request.relative_url, kSdchPath, | 125 if (base::StartsWith(request.relative_url, kSdchPath, |
| 146 base::CompareCase::SENSITIVE)) { | 126 base::CompareCase::SENSITIVE)) { |
| 147 base::FilePath file_path = dir_path.Append("sdch/index"); | 127 base::FilePath file_path = dir_path.Append("sdch/index"); |
| 148 scoped_ptr<CustomHttpResponse> response = | 128 scoped_ptr<net::test_server::RawHttpResponse> response = |
| 149 ConstructResponseBasedOnFile(file_path).Pass(); | 129 ConstructResponseBasedOnFile(file_path).Pass(); |
| 150 // Check for query params to see which dictionary to advertise. | 130 // Check for query params to see which dictionary to advertise. |
| 151 // For instance, ?q=dictionaryA will make the server advertise dictionaryA. | 131 // For instance, ?q=dictionaryA will make the server advertise dictionaryA. |
| 152 GURL url = g_test_server->GetURL(request.relative_url); | 132 GURL url = g_test_server->GetURL(request.relative_url); |
| 153 std::string dictionary; | 133 std::string dictionary; |
| 154 if (!net::GetValueForKeyInQuery(url, "q", &dictionary)) { | 134 if (!net::GetValueForKeyInQuery(url, "q", &dictionary)) { |
| 155 CHECK(false) << "dictionary is not found in query params of " | 135 CHECK(false) << "dictionary is not found in query params of " |
| 156 << request.relative_url; | 136 << request.relative_url; |
| 157 } | 137 } |
| 158 auto accept_encoding_header = request.headers.find("Accept-Encoding"); | 138 auto accept_encoding_header = request.headers.find("Accept-Encoding"); |
| (...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 333 #else | 313 #else |
| 334 return JNI_FALSE; | 314 return JNI_FALSE; |
| 335 #endif | 315 #endif |
| 336 } | 316 } |
| 337 | 317 |
| 338 bool RegisterNativeTestServer(JNIEnv* env) { | 318 bool RegisterNativeTestServer(JNIEnv* env) { |
| 339 return RegisterNativesImpl(env); | 319 return RegisterNativesImpl(env); |
| 340 } | 320 } |
| 341 | 321 |
| 342 } // namespace cronet | 322 } // namespace cronet |
| OLD | NEW |