| 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 AddUrlHandlerForHostname(kMockHostname); |
| 63 } |
| 64 |
| 65 // static |
| 66 void URLRequestFailedJob::AddUrlHandlerForHostname( |
| 67 const std::string& hostname) { |
| 68 // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS. |
| 62 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); | 69 net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance(); |
| 63 filter->AddHostnameHandler("http", kMockHostname, | 70 filter->AddHostnameHandler("http", hostname, URLRequestFailedJob::Factory); |
| 64 URLRequestFailedJob::Factory); | 71 filter->AddHostnameHandler("https", hostname, URLRequestFailedJob::Factory); |
| 65 filter->AddHostnameHandler("https", kMockHostname, | |
| 66 URLRequestFailedJob::Factory); | |
| 67 } | 72 } |
| 68 | 73 |
| 69 // static | 74 // static |
| 70 GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) { | 75 GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) { |
| 71 return GetMockUrl("http", net_error); | 76 return GetMockHttpUrlForHostname(net_error, kMockHostname); |
| 72 } | 77 } |
| 73 | 78 |
| 74 // static | 79 // static |
| 75 GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) { | 80 GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) { |
| 76 return GetMockUrl("https", net_error); | 81 return GetMockHttpsUrlForHostname(net_error, kMockHostname); |
| 82 } |
| 83 |
| 84 // static |
| 85 GURL URLRequestFailedJob::GetMockHttpUrlForHostname( |
| 86 int net_error, const std::string& hostname) { |
| 87 return GetMockUrl("http", hostname, net_error); |
| 88 } |
| 89 |
| 90 // static |
| 91 GURL URLRequestFailedJob::GetMockHttpsUrlForHostname( |
| 92 int net_error, const std::string& hostname) { |
| 93 return GetMockUrl("https", hostname, net_error); |
| 77 } | 94 } |
| 78 | 95 |
| 79 // static | 96 // static |
| 80 net::URLRequestJob* URLRequestFailedJob::Factory( | 97 net::URLRequestJob* URLRequestFailedJob::Factory( |
| 81 net::URLRequest* request, | 98 net::URLRequest* request, |
| 82 net::NetworkDelegate* network_delegate, | 99 net::NetworkDelegate* network_delegate, |
| 83 const std::string& scheme) { | 100 const std::string& scheme) { |
| 84 return new URLRequestFailedJob( | 101 return new URLRequestFailedJob( |
| 85 request, network_delegate, GetErrorCode(request)); | 102 request, network_delegate, GetErrorCode(request)); |
| 86 } | 103 } |
| 87 | 104 |
| 88 void URLRequestFailedJob::StartAsync() { | 105 void URLRequestFailedJob::StartAsync() { |
| 89 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, | 106 NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED, |
| 90 net_error_)); | 107 net_error_)); |
| 91 } | 108 } |
| 92 | 109 |
| 93 } // namespace content | 110 } // namespace content |
| OLD | NEW |