OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/url_request/url_request_test_job.h" | 5 #include "net/url_request/url_request_test_job.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <list> | 8 #include <list> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 26 matching lines...) Expand all Loading... |
37 return new URLRequestTestJob(request, network_delegate); | 37 return new URLRequestTestJob(request, network_delegate); |
38 } | 38 } |
39 }; | 39 }; |
40 | 40 |
41 } // namespace | 41 } // namespace |
42 | 42 |
43 // static getters for known URLs | 43 // static getters for known URLs |
44 GURL URLRequestTestJob::test_url_1() { | 44 GURL URLRequestTestJob::test_url_1() { |
45 return GURL("test:url1"); | 45 return GURL("test:url1"); |
46 } | 46 } |
| 47 |
47 GURL URLRequestTestJob::test_url_2() { | 48 GURL URLRequestTestJob::test_url_2() { |
48 return GURL("test:url2"); | 49 return GURL("test:url2"); |
49 } | 50 } |
| 51 |
50 GURL URLRequestTestJob::test_url_3() { | 52 GURL URLRequestTestJob::test_url_3() { |
51 return GURL("test:url3"); | 53 return GURL("test:url3"); |
52 } | 54 } |
| 55 |
53 GURL URLRequestTestJob::test_url_4() { | 56 GURL URLRequestTestJob::test_url_4() { |
54 return GURL("test:url4"); | 57 return GURL("test:url4"); |
55 } | 58 } |
| 59 |
| 60 GURL URLRequestTestJob::test_url_auto_advance_async_reads_1() { |
| 61 return GURL("test:url_auto_advance_async_reads_1"); |
| 62 } |
| 63 |
56 GURL URLRequestTestJob::test_url_error() { | 64 GURL URLRequestTestJob::test_url_error() { |
57 return GURL("test:error"); | 65 return GURL("test:error"); |
58 } | 66 } |
| 67 |
59 GURL URLRequestTestJob::test_url_redirect_to_url_1() { | 68 GURL URLRequestTestJob::test_url_redirect_to_url_1() { |
60 return GURL("test:redirect_to_1"); | 69 return GURL("test:redirect_to_1"); |
61 } | 70 } |
| 71 |
62 GURL URLRequestTestJob::test_url_redirect_to_url_2() { | 72 GURL URLRequestTestJob::test_url_redirect_to_url_2() { |
63 return GURL("test:redirect_to_2"); | 73 return GURL("test:redirect_to_2"); |
64 } | 74 } |
65 | 75 |
66 // static getters for known URL responses | 76 // static getters for known URL responses |
67 std::string URLRequestTestJob::test_data_1() { | 77 std::string URLRequestTestJob::test_data_1() { |
68 return std::string("<html><title>Test One</title></html>"); | 78 return std::string("<html><title>Test One</title></html>"); |
69 } | 79 } |
70 std::string URLRequestTestJob::test_data_2() { | 80 std::string URLRequestTestJob::test_data_2() { |
71 return std::string("<html><title>Test Two Two</title></html>"); | 81 return std::string("<html><title>Test Two Two</title></html>"); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 } | 136 } |
127 | 137 |
128 // static | 138 // static |
129 std::unique_ptr<URLRequestJobFactory::ProtocolHandler> | 139 std::unique_ptr<URLRequestJobFactory::ProtocolHandler> |
130 URLRequestTestJob::CreateProtocolHandler() { | 140 URLRequestTestJob::CreateProtocolHandler() { |
131 return base::MakeUnique<TestJobProtocolHandler>(); | 141 return base::MakeUnique<TestJobProtocolHandler>(); |
132 } | 142 } |
133 | 143 |
134 URLRequestTestJob::URLRequestTestJob(URLRequest* request, | 144 URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
135 NetworkDelegate* network_delegate) | 145 NetworkDelegate* network_delegate) |
136 : URLRequestJob(request, network_delegate), | 146 : URLRequestTestJob(request, network_delegate, false) {} |
137 auto_advance_(false), | |
138 stage_(WAITING), | |
139 priority_(DEFAULT_PRIORITY), | |
140 offset_(0), | |
141 async_buf_(NULL), | |
142 async_buf_size_(0), | |
143 response_headers_length_(0), | |
144 weak_factory_(this) {} | |
145 | 147 |
146 URLRequestTestJob::URLRequestTestJob(URLRequest* request, | 148 URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
147 NetworkDelegate* network_delegate, | 149 NetworkDelegate* network_delegate, |
148 bool auto_advance) | 150 bool auto_advance) |
149 : URLRequestJob(request, network_delegate), | 151 : URLRequestJob(request, network_delegate), |
150 auto_advance_(auto_advance), | 152 auto_advance_(auto_advance), |
151 stage_(WAITING), | 153 stage_(WAITING), |
152 priority_(DEFAULT_PRIORITY), | 154 priority_(DEFAULT_PRIORITY), |
153 offset_(0), | 155 offset_(0), |
154 async_buf_(NULL), | 156 async_buf_(NULL), |
155 async_buf_size_(0), | 157 async_buf_size_(0), |
156 response_headers_length_(0), | 158 response_headers_length_(0), |
| 159 async_reads_(false), |
157 weak_factory_(this) {} | 160 weak_factory_(this) {} |
158 | 161 |
159 URLRequestTestJob::URLRequestTestJob(URLRequest* request, | 162 URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
160 NetworkDelegate* network_delegate, | 163 NetworkDelegate* network_delegate, |
161 const std::string& response_headers, | 164 const std::string& response_headers, |
162 const std::string& response_data, | 165 const std::string& response_data, |
163 bool auto_advance) | 166 bool auto_advance) |
164 : URLRequestJob(request, network_delegate), | 167 : URLRequestJob(request, network_delegate), |
165 auto_advance_(auto_advance), | 168 auto_advance_(auto_advance), |
166 stage_(WAITING), | 169 stage_(WAITING), |
167 priority_(DEFAULT_PRIORITY), | 170 priority_(DEFAULT_PRIORITY), |
168 response_data_(response_data), | 171 response_data_(response_data), |
169 offset_(0), | 172 offset_(0), |
170 async_buf_(NULL), | 173 async_buf_(nullptr), |
171 async_buf_size_(0), | 174 async_buf_size_(0), |
172 response_headers_(new net::HttpResponseHeaders( | 175 response_headers_(new net::HttpResponseHeaders( |
173 net::HttpUtil::AssembleRawHeaders(response_headers.c_str(), | 176 net::HttpUtil::AssembleRawHeaders(response_headers.c_str(), |
174 response_headers.size()))), | 177 response_headers.size()))), |
175 response_headers_length_(response_headers.size()), | 178 response_headers_length_(response_headers.size()), |
| 179 async_reads_(false), |
176 weak_factory_(this) {} | 180 weak_factory_(this) {} |
177 | 181 |
178 URLRequestTestJob::~URLRequestTestJob() { | 182 URLRequestTestJob::~URLRequestTestJob() { |
179 g_pending_jobs.Get().erase( | 183 g_pending_jobs.Get().erase( |
180 std::remove( | 184 std::remove( |
181 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), | 185 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), |
182 g_pending_jobs.Get().end()); | 186 g_pending_jobs.Get().end()); |
183 } | 187 } |
184 | 188 |
185 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { | 189 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { |
(...skipping 11 matching lines...) Expand all Loading... |
197 // Start reading asynchronously so that all error reporting and data | 201 // Start reading asynchronously so that all error reporting and data |
198 // callbacks happen as they would for network requests. | 202 // callbacks happen as they would for network requests. |
199 base::ThreadTaskRunnerHandle::Get()->PostTask( | 203 base::ThreadTaskRunnerHandle::Get()->PostTask( |
200 FROM_HERE, | 204 FROM_HERE, |
201 base::Bind(&URLRequestTestJob::StartAsync, weak_factory_.GetWeakPtr())); | 205 base::Bind(&URLRequestTestJob::StartAsync, weak_factory_.GetWeakPtr())); |
202 } | 206 } |
203 | 207 |
204 void URLRequestTestJob::StartAsync() { | 208 void URLRequestTestJob::StartAsync() { |
205 if (!response_headers_.get()) { | 209 if (!response_headers_.get()) { |
206 SetResponseHeaders(test_headers()); | 210 SetResponseHeaders(test_headers()); |
207 if (request_->url().spec() == test_url_1().spec()) { | 211 if (request_->url() == test_url_1()) { |
208 response_data_ = test_data_1(); | 212 response_data_ = test_data_1(); |
209 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. | 213 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. |
210 } else if (request_->url().spec() == test_url_2().spec()) { | 214 } else if (request_->url() == test_url_2()) { |
211 response_data_ = test_data_2(); | 215 response_data_ = test_data_2(); |
212 } else if (request_->url().spec() == test_url_3().spec()) { | 216 } else if (request_->url() == test_url_3()) { |
213 response_data_ = test_data_3(); | 217 response_data_ = test_data_3(); |
214 } else if (request_->url().spec() == test_url_4().spec()) { | 218 } else if (request_->url() == test_url_4()) { |
215 response_data_ = test_data_4(); | 219 response_data_ = test_data_4(); |
216 } else if (request_->url().spec() == test_url_redirect_to_url_1().spec()) { | 220 } else if (request_->url() == test_url_auto_advance_async_reads_1()) { |
| 221 response_data_ = test_data_1(); |
| 222 stage_ = DATA_AVAILABLE; // Data is available immediately. |
| 223 async_reads_ = true; // All reads complete asynchronously. |
| 224 } else if (request_->url() == test_url_redirect_to_url_1()) { |
217 SetResponseHeaders(test_redirect_to_url_1_headers()); | 225 SetResponseHeaders(test_redirect_to_url_1_headers()); |
218 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { | 226 } else if (request_->url() == test_url_redirect_to_url_2()) { |
219 SetResponseHeaders(test_redirect_to_url_2_headers()); | 227 SetResponseHeaders(test_redirect_to_url_2_headers()); |
220 } else { | 228 } else { |
221 AdvanceJob(); | 229 AdvanceJob(); |
222 | 230 |
223 // unexpected url, return error | 231 // unexpected url, return error |
224 // FIXME(brettw) we may want to use WININET errors or have some more types | 232 // FIXME(brettw) we may want to use WININET errors or have some more types |
225 // of errors | 233 // of errors |
226 NotifyStartError( | 234 NotifyStartError( |
227 URLRequestStatus(URLRequestStatus::FAILED, ERR_INVALID_URL)); | 235 URLRequestStatus(URLRequestStatus::FAILED, ERR_INVALID_URL)); |
228 // FIXME(brettw): this should emulate a network error, and not just fail | 236 // FIXME(brettw): this should emulate a network error, and not just fail |
229 // initiating a connection | 237 // initiating a connection |
230 return; | 238 return; |
231 } | 239 } |
232 } | 240 } |
233 | 241 |
234 AdvanceJob(); | 242 AdvanceJob(); |
235 | 243 |
236 this->NotifyHeadersComplete(); | 244 this->NotifyHeadersComplete(); |
237 } | 245 } |
238 | 246 |
239 void URLRequestTestJob::SetResponseHeaders( | 247 void URLRequestTestJob::SetResponseHeaders( |
240 const std::string& response_headers) { | 248 const std::string& response_headers) { |
241 response_headers_ = new HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( | 249 response_headers_ = new HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
242 response_headers.c_str(), response_headers.size())); | 250 response_headers.c_str(), response_headers.size())); |
243 response_headers_length_ = response_headers.size(); | 251 response_headers_length_ = response_headers.size(); |
244 } | 252 } |
245 | 253 |
| 254 int URLRequestTestJob::CopyDataForRead(IOBuffer* buf, int buf_size) { |
| 255 int bytes_read = 0; |
| 256 if (offset_ < static_cast<int>(response_data_.length())) { |
| 257 bytes_read = buf_size; |
| 258 if (bytes_read + offset_ > static_cast<int>(response_data_.length())) |
| 259 bytes_read = static_cast<int>(response_data_.length()) - offset_; |
| 260 |
| 261 memcpy(buf->data(), &response_data_.c_str()[offset_], bytes_read); |
| 262 offset_ += bytes_read; |
| 263 } |
| 264 return bytes_read; |
| 265 } |
| 266 |
246 int URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size) { | 267 int URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size) { |
247 if (stage_ == WAITING) { | 268 if (stage_ == WAITING || async_reads_) { |
248 async_buf_ = buf; | 269 async_buf_ = buf; |
249 async_buf_size_ = buf_size; | 270 async_buf_size_ = buf_size; |
| 271 if (stage_ != WAITING) { |
| 272 stage_ = WAITING; |
| 273 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 274 FROM_HERE, base::Bind(&URLRequestTestJob::ProcessNextOperation, |
| 275 weak_factory_.GetWeakPtr())); |
| 276 } |
250 return ERR_IO_PENDING; | 277 return ERR_IO_PENDING; |
251 } | 278 } |
252 | 279 |
253 if (offset_ >= static_cast<int>(response_data_.length())) | 280 return CopyDataForRead(buf, buf_size); |
254 return 0; // done reading | |
255 | |
256 int to_read = buf_size; | |
257 if (to_read + offset_ > static_cast<int>(response_data_.length())) | |
258 to_read = static_cast<int>(response_data_.length()) - offset_; | |
259 | |
260 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); | |
261 offset_ += to_read; | |
262 | |
263 return to_read; | |
264 } | 281 } |
265 | 282 |
266 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { | 283 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { |
267 if (response_headers_.get()) | 284 if (response_headers_.get()) |
268 info->headers = response_headers_; | 285 info->headers = response_headers_; |
269 } | 286 } |
270 | 287 |
271 void URLRequestTestJob::GetLoadTimingInfo( | 288 void URLRequestTestJob::GetLoadTimingInfo( |
272 LoadTimingInfo* load_timing_info) const { | 289 LoadTimingInfo* load_timing_info) const { |
273 // Preserve the times the URLRequest is responsible for, but overwrite all | 290 // Preserve the times the URLRequest is responsible for, but overwrite all |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 | 332 |
316 void URLRequestTestJob::ProcessNextOperation() { | 333 void URLRequestTestJob::ProcessNextOperation() { |
317 switch (stage_) { | 334 switch (stage_) { |
318 case WAITING: | 335 case WAITING: |
319 // Must call AdvanceJob() prior to NotifyReadComplete() since that may | 336 // Must call AdvanceJob() prior to NotifyReadComplete() since that may |
320 // delete |this|. | 337 // delete |this|. |
321 AdvanceJob(); | 338 AdvanceJob(); |
322 stage_ = DATA_AVAILABLE; | 339 stage_ = DATA_AVAILABLE; |
323 // OK if ReadRawData wasn't called yet. | 340 // OK if ReadRawData wasn't called yet. |
324 if (async_buf_) { | 341 if (async_buf_) { |
325 int result = ReadRawData(async_buf_, async_buf_size_); | 342 int result = CopyDataForRead(async_buf_, async_buf_size_); |
326 if (result < 0) | 343 if (result < 0) |
327 NOTREACHED() << "Reads should not fail in DATA_AVAILABLE."; | 344 NOTREACHED() << "Reads should not fail in DATA_AVAILABLE."; |
328 if (NextReadAsync()) { | 345 if (NextReadAsync()) { |
329 // Make all future reads return io pending until the next | 346 // Make all future reads return io pending until the next |
330 // ProcessNextOperation(). | 347 // ProcessNextOperation(). |
331 stage_ = WAITING; | 348 stage_ = WAITING; |
332 } | 349 } |
333 ReadRawDataComplete(result); | 350 ReadRawDataComplete(result); |
334 } | 351 } |
335 break; | 352 break; |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 | 386 |
370 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); | 387 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); |
371 g_pending_jobs.Get().pop_front(); | 388 g_pending_jobs.Get().pop_front(); |
372 | 389 |
373 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q | 390 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q |
374 next_job->ProcessNextOperation(); | 391 next_job->ProcessNextOperation(); |
375 return true; | 392 return true; |
376 } | 393 } |
377 | 394 |
378 } // namespace net | 395 } // namespace net |
OLD | NEW |