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;
29 // static getters for known URLs
30 GURL URLRequestTestJob::test_url_1() {
31 return GURL("test:url1");
33 GURL URLRequestTestJob::test_url_2() {
34 return GURL("test:url2");
36 GURL URLRequestTestJob::test_url_3() {
37 return GURL("test:url3");
39 GURL URLRequestTestJob::test_url_error() {
40 return GURL("test:error");
43 // static getters for known URL responses
44 std::string URLRequestTestJob::test_data_1() {
45 return std::string("<html><title>Test One</title></html>");
47 std::string URLRequestTestJob::test_data_2() {
48 return std::string("<html><title>Test Two Two</title></html>");
50 std::string URLRequestTestJob::test_data_3() {
51 return std::string("<html><title>Test Three Three Three</title></html>");
54 // static getter for simple response headers
55 std::string URLRequestTestJob::test_headers() {
56 static const char kHeaders[] =
58 "Content-type: text/html\0"
60 return std::string(kHeaders, arraysize(kHeaders));
63 // static getter for redirect response headers
64 std::string URLRequestTestJob::test_redirect_headers() {
65 static const char kHeaders[] =
66 "HTTP/1.1 302 MOVED\0"
67 "Location: somewhere\0"
69 return std::string(kHeaders, arraysize(kHeaders));
72 // static getter for error response headers
73 std::string URLRequestTestJob::test_error_headers() {
74 static const char kHeaders[] =
75 "HTTP/1.1 500 BOO HOO\0"
77 return std::string(kHeaders, arraysize(kHeaders));
81 URLRequestJob* URLRequestTestJob::Factory(URLRequest* request,
82 NetworkDelegate* network_delegate,
83 const std::string& scheme) {
84 return new URLRequestTestJob(request, network_delegate);
87 URLRequestTestJob::URLRequestTestJob(URLRequest* request,
88 NetworkDelegate* network_delegate)
89 : URLRequestJob(request, network_delegate),
92 priority_(DEFAULT_PRIORITY),
99 URLRequestTestJob::URLRequestTestJob(URLRequest* request,
100 NetworkDelegate* network_delegate,
102 : URLRequestJob(request, network_delegate),
103 auto_advance_(auto_advance),
105 priority_(DEFAULT_PRIORITY),
109 weak_factory_(this) {
112 URLRequestTestJob::URLRequestTestJob(URLRequest* request,
113 NetworkDelegate* network_delegate,
114 const std::string& response_headers,
115 const std::string& response_data,
117 : URLRequestJob(request, network_delegate),
118 auto_advance_(auto_advance),
120 priority_(DEFAULT_PRIORITY),
121 response_headers_(new HttpResponseHeaders(response_headers)),
122 response_data_(response_data),
126 weak_factory_(this) {
129 URLRequestTestJob::~URLRequestTestJob() {
130 g_pending_jobs.Get().erase(
132 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this),
133 g_pending_jobs.Get().end());
136 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const {
138 if (!response_headers_.get())
140 return response_headers_->GetMimeType(mime_type);
143 void URLRequestTestJob::SetPriority(RequestPriority priority) {
144 priority_ = priority;
147 void URLRequestTestJob::Start() {
148 // Start reading asynchronously so that all error reporting and data
149 // callbacks happen as they would for network requests.
150 base::MessageLoop::current()->PostTask(
151 FROM_HERE, base::Bind(&URLRequestTestJob::StartAsync,
152 weak_factory_.GetWeakPtr()));
155 void URLRequestTestJob::StartAsync() {
156 if (!response_headers_.get()) {
157 response_headers_ = new HttpResponseHeaders(test_headers());
158 if (request_->url().spec() == test_url_1().spec()) {
159 response_data_ = test_data_1();
160 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one.
161 } else if (request_->url().spec() == test_url_2().spec()) {
162 response_data_ = test_data_2();
163 } else if (request_->url().spec() == test_url_3().spec()) {
164 response_data_ = test_data_3();
168 // unexpected url, return error
169 // FIXME(brettw) we may want to use WININET errors or have some more types
171 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
173 // FIXME(brettw): this should emulate a network error, and not just fail
174 // initiating a connection
181 this->NotifyHeadersComplete();
184 bool URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size,
186 if (stage_ == WAITING) {
188 async_buf_size_ = buf_size;
189 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
196 if (offset_ >= static_cast<int>(response_data_.length())) {
197 return true; // done reading
200 int to_read = buf_size;
201 if (to_read + offset_ > static_cast<int>(response_data_.length()))
202 to_read = static_cast<int>(response_data_.length()) - offset_;
204 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read);
207 *bytes_read = to_read;
211 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) {
212 if (response_headers_.get())
213 info->headers = response_headers_;
216 void URLRequestTestJob::GetLoadTimingInfo(
217 LoadTimingInfo* load_timing_info) const {
218 // Preserve the times the URLRequest is responsible for, but overwrite all
220 base::TimeTicks request_start = load_timing_info->request_start;
221 base::Time request_start_time = load_timing_info->request_start_time;
222 *load_timing_info = load_timing_info_;
223 load_timing_info->request_start = request_start;
224 load_timing_info->request_start_time = request_start_time;
227 int URLRequestTestJob::GetResponseCode() const {
228 if (response_headers_.get())
229 return response_headers_->response_code();
233 bool URLRequestTestJob::IsRedirectResponse(GURL* location,
234 int* http_status_code) {
235 if (!response_headers_.get())
239 if (!response_headers_->IsRedirect(&value))
242 *location = request_->url().Resolve(value);
243 *http_status_code = response_headers_->response_code();
247 void URLRequestTestJob::Kill() {
249 URLRequestJob::Kill();
250 weak_factory_.InvalidateWeakPtrs();
251 g_pending_jobs.Get().erase(
253 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this),
254 g_pending_jobs.Get().end());
257 void URLRequestTestJob::ProcessNextOperation() {
260 // Must call AdvanceJob() prior to NotifyReadComplete() since that may
263 stage_ = DATA_AVAILABLE;
264 // OK if ReadRawData wasn't called yet.
267 if (!ReadRawData(async_buf_, async_buf_size_, &bytes_read))
268 NOTREACHED() << "This should not return false in DATA_AVAILABLE.";
269 SetStatus(URLRequestStatus()); // clear the io pending flag
270 if (NextReadAsync()) {
271 // Make all future reads return io pending until the next
272 // ProcessNextOperation().
275 NotifyReadComplete(bytes_read);
280 stage_ = ALL_DATA; // done sending data
288 NOTREACHED() << "Invalid stage";
293 bool URLRequestTestJob::NextReadAsync() {
297 void URLRequestTestJob::AdvanceJob() {
299 base::MessageLoop::current()->PostTask(
300 FROM_HERE, base::Bind(&URLRequestTestJob::ProcessNextOperation,
301 weak_factory_.GetWeakPtr()));
304 g_pending_jobs.Get().push_back(this);
308 bool URLRequestTestJob::ProcessOnePendingMessage() {
309 if (g_pending_jobs.Get().empty())
312 URLRequestTestJob* next_job(g_pending_jobs.Get().front());
313 g_pending_jobs.Get().pop_front();
315 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q
316 next_job->ProcessNextOperation();