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/url_request/url_request.h"
12 #include "net/url_request/url_request_job.h"
16 // This job type is designed to help with simple unit tests. To use, you
17 // probably want to inherit from it to set up the state you want. Then install
18 // it as the protocol handler for the "test" scheme.
20 // It will respond to three URLs, which you can retrieve using the test_url*
21 // getters, which will in turn respond with the corresponding responses returned
22 // by test_data*. Any other URLs that begin with "test:" will return an error,
23 // which might also be useful, you can use test_url_error() to retreive a
26 // You can override the known URLs or the response data by overriding Start().
28 // Optionally, you can also construct test jobs to return a headers and data
29 // provided to the contstructor in response to any request url.
31 // When a job is created, it gets put on a queue of pending test jobs. To
32 // process jobs on this queue, use ProcessOnePendingMessage, which will process
33 // one step of the next job. If the job is incomplete, it will be added to the
36 // Optionally, you can also construct test jobs that advance automatically
37 // without having to call ProcessOnePendingMessage.
38 class NET_EXPORT_PRIVATE URLRequestTestJob
: public URLRequestJob
{
40 // Constructs a job to return one of the canned responses depending on the
41 // request url, with auto advance disabled.
42 URLRequestTestJob(URLRequest
* request
, NetworkDelegate
* network_delegate
);
44 // Constructs a job to return one of the canned responses depending on the
45 // request url, optionally with auto advance enabled.
46 URLRequestTestJob(URLRequest
* request
,
47 NetworkDelegate
* network_delegate
,
50 // Constructs a job to return the given response regardless of the request
51 // url. The headers should include the HTTP status line and be formatted as
52 // expected by HttpResponseHeaders.
53 URLRequestTestJob(URLRequest
* request
,
54 net::NetworkDelegate
* network_delegate
,
55 const std::string
& response_headers
,
56 const std::string
& response_data
,
59 // The three canned URLs this handler will respond to without having been
60 // explicitly initialized with response headers and data.
61 // FIXME(brettw): we should probably also have a redirect one
62 static GURL
test_url_1();
63 static GURL
test_url_2();
64 static GURL
test_url_3();
65 static GURL
test_url_error();
67 // The data that corresponds to each of the URLs above
68 static std::string
test_data_1();
69 static std::string
test_data_2();
70 static std::string
test_data_3();
72 // The headers that correspond to each of the URLs above
73 static std::string
test_headers();
75 // The headers for a redirect response
76 static std::string
test_redirect_headers();
78 // The headers for a server error response
79 static std::string
test_error_headers();
81 // Processes one pending message from the stack, returning true if any
82 // message was processed, or false if there are no more pending request
83 // notifications to send. This is not applicable when using auto_advance.
84 static bool ProcessOnePendingMessage();
86 // With auto advance enabled, the job will advance thru the stages without
87 // the caller having to call ProcessOnePendingMessage. Auto advance depends
88 // on having a message loop running. The default is to not auto advance.
89 // Should not be altered after the job has started.
90 bool auto_advance() { return auto_advance_
; }
91 void set_auto_advance(bool auto_advance
) { auto_advance_
= auto_advance
; }
93 // Factory method for protocol factory registration if callers don't subclass
94 static URLRequest::ProtocolFactory Factory
;
97 virtual void Start() OVERRIDE
;
98 virtual bool ReadRawData(IOBuffer
* buf
,
100 int *bytes_read
) OVERRIDE
;
101 virtual void Kill() OVERRIDE
;
102 virtual bool GetMimeType(std::string
* mime_type
) const OVERRIDE
;
103 virtual void GetResponseInfo(HttpResponseInfo
* info
) OVERRIDE
;
104 virtual int GetResponseCode() const OVERRIDE
;
105 virtual bool IsRedirectResponse(GURL
* location
,
106 int* http_status_code
) OVERRIDE
;
109 // Override to specify whether the next read done from this job will
110 // return IO pending. This controls whether or not the WAITING state will
111 // transition back to WAITING or to DATA_AVAILABLE after an asynchronous
112 // read is processed.
113 virtual bool NextReadAsync();
115 // This is what operation we are going to do next when this job is handled.
116 // When the stage is DONE, this job will not be put on the queue.
117 enum Stage
{ WAITING
, DATA_AVAILABLE
, ALL_DATA
, DONE
};
119 virtual ~URLRequestTestJob();
121 // Call to process the next opeation, usually sending a notification, and
122 // advancing the stage if necessary. THIS MAY DELETE THE OBJECT.
123 void ProcessNextOperation();
125 // Call to move the job along to the next operation.
128 // Called via InvokeLater to cause callbacks to occur after Start() returns.
129 virtual void StartAsync();
135 // The headers the job should return, will be set in Start() if not provided
136 // in the explicit ctor.
137 scoped_refptr
<HttpResponseHeaders
> response_headers_
;
139 // The data to send, will be set in Start() if not provided in the explicit
141 std::string response_data_
;
143 // current offset within response_data_
146 // Holds the buffer for an asynchronous ReadRawData call
147 IOBuffer
* async_buf_
;
150 base::WeakPtrFactory
<URLRequestTestJob
> weak_factory_
;
155 #endif // NET_URL_REQUEST_URL_REQUEST_TEST_JOB_H_