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