OLD | NEW |
1 // Copyright (c) 2011 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 "content/test/net/url_request_failed_dns_job.h" | 5 #include "content/test/net/url_request_failed_job.h" |
6 | 6 |
7 #include "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/compiler_specific.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
10 #include "googleurl/src/gurl.h" | 10 #include "base/string_number_conversions.h" |
11 #include "net/base/net_errors.h" | 11 #include "net/base/net_errors.h" |
12 #include "net/url_request/url_request.h" | 12 #include "net/url_request/url_request.h" |
13 #include "net/url_request/url_request_filter.h" | 13 #include "net/url_request/url_request_filter.h" |
14 | 14 |
15 const char URLRequestFailedDnsJob::kTestUrl[] = | 15 namespace { |
16 "http://url.handled.by.fake.dns/"; | |
17 | 16 |
18 URLRequestFailedDnsJob::URLRequestFailedDnsJob(net::URLRequest* request) | 17 const char kMockHostname[] = "mock.failed.request"; |
| 18 |
| 19 // Gets the numeric net error code from URL of the form: |
| 20 // scheme://kMockHostname/error_code. The error code must be a valid |
| 21 // net error code, and not net::OK or net::ERR_IO_PENDING. |
| 22 int GetErrorCode(net::URLRequest* request) { |
| 23 int net_error; |
| 24 std::string path = request->url().path(); |
| 25 if (path[0] == '/' && base::StringToInt(path.c_str() + 1, &net_error)) { |
| 26 CHECK_LT(net_error, 0); |
| 27 CHECK_NE(net_error, net::ERR_IO_PENDING); |
| 28 return net_error; |
| 29 } |
| 30 NOTREACHED(); |
| 31 return net::ERR_UNEXPECTED; |
| 32 } |
| 33 |
| 34 GURL GetMockUrl(const std::string& scheme, int net_error) { |
| 35 CHECK_LT(net_error, 0); |
| 36 CHECK_NE(net_error, net::ERR_IO_PENDING); |
| 37 return GURL(scheme + "://" + kMockHostname + "/" + |
| 38 base::IntToString(net_error)); |
| 39 } |
| 40 |
| 41 } // namespace |
| 42 |
| 43 URLRequestFailedJob::URLRequestFailedJob(net::URLRequest* request, |
| 44 int net_error) |
19 : net::URLRequestJob(request), | 45 : net::URLRequestJob(request), |
| 46 net_error_(net_error), |
20 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} | 47 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {} |
21 | 48 |
22 URLRequestFailedDnsJob::~URLRequestFailedDnsJob() {} | 49 URLRequestFailedJob::~URLRequestFailedJob() {} |
23 | 50 |
24 void URLRequestFailedDnsJob::Start() { | 51 void URLRequestFailedJob::Start() { |
25 MessageLoop::current()->PostTask( | 52 MessageLoop::current()->PostTask( |
26 FROM_HERE, | 53 FROM_HERE, |
27 base::Bind(&URLRequestFailedDnsJob::StartAsync, | 54 base::Bind(&URLRequestFailedJob::StartAsync, |
28 weak_factory_.GetWeakPtr())); | 55 weak_factory_.GetWeakPtr())); |
29 } | 56 } |
30 | 57 |
31 // static | 58 // static |
32 void URLRequestFailedDnsJob::AddUrlHandler() { | 59 void URLRequestFailedJob::AddUrlHandler() { |
| 60 // Add kMockHostname to net::URLRequestFilter for HTTP and HTTPS. |
33 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 61 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
34 filter->AddUrlHandler(GURL(kTestUrl), | 62 filter->AddHostnameHandler("http", kMockHostname, |
35 &URLRequestFailedDnsJob::Factory); | 63 URLRequestFailedJob::Factory); |
| 64 filter->AddHostnameHandler("https", kMockHostname, |
| 65 URLRequestFailedJob::Factory); |
36 } | 66 } |
37 | 67 |
38 /*static */ | 68 // static |
39 net::URLRequestJob* URLRequestFailedDnsJob::Factory(net::URLRequest* request, | 69 GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) { |
40 const std::string& scheme) { | 70 return GetMockUrl("http", net_error); |
41 return new URLRequestFailedDnsJob(request); | |
42 } | 71 } |
43 | 72 |
44 void URLRequestFailedDnsJob::StartAsync() { | 73 // static |
| 74 GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) { |
| 75 return GetMockUrl("https", net_error); |
| 76 } |
| 77 |
| 78 // static |
| 79 net::URLRequestJob* URLRequestFailedJob::Factory(net::URLRequest* request, |
| 80 const std::string& scheme) { |
| 81 return new URLRequestFailedJob(request, GetErrorCode(request)); |
| 82 } |
| 83 |
| 84 void URLRequestFailedJob::StartAsync() { |
45 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 85 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
46 net::ERR_NAME_NOT_RESOLVED)); | 86 net_error_)); |
47 } | 87 } |
OLD | NEW |