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/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
| 10 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_util.h" | 11 #include "base/strings/string_util.h" |
10 #include "base/strings/utf_string_conversions.h" | 12 #include "base/strings/utf_string_conversions.h" |
11 #include "base/task_runner_util.h" | 13 #include "base/task_runner_util.h" |
12 #include "base/threading/sequenced_worker_pool.h" | 14 #include "base/threading/sequenced_worker_pool.h" |
13 #include "base/threading/thread_restrictions.h" | 15 #include "base/threading/thread_restrictions.h" |
14 #include "net/base/filename_util.h" | 16 #include "net/base/filename_util.h" |
| 17 #include "net/base/net_errors.h" |
| 18 #include "net/base/url_util.h" |
15 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
16 #include "net/url_request/url_request_filter.h" | 20 #include "net/url_request/url_request_filter.h" |
17 #include "net/url_request/url_request_interceptor.h" | 21 #include "net/url_request/url_request_interceptor.h" |
18 | 22 |
| 23 namespace net { |
| 24 |
| 25 namespace { |
| 26 |
19 const char kMockHostname[] = "mock.http"; | 27 const char kMockHostname[] = "mock.http"; |
20 const base::FilePath::CharType kMockHeaderFileSuffix[] = | 28 const base::FilePath::CharType kMockHeaderFileSuffix[] = |
21 FILE_PATH_LITERAL(".mock-http-headers"); | 29 FILE_PATH_LITERAL(".mock-http-headers"); |
22 | 30 |
23 namespace net { | 31 // String names of failure phases matching FailurePhase enum. |
24 | 32 const char* kFailurePhase[] { |
25 namespace { | 33 "start", // START |
| 34 "readasync", // READ_ASYNC |
| 35 "readsync", // READ_SYNC |
| 36 }; |
26 | 37 |
27 class MockJobInterceptor : public net::URLRequestInterceptor { | 38 class MockJobInterceptor : public net::URLRequestInterceptor { |
28 public: | 39 public: |
29 // When |map_all_requests_to_base_path| is true, all request should return the | 40 // When |map_all_requests_to_base_path| is true, all request should return the |
30 // contents of the file at |base_path|. When |map_all_requests_to_base_path| | 41 // contents of the file at |base_path|. When |map_all_requests_to_base_path| |
31 // is false, |base_path| is the file path leading to the root of the directory | 42 // is false, |base_path| is the file path leading to the root of the directory |
32 // to use as the root of the HTTP server. | 43 // to use as the root of the HTTP server. |
33 MockJobInterceptor( | 44 MockJobInterceptor( |
34 const base::FilePath& base_path, | 45 const base::FilePath& base_path, |
35 bool map_all_requests_to_base_path, | 46 bool map_all_requests_to_base_path, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
112 std::string url = "http://"; | 123 std::string url = "http://"; |
113 url.append(kMockHostname); | 124 url.append(kMockHostname); |
114 url.append("/"); | 125 url.append("/"); |
115 std::string path_str = path.MaybeAsASCII(); | 126 std::string path_str = path.MaybeAsASCII(); |
116 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. | 127 DCHECK(!path_str.empty()); // We only expect ASCII paths in tests. |
117 url.append(path_str); | 128 url.append(path_str); |
118 return GURL(url); | 129 return GURL(url); |
119 } | 130 } |
120 | 131 |
121 // static | 132 // static |
| 133 GURL URLRequestMockHTTPJob::GetMockUrlWithFailure(const base::FilePath& path, |
| 134 FailurePhase phase, |
| 135 int net_error) { |
| 136 COMPILE_ASSERT(arraysize(kFailurePhase) == MAX_FAILURE_PHASE, |
| 137 kFailurePhase_must_match_FailurePhase_enum); |
| 138 DCHECK_GE(phase, START); |
| 139 DCHECK_LE(phase, READ_SYNC); |
| 140 std::string url(GetMockUrl(path).spec()); |
| 141 url.append("?"); |
| 142 url.append(kFailurePhase[phase]); |
| 143 url.append("="); |
| 144 url.append(base::IntToString(net_error)); |
| 145 return GURL(url); |
| 146 } |
| 147 |
| 148 // static |
122 scoped_ptr<net::URLRequestInterceptor> URLRequestMockHTTPJob::CreateInterceptor( | 149 scoped_ptr<net::URLRequestInterceptor> URLRequestMockHTTPJob::CreateInterceptor( |
123 const base::FilePath& base_path, | 150 const base::FilePath& base_path, |
124 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 151 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
125 return scoped_ptr<net::URLRequestInterceptor>( | 152 return scoped_ptr<net::URLRequestInterceptor>( |
126 new MockJobInterceptor(base_path, false, worker_pool)); | 153 new MockJobInterceptor(base_path, false, worker_pool)); |
127 } | 154 } |
128 | 155 |
129 // static | 156 // static |
130 scoped_ptr<net::URLRequestInterceptor> | 157 scoped_ptr<net::URLRequestInterceptor> |
131 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( | 158 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( |
(...skipping 24 matching lines...) Expand all Loading... |
156 | 183 |
157 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 184 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
158 int* http_status_code) { | 185 int* http_status_code) { |
159 // Override the net::URLRequestFileJob implementation to invoke the default | 186 // Override the net::URLRequestFileJob implementation to invoke the default |
160 // one based on HttpResponseInfo. | 187 // one based on HttpResponseInfo. |
161 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); | 188 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); |
162 } | 189 } |
163 | 190 |
164 // Public virtual version. | 191 // Public virtual version. |
165 void URLRequestMockHTTPJob::Start() { | 192 void URLRequestMockHTTPJob::Start() { |
| 193 if (MaybeReportErrorOnPhase(START)) |
| 194 return; |
166 base::PostTaskAndReplyWithResult( | 195 base::PostTaskAndReplyWithResult( |
167 task_runner_.get(), | 196 task_runner_.get(), |
168 FROM_HERE, | 197 FROM_HERE, |
169 base::Bind(&DoFileIO, file_path_), | 198 base::Bind(&DoFileIO, file_path_), |
170 base::Bind(&URLRequestMockHTTPJob::GetRawHeaders, | 199 base::Bind(&URLRequestMockHTTPJob::SetHeadersAndStart, |
171 weak_ptr_factory_.GetWeakPtr())); | 200 weak_ptr_factory_.GetWeakPtr())); |
172 } | 201 } |
173 | 202 |
174 void URLRequestMockHTTPJob::GetRawHeaders(std::string raw_headers) { | 203 // Public virtual version. |
| 204 bool URLRequestMockHTTPJob::ReadRawData(IOBuffer* buf, |
| 205 int buf_size, |
| 206 int* bytes_read) { |
| 207 if (MaybeReportErrorOnPhase(READ_SYNC)) |
| 208 return false; |
| 209 if (MaybeReportErrorOnPhase(READ_ASYNC)) |
| 210 return false; |
| 211 return URLRequestFileJob::ReadRawData(buf, buf_size, bytes_read); |
| 212 } |
| 213 |
| 214 void URLRequestMockHTTPJob::SetHeadersAndStart(const std::string& raw_headers) { |
| 215 if (MaybeReportErrorOnPhase(START)) |
| 216 return; |
| 217 raw_headers_ = raw_headers; |
175 // Handle CRLF line-endings. | 218 // Handle CRLF line-endings. |
176 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n"); | 219 ReplaceSubstringsAfterOffset(&raw_headers_, 0, "\r\n", "\n"); |
177 // ParseRawHeaders expects \0 to end each header line. | 220 // ParseRawHeaders expects \0 to end each header line. |
178 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 221 ReplaceSubstringsAfterOffset(&raw_headers_, 0, "\n", std::string("\0", 1)); |
179 raw_headers_ = raw_headers; | |
180 URLRequestFileJob::Start(); | 222 URLRequestFileJob::Start(); |
181 } | 223 } |
182 | 224 |
| 225 bool URLRequestMockHTTPJob::MaybeReportErrorOnPhase( |
| 226 FailurePhase current_phase) { |
| 227 DCHECK_GE(current_phase, START); |
| 228 DCHECK_LE(current_phase, READ_SYNC); |
| 229 std::string phase_key(kFailurePhase[current_phase]); |
| 230 std::string phase_error_string; |
| 231 if (!GetValueForKeyInQuery(request_->url(), phase_key, &phase_error_string)) |
| 232 return false; |
| 233 |
| 234 int net_error; |
| 235 if (!base::StringToInt(phase_error_string, &net_error)) |
| 236 return false; |
| 237 |
| 238 if (net_error != net::ERR_IO_PENDING && |
| 239 (current_phase == START || current_phase == READ_SYNC)) { |
| 240 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error)); |
| 241 return true; |
| 242 } |
| 243 |
| 244 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 245 |
| 246 if (current_phase != READ_ASYNC) |
| 247 return true; |
| 248 |
| 249 base::MessageLoopProxy::current()->PostTask( |
| 250 FROM_HERE, |
| 251 base::Bind( |
| 252 &URLRequestMockHTTPJob::NotifyDone, |
| 253 weak_ptr_factory_.GetWeakPtr(), |
| 254 net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error))); |
| 255 |
| 256 return true; |
| 257 } |
| 258 |
183 // Private const version. | 259 // Private const version. |
184 void URLRequestMockHTTPJob::GetResponseInfoConst( | 260 void URLRequestMockHTTPJob::GetResponseInfoConst( |
185 net::HttpResponseInfo* info) const { | 261 net::HttpResponseInfo* info) const { |
186 info->headers = new net::HttpResponseHeaders(raw_headers_); | 262 info->headers = new net::HttpResponseHeaders(raw_headers_); |
187 } | 263 } |
188 | 264 |
189 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { | 265 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { |
190 net::HttpResponseInfo info; | 266 net::HttpResponseInfo info; |
191 GetResponseInfoConst(&info); | 267 GetResponseInfoConst(&info); |
192 return info.headers.get() && info.headers->GetMimeType(mime_type); | 268 return info.headers.get() && info.headers->GetMimeType(mime_type); |
193 } | 269 } |
194 | 270 |
195 int URLRequestMockHTTPJob::GetResponseCode() const { | 271 int URLRequestMockHTTPJob::GetResponseCode() const { |
196 net::HttpResponseInfo info; | 272 net::HttpResponseInfo info; |
197 GetResponseInfoConst(&info); | 273 GetResponseInfoConst(&info); |
198 // If we have headers, get the response code from them. | 274 // If we have headers, get the response code from them. |
199 if (info.headers.get()) | 275 if (info.headers.get()) |
200 return info.headers->response_code(); | 276 return info.headers->response_code(); |
201 return net::URLRequestJob::GetResponseCode(); | 277 return net::URLRequestJob::GetResponseCode(); |
202 } | 278 } |
203 | 279 |
204 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 280 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
205 net::HttpResponseInfo info; | 281 net::HttpResponseInfo info; |
206 GetResponseInfo(&info); | 282 GetResponseInfo(&info); |
207 return info.headers.get() && info.headers->GetCharset(charset); | 283 return info.headers.get() && info.headers->GetCharset(charset); |
208 } | 284 } |
209 | 285 |
210 } // namespace net | 286 } // namespace net |
OLD | NEW |