blob: 05b562b68301c15dcc2c8fc2794e378d484f318f [file] [log] [blame]
David Ghandehari9e5b5872016-07-28 09:50:04 -07001// 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.
4
5#include "net/url_request/url_request_test_job.h"
6
7#include <algorithm>
8#include <list>
9
10#include "base/bind.h"
11#include "base/compiler_specific.h"
12#include "base/lazy_instance.h"
13#include "base/message_loop.h"
14#include "base/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"
18
19namespace net {
20
21namespace {
22
23typedef std::list<URLRequestTestJob*> URLRequestJobList;
24base::LazyInstance<URLRequestJobList>::Leaky
25 g_pending_jobs = LAZY_INSTANCE_INITIALIZER;
26
27} // namespace
28
29// static getters for known URLs
30GURL URLRequestTestJob::test_url_1() {
31 return GURL("test:url1");
32}
33GURL URLRequestTestJob::test_url_2() {
34 return GURL("test:url2");
35}
36GURL URLRequestTestJob::test_url_3() {
37 return GURL("test:url3");
38}
39GURL URLRequestTestJob::test_url_error() {
40 return GURL("test:error");
41}
42
43// static getters for known URL responses
44std::string URLRequestTestJob::test_data_1() {
45 return std::string("<html><title>Test One</title></html>");
46}
47std::string URLRequestTestJob::test_data_2() {
48 return std::string("<html><title>Test Two Two</title></html>");
49}
50std::string URLRequestTestJob::test_data_3() {
51 return std::string("<html><title>Test Three Three Three</title></html>");
52}
53
54// static getter for simple response headers
55std::string URLRequestTestJob::test_headers() {
56 static const char kHeaders[] =
57 "HTTP/1.1 200 OK\0"
58 "Content-type: text/html\0"
59 "\0";
60 return std::string(kHeaders, arraysize(kHeaders));
61}
62
63// static getter for redirect response headers
64std::string URLRequestTestJob::test_redirect_headers() {
65 static const char kHeaders[] =
66 "HTTP/1.1 302 MOVED\0"
67 "Location: somewhere\0"
68 "\0";
69 return std::string(kHeaders, arraysize(kHeaders));
70}
71
72// static getter for error response headers
73std::string URLRequestTestJob::test_error_headers() {
74 static const char kHeaders[] =
75 "HTTP/1.1 500 BOO HOO\0"
76 "\0";
77 return std::string(kHeaders, arraysize(kHeaders));
78}
79
80// static
81URLRequestJob* URLRequestTestJob::Factory(URLRequest* request,
82 NetworkDelegate* network_delegate,
83 const std::string& scheme) {
84 return new URLRequestTestJob(request, network_delegate);
85}
86
87URLRequestTestJob::URLRequestTestJob(URLRequest* request,
88 NetworkDelegate* network_delegate)
89 : URLRequestJob(request, network_delegate),
90 auto_advance_(false),
91 stage_(WAITING),
92 offset_(0),
93 async_buf_(NULL),
94 async_buf_size_(0),
95 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
96}
97
98URLRequestTestJob::URLRequestTestJob(URLRequest* request,
99 NetworkDelegate* network_delegate,
100 bool auto_advance)
101 : URLRequestJob(request, network_delegate),
102 auto_advance_(auto_advance),
103 stage_(WAITING),
104 offset_(0),
105 async_buf_(NULL),
106 async_buf_size_(0),
107 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
108}
109
110URLRequestTestJob::URLRequestTestJob(URLRequest* request,
111 NetworkDelegate* network_delegate,
112 const std::string& response_headers,
113 const std::string& response_data,
114 bool auto_advance)
115 : URLRequestJob(request, network_delegate),
116 auto_advance_(auto_advance),
117 stage_(WAITING),
118 response_headers_(new HttpResponseHeaders(response_headers)),
119 response_data_(response_data),
120 offset_(0),
121 async_buf_(NULL),
122 async_buf_size_(0),
123 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
124}
125
126URLRequestTestJob::~URLRequestTestJob() {
127 g_pending_jobs.Get().erase(
128 std::remove(
129 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this),
130 g_pending_jobs.Get().end());
131}
132
133bool URLRequestTestJob::GetMimeType(std::string* mime_type) const {
134 DCHECK(mime_type);
135 if (!response_headers_)
136 return false;
137 return response_headers_->GetMimeType(mime_type);
138}
139
140void URLRequestTestJob::Start() {
141 // Start reading asynchronously so that all error reporting and data
142 // callbacks happen as they would for network requests.
143 MessageLoop::current()->PostTask(
144 FROM_HERE, base::Bind(&URLRequestTestJob::StartAsync,
145 weak_factory_.GetWeakPtr()));
146}
147
148void URLRequestTestJob::StartAsync() {
149 if (!response_headers_) {
150 response_headers_ = new HttpResponseHeaders(test_headers());
151 if (request_->url().spec() == test_url_1().spec()) {
152 response_data_ = test_data_1();
153 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one.
154 } else if (request_->url().spec() == test_url_2().spec()) {
155 response_data_ = test_data_2();
156 } else if (request_->url().spec() == test_url_3().spec()) {
157 response_data_ = test_data_3();
158 } else {
159 AdvanceJob();
160
161 // unexpected url, return error
162 // FIXME(brettw) we may want to use WININET errors or have some more types
163 // of errors
164 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
165 ERR_INVALID_URL));
166 // FIXME(brettw): this should emulate a network error, and not just fail
167 // initiating a connection
168 return;
169 }
170 }
171
172 AdvanceJob();
173
174 this->NotifyHeadersComplete();
175}
176
177bool URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size,
178 int *bytes_read) {
179 if (stage_ == WAITING) {
180 async_buf_ = buf;
181 async_buf_size_ = buf_size;
182 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
183 return false;
184 }
185
186 DCHECK(bytes_read);
187 *bytes_read = 0;
188
189 if (offset_ >= static_cast<int>(response_data_.length())) {
190 return true; // done reading
191 }
192
193 int to_read = buf_size;
194 if (to_read + offset_ > static_cast<int>(response_data_.length()))
195 to_read = static_cast<int>(response_data_.length()) - offset_;
196
197 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read);
198 offset_ += to_read;
199
200 *bytes_read = to_read;
201 return true;
202}
203
204void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) {
205 if (response_headers_)
206 info->headers = response_headers_;
207}
208
209int URLRequestTestJob::GetResponseCode() const {
210 if (response_headers_)
211 return response_headers_->response_code();
212 return -1;
213}
214
215bool URLRequestTestJob::IsRedirectResponse(GURL* location,
216 int* http_status_code) {
217 if (!response_headers_)
218 return false;
219
220 std::string value;
221 if (!response_headers_->IsRedirect(&value))
222 return false;
223
224 *location = request_->url().Resolve(value);
225 *http_status_code = response_headers_->response_code();
226 return true;
227}
228
229
230void URLRequestTestJob::Kill() {
231 stage_ = DONE;
232 URLRequestJob::Kill();
233 weak_factory_.InvalidateWeakPtrs();
234 g_pending_jobs.Get().erase(
235 std::remove(
236 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this),
237 g_pending_jobs.Get().end());
238}
239
240void URLRequestTestJob::ProcessNextOperation() {
241 switch (stage_) {
242 case WAITING:
243 // Must call AdvanceJob() prior to NotifyReadComplete() since that may
244 // delete |this|.
245 AdvanceJob();
246 stage_ = DATA_AVAILABLE;
247 // OK if ReadRawData wasn't called yet.
248 if (async_buf_) {
249 int bytes_read;
250 if (!ReadRawData(async_buf_, async_buf_size_, &bytes_read))
251 NOTREACHED() << "This should not return false in DATA_AVAILABLE.";
252 SetStatus(URLRequestStatus()); // clear the io pending flag
253 if (NextReadAsync()) {
254 // Make all future reads return io pending until the next
255 // ProcessNextOperation().
256 stage_ = WAITING;
257 }
258 NotifyReadComplete(bytes_read);
259 }
260 break;
261 case DATA_AVAILABLE:
262 AdvanceJob();
263 stage_ = ALL_DATA; // done sending data
264 break;
265 case ALL_DATA:
266 stage_ = DONE;
267 return;
268 case DONE:
269 return;
270 default:
271 NOTREACHED() << "Invalid stage";
272 return;
273 }
274}
275
276bool URLRequestTestJob::NextReadAsync() {
277 return false;
278}
279
280void URLRequestTestJob::AdvanceJob() {
281 if (auto_advance_) {
282 MessageLoop::current()->PostTask(
283 FROM_HERE, base::Bind(&URLRequestTestJob::ProcessNextOperation,
284 weak_factory_.GetWeakPtr()));
285 return;
286 }
287 g_pending_jobs.Get().push_back(this);
288}
289
290// static
291bool URLRequestTestJob::ProcessOnePendingMessage() {
292 if (g_pending_jobs.Get().empty())
293 return false;
294
295 URLRequestTestJob* next_job(g_pending_jobs.Get().front());
296 g_pending_jobs.Get().pop_front();
297
298 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q
299 next_job->ProcessNextOperation();
300 return true;
301}
302
303} // namespace net