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 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
89 if (!base::PathExists(header_file)) { | 89 if (!base::PathExists(header_file)) { |
90 // If there is no mock-http-headers file, fake a 200 OK. | 90 // If there is no mock-http-headers file, fake a 200 OK. |
91 return "HTTP/1.0 200 OK\n"; | 91 return "HTTP/1.0 200 OK\n"; |
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 // For a given file |path| and |scheme|, return the URL served by the |
| 100 // URlRequestMockHTTPJob. |
| 101 GURL GetMockUrlForScheme(const base::FilePath& path, |
| 102 const std::string& scheme) { |
| 103 std::string url = scheme + "://" + kMockHostname + "/"; |
| 104 std::string path_str = path.MaybeAsASCII(); |
| 105 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
| 106 url.append(path_str); |
| 107 return GURL(url); |
| 108 } |
| 109 |
99 } // namespace | 110 } // namespace |
100 | 111 |
101 // static | 112 // static |
102 void URLRequestMockHTTPJob::AddUrlHandler( | 113 void URLRequestMockHTTPJob::AddUrlHandlers( |
103 const base::FilePath& base_path, | 114 const base::FilePath& base_path, |
104 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 115 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
105 // Add kMockHostname to net::URLRequestFilter. | 116 // Add kMockHostname to net::URLRequestFilter, for both HTTP and HTTPS. |
106 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 117 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
107 filter->AddHostnameInterceptor( | 118 filter->AddHostnameInterceptor( |
108 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); | 119 "http", kMockHostname, CreateInterceptor(base_path, worker_pool)); |
| 120 filter->AddHostnameInterceptor("https", kMockHostname, |
| 121 CreateInterceptor(base_path, worker_pool)); |
109 } | 122 } |
110 | 123 |
111 // static | 124 // static |
112 void URLRequestMockHTTPJob::AddHostnameToFileHandler( | 125 void URLRequestMockHTTPJob::AddHostnameToFileHandler( |
113 const std::string& hostname, | 126 const std::string& hostname, |
114 const base::FilePath& file, | 127 const base::FilePath& file, |
115 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 128 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
116 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 129 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
117 filter->AddHostnameInterceptor( | 130 filter->AddHostnameInterceptor( |
118 "http", hostname, CreateInterceptorForSingleFile(file, worker_pool)); | 131 "http", hostname, CreateInterceptorForSingleFile(file, worker_pool)); |
119 } | 132 } |
120 | 133 |
121 // static | 134 // static |
122 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { | 135 GURL URLRequestMockHTTPJob::GetMockUrl(const base::FilePath& path) { |
123 std::string url = "http://"; | 136 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 } | 137 } |
131 | 138 |
132 // static | 139 // static |
| 140 GURL URLRequestMockHTTPJob::GetMockHttpsUrl(const base::FilePath& path) { |
| 141 return GetMockUrlForScheme(path, "https"); |
| 142 } |
| 143 |
| 144 // static |
133 GURL URLRequestMockHTTPJob::GetMockUrlWithFailure(const base::FilePath& path, | 145 GURL URLRequestMockHTTPJob::GetMockUrlWithFailure(const base::FilePath& path, |
134 FailurePhase phase, | 146 FailurePhase phase, |
135 int net_error) { | 147 int net_error) { |
136 static_assert(arraysize(kFailurePhase) == MAX_FAILURE_PHASE, | 148 static_assert(arraysize(kFailurePhase) == MAX_FAILURE_PHASE, |
137 "kFailurePhase must match FailurePhase enum"); | 149 "kFailurePhase must match FailurePhase enum"); |
138 DCHECK_GE(phase, START); | 150 DCHECK_GE(phase, START); |
139 DCHECK_LE(phase, READ_SYNC); | 151 DCHECK_LE(phase, READ_SYNC); |
140 std::string url(GetMockUrl(path).spec()); | 152 std::string url(GetMockUrl(path).spec()); |
141 url.append("?"); | 153 url.append("?"); |
142 url.append(kFailurePhase[phase]); | 154 url.append(kFailurePhase[phase]); |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
277 return net::URLRequestJob::GetResponseCode(); | 289 return net::URLRequestJob::GetResponseCode(); |
278 } | 290 } |
279 | 291 |
280 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 292 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
281 net::HttpResponseInfo info; | 293 net::HttpResponseInfo info; |
282 GetResponseInfo(&info); | 294 GetResponseInfo(&info); |
283 return info.headers.get() && info.headers->GetCharset(charset); | 295 return info.headers.get() && info.headers->GetCharset(charset); |
284 } | 296 } |
285 | 297 |
286 } // namespace net | 298 } // namespace net |
OLD | NEW |