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.
5 #ifndef NET_TEST_URL_REQUEST_URL_REQUEST_FAILED_JOB_H_
6 #define NET_TEST_URL_REQUEST_URL_REQUEST_FAILED_JOB_H_
10 #include "base/compiler_specific.h"
11 #include "base/memory/weak_ptr.h"
12 #include "net/url_request/url_request_job.h"
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
{
21 // A Java counterpart will be generated for this enum.
22 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net.test
27 MAX_FAILURE_PHASE
= 3,
30 URLRequestFailedJob(URLRequest
* request
,
31 NetworkDelegate
* network_delegate
,
35 // Same as above, except that the job fails at FailurePhase.START.
36 URLRequestFailedJob(URLRequest
* request
,
37 NetworkDelegate
* network_delegate
,
40 // URLRequestJob implementation:
41 void Start() override
;
42 bool ReadRawData(IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
43 int GetResponseCode() const override
;
44 void GetResponseInfo(HttpResponseInfo
* info
) override
;
46 // Adds the testing URLs to the URLRequestFilter.
47 static void AddUrlHandler();
48 static void AddUrlHandlerForHostname(const std::string
& hostname
);
50 // Given a net error code, constructs a mock URL that will return that error
51 // asynchronously when started. |net_error| must be a valid net error code
52 // other than net::OK. Passing net::ERR_IO_PENDING for |net_error| causes the
53 // resulting request to hang.
54 static GURL
GetMockHttpUrl(int net_error
);
55 static GURL
GetMockHttpsUrl(int net_error
);
57 // Constructs a mock URL that reports |net_error| at given |phase| of the
58 // request. |net_error| must be a valid net error code other than net::OK.
59 // Passing net::ERR_IO_PENDING for |net_error| causes the resulting request to
61 static GURL
GetMockHttpUrlWithFailurePhase(FailurePhase phase
, int net_error
);
63 // Given a net error code and a host name, constructs a mock URL that will
64 // return that error asynchronously when started. |net_error| must be a valid
65 // net error code other than net::OK. Passing net::ERR_IO_PENDING for
66 // |net_error| causes the resulting request to hang.
67 static GURL
GetMockHttpUrlForHostname(int net_error
,
68 const std::string
& hostname
);
69 static GURL
GetMockHttpsUrlForHostname(int net_error
,
70 const std::string
& hostname
);
73 ~URLRequestFailedJob() override
;
76 HttpResponseInfo response_info_
;
77 const FailurePhase phase_
;
80 base::WeakPtrFactory
<URLRequestFailedJob
> weak_factory_
;
82 DISALLOW_COPY_AND_ASSIGN(URLRequestFailedJob
);
87 #endif // NET_TEST_URL_REQUEST_URL_REQUEST_FAILED_JOB_H_