Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 <vector> | 5 #include <vector> |
| 6 | 6 |
| 7 #include "net/url_request/url_request_test_job.h" | 7 #include "net/url_request/url_request_test_job.h" |
| 8 | 8 |
| 9 #include "base/message_loop.h" | 9 #include "base/message_loop.h" |
| 10 #include "base/string_util.h" | 10 #include "base/string_util.h" |
| 11 #include "net/base/io_buffer.h" | 11 #include "net/base/io_buffer.h" |
| 12 #include "net/base/net_errors.h" | 12 #include "net/base/net_errors.h" |
| 13 #include "net/http/http_response_headers.h" | 13 #include "net/http/http_response_headers.h" |
| 14 #include "net/url_request/url_request.h" | 14 #include "net/url_request/url_request.h" |
| 15 | 15 |
| 16 // This emulates the global message loop for the test URL request class, since | 16 // This emulates the global message loop for the test URL request class, since |
| 17 // this is only test code, it's probably not too dangerous to have this static | 17 // this is only test code, it's probably not too dangerous to have this static |
| 18 // object. | 18 // object. |
| 19 static std::vector< scoped_refptr<URLRequestTestJob> > pending_jobs; | 19 static std::vector< scoped_refptr<URLRequestTestJob> > pending_jobs; |
|
wtc
2011/01/05 00:19:41
Nit: add a g_ prefix to the 'pending_jobs' variabl
tfarina
2011/01/05 01:09:59
Done.
| |
| 20 | 20 |
| 21 namespace net { | |
| 22 | |
| 21 // static getters for known URLs | 23 // static getters for known URLs |
| 22 GURL URLRequestTestJob::test_url_1() { | 24 GURL URLRequestTestJob::test_url_1() { |
| 23 return GURL("test:url1"); | 25 return GURL("test:url1"); |
| 24 } | 26 } |
| 25 GURL URLRequestTestJob::test_url_2() { | 27 GURL URLRequestTestJob::test_url_2() { |
| 26 return GURL("test:url2"); | 28 return GURL("test:url2"); |
| 27 } | 29 } |
| 28 GURL URLRequestTestJob::test_url_3() { | 30 GURL URLRequestTestJob::test_url_3() { |
| 29 return GURL("test:url3"); | 31 return GURL("test:url3"); |
| 30 } | 32 } |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 63 | 65 |
| 64 // static getter for error response headers | 66 // static getter for error response headers |
| 65 std::string URLRequestTestJob::test_error_headers() { | 67 std::string URLRequestTestJob::test_error_headers() { |
| 66 const char headers[] = | 68 const char headers[] = |
| 67 "HTTP/1.1 500 BOO HOO\0" | 69 "HTTP/1.1 500 BOO HOO\0" |
| 68 "\0"; | 70 "\0"; |
| 69 return std::string(headers, arraysize(headers)); | 71 return std::string(headers, arraysize(headers)); |
| 70 } | 72 } |
| 71 | 73 |
| 72 // static | 74 // static |
| 73 net::URLRequestJob* URLRequestTestJob::Factory(net::URLRequest* request, | 75 URLRequestJob* URLRequestTestJob::Factory(URLRequest* request, |
| 74 const std::string& scheme) { | 76 const std::string& scheme) { |
| 75 return new URLRequestTestJob(request); | 77 return new URLRequestTestJob(request); |
| 76 } | 78 } |
| 77 | 79 |
| 78 URLRequestTestJob::URLRequestTestJob(net::URLRequest* request) | 80 URLRequestTestJob::URLRequestTestJob(URLRequest* request) |
| 79 : net::URLRequestJob(request), | 81 : URLRequestJob(request), |
| 80 auto_advance_(false), | 82 auto_advance_(false), |
| 81 stage_(WAITING), | 83 stage_(WAITING), |
| 82 offset_(0), | 84 offset_(0), |
| 83 async_buf_(NULL), | 85 async_buf_(NULL), |
| 84 async_buf_size_(0) { | 86 async_buf_size_(0) { |
| 85 } | 87 } |
| 86 | 88 |
| 87 URLRequestTestJob::URLRequestTestJob(net::URLRequest* request, | 89 URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| 88 bool auto_advance) | 90 bool auto_advance) |
| 89 : net::URLRequestJob(request), | 91 : URLRequestJob(request), |
| 90 auto_advance_(auto_advance), | 92 auto_advance_(auto_advance), |
| 91 stage_(WAITING), | 93 stage_(WAITING), |
| 92 offset_(0), | 94 offset_(0), |
| 93 async_buf_(NULL), | 95 async_buf_(NULL), |
| 94 async_buf_size_(0) { | 96 async_buf_size_(0) { |
| 95 } | 97 } |
| 96 | 98 |
| 97 URLRequestTestJob::URLRequestTestJob(net::URLRequest* request, | 99 URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| 98 const std::string& response_headers, | 100 const std::string& response_headers, |
| 99 const std::string& response_data, | 101 const std::string& response_data, |
| 100 bool auto_advance) | 102 bool auto_advance) |
| 101 : net::URLRequestJob(request), | 103 : URLRequestJob(request), |
| 102 auto_advance_(auto_advance), | 104 auto_advance_(auto_advance), |
| 103 stage_(WAITING), | 105 stage_(WAITING), |
| 104 response_headers_(new net::HttpResponseHeaders(response_headers)), | 106 response_headers_(new HttpResponseHeaders(response_headers)), |
| 105 response_data_(response_data), | 107 response_data_(response_data), |
| 106 offset_(0), | 108 offset_(0), |
| 107 async_buf_(NULL), | 109 async_buf_(NULL), |
| 108 async_buf_size_(0) { | 110 async_buf_size_(0) { |
| 109 } | 111 } |
| 110 | 112 |
| 111 URLRequestTestJob::~URLRequestTestJob() { | 113 URLRequestTestJob::~URLRequestTestJob() { |
| 112 } | 114 } |
| 113 | 115 |
| 114 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { | 116 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { |
| 115 DCHECK(mime_type); | 117 DCHECK(mime_type); |
| 116 if (!response_headers_) | 118 if (!response_headers_) |
| 117 return false; | 119 return false; |
| 118 return response_headers_->GetMimeType(mime_type); | 120 return response_headers_->GetMimeType(mime_type); |
| 119 } | 121 } |
| 120 | 122 |
| 121 void URLRequestTestJob::Start() { | 123 void URLRequestTestJob::Start() { |
| 122 // Start reading asynchronously so that all error reporting and data | 124 // Start reading asynchronously so that all error reporting and data |
| 123 // callbacks happen as they would for network requests. | 125 // callbacks happen as they would for network requests. |
| 124 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( | 126 MessageLoop::current()->PostTask(FROM_HERE, NewRunnableMethod( |
| 125 this, &URLRequestTestJob::StartAsync)); | 127 this, &URLRequestTestJob::StartAsync)); |
| 126 } | 128 } |
| 127 | 129 |
| 128 void URLRequestTestJob::StartAsync() { | 130 void URLRequestTestJob::StartAsync() { |
| 129 if (!response_headers_) { | 131 if (!response_headers_) { |
| 130 response_headers_ = new net::HttpResponseHeaders(test_headers()); | 132 response_headers_ = new HttpResponseHeaders(test_headers()); |
| 131 if (request_->url().spec() == test_url_1().spec()) { | 133 if (request_->url().spec() == test_url_1().spec()) { |
| 132 response_data_ = test_data_1(); | 134 response_data_ = test_data_1(); |
| 133 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. | 135 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. |
| 134 } else if (request_->url().spec() == test_url_2().spec()) { | 136 } else if (request_->url().spec() == test_url_2().spec()) { |
| 135 response_data_ = test_data_2(); | 137 response_data_ = test_data_2(); |
| 136 } else if (request_->url().spec() == test_url_3().spec()) { | 138 } else if (request_->url().spec() == test_url_3().spec()) { |
| 137 response_data_ = test_data_3(); | 139 response_data_ = test_data_3(); |
| 138 } else { | 140 } else { |
| 139 // unexpected url, return error | 141 // unexpected url, return error |
| 140 // FIXME(brettw) we may want to use WININET errors or have some more types | 142 // FIXME(brettw) we may want to use WININET errors or have some more types |
| 141 // of errors | 143 // of errors |
| 142 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 144 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 143 net::ERR_INVALID_URL)); | 145 ERR_INVALID_URL)); |
| 144 // FIXME(brettw): this should emulate a network error, and not just fail | 146 // FIXME(brettw): this should emulate a network error, and not just fail |
| 145 // initiating a connection | 147 // initiating a connection |
| 146 return; | 148 return; |
| 147 } | 149 } |
| 148 } | 150 } |
| 149 | 151 |
| 150 AdvanceJob(); | 152 AdvanceJob(); |
| 151 | 153 |
| 152 this->NotifyHeadersComplete(); | 154 this->NotifyHeadersComplete(); |
| 153 } | 155 } |
| 154 | 156 |
| 155 bool URLRequestTestJob::ReadRawData(net::IOBuffer* buf, int buf_size, | 157 bool URLRequestTestJob::ReadRawData(IOBuffer* buf, int buf_size, |
| 156 int *bytes_read) { | 158 int *bytes_read) { |
| 157 if (stage_ == WAITING) { | 159 if (stage_ == WAITING) { |
| 158 async_buf_ = buf; | 160 async_buf_ = buf; |
| 159 async_buf_size_ = buf_size; | 161 async_buf_size_ = buf_size; |
| 160 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); | 162 SetStatus(URLRequestStatus(URLRequestStatus::IO_PENDING, 0)); |
| 161 return false; | 163 return false; |
| 162 } | 164 } |
| 163 | 165 |
| 164 DCHECK(bytes_read); | 166 DCHECK(bytes_read); |
| 165 *bytes_read = 0; | 167 *bytes_read = 0; |
| 166 | 168 |
| 167 if (offset_ >= static_cast<int>(response_data_.length())) { | 169 if (offset_ >= static_cast<int>(response_data_.length())) { |
| 168 return true; // done reading | 170 return true; // done reading |
| 169 } | 171 } |
| 170 | 172 |
| 171 int to_read = buf_size; | 173 int to_read = buf_size; |
| 172 if (to_read + offset_ > static_cast<int>(response_data_.length())) | 174 if (to_read + offset_ > static_cast<int>(response_data_.length())) |
| 173 to_read = static_cast<int>(response_data_.length()) - offset_; | 175 to_read = static_cast<int>(response_data_.length()) - offset_; |
| 174 | 176 |
| 175 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); | 177 memcpy(buf->data(), &response_data_.c_str()[offset_], to_read); |
| 176 offset_ += to_read; | 178 offset_ += to_read; |
| 177 | 179 |
| 178 *bytes_read = to_read; | 180 *bytes_read = to_read; |
| 179 return true; | 181 return true; |
| 180 } | 182 } |
| 181 | 183 |
| 182 void URLRequestTestJob::GetResponseInfo(net::HttpResponseInfo* info) { | 184 void URLRequestTestJob::GetResponseInfo(HttpResponseInfo* info) { |
| 183 if (response_headers_) | 185 if (response_headers_) |
| 184 info->headers = response_headers_; | 186 info->headers = response_headers_; |
| 185 } | 187 } |
| 186 | 188 |
| 187 int URLRequestTestJob::GetResponseCode() const { | 189 int URLRequestTestJob::GetResponseCode() const { |
| 188 if (response_headers_) | 190 if (response_headers_) |
| 189 return response_headers_->response_code(); | 191 return response_headers_->response_code(); |
| 190 return -1; | 192 return -1; |
| 191 } | 193 } |
| 192 | 194 |
| 193 bool URLRequestTestJob::IsRedirectResponse(GURL* location, | 195 bool URLRequestTestJob::IsRedirectResponse(GURL* location, |
| 194 int* http_status_code) { | 196 int* http_status_code) { |
| 195 if (!response_headers_) | 197 if (!response_headers_) |
| 196 return false; | 198 return false; |
| 197 | 199 |
| 198 std::string value; | 200 std::string value; |
| 199 if (!response_headers_->IsRedirect(&value)) | 201 if (!response_headers_->IsRedirect(&value)) |
| 200 return false; | 202 return false; |
| 201 | 203 |
| 202 *location = request_->url().Resolve(value); | 204 *location = request_->url().Resolve(value); |
| 203 *http_status_code = response_headers_->response_code(); | 205 *http_status_code = response_headers_->response_code(); |
| 204 return true; | 206 return true; |
| 205 } | 207 } |
| 206 | 208 |
| 207 | 209 |
| 208 void URLRequestTestJob::Kill() { | 210 void URLRequestTestJob::Kill() { |
| 209 stage_ = DONE; | 211 stage_ = DONE; |
| 210 net::URLRequestJob::Kill(); | 212 URLRequestJob::Kill(); |
| 211 } | 213 } |
| 212 | 214 |
| 213 void URLRequestTestJob::ProcessNextOperation() { | 215 void URLRequestTestJob::ProcessNextOperation() { |
| 214 switch (stage_) { | 216 switch (stage_) { |
| 215 case WAITING: | 217 case WAITING: |
| 216 stage_ = DATA_AVAILABLE; | 218 stage_ = DATA_AVAILABLE; |
| 217 // OK if ReadRawData wasn't called yet. | 219 // OK if ReadRawData wasn't called yet. |
| 218 if (async_buf_) { | 220 if (async_buf_) { |
| 219 int bytes_read; | 221 int bytes_read; |
| 220 if (!ReadRawData(async_buf_, async_buf_size_, &bytes_read)) | 222 if (!ReadRawData(async_buf_, async_buf_size_, &bytes_read)) |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 252 if (pending_jobs.empty()) | 254 if (pending_jobs.empty()) |
| 253 return false; | 255 return false; |
| 254 | 256 |
| 255 scoped_refptr<URLRequestTestJob> next_job(pending_jobs[0]); | 257 scoped_refptr<URLRequestTestJob> next_job(pending_jobs[0]); |
| 256 pending_jobs.erase(pending_jobs.begin()); | 258 pending_jobs.erase(pending_jobs.begin()); |
| 257 | 259 |
| 258 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q | 260 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q |
| 259 next_job->ProcessNextOperation(); | 261 next_job->ProcessNextOperation(); |
| 260 return true; | 262 return true; |
| 261 } | 263 } |
| 264 | |
| 265 } // namespace net | |
| OLD | NEW |