Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / net / test / url_request / url_request_failed_job.cc
blobe4ac6a675d0f459b96fa4ba1538def7372968be3
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
3 // found in the LICENSE file.
5 #include "net/test/url_request/url_request_failed_job.h"
7 #include "base/bind.h"
8 #include "base/location.h"
9 #include "base/logging.h"
10 #include "base/macros.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/strings/string_number_conversions.h"
13 #include "base/thread_task_runner_handle.h"
14 #include "net/base/net_errors.h"
15 #include "net/base/url_util.h"
16 #include "net/http/http_response_headers.h"
17 #include "net/url_request/url_request.h"
18 #include "net/url_request/url_request_filter.h"
19 #include "net/url_request/url_request_interceptor.h"
21 namespace net {
23 namespace {
25 const char kMockHostname[] = "mock.failed.request";
27 // String names of failure phases matching FailurePhase enum.
28 const char* kFailurePhase[]{
29 "start", // START
30 "readsync", // READ_SYNC
31 "readasync", // READ_ASYNC
34 static_assert(arraysize(kFailurePhase) ==
35 URLRequestFailedJob::FailurePhase::MAX_FAILURE_PHASE,
36 "kFailurePhase must match FailurePhase enum");
38 class MockJobInterceptor : public URLRequestInterceptor {
39 public:
40 MockJobInterceptor() {}
41 ~MockJobInterceptor() override {}
43 // URLRequestJobFactory::ProtocolHandler implementation:
44 URLRequestJob* MaybeInterceptRequest(
45 URLRequest* request,
46 NetworkDelegate* network_delegate) const override {
47 int net_error = OK;
48 URLRequestFailedJob::FailurePhase phase =
49 URLRequestFailedJob::FailurePhase::MAX_FAILURE_PHASE;
50 for (size_t i = 0; i < arraysize(kFailurePhase); i++) {
51 std::string phase_error_string;
52 if (GetValueForKeyInQuery(request->url(), kFailurePhase[i],
53 &phase_error_string)) {
54 if (base::StringToInt(phase_error_string, &net_error)) {
55 phase = static_cast<URLRequestFailedJob::FailurePhase>(i);
56 break;
60 return new URLRequestFailedJob(request, network_delegate, phase, net_error);
63 private:
64 DISALLOW_COPY_AND_ASSIGN(MockJobInterceptor);
67 GURL GetMockUrl(const std::string& scheme,
68 const std::string& hostname,
69 URLRequestFailedJob::FailurePhase phase,
70 int net_error) {
71 CHECK_GE(phase, URLRequestFailedJob::FailurePhase::START);
72 CHECK_LE(phase, URLRequestFailedJob::FailurePhase::READ_ASYNC);
73 CHECK_LT(net_error, OK);
74 return GURL(scheme + "://" + hostname + "/error?" + kFailurePhase[phase] +
75 "=" + base::IntToString(net_error));
78 } // namespace
80 URLRequestFailedJob::URLRequestFailedJob(URLRequest* request,
81 NetworkDelegate* network_delegate,
82 FailurePhase phase,
83 int net_error)
84 : URLRequestJob(request, network_delegate),
85 phase_(phase),
86 net_error_(net_error),
87 weak_factory_(this) {
88 CHECK_GE(phase, URLRequestFailedJob::FailurePhase::START);
89 CHECK_LE(phase, URLRequestFailedJob::FailurePhase::READ_ASYNC);
90 CHECK_LT(net_error, OK);
93 URLRequestFailedJob::URLRequestFailedJob(URLRequest* request,
94 NetworkDelegate* network_delegate,
95 int net_error)
96 : URLRequestFailedJob(request, network_delegate, START, net_error) {
99 void URLRequestFailedJob::Start() {
100 if (phase_ == START) {
101 if (net_error_ != ERR_IO_PENDING) {
102 NotifyStartError(URLRequestStatus(URLRequestStatus::FAILED, net_error_));
103 return;
105 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
106 return;
108 response_info_.headers = new net::HttpResponseHeaders("HTTP/1.1 200 OK");
109 NotifyHeadersComplete();
112 bool URLRequestFailedJob::ReadRawData(IOBuffer* buf,
113 int buf_size,
114 int* bytes_read) {
115 CHECK(phase_ == READ_SYNC || phase_ == READ_ASYNC);
116 if (net_error_ != ERR_IO_PENDING && phase_ == READ_SYNC) {
117 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, net_error_));
118 return false;
121 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
123 if (net_error_ == ERR_IO_PENDING)
124 return false;
126 DCHECK_EQ(READ_ASYNC, phase_);
127 DCHECK_NE(ERR_IO_PENDING, net_error_);
129 base::ThreadTaskRunnerHandle::Get()->PostTask(
130 FROM_HERE,
131 base::Bind(&URLRequestFailedJob::NotifyDone, weak_factory_.GetWeakPtr(),
132 URLRequestStatus(URLRequestStatus::FAILED, net_error_)));
133 return false;
136 int URLRequestFailedJob::GetResponseCode() const {
137 // If we have headers, get the response code from them.
138 if (response_info_.headers)
139 return response_info_.headers->response_code();
140 return URLRequestJob::GetResponseCode();
143 void URLRequestFailedJob::GetResponseInfo(HttpResponseInfo* info) {
144 *info = response_info_;
147 // static
148 void URLRequestFailedJob::AddUrlHandler() {
149 return AddUrlHandlerForHostname(kMockHostname);
152 // static
153 void URLRequestFailedJob::AddUrlHandlerForHostname(
154 const std::string& hostname) {
155 URLRequestFilter* filter = URLRequestFilter::GetInstance();
156 // Add |hostname| to URLRequestFilter for HTTP and HTTPS.
157 filter->AddHostnameInterceptor(
158 "http", hostname,
159 scoped_ptr<URLRequestInterceptor>(new MockJobInterceptor()));
160 filter->AddHostnameInterceptor(
161 "https", hostname,
162 scoped_ptr<URLRequestInterceptor>(new MockJobInterceptor()));
165 // static
166 GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) {
167 return GetMockHttpUrlForHostname(net_error, kMockHostname);
170 // static
171 GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) {
172 return GetMockHttpsUrlForHostname(net_error, kMockHostname);
175 // static
176 GURL URLRequestFailedJob::GetMockHttpUrlWithFailurePhase(FailurePhase phase,
177 int net_error) {
178 return GetMockUrl("http", kMockHostname, phase, net_error);
181 // static
182 GURL URLRequestFailedJob::GetMockHttpUrlForHostname(
183 int net_error,
184 const std::string& hostname) {
185 return GetMockUrl("http", hostname, START, net_error);
188 // static
189 GURL URLRequestFailedJob::GetMockHttpsUrlForHostname(
190 int net_error,
191 const std::string& hostname) {
192 return GetMockUrl("https", hostname, START, net_error);
195 URLRequestFailedJob::~URLRequestFailedJob() {
198 } // namespace net