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_data_(response_data), | 153 response_data_(response_data), |
154 offset_(0), | 154 offset_(0), |
155 async_buf_(NULL), | 155 async_buf_(NULL), |
156 async_buf_size_(0), | 156 async_buf_size_(0), |
157 weak_factory_(this) { | 157 weak_factory_(this) { |
158 response_headers_ = | |
159 new net::HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( | |
160 response_headers.c_str(), response_headers.size())); | |
mmenke
2015/10/27 16:49:31
nit: Can still do this in the initializer list.
martijnc
2015/10/27 21:10:15
Done.
| |
158 } | 161 } |
159 | 162 |
160 URLRequestTestJob::~URLRequestTestJob() { | 163 URLRequestTestJob::~URLRequestTestJob() { |
161 g_pending_jobs.Get().erase( | 164 g_pending_jobs.Get().erase( |
162 std::remove( | 165 std::remove( |
163 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), | 166 g_pending_jobs.Get().begin(), g_pending_jobs.Get().end(), this), |
164 g_pending_jobs.Get().end()); | 167 g_pending_jobs.Get().end()); |
165 } | 168 } |
166 | 169 |
167 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { | 170 bool URLRequestTestJob::GetMimeType(std::string* mime_type) const { |
(...skipping 10 matching lines...) Expand all Loading... | |
178 void URLRequestTestJob::Start() { | 181 void URLRequestTestJob::Start() { |
179 // Start reading asynchronously so that all error reporting and data | 182 // Start reading asynchronously so that all error reporting and data |
180 // callbacks happen as they would for network requests. | 183 // callbacks happen as they would for network requests. |
181 base::ThreadTaskRunnerHandle::Get()->PostTask( | 184 base::ThreadTaskRunnerHandle::Get()->PostTask( |
182 FROM_HERE, | 185 FROM_HERE, |
183 base::Bind(&URLRequestTestJob::StartAsync, weak_factory_.GetWeakPtr())); | 186 base::Bind(&URLRequestTestJob::StartAsync, weak_factory_.GetWeakPtr())); |
184 } | 187 } |
185 | 188 |
186 void URLRequestTestJob::StartAsync() { | 189 void URLRequestTestJob::StartAsync() { |
187 if (!response_headers_.get()) { | 190 if (!response_headers_.get()) { |
188 response_headers_ = new HttpResponseHeaders(test_headers()); | 191 std::string headers = test_headers(); |
192 response_headers_ = new HttpResponseHeaders( | |
193 net::HttpUtil::AssembleRawHeaders(headers.c_str(), headers.size())); | |
189 if (request_->url().spec() == test_url_1().spec()) { | 194 if (request_->url().spec() == test_url_1().spec()) { |
190 response_data_ = test_data_1(); | 195 response_data_ = test_data_1(); |
191 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. | 196 stage_ = DATA_AVAILABLE; // Simulate a synchronous response for this one. |
192 } else if (request_->url().spec() == test_url_2().spec()) { | 197 } else if (request_->url().spec() == test_url_2().spec()) { |
193 response_data_ = test_data_2(); | 198 response_data_ = test_data_2(); |
194 } else if (request_->url().spec() == test_url_3().spec()) { | 199 } else if (request_->url().spec() == test_url_3().spec()) { |
195 response_data_ = test_data_3(); | 200 response_data_ = test_data_3(); |
196 } else if (request_->url().spec() == test_url_4().spec()) { | 201 } else if (request_->url().spec() == test_url_4().spec()) { |
197 response_data_ = test_data_4(); | 202 response_data_ = test_data_4(); |
198 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { | 203 } else if (request_->url().spec() == test_url_redirect_to_url_2().spec()) { |
204 std::string redirect_headers = test_redirect_to_url_2_headers(); | |
199 response_headers_ = | 205 response_headers_ = |
200 new HttpResponseHeaders(test_redirect_to_url_2_headers()); | 206 new HttpResponseHeaders(net::HttpUtil::AssembleRawHeaders( |
207 redirect_headers.c_str(), redirect_headers.size())); | |
201 } else { | 208 } else { |
202 AdvanceJob(); | 209 AdvanceJob(); |
203 | 210 |
204 // unexpected url, return error | 211 // unexpected url, return error |
205 // FIXME(brettw) we may want to use WININET errors or have some more types | 212 // FIXME(brettw) we may want to use WININET errors or have some more types |
206 // of errors | 213 // of errors |
207 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, | 214 NotifyDone(URLRequestStatus(URLRequestStatus::FAILED, |
208 ERR_INVALID_URL)); | 215 ERR_INVALID_URL)); |
209 // FIXME(brettw): this should emulate a network error, and not just fail | 216 // FIXME(brettw): this should emulate a network error, and not just fail |
210 // initiating a connection | 217 // initiating a connection |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
347 | 354 |
348 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); | 355 URLRequestTestJob* next_job(g_pending_jobs.Get().front()); |
349 g_pending_jobs.Get().pop_front(); | 356 g_pending_jobs.Get().pop_front(); |
350 | 357 |
351 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q | 358 DCHECK(!next_job->auto_advance()); // auto_advance jobs should be in this q |
352 next_job->ProcessNextOperation(); | 359 next_job->ProcessNextOperation(); |
353 return true; | 360 return true; |
354 } | 361 } |
355 | 362 |
356 } // namespace net | 363 } // namespace net |
OLD | NEW |