• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2012 The Chromium Authors
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 
15 namespace net {
16 
17 // This class simulates a URLRequestJob failing with a given error code at
18 // a particular phase while trying to connect.
19 class URLRequestFailedJob : public URLRequestJob {
20  public:
21   // A Java counterpart will be generated for this enum.
22   // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net.test
23   enum FailurePhase {
24     START = 0,
25     READ_SYNC = 1,
26     READ_ASYNC = 2,
27     MAX_FAILURE_PHASE = 3,
28   };
29 
30   URLRequestFailedJob(URLRequest* request,
31                       FailurePhase phase,
32                       int net_error);
33 
34   // Same as above, except that the job fails at FailurePhase.START.
35   URLRequestFailedJob(URLRequest* request,
36                       int net_error);
37 
38   URLRequestFailedJob(const URLRequestFailedJob&) = delete;
39   URLRequestFailedJob& operator=(const URLRequestFailedJob&) = delete;
40 
41   ~URLRequestFailedJob() override;
42 
43   // URLRequestJob implementation:
44   void Start() override;
45   int ReadRawData(IOBuffer* buf, int buf_size) override;
46   void GetResponseInfo(HttpResponseInfo* info) override;
47   void PopulateNetErrorDetails(NetErrorDetails* details) const override;
48   int64_t GetTotalReceivedBytes() const override;
49 
50   // Adds the testing URLs to the URLRequestFilter.
51   static void AddUrlHandler();
52   static void AddUrlHandlerForHostname(const std::string& hostname);
53 
54   // Given a net error code, constructs a mock URL that will return that error
55   // asynchronously when started. |net_error| must be a valid net error code
56   // other than net::OK. Passing net::ERR_IO_PENDING for |net_error| causes the
57   // resulting request to hang.
58   static GURL GetMockHttpUrl(int net_error);
59   static GURL GetMockHttpsUrl(int net_error);
60 
61   // Constructs a mock URL that reports |net_error| at given |phase| of the
62   // request. |net_error| must be a valid net error code other than net::OK.
63   // Passing net::ERR_IO_PENDING for |net_error| causes the resulting request to
64   // hang.
65   static GURL GetMockHttpUrlWithFailurePhase(FailurePhase phase, int net_error);
66 
67   // Given a net error code and a host name, constructs a mock URL that will
68   // return that error asynchronously when started. |net_error| must be a valid
69   // net error code other than net::OK. Passing net::ERR_IO_PENDING for
70   // |net_error| causes the resulting request to hang.
71   static GURL GetMockHttpUrlForHostname(int net_error,
72                                         const std::string& hostname);
73   static GURL GetMockHttpsUrlForHostname(int net_error,
74                                          const std::string& hostname);
75 
76  protected:
77   void StartAsync();
78 
79  private:
80   HttpResponseInfo response_info_;
81   const FailurePhase phase_;
82   const int net_error_;
83   int64_t total_received_bytes_ = 0;
84 
85   base::WeakPtrFactory<URLRequestFailedJob> weak_factory_{this};
86 };
87 
88 }  // namespace net
89 
90 #endif  // NET_TEST_URL_REQUEST_URL_REQUEST_FAILED_JOB_H_
91