Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(508)

Side by Side Diff: net/url_request/url_request_test_job.cc

Issue 2552463002: Add more ResourceLoaderTests (Closed)
Patch Set: Merge Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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
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
(...skipping 14 matching lines...) Expand all
243 response_headers_length_ = response_headers.size(); 251 response_headers_length_ = response_headers.size();
244 } 252 }
245 253
246 int URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size) { 254 int URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size) {
247 if (stage_ == WAITING) { 255 if (stage_ == WAITING) {
248 async_buf_ = buf; 256 async_buf_ = buf;
249 async_buf_size_ = buf_size; 257 async_buf_size_ = buf_size;
250 return ERR_IO_PENDING; 258 return ERR_IO_PENDING;
251 } 259 }
252 260
253 if (offset_ >= static_cast<int>(response_data_.length())) 261 int bytes_read = 0;
254 return 0; // done reading 262 if (offset_ < static_cast<int>(response_data_.length())) {
263 bytes_read = buf_size;
264 if (bytes_read + offset_ > static_cast<int>(response_data_.length()))
265 bytes_read = static_cast<int>(response_data_.length()) - offset_;
255 266
256 int to_read = buf_size; 267 memcpy(buf->data(), &response_data_.c_str()[offset_], bytes_read);
257 if (to_read + offset_ > static_cast<int>(response_data_.length())) 268 offset_ += bytes_read;
258 to_read = static_cast<int>(response_data_.length()) - offset_; 269 }
259 270
260 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); 271 if (async_reads_) {
261 offset_ += to_read; 272 base::ThreadTaskRunnerHandle::Get()->PostTask(
262 273 FROM_HERE, base::Bind(&URLRequestTestJob::ReadRawDataComplete,
263 return to_read; 274 weak_factory_.GetWeakPtr(), bytes_read));
Randy Smith (Not in Mondays) 2016/12/08 22:15:09 I assume you thought about whether there were poss
mmenke 2016/12/08 22:46:37 This is a valid concern - I've suggested it to oth
275 return ERR_IO_PENDING;
276 }
277 return bytes_read;
264 } 278 }
265 279
266 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { 280 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) {
267 if (response_headers_.get()) 281 if (response_headers_.get())
268 info->headers = response_headers_; 282 info->headers = response_headers_;
269 } 283 }
270 284
271 void URLRequestTestJob::GetLoadTimingInfo( 285 void URLRequestTestJob::GetLoadTimingInfo(
272 LoadTimingInfo* load_timing_info) const { 286 LoadTimingInfo* load_timing_info) const {
273 // Preserve the times the URLRequest is responsible for, but overwrite all 287 // Preserve the times the URLRequest is responsible for, but overwrite all
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 383
370 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); 384 URLRequestTestJob* next_job(g_pending_jobs.Get().front());
371 g_pending_jobs.Get().pop_front(); 385 g_pending_jobs.Get().pop_front();
372 386
373 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q 387 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q
374 next_job->ProcessNextOperation(); 388 next_job->ProcessNextOperation();
375 return true; 389 return true;
376 } 390 }
377 391
378 } // namespace net 392 } // namespace net
OLDNEW
« net/url_request/url_request_test_job.h ('K') | « net/url_request/url_request_test_job.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698