Chromium Code Reviews| 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 81 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 92 } | 92 } |
| 93 | 93 |
| 94 std::string raw_headers; | 94 std::string raw_headers; |
| 95 base::ReadFileToString(header_file, &raw_headers); | 95 base::ReadFileToString(header_file, &raw_headers); |
| 96 return raw_headers; | 96 return raw_headers; |
| 97 } | 97 } |
| 98 | 98 |
| 99 } // namespace | 99 } // namespace |
| 100 | 100 |
| 101 // static | 101 // static |
| 102 void URLRequestMockHTTPJob::AddUrlHandler( | 102 void URLRequestMockHTTPJob::AddUrlHandlers( |
| 103 const base::FilePath& base_path, | 103 const base::FilePath& base_path, |
| 104 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 104 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
| 105 // Add kMockHostname to net::URLRequestFilter. | 105 // Add kMockHostname to net::URLRequestFilter, for both HTTP and HTTPS. |
| 106 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 106 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 107 filter->AddHostnameInterceptor( | 107 filter->AddHostnameInterceptor( |
| 108 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); | 108 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); |
| 109 filter->AddHostnameInterceptor("https", kMockHostname, | |
| 110 CreateInterceptor(base_path, worker_pool)); | |
| 109 } | 111 } |
| 110 | 112 |
| 111 // static | 113 // static |
| 112 void URLRequestMockHTTPJob::AddHostnameToFileHandler( | 114 void URLRequestMockHTTPJob::AddHostnameToFileHandler( |
| 113 const std::string& hostname, | 115 const std::string& hostname, |
| 114 const base::FilePath& file, | 116 const base::FilePath& file, |
| 115 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 117 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
| 116 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 118 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 117 filter->AddHostnameInterceptor( | 119 filter->AddHostnameInterceptor( |
| 118 "http", hostname, CreateInterceptorForSingleFile(file, worker_pool)); | 120 "http", hostname, CreateInterceptorForSingleFile(file, worker_pool)); |
| 119 } | 121 } |
| 120 | 122 |
| 121 // static | 123 // static |
| 122 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { | 124 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { |
| 123 std::string url = "http://"; | 125 return GetMockUrlForScheme(path, "http"); |
| 124 url.append(kMockHostname); | |
| 125 url.append("/"); | |
| 126 std::string path_str = path.MaybeAsASCII(); | |
| 127 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | |
| 128 url.append(path_str); | |
| 129 return GURL(url); | |
| 130 } | 126 } |
| 131 | 127 |
| 132 // static | 128 // static |
| 129 GURL URLRequestMockHTTPJob::GetMockHttpsUrl(const base::FilePath& path) { | |
| 130 return GetMockUrlForScheme(path, "https"); | |
| 131 } | |
| 132 | |
| 133 // static | |
| 133 GURL URLRequestMockHTTPJob::GetMockUrlWithFailure(const base::FilePath& path, | 134 GURL URLRequestMockHTTPJob::GetMockUrlWithFailure(const base::FilePath& path, |
| 134 FailurePhase phase, | 135 FailurePhase phase, |
| 135 int net_error) { | 136 int net_error) { |
| 136 COMPILE_ASSERT(arraysize(kFailurePhase) == MAX_FAILURE_PHASE, | 137 COMPILE_ASSERT(arraysize(kFailurePhase) == MAX_FAILURE_PHASE, |
| 137 kFailurePhase_must_match_FailurePhase_enum); | 138 kFailurePhase_must_match_FailurePhase_enum); |
| 138 DCHECK_GE(phase, START); | 139 DCHECK_GE(phase, START); |
| 139 DCHECK_LE(phase, READ_SYNC); | 140 DCHECK_LE(phase, READ_SYNC); |
| 140 std::string url(GetMockUrl(path).spec()); | 141 std::string url(GetMockUrl(path).spec()); |
| 141 url.append("?"); | 142 url.append("?"); |
| 142 url.append(kFailurePhase[phase]); | 143 url.append(kFailurePhase[phase]); |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 168 const base::FilePath& file_path, | 169 const base::FilePath& file_path, |
| 169 const scoped_refptr<base::TaskRunner>& task_runner) | 170 const scoped_refptr<base::TaskRunner>& task_runner) |
| 170 : net::URLRequestFileJob(request, network_delegate, file_path, task_runner), | 171 : net::URLRequestFileJob(request, network_delegate, file_path, task_runner), |
| 171 task_runner_(task_runner), | 172 task_runner_(task_runner), |
| 172 weak_ptr_factory_(this) { | 173 weak_ptr_factory_(this) { |
| 173 } | 174 } |
| 174 | 175 |
| 175 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { | 176 URLRequestMockHTTPJob::~URLRequestMockHTTPJob() { |
| 176 } | 177 } |
| 177 | 178 |
| 179 // static | |
| 180 GURL URLRequestMockHTTPJob::GetMockUrlForScheme(const base::FilePath& path, | |
|
mmenke
2015/01/15 22:35:30
nit: Instead of making this a static member of UR
Mathieu
2015/01/22 19:21:59
Done.
| |
| 181 const std::string& scheme) { | |
| 182 std::string url = scheme + "://"; | |
| 183 url.append(kMockHostname); | |
| 184 url.append("/"); | |
| 185 std::string path_str = path.MaybeAsASCII(); | |
| 186 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | |
| 187 url.append(path_str); | |
| 188 return GURL(url); | |
| 189 } | |
| 190 | |
| 178 // Public virtual version. | 191 // Public virtual version. |
| 179 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { | 192 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { |
| 180 // Forward to private const version. | 193 // Forward to private const version. |
| 181 GetResponseInfoConst(info); | 194 GetResponseInfoConst(info); |
| 182 } | 195 } |
| 183 | 196 |
| 184 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 197 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
| 185 int* http_status_code) { | 198 int* http_status_code) { |
| 186 // Override the net::URLRequestFileJob implementation to invoke the default | 199 // Override the net::URLRequestFileJob implementation to invoke the default |
| 187 // one based on HttpResponseInfo. | 200 // one based on HttpResponseInfo. |
| (...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 277 return net::URLRequestJob::GetResponseCode(); | 290 return net::URLRequestJob::GetResponseCode(); |
| 278 } | 291 } |
| 279 | 292 |
| 280 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 293 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 281 net::HttpResponseInfo info; | 294 net::HttpResponseInfo info; |
| 282 GetResponseInfo(&info); | 295 GetResponseInfo(&info); |
| 283 return info.headers.get() && info.headers->GetCharset(charset); | 296 return info.headers.get() && info.headers->GetCharset(charset); |
| 284 } | 297 } |
| 285 | 298 |
| 286 } // namespace net | 299 } // namespace net |
| OLD | NEW |