| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "net/test/url_request/url_request_mock_http_job.h" | 5 #include "net/test/url_request/url_request_mock_http_job.h" |
| 6 | 6 |
| 7 #include "base/files/file_util.h" | 7 #include "base/files/file_util.h" |
| 8 #include "base/macros.h" | 8 #include "base/macros.h" |
| 9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 84 return "HTTP/1.0 200 OK\n"; | 84 return "HTTP/1.0 200 OK\n"; |
| 85 } | 85 } |
| 86 | 86 |
| 87 std::string raw_headers; | 87 std::string raw_headers; |
| 88 base::ReadFileToString(header_file, &raw_headers); | 88 base::ReadFileToString(header_file, &raw_headers); |
| 89 return raw_headers; | 89 return raw_headers; |
| 90 } | 90 } |
| 91 | 91 |
| 92 // For a given file |path| and |scheme|, return the URL served by the | 92 // For a given file |path| and |scheme|, return the URL served by the |
| 93 // URlRequestMockHTTPJob. | 93 // URlRequestMockHTTPJob. |
| 94 GURL GetMockUrlForScheme(const base::FilePath& path, | 94 GURL GetMockUrlForScheme(const std::string& path, const std::string& scheme) { |
| 95 const std::string& scheme) { | 95 return GURL(scheme + "://" + kMockHostname + "/" + path); |
| 96 std::string url = scheme + "://" + kMockHostname + "/"; | |
| 97 std::string path_str = path.MaybeAsASCII(); | |
| 98 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | |
| 99 url.append(path_str); | |
| 100 return GURL(url); | |
| 101 } | 96 } |
| 102 | 97 |
| 103 } // namespace | 98 } // namespace |
| 104 | 99 |
| 105 // static | 100 // static |
| 106 void URLRequestMockHTTPJob::AddUrlHandlers( | 101 void URLRequestMockHTTPJob::AddUrlHandlers( |
| 107 const base::FilePath& base_path, | 102 const base::FilePath& base_path, |
| 108 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 103 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
| 109 // Add kMockHostname to URLRequestFilter, for both HTTP and HTTPS. | 104 // Add kMockHostname to URLRequestFilter, for both HTTP and HTTPS. |
| 110 URLRequestFilter* filter = URLRequestFilter::GetInstance(); | 105 URLRequestFilter* filter = URLRequestFilter::GetInstance(); |
| 111 filter->AddHostnameInterceptor( | 106 filter->AddHostnameInterceptor( |
| 112 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); | 107 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); |
| 113 filter->AddHostnameInterceptor("https", kMockHostname, | 108 filter->AddHostnameInterceptor("https", kMockHostname, |
| 114 CreateInterceptor(base_path, worker_pool)); | 109 CreateInterceptor(base_path, worker_pool)); |
| 115 } | 110 } |
| 116 | 111 |
| 117 // static | 112 // static |
| 113 GURL URLRequestMockHTTPJob::GetMockUrl(const std::string& path) { |
| 114 return GetMockUrlForScheme(path, "http"); |
| 115 } |
| 116 |
| 117 // static |
| 118 GURL URLRequestMockHTTPJob::GetMockHttpsUrl(const std::string& path) { |
| 119 return GetMockUrlForScheme(path, "https"); |
| 120 } |
| 121 |
| 122 // static |
| 118 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { | 123 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { |
| 119 return GetMockUrlForScheme(path, "http"); | 124 std::string path_str = path.MaybeAsASCII(); |
| 125 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
| 126 return GetMockUrlForScheme(path_str, "http"); |
| 120 } | 127 } |
| 121 | 128 |
| 122 // static | 129 // static |
| 123 GURL URLRequestMockHTTPJob::GetMockHttpsUrl(const base::FilePath& path) { | 130 GURL URLRequestMockHTTPJob::GetMockHttpsUrl(const base::FilePath& path) { |
| 124 return GetMockUrlForScheme(path, "https"); | 131 std::string path_str = path.MaybeAsASCII(); |
| 132 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
| 133 return GetMockUrlForScheme(path_str, "https"); |
| 125 } | 134 } |
| 126 | 135 |
| 127 // static | 136 // static |
| 128 scoped_ptr<URLRequestInterceptor> URLRequestMockHTTPJob::CreateInterceptor( | 137 scoped_ptr<URLRequestInterceptor> URLRequestMockHTTPJob::CreateInterceptor( |
| 129 const base::FilePath& base_path, | 138 const base::FilePath& base_path, |
| 130 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 139 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
| 131 return scoped_ptr<URLRequestInterceptor>( | 140 return scoped_ptr<URLRequestInterceptor>( |
| 132 new MockJobInterceptor(base_path, false, worker_pool)); | 141 new MockJobInterceptor(base_path, false, worker_pool)); |
| 133 } | 142 } |
| 134 | 143 |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 return URLRequestJob::GetResponseCode(); | 215 return URLRequestJob::GetResponseCode(); |
| 207 } | 216 } |
| 208 | 217 |
| 209 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 218 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 210 HttpResponseInfo info; | 219 HttpResponseInfo info; |
| 211 GetResponseInfo(&info); | 220 GetResponseInfo(&info); |
| 212 return info.headers.get() && info.headers->GetCharset(charset); | 221 return info.headers.get() && info.headers->GetCharset(charset); |
| 213 } | 222 } |
| 214 | 223 |
| 215 } // namespace net | 224 } // namespace net |
| OLD | NEW |