| 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[]{ |
| 25 namespace { | 32 "start", // START |
| 33 "readasync", // READ_ASYNC |
| 34 "readsync", // READ_SYNC |
| 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 120 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 156 | 166 |
| 157 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 167 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
| 158 int* http_status_code) { | 168 int* http_status_code) { |
| 159 // Override the net::URLRequestFileJob implementation to invoke the default | 169 // Override the net::URLRequestFileJob implementation to invoke the default |
| 160 // one based on HttpResponseInfo. | 170 // one based on HttpResponseInfo. |
| 161 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); | 171 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); |
| 162 } | 172 } |
| 163 | 173 |
| 164 // Public virtual version. | 174 // Public virtual version. |
| 165 void URLRequestMockHTTPJob::Start() { | 175 void URLRequestMockHTTPJob::Start() { |
| 176 if (MaybeReportErrorOnPhase(START)) |
| 177 return; |
| 166 base::PostTaskAndReplyWithResult( | 178 base::PostTaskAndReplyWithResult( |
| 167 task_runner_.get(), | 179 task_runner_.get(), |
| 168 FROM_HERE, | 180 FROM_HERE, |
| 169 base::Bind(&DoFileIO, file_path_), | 181 base::Bind(&DoFileIO, file_path_), |
| 170 base::Bind(&URLRequestMockHTTPJob::GetRawHeaders, | 182 base::Bind(&URLRequestMockHTTPJob::SetHeadersAndStart, |
| 171 weak_ptr_factory_.GetWeakPtr())); | 183 weak_ptr_factory_.GetWeakPtr())); |
| 172 } | 184 } |
| 173 | 185 |
| 174 void URLRequestMockHTTPJob::GetRawHeaders(std::string raw_headers) { | 186 // Public virtual version. |
| 187 bool URLRequestMockHTTPJob::ReadRawData(IOBuffer* buf, |
| 188 int buf_size, |
| 189 int* bytes_read) { |
| 190 if (MaybeReportErrorOnPhase(READ_SYNC)) |
| 191 return false; |
| 192 if (MaybeReportErrorOnPhase(READ_ASYNC)) |
| 193 return false; |
| 194 return URLRequestFileJob::ReadRawData(buf, buf_size, bytes_read); |
| 195 } |
| 196 |
| 197 void URLRequestMockHTTPJob::SetHeadersAndStart(const std::string& raw_headers) { |
| 198 if (MaybeReportErrorOnPhase(START)) |
| 199 return; |
| 200 raw_headers_ = raw_headers; |
| 175 // Handle CRLF line-endings. | 201 // Handle CRLF line-endings. |
| 176 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n"); | 202 ReplaceSubstringsAfterOffset(&raw_headers_, 0, "\r\n", "\n"); |
| 177 // ParseRawHeaders expects \0 to end each header line. | 203 // ParseRawHeaders expects \0 to end each header line. |
| 178 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 204 ReplaceSubstringsAfterOffset(&raw_headers_, 0, "\n", std::string("\0", 1)); |
| 179 raw_headers_ = raw_headers; | |
| 180 URLRequestFileJob::Start(); | 205 URLRequestFileJob::Start(); |
| 181 } | 206 } |
| 182 | 207 |
| 208 bool URLRequestMockHTTPJob::MaybeReportErrorOnPhase(FailurePhase phase) { |
| 209 DCHECK(phase >= START && phase <= READ_SYNC); |
| 210 std::string phase_key(kFailurePhase[phase]); |
| 211 |
| 212 std::string value; |
| 213 if (!GetValueForKeyInQuery(request_->url(), phase_key, &value)) |
| 214 return false; |
| 215 |
| 216 int net_error; |
| 217 if (!base::StringToInt(value, &net_error)) |
| 218 return false; |
| 219 |
| 220 if (net_error == net::ERR_IO_PENDING) { |
| 221 DLOG(ERROR) << "Report Mock ERR_IO_PENDING for phase " << phase_key; |
| 222 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 223 } else { |
| 224 DLOG(ERROR) << "Report Mock Error " << net_error << " for phase " |
| 225 << phase_key; |
| 226 if (phase == READ_ASYNC) { |
| 227 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 228 base::MessageLoopProxy::current()->PostTask( |
| 229 FROM_HERE, |
| 230 base::Bind( |
| 231 &URLRequestMockHTTPJob::NotifyDone, |
| 232 weak_ptr_factory_.GetWeakPtr(), |
| 233 net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error))); |
| 234 } else { |
| 235 NotifyDone( |
| 236 net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error)); |
| 237 } |
| 238 } |
| 239 return true; |
| 240 } |
| 241 |
| 183 // Private const version. | 242 // Private const version. |
| 184 void URLRequestMockHTTPJob::GetResponseInfoConst( | 243 void URLRequestMockHTTPJob::GetResponseInfoConst( |
| 185 net::HttpResponseInfo* info) const { | 244 net::HttpResponseInfo* info) const { |
| 186 info->headers = new net::HttpResponseHeaders(raw_headers_); | 245 info->headers = new net::HttpResponseHeaders(raw_headers_); |
| 187 } | 246 } |
| 188 | 247 |
| 189 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { | 248 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { |
| 190 net::HttpResponseInfo info; | 249 net::HttpResponseInfo info; |
| 191 GetResponseInfoConst(&info); | 250 GetResponseInfoConst(&info); |
| 192 return info.headers.get() && info.headers->GetMimeType(mime_type); | 251 return info.headers.get() && info.headers->GetMimeType(mime_type); |
| 193 } | 252 } |
| 194 | 253 |
| 195 int URLRequestMockHTTPJob::GetResponseCode() const { | 254 int URLRequestMockHTTPJob::GetResponseCode() const { |
| 196 net::HttpResponseInfo info; | 255 net::HttpResponseInfo info; |
| 197 GetResponseInfoConst(&info); | 256 GetResponseInfoConst(&info); |
| 198 // If we have headers, get the response code from them. | 257 // If we have headers, get the response code from them. |
| 199 if (info.headers.get()) | 258 if (info.headers.get()) |
| 200 return info.headers->response_code(); | 259 return info.headers->response_code(); |
| 201 return net::URLRequestJob::GetResponseCode(); | 260 return net::URLRequestJob::GetResponseCode(); |
| 202 } | 261 } |
| 203 | 262 |
| 204 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 263 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 205 net::HttpResponseInfo info; | 264 net::HttpResponseInfo info; |
| 206 GetResponseInfo(&info); | 265 GetResponseInfo(&info); |
| 207 return info.headers.get() && info.headers->GetCharset(charset); | 266 return info.headers.get() && info.headers->GetCharset(charset); |
| 208 } | 267 } |
| 209 | 268 |
| 210 } // namespace net | 269 } // namespace net |
| OLD | NEW |