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