chromeos: bluetooth: add BluetoothInputClient
[chromium-blink-merge.git] / net / url_request / url_request_test_job.cc
blob4a0ba6e21291ca62a345ff57ca84526eefd59129
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"
7 #include <algorithm>
8 #include <list>
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 #include "net/url_request/url_request.h"
20 namespace net {
22 namespace {
24 typedef std::list<URLRequestTestJob*> URLRequestJobList;
25 base::LazyInstance<URLRequestJobList>::Leaky
26 g_pending_jobs = LAZY_INSTANCE_INITIALIZER;
28 } // namespace
30 // static getters for known URLs
31 GURL URLRequestTestJob::test_url_1() {
32 return GURL("test:url1");
34 GURL URLRequestTestJob::test_url_2() {
35 return GURL("test:url2");
37 GURL URLRequestTestJob::test_url_3() {
38 return GURL("test:url3");
40 GURL URLRequestTestJob::test_url_error() {
41 return GURL("test:error");
44 // static getters for known URL responses
45 std::string URLRequestTestJob::test_data_1() {
46 return std::string("<html><title>Test One</title></html>");
48 std::string URLRequestTestJob::test_data_2() {
49 return std::string("<html><title>Test Two Two</title></html>");
51 std::string URLRequestTestJob::test_data_3() {
52 return std::string("<html><title>Test Three Three Three</title></html>");
55 // static getter for simple response headers
56 std::string URLRequestTestJob::test_headers() {
57 static const char kHeaders[] =
58 "HTTP/1.1 200 OK\0"
59 "Content-type: text/html\0"
60 "\0";
61 return std::string(kHeaders, arraysize(kHeaders));
64 // static getter for redirect response headers
65 std::string URLRequestTestJob::test_redirect_headers() {
66 static const char kHeaders[] =
67 "HTTP/1.1 302 MOVED\0"
68 "Location: somewhere\0"
69 "\0";
70 return std::string(kHeaders, arraysize(kHeaders));
73 // static getter for error response headers
74 std::string URLRequestTestJob::test_error_headers() {
75 static const char kHeaders[] =
76 "HTTP/1.1 500 BOO HOO\0"
77 "\0";
78 return std::string(kHeaders, arraysize(kHeaders));
81 // static
82 URLRequestJob* URLRequestTestJob::Factory(URLRequest* request,
83 const std::string& scheme) {
84 return new URLRequestTestJob(request);
87 URLRequestTestJob::URLRequestTestJob(URLRequest* request)
88 : URLRequestJob(request),
89 auto_advance_(false),
90 stage_(WAITING),
91 offset_(0),
92 async_buf_(NULL),
93 async_buf_size_(0),
94 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
97 URLRequestTestJob::URLRequestTestJob(URLRequest* request,
98 bool auto_advance)
99 : URLRequestJob(request),
100 auto_advance_(auto_advance),
101 stage_(WAITING),
102 offset_(0),
103 async_buf_(NULL),
104 async_buf_size_(0),
105 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
108 URLRequestTestJob::URLRequestTestJob(URLRequest* request,
109 const std::string& response_headers,
110 const std::string& response_data,
111 bool auto_advance)
112 : URLRequestJob(request),
113 auto_advance_(auto_advance),
114 stage_(WAITING),
115 response_headers_(new HttpResponseHeaders(response_headers)),
116 response_data_(response_data),
117 offset_(0),
118 async_buf_(NULL),
119 async_buf_size_(0),
120 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
123 URLRequestTestJob::~URLRequestTestJob() {
124 g_pending_jobs.Get().erase(
125 std::remove(
126 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this),
127 g_pending_jobs.Get().end());
130 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const {
131 DCHECK(mime_type);
132 if (!response_headers_)
133 return false;
134 return response_headers_->GetMimeType(mime_type);
137 void URLRequestTestJob::Start() {
138 // Start reading asynchronously so that all error reporting and data
139 // callbacks happen as they would for network requests.
140 MessageLoop::current()->PostTask(
141 FROM_HERE, base::Bind(&URLRequestTestJob::StartAsync,
142 weak_factory_.GetWeakPtr()));
145 void URLRequestTestJob::StartAsync() {
146 if (!response_headers_) {
147 response_headers_ = new HttpResponseHeaders(test_headers());
148 if (request_->url().spec() == test_url_1().spec()) {
149 response_data_ = test_data_1();
150 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one.
151 } else if (request_->url().spec() == test_url_2().spec()) {
152 response_data_ = test_data_2();
153 } else if (request_->url().spec() == test_url_3().spec()) {
154 response_data_ = test_data_3();
155 } else {
156 AdvanceJob();
158 // unexpected url, return error
159 // FIXME(brettw) we may want to use WININET errors or have some more types
160 // of errors
161 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED,
162 ERR_INVALID_URL));
163 // FIXME(brettw): this should emulate a network error, and not just fail
164 // initiating a connection
165 return;
169 AdvanceJob();
171 this->NotifyHeadersComplete();
174 bool URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size,
175 int *bytes_read) {
176 if (stage_ == WAITING) {
177 async_buf_ = buf;
178 async_buf_size_ = buf_size;
179 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0));
180 return false;
183 DCHECK(bytes_read);
184 *bytes_read = 0;
186 if (offset_ >= static_cast<int>(response_data_.length())) {
187 return true; // done reading
190 int to_read = buf_size;
191 if (to_read + offset_ > static_cast<int>(response_data_.length()))
192 to_read = static_cast<int>(response_data_.length()) - offset_;
194 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read);
195 offset_ += to_read;
197 *bytes_read = to_read;
198 return true;
201 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) {
202 if (response_headers_)
203 info->headers = response_headers_;
206 int URLRequestTestJob::GetResponseCode() const {
207 if (response_headers_)
208 return response_headers_->response_code();
209 return -1;
212 bool URLRequestTestJob::IsRedirectResponse(GURL* location,
213 int* http_status_code) {
214 if (!response_headers_)
215 return false;
217 std::string value;
218 if (!response_headers_->IsRedirect(&value))
219 return false;
221 *location = request_->url().Resolve(value);
222 *http_status_code = response_headers_->response_code();
223 return true;
227 void URLRequestTestJob::Kill() {
228 stage_ = DONE;
229 URLRequestJob::Kill();
230 weak_factory_.InvalidateWeakPtrs();
231 g_pending_jobs.Get().erase(
232 std::remove(
233 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this),
234 g_pending_jobs.Get().end());
237 void URLRequestTestJob::ProcessNextOperation() {
238 switch (stage_) {
239 case WAITING:
240 // Must call AdvanceJob() prior to NotifyReadComplete() since that may
241 // delete |this|.
242 AdvanceJob();
243 stage_ = DATA_AVAILABLE;
244 // OK if ReadRawData wasn't called yet.
245 if (async_buf_) {
246 int bytes_read;
247 if (!ReadRawData(async_buf_, async_buf_size_, &bytes_read))
248 NOTREACHED() << "This should not return false in DATA_AVAILABLE.";
249 SetStatus(URLRequestStatus()); // clear the io pending flag
250 NotifyReadComplete(bytes_read);
252 break;
253 case DATA_AVAILABLE:
254 AdvanceJob();
255 stage_ = ALL_DATA; // done sending data
256 break;
257 case ALL_DATA:
258 stage_ = DONE;
259 return;
260 case DONE:
261 return;
262 default:
263 NOTREACHED() << "Invalid stage";
264 return;
268 void URLRequestTestJob::AdvanceJob() {
269 if (auto_advance_) {
270 MessageLoop::current()->PostTask(
271 FROM_HERE, base::Bind(&URLRequestTestJob::ProcessNextOperation,
272 weak_factory_.GetWeakPtr()));
273 return;
275 g_pending_jobs.Get().push_back(this);
278 // static
279 bool URLRequestTestJob::ProcessOnePendingMessage() {
280 if (g_pending_jobs.Get().empty())
281 return false;
283 URLRequestTestJob* next_job(g_pending_jobs.Get().front());
284 g_pending_jobs.Get().pop_front();
286 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q
287 next_job->ProcessNextOperation();
288 return true;
291 } // namespace net