| 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 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) { |
| 135 DCHECK_GE(phase, START); |
| 136 DCHECK_LE(phase, READ_SYNC); |
| 137 std::string url(GetMockUrl(path).spec()); |
| 138 url.append("?"); |
| 139 url.append(kFailurePhase[phase]); |
| 140 url.append("="); |
| 141 url.append(base::IntToString(net_error)); |
| 142 return GURL(url); |
| 143 } |
| 144 |
| 145 // static |
| 122 scoped_ptr<net::URLRequestInterceptor> URLRequestMockHTTPJob::CreateInterceptor( | 146 scoped_ptr<net::URLRequestInterceptor> URLRequestMockHTTPJob::CreateInterceptor( |
| 123 const base::FilePath& base_path, | 147 const base::FilePath& base_path, |
| 124 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { | 148 const scoped_refptr<base::SequencedWorkerPool>& worker_pool) { |
| 125 return scoped_ptr<net::URLRequestInterceptor>( | 149 return scoped_ptr<net::URLRequestInterceptor>( |
| 126 new MockJobInterceptor(base_path, false, worker_pool)); | 150 new MockJobInterceptor(base_path, false, worker_pool)); |
| 127 } | 151 } |
| 128 | 152 |
| 129 // static | 153 // static |
| 130 scoped_ptr<net::URLRequestInterceptor> | 154 scoped_ptr<net::URLRequestInterceptor> |
| 131 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( | 155 URLRequestMockHTTPJob::CreateInterceptorForSingleFile( |
| (...skipping 24 matching lines...) Expand all Loading... |
| 156 | 180 |
| 157 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, | 181 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, |
| 158 int* http_status_code) { | 182 int* http_status_code) { |
| 159 // Override the net::URLRequestFileJob implementation to invoke the default | 183 // Override the net::URLRequestFileJob implementation to invoke the default |
| 160 // one based on HttpResponseInfo. | 184 // one based on HttpResponseInfo. |
| 161 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); | 185 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); |
| 162 } | 186 } |
| 163 | 187 |
| 164 // Public virtual version. | 188 // Public virtual version. |
| 165 void URLRequestMockHTTPJob::Start() { | 189 void URLRequestMockHTTPJob::Start() { |
| 190 if (MaybeReportErrorOnPhase(START)) |
| 191 return; |
| 166 base::PostTaskAndReplyWithResult( | 192 base::PostTaskAndReplyWithResult( |
| 167 task_runner_.get(), | 193 task_runner_.get(), |
| 168 FROM_HERE, | 194 FROM_HERE, |
| 169 base::Bind(&DoFileIO, file_path_), | 195 base::Bind(&DoFileIO, file_path_), |
| 170 base::Bind(&URLRequestMockHTTPJob::GetRawHeaders, | 196 base::Bind(&URLRequestMockHTTPJob::SetHeadersAndStart, |
| 171 weak_ptr_factory_.GetWeakPtr())); | 197 weak_ptr_factory_.GetWeakPtr())); |
| 172 } | 198 } |
| 173 | 199 |
| 174 void URLRequestMockHTTPJob::GetRawHeaders(std::string raw_headers) { | 200 // Public virtual version. |
| 201 bool URLRequestMockHTTPJob::ReadRawData(IOBuffer* buf, |
| 202 int buf_size, |
| 203 int* bytes_read) { |
| 204 if (MaybeReportErrorOnPhase(READ_SYNC)) |
| 205 return false; |
| 206 if (MaybeReportErrorOnPhase(READ_ASYNC)) |
| 207 return false; |
| 208 return URLRequestFileJob::ReadRawData(buf, buf_size, bytes_read); |
| 209 } |
| 210 |
| 211 void URLRequestMockHTTPJob::SetHeadersAndStart(const std::string& raw_headers) { |
| 212 if (MaybeReportErrorOnPhase(START)) |
| 213 return; |
| 214 raw_headers_ = raw_headers; |
| 175 // Handle CRLF line-endings. | 215 // Handle CRLF line-endings. |
| 176 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n"); | 216 ReplaceSubstringsAfterOffset(&raw_headers_, 0, "\r\n", "\n"); |
| 177 // ParseRawHeaders expects \0 to end each header line. | 217 // ParseRawHeaders expects \0 to end each header line. |
| 178 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); | 218 ReplaceSubstringsAfterOffset(&raw_headers_, 0, "\n", std::string("\0", 1)); |
| 179 raw_headers_ = raw_headers; | |
| 180 URLRequestFileJob::Start(); | 219 URLRequestFileJob::Start(); |
| 181 } | 220 } |
| 182 | 221 |
| 222 bool URLRequestMockHTTPJob::MaybeReportErrorOnPhase( |
| 223 FailurePhase current_phase) { |
| 224 DCHECK_GE(current_phase, START); |
| 225 DCHECK_LE(current_phase, READ_SYNC); |
| 226 std::string phase_key(kFailurePhase[current_phase]); |
| 227 |
| 228 std::string value; |
| 229 if (!GetValueForKeyInQuery(request_->url(), phase_key, &value)) |
| 230 return false; |
| 231 |
| 232 int net_error; |
| 233 if (!base::StringToInt(value, &net_error)) |
| 234 return false; |
| 235 |
| 236 if (net_error != net::ERR_IO_PENDING && |
| 237 (current_phase == START || current_phase == READ_SYNC)) { |
| 238 DLOG(ERROR) << "Report Mock Error " << net_error << " for phase " |
| 239 << phase_key; |
| 240 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error)); |
| 241 return true; |
| 242 } |
| 243 |
| 244 DLOG(ERROR) << "Report Mock ERR_IO_PENDING for phase " << phase_key; |
| 245 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0)); |
| 246 |
| 247 if (current_phase != READ_ASYNC) |
| 248 return true; |
| 249 |
| 250 DLOG(ERROR) << "Report Async Mock Error " << net_error << " for phase " |
| 251 << phase_key; |
| 252 base::MessageLoopProxy::current()->PostTask( |
| 253 FROM_HERE, |
| 254 base::Bind( |
| 255 &URLRequestMockHTTPJob::NotifyDone, |
| 256 weak_ptr_factory_.GetWeakPtr(), |
| 257 net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error))); |
| 258 |
| 259 return true; |
| 260 } |
| 261 |
| 183 // Private const version. | 262 // Private const version. |
| 184 void URLRequestMockHTTPJob::GetResponseInfoConst( | 263 void URLRequestMockHTTPJob::GetResponseInfoConst( |
| 185 net::HttpResponseInfo* info) const { | 264 net::HttpResponseInfo* info) const { |
| 186 info->headers = new net::HttpResponseHeaders(raw_headers_); | 265 info->headers = new net::HttpResponseHeaders(raw_headers_); |
| 187 } | 266 } |
| 188 | 267 |
| 189 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { | 268 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { |
| 190 net::HttpResponseInfo info; | 269 net::HttpResponseInfo info; |
| 191 GetResponseInfoConst(&info); | 270 GetResponseInfoConst(&info); |
| 192 return info.headers.get() && info.headers->GetMimeType(mime_type); | 271 return info.headers.get() && info.headers->GetMimeType(mime_type); |
| 193 } | 272 } |
| 194 | 273 |
| 195 int URLRequestMockHTTPJob::GetResponseCode() const { | 274 int URLRequestMockHTTPJob::GetResponseCode() const { |
| 196 net::HttpResponseInfo info; | 275 net::HttpResponseInfo info; |
| 197 GetResponseInfoConst(&info); | 276 GetResponseInfoConst(&info); |
| 198 // If we have headers, get the response code from them. | 277 // If we have headers, get the response code from them. |
| 199 if (info.headers.get()) | 278 if (info.headers.get()) |
| 200 return info.headers->response_code(); | 279 return info.headers->response_code(); |
| 201 return net::URLRequestJob::GetResponseCode(); | 280 return net::URLRequestJob::GetResponseCode(); |
| 202 } | 281 } |
| 203 | 282 |
| 204 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { | 283 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { |
| 205 net::HttpResponseInfo info; | 284 net::HttpResponseInfo info; |
| 206 GetResponseInfo(&info); | 285 GetResponseInfo(&info); |
| 207 return info.headers.get() && info.headers->GetCharset(charset); | 286 return info.headers.get() && info.headers->GetCharset(charset); |
| 208 } | 287 } |
| 209 | 288 |
| 210 } // namespace net | 289 } // namespace net |
| OLD | NEW |