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_URL_REQUEST_URL_REQUEST_TEST_JOB_H_
6 #define NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_
10 #include "base/memory/weak_ptr.h"
11 #include "net/base/load_timing_info.h"
12 #include "net/url_request/url_request.h"
13 #include "net/url_request/url_request_job.h"
14 #include "net/url_request/url_request_job_factory.h"
18 // This job type is designed to help with simple unit tests. To use, you
19 // probably want to inherit from it to set up the state you want. Then install
20 // it as the protocol handler for the "test" scheme.
22 // It will respond to several URLs, which you can retrieve using the test_url*
23 // getters, which will in turn respond with the corresponding responses returned
24 // by test_data*. Any other URLs that begin with "test:" will return an error,
25 // which might also be useful, you can use test_url_error() to retreive a
28 // You can override the known URLs or the response data by overriding Start().
30 // Optionally, you can also construct test jobs to return a headers and data
31 // provided to the contstructor in response to any request url.
33 // When a job is created, it gets put on a queue of pending test jobs. To
34 // process jobs on this queue, use ProcessOnePendingMessage, which will process
35 // one step of the next job. If the job is incomplete, it will be added to the
38 // Optionally, you can also construct test jobs that advance automatically
39 // without having to call ProcessOnePendingMessage.
40 class NET_EXPORT_PRIVATE URLRequestTestJob
: public URLRequestJob
{
42 // Constructs a job to return one of the canned responses depending on the
43 // request url, with auto advance disabled.
44 URLRequestTestJob(URLRequest
* request
, NetworkDelegate
* network_delegate
);
46 // Constructs a job to return one of the canned responses depending on the
47 // request url, optionally with auto advance enabled.
48 URLRequestTestJob(URLRequest
* request
,
49 NetworkDelegate
* network_delegate
,
52 // Constructs a job to return the given response regardless of the request
53 // url. The headers should include the HTTP status line and be formatted as
54 // expected by HttpResponseHeaders.
55 URLRequestTestJob(URLRequest
* request
,
56 NetworkDelegate
* network_delegate
,
57 const std::string
& response_headers
,
58 const std::string
& response_data
,
61 // The canned URLs this handler will respond to without having been
62 // explicitly initialized with response headers and data.
63 // FIXME(brettw): we should probably also have a redirect one
64 static GURL
test_url_1();
65 static GURL
test_url_2();
66 static GURL
test_url_3();
67 static GURL
test_url_4();
68 static GURL
test_url_error();
69 static GURL
test_url_redirect_to_url_2();
71 // The data that corresponds to each of the URLs above
72 static std::string
test_data_1();
73 static std::string
test_data_2();
74 static std::string
test_data_3();
75 static std::string
test_data_4();
77 // The headers that correspond to each of the URLs above
78 static std::string
test_headers();
80 // The headers for a redirect response
81 static std::string
test_redirect_headers();
83 // The headers for a redirect response to the second test url.
84 static std::string
test_redirect_to_url_2_headers();
86 // The headers for a server error response
87 static std::string
test_error_headers();
89 // Processes one pending message from the stack, returning true if any
90 // message was processed, or false if there are no more pending request
91 // notifications to send. This is not applicable when using auto_advance.
92 static bool ProcessOnePendingMessage();
94 // With auto advance enabled, the job will advance thru the stages without
95 // the caller having to call ProcessOnePendingMessage. Auto advance depends
96 // on having a message loop running. The default is to not auto advance.
97 // Should not be altered after the job has started.
98 bool auto_advance() { return auto_advance_
; }
99 void set_auto_advance(bool auto_advance
) { auto_advance_
= auto_advance
; }
101 void set_load_timing_info(const LoadTimingInfo
& load_timing_info
) {
102 load_timing_info_
= load_timing_info
;
105 RequestPriority
priority() const { return priority_
; }
107 // Create a protocol handler for callers that don't subclass.
108 static scoped_ptr
<URLRequestJobFactory::ProtocolHandler
>
109 CreateProtocolHandler();
112 void SetPriority(RequestPriority priority
) override
;
113 void Start() override
;
114 bool ReadRawData(IOBuffer
* buf
, int buf_size
, int* bytes_read
) override
;
115 void Kill() override
;
116 bool GetMimeType(std::string
* mime_type
) const override
;
117 void GetResponseInfo(HttpResponseInfo
* info
) override
;
118 void GetLoadTimingInfo(LoadTimingInfo
* load_timing_info
) const override
;
119 int GetResponseCode() const override
;
120 bool IsRedirectResponse(GURL
* location
, int* http_status_code
) override
;
123 // Override to specify whether the next read done from this job will
124 // return IO pending. This controls whether or not the WAITING state will
125 // transition back to WAITING or to DATA_AVAILABLE after an asynchronous
126 // read is processed.
127 virtual bool NextReadAsync();
129 // This is what operation we are going to do next when this job is handled.
130 // When the stage is DONE, this job will not be put on the queue.
131 enum Stage
{ WAITING
, DATA_AVAILABLE
, ALL_DATA
, DONE
};
133 ~URLRequestTestJob() override
;
135 // Call to process the next opeation, usually sending a notification, and
136 // advancing the stage if necessary. THIS MAY DELETE THE OBJECT.
137 void ProcessNextOperation();
139 // Call to move the job along to the next operation.
142 // Called via InvokeLater to cause callbacks to occur after Start() returns.
143 virtual void StartAsync();
149 RequestPriority priority_
;
151 // The headers the job should return, will be set in Start() if not provided
152 // in the explicit ctor.
153 scoped_refptr
<HttpResponseHeaders
> response_headers_
;
155 // The data to send, will be set in Start() if not provided in the explicit
157 std::string response_data_
;
159 // current offset within response_data_
162 // Holds the buffer for an asynchronous ReadRawData call
163 IOBuffer
* async_buf_
;
166 LoadTimingInfo load_timing_info_
;
168 base::WeakPtrFactory
<URLRequestTestJob
> weak_factory_
;
173 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_