Chromium Code Reviews| Index: net/url_request/url_request_test_job.cc |
| diff --git a/net/url_request/url_request_test_job.cc b/net/url_request/url_request_test_job.cc |
| index 42e8c68fb1d85435e7e05e4ab65aa35f5138de26..238e5d6944a34c74bd16db54b36713da584f8bfc 100644 |
| --- a/net/url_request/url_request_test_job.cc |
| +++ b/net/url_request/url_request_test_job.cc |
| @@ -44,21 +44,31 @@ class TestJobProtocolHandler : public URLRequestJobFactory::ProtocolHandler { |
| GURL URLRequestTestJob::test_url_1() { |
| return GURL("test:url1"); |
| } |
| + |
| GURL URLRequestTestJob::test_url_2() { |
| return GURL("test:url2"); |
| } |
| + |
| GURL URLRequestTestJob::test_url_3() { |
| return GURL("test:url3"); |
| } |
| + |
| GURL URLRequestTestJob::test_url_4() { |
| return GURL("test:url4"); |
| } |
| + |
| +GURL URLRequestTestJob::test_url_auto_advance_async_reads_1() { |
| + return GURL("test:url_auto_advance_async_reads_1"); |
| +} |
| + |
| GURL URLRequestTestJob::test_url_error() { |
| return GURL("test:error"); |
| } |
| + |
| GURL URLRequestTestJob::test_url_redirect_to_url_1() { |
| return GURL("test:redirect_to_1"); |
| } |
| + |
| GURL URLRequestTestJob::test_url_redirect_to_url_2() { |
| return GURL("test:redirect_to_2"); |
| } |
| @@ -133,15 +143,7 @@ URLRequestTestJob::CreateProtocolHandler() { |
| URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| NetworkDelegate* network_delegate) |
| - : URLRequestJob(request, network_delegate), |
| - auto_advance_(false), |
| - stage_(WAITING), |
| - priority_(DEFAULT_PRIORITY), |
| - offset_(0), |
| - async_buf_(NULL), |
| - async_buf_size_(0), |
| - response_headers_length_(0), |
| - weak_factory_(this) {} |
| + : URLRequestTestJob(request, network_delegate, false) {} |
| URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| NetworkDelegate* network_delegate, |
| @@ -154,6 +156,7 @@ URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| async_buf_(NULL), |
| async_buf_size_(0), |
| response_headers_length_(0), |
| + async_reads_(false), |
| weak_factory_(this) {} |
| URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| @@ -167,12 +170,13 @@ URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| priority_(DEFAULT_PRIORITY), |
| response_data_(response_data), |
| offset_(0), |
| - async_buf_(NULL), |
| + async_buf_(nullptr), |
| async_buf_size_(0), |
| response_headers_(new net::HttpResponseHeaders( |
| net::HttpUtil::AssembleRawHeaders(response_headers.c_str(), |
| response_headers.size()))), |
| response_headers_length_(response_headers.size()), |
| + async_reads_(false), |
| weak_factory_(this) {} |
| URLRequestTestJob::~URLRequestTestJob() { |
| @@ -204,18 +208,22 @@ void URLRequestTestJob::Start() { |
| void URLRequestTestJob::StartAsync() { |
| if (!response_headers_.get()) { |
| SetResponseHeaders(test_headers()); |
| - if (request_->url().spec() == test_url_1().spec()) { |
| + if (request_->url() == test_url_1()) { |
| response_data_ = test_data_1(); |
| stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. |
| - } else if (request_->url().spec() == test_url_2().spec()) { |
| + } else if (request_->url() == test_url_2()) { |
| response_data_ = test_data_2(); |
| - } else if (request_->url().spec() == test_url_3().spec()) { |
| + } else if (request_->url() == test_url_3()) { |
| response_data_ = test_data_3(); |
| - } else if (request_->url().spec() == test_url_4().spec()) { |
| + } else if (request_->url() == test_url_4()) { |
| response_data_ = test_data_4(); |
| - } else if (request_->url().spec() == test_url_redirect_to_url_1().spec()) { |
| + } else if (request_->url() == test_url_auto_advance_async_reads_1()) { |
| + response_data_ = test_data_1(); |
| + stage_ = DATA_AVAILABLE; // Data is available immediately. |
| + async_reads_ = true; // All reads complete asynchronously. |
| + } else if (request_->url() == test_url_redirect_to_url_1()) { |
| SetResponseHeaders(test_redirect_to_url_1_headers()); |
| - } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { |
| + } else if (request_->url() == test_url_redirect_to_url_2()) { |
| SetResponseHeaders(test_redirect_to_url_2_headers()); |
| } else { |
| AdvanceJob(); |
| @@ -250,17 +258,23 @@ int URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size) { |
| return ERR_IO_PENDING; |
| } |
| - if (offset_ >= static_cast<int>(response_data_.length())) |
| - return 0; // done reading |
| + int bytes_read = 0; |
| + if (offset_ < static_cast<int>(response_data_.length())) { |
| + bytes_read = buf_size; |
| + if (bytes_read + offset_ > static_cast<int>(response_data_.length())) |
| + bytes_read = static_cast<int>(response_data_.length()) - offset_; |
| - int to_read = buf_size; |
| - if (to_read + offset_ > static_cast<int>(response_data_.length())) |
| - to_read = static_cast<int>(response_data_.length()) - offset_; |
| - |
| - memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); |
| - offset_ += to_read; |
| + memcpy(buf->data(), &response_data_.c_str()[offset_], bytes_read); |
| + offset_ += bytes_read; |
| + } |
| - return to_read; |
| + if (async_reads_) { |
| + base::ThreadTaskRunnerHandle::Get()->PostTask( |
| + FROM_HERE, base::Bind(&URLRequestTestJob::ReadRawDataComplete, |
| + 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
|
| + return ERR_IO_PENDING; |
| + } |
| + return bytes_read; |
| } |
| void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { |