Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(312)

Side by Side Diff: net/test/url_request/url_request_mock_http_job.cc

Issue 586143002: Initial implementation of Cronet Async API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Added ReportMockErrorFromQuery to URLRequestMockHTTPJob. Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
19 const char kMockHostname[] = "mock.http"; 22 const char kMockHostname[] = "mock.http";
20 const base::FilePath::CharType kMockHeaderFileSuffix[] = 23 const base::FilePath::CharType kMockHeaderFileSuffix[] =
21 FILE_PATH_LITERAL(".mock-http-headers"); 24 FILE_PATH_LITERAL(".mock-http-headers");
22 25
26 const char kMockErrorStepRedirect[] = "redirect";
27 const char kMockErrorStepHeaders[] = "headers";
28 const char kMockErrorStepData[] = "data";
29
23 namespace net { 30 namespace net {
24 31
25 namespace { 32 namespace {
26 33
27 class MockJobInterceptor : public net::URLRequestInterceptor { 34 class MockJobInterceptor : public net::URLRequestInterceptor {
28 public: 35 public:
29 // When |map_all_requests_to_base_path| is true, all request should return the 36 // 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| 37 // 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 38 // 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. 39 // to use as the root of the HTTP server.
(...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after
149 } 156 }
150 157
151 // Public virtual version. 158 // Public virtual version.
152 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) { 159 void URLRequestMockHTTPJob::GetResponseInfo(net::HttpResponseInfo* info) {
153 // Forward to private const version. 160 // Forward to private const version.
154 GetResponseInfoConst(info); 161 GetResponseInfoConst(info);
155 } 162 }
156 163
157 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location, 164 bool URLRequestMockHTTPJob::IsRedirectResponse(GURL* location,
158 int* http_status_code) { 165 int* http_status_code) {
166 if (ReportMockErrorFromQuery(kMockErrorStepRedirect))
167 return false;
mmenke 2014/10/16 16:39:34 Hrm...I don't think this makes any sense. For err
mef 2014/10/16 21:36:50 Done.
159 // Override the net::URLRequestFileJob implementation to invoke the default 168 // Override the net::URLRequestFileJob implementation to invoke the default
160 // one based on HttpResponseInfo. 169 // one based on HttpResponseInfo.
161 return net::URLRequestJob::IsRedirectResponse(location, http_status_code); 170 return net::URLRequestJob::IsRedirectResponse(location, http_status_code);
162 } 171 }
163 172
164 // Public virtual version. 173 // Public virtual version.
165 void URLRequestMockHTTPJob::Start() { 174 void URLRequestMockHTTPJob::Start() {
166 base::PostTaskAndReplyWithResult( 175 base::PostTaskAndReplyWithResult(
167 task_runner_.get(), 176 task_runner_.get(),
168 FROM_HERE, 177 FROM_HERE,
169 base::Bind(&DoFileIO, file_path_), 178 base::Bind(&DoFileIO, file_path_),
170 base::Bind(&URLRequestMockHTTPJob::GetRawHeaders, 179 base::Bind(&URLRequestMockHTTPJob::GetRawHeaders,
171 weak_ptr_factory_.GetWeakPtr())); 180 weak_ptr_factory_.GetWeakPtr()));
172 } 181 }
173 182
183 // Public virtual version.
184 bool URLRequestMockHTTPJob::ReadRawData(IOBuffer* buf,
185 int buf_size,
186 int* bytes_read) {
187 if (ReportMockErrorFromQuery(kMockErrorStepData))
188 return false;
189 return URLRequestFileJob::ReadRawData(buf, buf_size, bytes_read);
190 }
191
174 void URLRequestMockHTTPJob::GetRawHeaders(std::string raw_headers) { 192 void URLRequestMockHTTPJob::GetRawHeaders(std::string raw_headers) {
mmenke 2014/10/16 16:39:34 While you're here, think this should probably be r
mef 2014/10/16 21:36:50 Done.
175 // Handle CRLF line-endings. 193 // Handle CRLF line-endings.
176 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n"); 194 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\r\n", "\n");
177 // ParseRawHeaders expects \0 to end each header line. 195 // ParseRawHeaders expects \0 to end each header line.
178 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1)); 196 ReplaceSubstringsAfterOffset(&raw_headers, 0, "\n", std::string("\0", 1));
179 raw_headers_ = raw_headers; 197 raw_headers_ = raw_headers;
198 if (ReportMockErrorFromQuery(kMockErrorStepHeaders))
199 return;
mmenke 2014/10/16 16:39:34 Hrm...wonder if we should set the headers or not i
mef 2014/10/16 21:36:50 Done.
180 URLRequestFileJob::Start(); 200 URLRequestFileJob::Start();
181 } 201 }
182 202
203 bool URLRequestMockHTTPJob::ReportMockErrorFromQuery(const std::string& step) {
204 std::string value;
205 if (!GetValueForKeyInQuery(request_->url(), step, &value))
206 return false;
207
208 int net_error;
209 if (!base::StringToInt(value, &net_error))
210 return false;
211
212 if (net_error == net::ERR_IO_PENDING) {
213 DLOG(ERROR) << "Report Mock ERR_IO_PENDING for step " << step;
214 SetStatus(net::URLRequestStatus(net::URLRequestStatus::IO_PENDING, 0));
215 } else {
216 DLOG(ERROR) << "Report Mock Error " << net_error << " for step " << step;
217 NotifyDone(net::URLRequestStatus(net::URLRequestStatus::FAILED, net_error));
218 }
219 return true;
220 }
221
183 // Private const version. 222 // Private const version.
184 void URLRequestMockHTTPJob::GetResponseInfoConst( 223 void URLRequestMockHTTPJob::GetResponseInfoConst(
185 net::HttpResponseInfo* info) const { 224 net::HttpResponseInfo* info) const {
186 info->headers = new net::HttpResponseHeaders(raw_headers_); 225 info->headers = new net::HttpResponseHeaders(raw_headers_);
187 } 226 }
188 227
189 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const { 228 bool URLRequestMockHTTPJob::GetMimeType(std::string* mime_type) const {
190 net::HttpResponseInfo info; 229 net::HttpResponseInfo info;
191 GetResponseInfoConst(&info); 230 GetResponseInfoConst(&info);
192 return info.headers.get() && info.headers->GetMimeType(mime_type); 231 return info.headers.get() && info.headers->GetMimeType(mime_type);
193 } 232 }
194 233
195 int URLRequestMockHTTPJob::GetResponseCode() const { 234 int URLRequestMockHTTPJob::GetResponseCode() const {
196 net::HttpResponseInfo info; 235 net::HttpResponseInfo info;
197 GetResponseInfoConst(&info); 236 GetResponseInfoConst(&info);
198 // If we have headers, get the response code from them. 237 // If we have headers, get the response code from them.
199 if (info.headers.get()) 238 if (info.headers.get())
200 return info.headers->response_code(); 239 return info.headers->response_code();
201 return net::URLRequestJob::GetResponseCode(); 240 return net::URLRequestJob::GetResponseCode();
202 } 241 }
203 242
204 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) { 243 bool URLRequestMockHTTPJob::GetCharset(std::string* charset) {
205 net::HttpResponseInfo info; 244 net::HttpResponseInfo info;
206 GetResponseInfo(&info); 245 GetResponseInfo(&info);
207 return info.headers.get() && info.headers->GetCharset(charset); 246 return info.headers.get() && info.headers->GetCharset(charset);
208 } 247 }
209 248
210 } // namespace net 249 } // namespace net
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698