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 #include "net/url_request/url_request_test_job.h"
10 #include "base/bind.h"
11 #include "base/compiler_specific.h"
12 #include "base/lazy_instance.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/strings/string_util.h"
15 #include "net/base/io_buffer.h"
16 #include "net/base/net_errors.h"
17 #include "net/http/http_response_headers.h"
23 typedef std::list
<URLRequestTestJob
*> URLRequestJobList
;
24 base::LazyInstance
<URLRequestJobList
>::Leaky
25 g_pending_jobs
= LAZY_INSTANCE_INITIALIZER
;
27 class TestJobProtocolHandler
: public URLRequestJobFactory::ProtocolHandler
{
29 // URLRequestJobFactory::ProtocolHandler implementation:
30 URLRequestJob
* MaybeCreateJob(
32 NetworkDelegate
* network_delegate
) const override
{
33 return new URLRequestTestJob(request
, network_delegate
);
39 // static getters for known URLs
40 GURL
URLRequestTestJob::test_url_1() {
41 return GURL("test:url1");
43 GURL
URLRequestTestJob::test_url_2() {
44 return GURL("test:url2");
46 GURL
URLRequestTestJob::test_url_3() {
47 return GURL("test:url3");
49 GURL
URLRequestTestJob::test_url_4() {
50 return GURL("test:url4");
52 GURL
URLRequestTestJob::test_url_error() {
53 return GURL("test:error");
55 GURL
URLRequestTestJob::test_url_redirect_to_url_2() {
56 return GURL("test:redirect_to_2");
59 // static getters for known URL responses
60 std::string
URLRequestTestJob::test_data_1() {
61 return std::string("<html><title>Test One</title></html>");
63 std::string
URLRequestTestJob::test_data_2() {
64 return std::string("<html><title>Test Two Two</title></html>");
66 std::string
URLRequestTestJob::test_data_3() {
67 return std::string("<html><title>Test Three Three Three</title></html>");
69 std::string
URLRequestTestJob::test_data_4() {
70 return std::string("<html><title>Test Four Four Four Four</title></html>");
73 // static getter for simple response headers
74 std::string
URLRequestTestJob::test_headers() {
75 static const char kHeaders
[] =
77 "Content-type: text/html\0"
79 return std::string(kHeaders
, arraysize(kHeaders
));
82 // static getter for redirect response headers
83 std::string
URLRequestTestJob::test_redirect_headers() {
84 static const char kHeaders
[] =
85 "HTTP/1.1 302 MOVED\0"
86 "Location: somewhere\0"
88 return std::string(kHeaders
, arraysize(kHeaders
));
91 // static getter for redirect response headers
92 std::string
URLRequestTestJob::test_redirect_to_url_2_headers() {
93 std::string headers
= "HTTP/1.1 302 MOVED";
94 headers
.push_back('\0');
95 headers
+= "Location: ";
96 headers
+= test_url_2().spec();
97 headers
.push_back('\0');
98 headers
.push_back('\0');
102 // static getter for error response headers
103 std::string
URLRequestTestJob::test_error_headers() {
104 static const char kHeaders
[] =
105 "HTTP/1.1 500 BOO HOO\0"
107 return std::string(kHeaders
, arraysize(kHeaders
));
111 URLRequestJobFactory::ProtocolHandler
*
112 URLRequestTestJob::CreateProtocolHandler() {
113 return new TestJobProtocolHandler();
116 URLRequestTestJob::URLRequestTestJob(URLRequest
* request
,
117 NetworkDelegate
* network_delegate
)
118 : URLRequestJob(request
, network_delegate
),
119 auto_advance_(false),
121 priority_(DEFAULT_PRIORITY
),
125 weak_factory_(this) {
128 URLRequestTestJob::URLRequestTestJob(URLRequest
* request
,
129 NetworkDelegate
* network_delegate
,
131 : URLRequestJob(request
, network_delegate
),
132 auto_advance_(auto_advance
),
134 priority_(DEFAULT_PRIORITY
),
138 weak_factory_(this) {
141 URLRequestTestJob::URLRequestTestJob(URLRequest
* request
,
142 NetworkDelegate
* network_delegate
,
143 const std::string
& response_headers
,
144 const std::string
& response_data
,
146 : URLRequestJob(request
, network_delegate
),
147 auto_advance_(auto_advance
),
149 priority_(DEFAULT_PRIORITY
),
150 response_headers_(new HttpResponseHeaders(response_headers
)),
151 response_data_(response_data
),
155 weak_factory_(this) {
158 URLRequestTestJob::~URLRequestTestJob() {
159 g_pending_jobs
.Get().erase(
161 g_pending_jobs
.Get().begin(), g_pending_jobs
.Get().end(), this),
162 g_pending_jobs
.Get().end());
165 bool URLRequestTestJob::GetMimeType(std::string
* mime_type
) const {
167 if (!response_headers_
.get())
169 return response_headers_
->GetMimeType(mime_type
);
172 void URLRequestTestJob::SetPriority(RequestPriority priority
) {
173 priority_
= priority
;
176 void URLRequestTestJob::Start() {
177 // Start reading asynchronously so that all error reporting and data
178 // callbacks happen as they would for network requests.
179 base::MessageLoop::current()->PostTask(
180 FROM_HERE
, base::Bind(&URLRequestTestJob::StartAsync
,
181 weak_factory_
.GetWeakPtr()));
184 void URLRequestTestJob::StartAsync() {
185 if (!response_headers_
.get()) {
186 response_headers_
= new HttpResponseHeaders(test_headers());
187 if (request_
->url().spec() == test_url_1().spec()) {
188 response_data_
= test_data_1();
189 stage_
= DATA_AVAILABLE
; // Simulate a synchronous response for this one.
190 } else if (request_
->url().spec() == test_url_2().spec()) {
191 response_data_
= test_data_2();
192 } else if (request_
->url().spec() == test_url_3().spec()) {
193 response_data_
= test_data_3();
194 } else if (request_
->url().spec() == test_url_4().spec()) {
195 response_data_
= test_data_4();
196 } else if (request_
->url().spec() == test_url_redirect_to_url_2().spec()) {
198 new HttpResponseHeaders(test_redirect_to_url_2_headers());
202 // unexpected url, return error
203 // FIXME(brettw) we may want to use WININET errors or have some more types
205 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED
,
207 // FIXME(brettw): this should emulate a network error, and not just fail
208 // initiating a connection
215 this->NotifyHeadersComplete();
218 bool URLRequestTestJob::ReadRawData(IOBuffer
* buf
, int buf_size
,
220 if (stage_
== WAITING
) {
222 async_buf_size_
= buf_size
;
223 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING
, 0));
230 if (offset_
>= static_cast<int>(response_data_
.length())) {
231 return true; // done reading
234 int to_read
= buf_size
;
235 if (to_read
+ offset_
> static_cast<int>(response_data_
.length()))
236 to_read
= static_cast<int>(response_data_
.length()) - offset_
;
238 memcpy(buf
->data(), &response_data_
.c_str()[offset_
], to_read
);
241 *bytes_read
= to_read
;
245 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo
* info
) {
246 if (response_headers_
.get())
247 info
->headers
= response_headers_
;
250 void URLRequestTestJob::GetLoadTimingInfo(
251 LoadTimingInfo
* load_timing_info
) const {
252 // Preserve the times the URLRequest is responsible for, but overwrite all
254 base::TimeTicks request_start
= load_timing_info
->request_start
;
255 base::Time request_start_time
= load_timing_info
->request_start_time
;
256 *load_timing_info
= load_timing_info_
;
257 load_timing_info
->request_start
= request_start
;
258 load_timing_info
->request_start_time
= request_start_time
;
261 int URLRequestTestJob::GetResponseCode() const {
262 if (response_headers_
.get())
263 return response_headers_
->response_code();
267 bool URLRequestTestJob::IsRedirectResponse(GURL
* location
,
268 int* http_status_code
) {
269 if (!response_headers_
.get())
273 if (!response_headers_
->IsRedirect(&value
))
276 *location
= request_
->url().Resolve(value
);
277 *http_status_code
= response_headers_
->response_code();
281 void URLRequestTestJob::Kill() {
283 URLRequestJob::Kill();
284 weak_factory_
.InvalidateWeakPtrs();
285 g_pending_jobs
.Get().erase(
287 g_pending_jobs
.Get().begin(), g_pending_jobs
.Get().end(), this),
288 g_pending_jobs
.Get().end());
291 void URLRequestTestJob::ProcessNextOperation() {
294 // Must call AdvanceJob() prior to NotifyReadComplete() since that may
297 stage_
= DATA_AVAILABLE
;
298 // OK if ReadRawData wasn't called yet.
301 if (!ReadRawData(async_buf_
, async_buf_size_
, &bytes_read
))
302 NOTREACHED() << "This should not return false in DATA_AVAILABLE.";
303 SetStatus(URLRequestStatus()); // clear the io pending flag
304 if (NextReadAsync()) {
305 // Make all future reads return io pending until the next
306 // ProcessNextOperation().
309 NotifyReadComplete(bytes_read
);
314 stage_
= ALL_DATA
; // done sending data
322 NOTREACHED() << "Invalid stage";
327 bool URLRequestTestJob::NextReadAsync() {
331 void URLRequestTestJob::AdvanceJob() {
333 base::MessageLoop::current()->PostTask(
334 FROM_HERE
, base::Bind(&URLRequestTestJob::ProcessNextOperation
,
335 weak_factory_
.GetWeakPtr()));
338 g_pending_jobs
.Get().push_back(this);
342 bool URLRequestTestJob::ProcessOnePendingMessage() {
343 if (g_pending_jobs
.Get().empty())
346 URLRequestTestJob
* next_job(g_pending_jobs
.Get().front());
347 g_pending_jobs
.Get().pop_front();
349 DCHECK(!next_job
->auto_advance()); // auto_advance jobs should be in this q
350 next_job
->ProcessNextOperation();