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 "content/test/net/url_request_failed_job.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "base/strings/string_number_conversions.h"
11 #include "net/base/net_errors.h"
12 #include "net/url_request/url_request.h"
13 #include "net/url_request/url_request_filter.h"
18 const char kMockHostname
[] = "mock.failed.request";
20 // Gets the numeric net error code from URL of the form:
21 // scheme://kMockHostname/error_code. The error code must be a valid
22 // net error code, and not net::OK or net::ERR_IO_PENDING.
23 int GetErrorCode(net::URLRequest
* request
) {
25 std::string path
= request
->url().path();
26 if (path
[0] == '/' && base::StringToInt(path
.c_str() + 1, &net_error
)) {
27 CHECK_LT(net_error
, 0);
28 CHECK_NE(net_error
, net::ERR_IO_PENDING
);
32 return net::ERR_UNEXPECTED
;
35 GURL
GetMockUrl(const std::string
& scheme
,
36 const std::string
& hostname
,
38 CHECK_LT(net_error
, 0);
39 CHECK_NE(net_error
, net::ERR_IO_PENDING
);
40 return GURL(scheme
+ "://" + hostname
+ "/" + base::IntToString(net_error
));
45 URLRequestFailedJob::URLRequestFailedJob(net::URLRequest
* request
,
46 net::NetworkDelegate
* network_delegate
,
48 : net::URLRequestJob(request
, network_delegate
),
49 net_error_(net_error
),
50 weak_factory_(this) {}
52 URLRequestFailedJob::~URLRequestFailedJob() {}
54 void URLRequestFailedJob::Start() {
55 base::MessageLoop::current()->PostTask(
57 base::Bind(&URLRequestFailedJob::StartAsync
, weak_factory_
.GetWeakPtr()));
61 void URLRequestFailedJob::AddUrlHandler() {
62 return AddUrlHandlerForHostname(kMockHostname
);
66 void URLRequestFailedJob::AddUrlHandlerForHostname(
67 const std::string
& hostname
) {
68 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS.
69 net::URLRequestFilter
* filter
= net::URLRequestFilter::GetInstance();
70 filter
->AddHostnameHandler("http", hostname
, URLRequestFailedJob::Factory
);
71 filter
->AddHostnameHandler("https", hostname
, URLRequestFailedJob::Factory
);
75 GURL
URLRequestFailedJob::GetMockHttpUrl(int net_error
) {
76 return GetMockHttpUrlForHostname(net_error
, kMockHostname
);
80 GURL
URLRequestFailedJob::GetMockHttpsUrl(int net_error
) {
81 return GetMockHttpsUrlForHostname(net_error
, kMockHostname
);
85 GURL
URLRequestFailedJob::GetMockHttpUrlForHostname(
86 int net_error
, const std::string
& hostname
) {
87 return GetMockUrl("http", hostname
, net_error
);
91 GURL
URLRequestFailedJob::GetMockHttpsUrlForHostname(
92 int net_error
, const std::string
& hostname
) {
93 return GetMockUrl("https", hostname
, net_error
);
97 net::URLRequestJob
* URLRequestFailedJob::Factory(
98 net::URLRequest
* request
,
99 net::NetworkDelegate
* network_delegate
,
100 const std::string
& scheme
) {
101 return new URLRequestFailedJob(
102 request
, network_delegate
, GetErrorCode(request
));
105 void URLRequestFailedJob::StartAsync() {
106 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED
,
110 } // namespace content