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