| 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" |
| 11 #include "base/compiler_specific.h" | 11 #include "base/compiler_specific.h" |
| 12 #include "base/lazy_instance.h" | 12 #include "base/lazy_instance.h" |
| 13 #include "base/location.h" | 13 #include "base/location.h" |
| 14 #include "base/single_thread_task_runner.h" | 14 #include "base/single_thread_task_runner.h" |
| 15 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 16 #include "base/thread_task_runner_handle.h" | 16 #include "base/thread_task_runner_handle.h" |
| 17 #include "net/base/io_buffer.h" | 17 #include "net/base/io_buffer.h" |
| 18 #include "net/base/net_errors.h" | 18 #include "net/base/net_errors.h" |
| 19 #include "net/http/http_response_headers.h" | 19 #include "net/http/http_response_headers.h" |
| 20 #include "net/http/http_util.h" |
| 20 | 21 |
| 21 namespace net { | 22 namespace net { |
| 22 | 23 |
| 23 namespace { | 24 namespace { |
| 24 | 25 |
| 25 typedef std::list<URLRequestTestJob*> URLRequestJobList; | 26 typedef std::list<URLRequestTestJob*> URLRequestJobList; |
| 26 base::LazyInstance<URLRequestJobList>::Leaky | 27 base::LazyInstance<URLRequestJobList>::Leaky |
| 27 g_pending_jobs = LAZY_INSTANCE_INITIALIZER; | 28 g_pending_jobs = LAZY_INSTANCE_INITIALIZER; |
| 28 | 29 |
| 29 class TestJobProtocolHandler : public URLRequestJobFactory::ProtocolHandler { | 30 class TestJobProtocolHandler : public URLRequestJobFactory::ProtocolHandler { |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 68 std::string URLRequestTestJob::test_data_3() { | 69 std::string URLRequestTestJob::test_data_3() { |
| 69 return std::string("<html><title>Test Three Three Three</title></html>"); | 70 return std::string("<html><title>Test Three Three Three</title></html>"); |
| 70 } | 71 } |
| 71 std::string URLRequestTestJob::test_data_4() { | 72 std::string URLRequestTestJob::test_data_4() { |
| 72 return std::string("<html><title>Test Four Four Four Four</title></html>"); | 73 return std::string("<html><title>Test Four Four Four Four</title></html>"); |
| 73 } | 74 } |
| 74 | 75 |
| 75 // static getter for simple response headers | 76 // static getter for simple response headers |
| 76 std::string URLRequestTestJob::test_headers() { | 77 std::string URLRequestTestJob::test_headers() { |
| 77 static const char kHeaders[] = | 78 static const char kHeaders[] = |
| 78 "HTTP/1.1 200 OK\0" | 79 "HTTP/1.1 200 OK\n" |
| 79 "Content-type: text/html\0" | 80 "Content-type: text/html\n" |
| 80 "\0"; | 81 "\n"; |
| 81 return std::string(kHeaders, arraysize(kHeaders)); | 82 return std::string(kHeaders, arraysize(kHeaders)); |
| 82 } | 83 } |
| 83 | 84 |
| 84 // static getter for redirect response headers | 85 // static getter for redirect response headers |
| 85 std::string URLRequestTestJob::test_redirect_headers() { | 86 std::string URLRequestTestJob::test_redirect_headers() { |
| 86 static const char kHeaders[] = | 87 static const char kHeaders[] = |
| 87 "HTTP/1.1 302 MOVED\0" | 88 "HTTP/1.1 302 MOVED\n" |
| 88 "Location: somewhere\0" | 89 "Location: somewhere\n" |
| 89 "\0"; | 90 "\n"; |
| 90 return std::string(kHeaders, arraysize(kHeaders)); | 91 return std::string(kHeaders, arraysize(kHeaders)); |
| 91 } | 92 } |
| 92 | 93 |
| 93 // static getter for redirect response headers | 94 // static getter for redirect response headers |
| 94 std::string URLRequestTestJob::test_redirect_to_url_2_headers() { | 95 std::string URLRequestTestJob::test_redirect_to_url_2_headers() { |
| 95 std::string headers = "HTTP/1.1 302 MOVED"; | 96 std::string headers = "HTTP/1.1 302 MOVED"; |
| 96 headers.push_back('\0'); | 97 headers.push_back('\n'); |
| 97 headers += "Location: "; | 98 headers += "Location: "; |
| 98 headers += test_url_2().spec(); | 99 headers += test_url_2().spec(); |
| 99 headers.push_back('\0'); | 100 headers.push_back('\n'); |
| 100 headers.push_back('\0'); | 101 headers.push_back('\n'); |
| 101 return headers; | 102 return headers; |
| 102 } | 103 } |
| 103 | 104 |
| 104 // static getter for error response headers | 105 // static getter for error response headers |
| 105 std::string URLRequestTestJob::test_error_headers() { | 106 std::string URLRequestTestJob::test_error_headers() { |
| 106 static const char kHeaders[] = | 107 static const char kHeaders[] = |
| 107 "HTTP/1.1 500 BOO HOO\0" | 108 "HTTP/1.1 500 BOO HOO\n" |
| 108 "\0"; | 109 "\n"; |
| 109 return std::string(kHeaders, arraysize(kHeaders)); | 110 return std::string(kHeaders, arraysize(kHeaders)); |
| 110 } | 111 } |
| 111 | 112 |
| 112 // static | 113 // static |
| 113 scoped_ptr<URLRequestJobFactory::ProtocolHandler> | 114 scoped_ptr<URLRequestJobFactory::ProtocolHandler> |
| 114 URLRequestTestJob::CreateProtocolHandler() { | 115 URLRequestTestJob::CreateProtocolHandler() { |
| 115 return make_scoped_ptr(new TestJobProtocolHandler()); | 116 return make_scoped_ptr(new TestJobProtocolHandler()); |
| 116 } | 117 } |
| 117 | 118 |
| 118 URLRequestTestJob::URLRequestTestJob(URLRequest* request, | 119 URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| (...skipping 23 matching lines...) Expand all Loading... |
| 142 | 143 |
| 143 URLRequestTestJob::URLRequestTestJob(URLRequest* request, | 144 URLRequestTestJob::URLRequestTestJob(URLRequest* request, |
| 144 NetworkDelegate* network_delegate, | 145 NetworkDelegate* network_delegate, |
| 145 const std::string& response_headers, | 146 const std::string& response_headers, |
| 146 const std::string& response_data, | 147 const std::string& response_data, |
| 147 bool auto_advance) | 148 bool auto_advance) |
| 148 : URLRequestJob(request, network_delegate), | 149 : URLRequestJob(request, network_delegate), |
| 149 auto_advance_(auto_advance), | 150 auto_advance_(auto_advance), |
| 150 stage_(WAITING), | 151 stage_(WAITING), |
| 151 priority_(DEFAULT_PRIORITY), | 152 priority_(DEFAULT_PRIORITY), |
| 152 response_headers_(new HttpResponseHeaders(response_headers)), | 153 response_headers_(new net::HttpResponseHeaders( |
| 154 net::HttpUtil::AssembleRawHeaders(response_headers.c_str(), |
| 155 response_headers.size()))), |
| 153 response_data_(response_data), | 156 response_data_(response_data), |
| 154 offset_(0), | 157 offset_(0), |
| 155 async_buf_(NULL), | 158 async_buf_(NULL), |
| 156 async_buf_size_(0), | 159 async_buf_size_(0), |
| 157 weak_factory_(this) { | 160 weak_factory_(this) {} |
| 158 } | |
| 159 | 161 |
| 160 URLRequestTestJob::~URLRequestTestJob() { | 162 URLRequestTestJob::~URLRequestTestJob() { |
| 161 g_pending_jobs.Get().erase( | 163 g_pending_jobs.Get().erase( |
| 162 std::remove( | 164 std::remove( |
| 163 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), | 165 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), |
| 164 g_pending_jobs.Get().end()); | 166 g_pending_jobs.Get().end()); |
| 165 } | 167 } |
| 166 | 168 |
| 167 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { | 169 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { |
| 168 DCHECK(mime_type); | 170 DCHECK(mime_type); |
| 169 if (!response_headers_.get()) | 171 if (!response_headers_.get()) |
| 170 return false; | 172 return false; |
| 171 return response_headers_->GetMimeType(mime_type); | 173 return response_headers_->GetMimeType(mime_type); |
| 172 } | 174 } |
| 173 | 175 |
| 174 void URLRequestTestJob::SetPriority(RequestPriority priority) { | 176 void URLRequestTestJob::SetPriority(RequestPriority priority) { |
| 175 priority_ = priority; | 177 priority_ = priority; |
| 176 } | 178 } |
| 177 | 179 |
| 178 void URLRequestTestJob::Start() { | 180 void URLRequestTestJob::Start() { |
| 179 // Start reading asynchronously so that all error reporting and data | 181 // Start reading asynchronously so that all error reporting and data |
| 180 // callbacks happen as they would for network requests. | 182 // callbacks happen as they would for network requests. |
| 181 base::ThreadTaskRunnerHandle::Get()->PostTask( | 183 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 182 FROM_HERE, | 184 FROM_HERE, |
| 183 base::Bind(&URLRequestTestJob::StartAsync, weak_factory_.GetWeakPtr())); | 185 base::Bind(&URLRequestTestJob::StartAsync, weak_factory_.GetWeakPtr())); |
| 184 } | 186 } |
| 185 | 187 |
| 186 void URLRequestTestJob::StartAsync() { | 188 void URLRequestTestJob::StartAsync() { |
| 187 if (!response_headers_.get()) { | 189 if (!response_headers_.get()) { |
| 188 response_headers_ = new HttpResponseHeaders(test_headers()); | 190 std::string headers = test_headers(); |
| 191 response_headers_ = new HttpResponseHeaders( |
| 192 net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); |
| 189 if (request_->url().spec() == test_url_1().spec()) { | 193 if (request_->url().spec() == test_url_1().spec()) { |
| 190 response_data_ = test_data_1(); | 194 response_data_ = test_data_1(); |
| 191 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. | 195 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. |
| 192 } else if (request_->url().spec() == test_url_2().spec()) { | 196 } else if (request_->url().spec() == test_url_2().spec()) { |
| 193 response_data_ = test_data_2(); | 197 response_data_ = test_data_2(); |
| 194 } else if (request_->url().spec() == test_url_3().spec()) { | 198 } else if (request_->url().spec() == test_url_3().spec()) { |
| 195 response_data_ = test_data_3(); | 199 response_data_ = test_data_3(); |
| 196 } else if (request_->url().spec() == test_url_4().spec()) { | 200 } else if (request_->url().spec() == test_url_4().spec()) { |
| 197 response_data_ = test_data_4(); | 201 response_data_ = test_data_4(); |
| 198 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { | 202 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { |
| 203 std::string redirect_headers = test_redirect_to_url_2_headers(); |
| 199 response_headers_ = | 204 response_headers_ = |
| 200 new HttpResponseHeaders(test_redirect_to_url_2_headers()); | 205 new HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
| 206 redirect_headers.c_str(), redirect_headers.size())); |
| 201 } else { | 207 } else { |
| 202 AdvanceJob(); | 208 AdvanceJob(); |
| 203 | 209 |
| 204 // unexpected url, return error | 210 // unexpected url, return error |
| 205 // FIXME(brettw) we may want to use WININET errors or have some more types | 211 // FIXME(brettw) we may want to use WININET errors or have some more types |
| 206 // of errors | 212 // of errors |
| 207 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 213 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
| 208 ERR_INVALID_URL)); | 214 ERR_INVALID_URL)); |
| 209 // FIXME(brettw): this should emulate a network error, and not just fail | 215 // FIXME(brettw): this should emulate a network error, and not just fail |
| 210 // initiating a connection | 216 // initiating a connection |
| (...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 347 | 353 |
| 348 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); | 354 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); |
| 349 g_pending_jobs.Get().pop_front(); | 355 g_pending_jobs.Get().pop_front(); |
| 350 | 356 |
| 351 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q | 357 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q |
| 352 next_job->ProcessNextOperation(); | 358 next_job->ProcessNextOperation(); |
| 353 return true; | 359 return true; |
| 354 } | 360 } |
| 355 | 361 |
| 356 } // namespace net | 362 } // namespace net |
| OLD | NEW |