url_request_failed_job.h revision 1320f92c476a1ad9d19dba2a48c72b75566198e9
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.
4
5#ifndef NET_TEST_URL_REQUEST_URL_REQUEST_FAILED_JOB_H_
6#define NET_TEST_URL_REQUEST_URL_REQUEST_FAILED_JOB_H_
7
8#include <string>
9
10#include "base/compiler_specific.h"
11#include "base/memory/weak_ptr.h"
12#include "net/url_request/url_request_job.h"
13#include "url/gurl.h"
14
15namespace net {
16
17// This class simulates a URLRequestJob failing with a given error code while
18// trying to connect.
19class URLRequestFailedJob : public URLRequestJob {
20 public:
21  URLRequestFailedJob(URLRequest* request,
22                      NetworkDelegate* network_delegate,
23                      int net_error);
24
25  virtual void Start() OVERRIDE;
26
27  // Adds the testing URLs to the URLRequestFilter.
28  static void AddUrlHandler();
29  static void AddUrlHandlerForHostname(const std::string& hostname);
30
31  // Given a net error code, constructs a mock URL that will return that error
32  // asynchronously when started.  |net_error| must be a valid net error code
33  // other than net::OK and net::ERR_IO_PENDING.
34  static GURL GetMockHttpUrl(int net_error);
35  static GURL GetMockHttpsUrl(int net_error);
36
37  // URLRequestFailedJob must be added as a handler for |hostname| for
38  // the returned URL to return |net_error|.
39  static GURL GetMockHttpUrlForHostname(int net_error,
40                                        const std::string& hostname);
41  static GURL GetMockHttpsUrlForHostname(int net_error,
42                                         const std::string& hostname);
43
44 protected:
45  virtual ~URLRequestFailedJob();
46
47 private:
48  static URLRequestJob* Factory(URLRequest* request,
49                                NetworkDelegate* network_delegate,
50                                const std::string& scheme);
51
52  // Simulate a failure.
53  void StartAsync();
54
55  const int net_error_;
56
57  base::WeakPtrFactory<URLRequestFailedJob> weak_factory_;
58
59  DISALLOW_COPY_AND_ASSIGN(URLRequestFailedJob);
60};
61
62}  // namespace net
63
64#endif  // NET_TEST_URL_REQUEST_URL_REQUEST_FAILED_JOB_H_
65