15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
51320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "net/test/url_request/url_request_failed_job.h"
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/bind.h"
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/logging.h"
99ab5563a3196760eb381d102cbb2bc0f7abc6a50Ben Murdoch#include "base/message_loop/message_loop.h"
10868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "base/strings/string_number_conversions.h"
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/base/net_errors.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "net/url_request/url_request_filter.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tuccinamespace net {
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace {
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)const char kMockHostname[] = "mock.failed.request";
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Gets the numeric net error code from URL of the form:
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// scheme://kMockHostname/error_code.  The error code must be a valid
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// net error code, and not net::OK or net::ERR_IO_PENDING.
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)int GetErrorCode(net::URLRequest* request) {
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int net_error;
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string path = request->url().path();
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  if (path[0] == '/' && base::StringToInt(path.c_str() + 1, &net_error)) {
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_LT(net_error, 0);
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    CHECK_NE(net_error, net::ERR_IO_PENDING);
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    return net_error;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  }
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NOTREACHED();
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return net::ERR_UNEXPECTED;
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
354e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)GURL GetMockUrl(const std::string& scheme,
364e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                const std::string& hostname,
374e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)                int net_error) {
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK_LT(net_error, 0);
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  CHECK_NE(net_error, net::ERR_IO_PENDING);
404e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return GURL(scheme + "://" + hostname + "/" + base::IntToString(net_error));
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)URLRequestFailedJob::URLRequestFailedJob(net::URLRequest* request,
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         net::NetworkDelegate* network_delegate,
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         int net_error)
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    : net::URLRequestJob(request, network_delegate),
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      net_error_(net_error),
50c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      weak_factory_(this) {}
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestFailedJob::Start() {
53c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MessageLoop::current()->PostTask(
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      FROM_HERE,
55c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)      base::Bind(&URLRequestFailedJob::StartAsync, weak_factory_.GetWeakPtr()));
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestFailedJob::AddUrlHandler() {
604e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return AddUrlHandlerForHostname(kMockHostname);
614e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
624e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
634e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
644e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)void URLRequestFailedJob::AddUrlHandlerForHostname(
654e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    const std::string& hostname) {
664e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  // Add |hostname| to net::URLRequestFilter for HTTP and HTTPS.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  net::URLRequestFilter* filter = net::URLRequestFilter::GetInstance();
684e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  filter->AddHostnameHandler("http", hostname, URLRequestFailedJob::Factory);
694e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  filter->AddHostnameHandler("https", hostname, URLRequestFailedJob::Factory);
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL URLRequestFailedJob::GetMockHttpUrl(int net_error) {
744e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return GetMockHttpUrlForHostname(net_error, kMockHostname);
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)GURL URLRequestFailedJob::GetMockHttpsUrl(int net_error) {
794e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return GetMockHttpsUrlForHostname(net_error, kMockHostname);
804e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
814e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
824e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
834e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)GURL URLRequestFailedJob::GetMockHttpUrlForHostname(
844e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    int net_error, const std::string& hostname) {
854e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return GetMockUrl("http", hostname, net_error);
864e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)}
874e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)
884e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)// static
894e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)GURL URLRequestFailedJob::GetMockHttpsUrlForHostname(
904e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)    int net_error, const std::string& hostname) {
914e180b6a0b4720a9b8e9e959a882386f690f08ffTorne (Richard Coles)  return GetMockUrl("https", hostname, net_error);
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)URLRequestFailedJob::~URLRequestFailedJob() {}
95f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// static
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)net::URLRequestJob* URLRequestFailedJob::Factory(
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::URLRequest* request,
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    net::NetworkDelegate* network_delegate,
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    const std::string& scheme) {
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  return new URLRequestFailedJob(
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      request, network_delegate, GetErrorCode(request));
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)void URLRequestFailedJob::StartAsync() {
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  NotifyStartError(net::URLRequestStatus(net::URLRequestStatus::FAILED,
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                         net_error_));
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1101320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci}  // namespace net
111