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 "content/test/net/url_request_failed_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/logging.h" | 8 #include "base/logging.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
(...skipping 14 matching lines...) Expand all Loading... |
25 std::string path = request->url().path(); | 25 std::string path = request->url().path(); |
26 if (path[0] == '/' && base::StringToInt(path.c_str() + 1, &net_error)) { | 26 if (path[0] == '/' && base::StringToInt(path.c_str() + 1, &net_error)) { |
27 CHECK_LT(net_error, 0); | 27 CHECK_LT(net_error, 0); |
28 CHECK_NE(net_error, net::ERR_IO_PENDING); | 28 CHECK_NE(net_error, net::ERR_IO_PENDING); |
29 return net_error; | 29 return net_error; |
30 } | 30 } |
31 NOTREACHED(); | 31 NOTREACHED(); |
32 return net::ERR_UNEXPECTED; | 32 return net::ERR_UNEXPECTED; |
33 } | 33 } |
34 | 34 |
35 GURL GetMockUrl(const std::string& scheme, int net_error) { | 35 GURL GetMockUrl(const std::string& scheme, |
| 36 const std::string& hostname, |
| 37 int net_error) { |
36 CHECK_LT(net_error, 0); | 38 CHECK_LT(net_error, 0); |
37 CHECK_NE(net_error, net::ERR_IO_PENDING); | 39 CHECK_NE(net_error, net::ERR_IO_PENDING); |
38 return GURL(scheme + "://" + kMockHostname + "/" + | 40 return GURL(scheme + "://" + hostname + "/" + base::IntToString(net_error)); |
39 base::IntToString(net_error)); | |
40 } | 41 } |
41 | 42 |
42 } // namespace | 43 } // namespace |
43 | 44 |
44 URLRequestFailedJob::URLRequestFailedJob(net::URLRequest* request, | 45 URLRequestFailedJob::URLRequestFailedJob(net::URLRequest* request, |
45 net::NetworkDelegate* network_delegate, | 46 net::NetworkDelegate* network_delegate, |
46 int net_error) | 47 int net_error) |
47 : net::URLRequestJob(request, network_delegate), | 48 : net::URLRequestJob(request, network_delegate), |
48 net_error_(net_error), | 49 net_error_(net_error), |
49 weak_factory_(this) {} | 50 weak_factory_(this) {} |
50 | 51 |
51 URLRequestFailedJob::~URLRequestFailedJob() {} | 52 URLRequestFailedJob::~URLRequestFailedJob() {} |
52 | 53 |
53 void URLRequestFailedJob::Start() { | 54 void URLRequestFailedJob::Start() { |
54 base::MessageLoop::current()->PostTask( | 55 base::MessageLoop::current()->PostTask( |
55 FROM_HERE, | 56 FROM_HERE, |
56 base::Bind(&URLRequestFailedJob::StartAsync, weak_factory_.GetWeakPtr())); | 57 base::Bind(&URLRequestFailedJob::StartAsync, weak_factory_.GetWeakPtr())); |
57 } | 58 } |
58 | 59 |
59 // static | 60 // static |
60 void URLRequestFailedJob::AddUrlHandler() { | 61 void URLRequestFailedJob::AddUrlHandler() { |
61 // Add kMockHostname to net::URLRequestFilter for HTTP and HTTPS. | 62 return AddUrlHandler(kMockHostname); |
| 63 } |
| 64 |
| 65 // static |
| 66 void URLRequestFailedJob::AddUrlHandler(const std::string& hostname) { |
| 67 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS. |
62 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 68 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
63 filter->AddHostnameHandler("http", kMockHostname, | 69 filter->AddHostnameHandler("http", hostname, URLRequestFailedJob::Factory); |
64 URLRequestFailedJob::Factory); | 70 filter->AddHostnameHandler("https", hostname, URLRequestFailedJob::Factory); |
65 filter->AddHostnameHandler("https", kMockHostname, | |
66 URLRequestFailedJob::Factory); | |
67 } | 71 } |
68 | 72 |
69 // static | 73 // static |
70 GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) { | 74 GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) { |
71 return GetMockUrl("http", net_error); | 75 return GetMockHttpUrl(net_error, kMockHostname); |
72 } | 76 } |
73 | 77 |
74 // static | 78 // static |
75 GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) { | 79 GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) { |
76 return GetMockUrl("https", net_error); | 80 return GetMockHttpsUrl(net_error, kMockHostname); |
| 81 } |
| 82 |
| 83 // static |
| 84 GURL URLRequestFailedJob::GetMockHttpUrl( |
| 85 int net_error, const std::string& hostname) { |
| 86 return GetMockUrl("http", hostname, net_error); |
| 87 } |
| 88 |
| 89 // static |
| 90 GURL URLRequestFailedJob::GetMockHttpsUrl( |
| 91 int net_error, const std::string& hostname) { |
| 92 return GetMockUrl("https", hostname, net_error); |
77 } | 93 } |
78 | 94 |
79 // static | 95 // static |
80 net::URLRequestJob* URLRequestFailedJob::Factory( | 96 net::URLRequestJob* URLRequestFailedJob::Factory( |
81 net::URLRequest* request, | 97 net::URLRequest* request, |
82 net::NetworkDelegate* network_delegate, | 98 net::NetworkDelegate* network_delegate, |
83 const std::string& scheme) { | 99 const std::string& scheme) { |
84 return new URLRequestFailedJob( | 100 return new URLRequestFailedJob( |
85 request, network_delegate, GetErrorCode(request)); | 101 request, network_delegate, GetErrorCode(request)); |
86 } | 102 } |
87 | 103 |
88 void URLRequestFailedJob::StartAsync() { | 104 void URLRequestFailedJob::StartAsync() { |
89 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 105 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
90 net_error_)); | 106 net_error_)); |
91 } | 107 } |
92 | 108 |
93 } // namespace content | 109 } // namespace content |
OLD | NEW |