OLD | NEW |
1 // Copyright (c) 2011 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 "content/test/test_url_fetcher_factory.h" | 5 #include "content/test/test_url_fetcher_factory.h" |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 | 8 |
9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
11 #include "content/common/net/url_fetcher_impl.h" | 11 #include "content/common/net/url_fetcher_impl.h" |
12 #include "content/public/common/url_fetcher_delegate.h" | 12 #include "content/public/common/url_fetcher_delegate.h" |
13 #include "net/base/host_port_pair.h" | 13 #include "net/base/host_port_pair.h" |
14 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
15 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
16 | 16 |
17 ScopedURLFetcherFactory::ScopedURLFetcherFactory( | 17 ScopedURLFetcherFactory::ScopedURLFetcherFactory( |
18 content::URLFetcherFactory* factory) { | 18 content::URLFetcherFactory* factory) { |
19 DCHECK(!URLFetcherImpl::factory()); | 19 DCHECK(!URLFetcherImpl::factory()); |
20 URLFetcherImpl::set_factory(factory); | 20 URLFetcherImpl::set_factory(factory); |
21 } | 21 } |
22 | 22 |
23 ScopedURLFetcherFactory::~ScopedURLFetcherFactory() { | 23 ScopedURLFetcherFactory::~ScopedURLFetcherFactory() { |
24 DCHECK(URLFetcherImpl::factory()); | 24 DCHECK(URLFetcherImpl::factory()); |
25 URLFetcherImpl::set_factory(NULL); | 25 URLFetcherImpl::set_factory(NULL); |
26 } | 26 } |
27 | 27 |
28 TestURLFetcher::TestURLFetcher(int id, | 28 TestURLFetcher::TestURLFetcher(int id, |
29 const GURL& url, | 29 const GURL& url, |
30 URLFetcher::RequestType request_type, | |
31 content::URLFetcherDelegate* d) | 30 content::URLFetcherDelegate* d) |
32 : id_(id), | 31 : id_(id), |
33 original_url_(url), | 32 original_url_(url), |
34 delegate_(d), | 33 delegate_(d), |
35 did_receive_last_chunk_(false), | 34 did_receive_last_chunk_(false), |
36 fake_load_flags_(0), | 35 fake_load_flags_(0), |
37 fake_response_code_(-1), | 36 fake_response_code_(-1), |
38 fake_response_destination_(STRING), | 37 fake_response_destination_(STRING), |
39 fake_was_fetched_via_proxy_(false), | 38 fake_was_fetched_via_proxy_(false), |
40 fake_max_retries_(0) { | 39 fake_max_retries_(0) { |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
117 | 116 |
118 void TestURLFetcher::Start() { | 117 void TestURLFetcher::Start() { |
119 // Overriden to do nothing. It is assumed the caller will notify the delegate. | 118 // Overriden to do nothing. It is assumed the caller will notify the delegate. |
120 } | 119 } |
121 | 120 |
122 void TestURLFetcher::StartWithRequestContextGetter( | 121 void TestURLFetcher::StartWithRequestContextGetter( |
123 net::URLRequestContextGetter* request_context_getter) { | 122 net::URLRequestContextGetter* request_context_getter) { |
124 NOTIMPLEMENTED(); | 123 NOTIMPLEMENTED(); |
125 } | 124 } |
126 | 125 |
127 const GURL& TestURLFetcher::GetOriginalUrl() const { | 126 const GURL& TestURLFetcher::GetOriginalURL() const { |
128 return original_url_; | 127 return original_url_; |
129 } | 128 } |
130 | 129 |
131 const GURL& TestURLFetcher::GetUrl() const { | 130 const GURL& TestURLFetcher::GetURL() const { |
132 return fake_url_; | 131 return fake_url_; |
133 } | 132 } |
134 | 133 |
135 const net::URLRequestStatus& TestURLFetcher::GetStatus() const { | 134 const net::URLRequestStatus& TestURLFetcher::GetStatus() const { |
136 return fake_status_; | 135 return fake_status_; |
137 } | 136 } |
138 | 137 |
139 int TestURLFetcher::GetResponseCode() const { | 138 int TestURLFetcher::GetResponseCode() const { |
140 return fake_response_code_; | 139 return fake_response_code_; |
141 } | 140 } |
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
202 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 201 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
203 } | 202 } |
204 | 203 |
205 TestURLFetcherFactory::~TestURLFetcherFactory() {} | 204 TestURLFetcherFactory::~TestURLFetcherFactory() {} |
206 | 205 |
207 content::URLFetcher* TestURLFetcherFactory::CreateURLFetcher( | 206 content::URLFetcher* TestURLFetcherFactory::CreateURLFetcher( |
208 int id, | 207 int id, |
209 const GURL& url, | 208 const GURL& url, |
210 content::URLFetcher::RequestType request_type, | 209 content::URLFetcher::RequestType request_type, |
211 content::URLFetcherDelegate* d) { | 210 content::URLFetcherDelegate* d) { |
212 TestURLFetcher* fetcher = new TestURLFetcher(id, url, request_type, d); | 211 TestURLFetcher* fetcher = new TestURLFetcher(id, url, d); |
213 fetchers_[id] = fetcher; | 212 fetchers_[id] = fetcher; |
214 return fetcher; | 213 return fetcher; |
215 } | 214 } |
216 | 215 |
217 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { | 216 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { |
218 Fetchers::const_iterator i = fetchers_.find(id); | 217 Fetchers::const_iterator i = fetchers_.find(id); |
219 return i == fetchers_.end() ? NULL : i->second; | 218 return i == fetchers_.end() ? NULL : i->second; |
220 } | 219 } |
221 | 220 |
222 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { | 221 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { |
223 Fetchers::iterator i = fetchers_.find(id); | 222 Fetchers::iterator i = fetchers_.find(id); |
224 DCHECK(i != fetchers_.end()); | 223 DCHECK(i != fetchers_.end()); |
225 fetchers_.erase(i); | 224 fetchers_.erase(i); |
226 } | 225 } |
227 | 226 |
228 // This class is used by the FakeURLFetcherFactory below. | 227 // This class is used by the FakeURLFetcherFactory below. |
229 class FakeURLFetcher : public TestURLFetcher { | 228 class FakeURLFetcher : public TestURLFetcher { |
230 public: | 229 public: |
231 // Normal URL fetcher constructor but also takes in a pre-baked response. | 230 // Normal URL fetcher constructor but also takes in a pre-baked response. |
232 FakeURLFetcher(const GURL& url, RequestType request_type, | 231 FakeURLFetcher(const GURL& url, |
233 content::URLFetcherDelegate* d, | 232 content::URLFetcherDelegate* d, |
234 const std::string& response_data, bool success) | 233 const std::string& response_data, bool success) |
235 : TestURLFetcher(0, url, request_type, d), | 234 : TestURLFetcher(0, url, d), |
236 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 235 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
237 set_status(net::URLRequestStatus( | 236 set_status(net::URLRequestStatus( |
238 success ? net::URLRequestStatus::SUCCESS :net::URLRequestStatus::FAILED, | 237 success ? net::URLRequestStatus::SUCCESS : |
| 238 net::URLRequestStatus::FAILED, |
239 0)); | 239 0)); |
240 set_response_code(success ? 200 : 500); | 240 set_response_code(success ? 200 : 500); |
241 SetResponseString(response_data); | 241 SetResponseString(response_data); |
242 } | 242 } |
243 | 243 |
244 // Start the request. This will call the given delegate asynchronously | 244 // Start the request. This will call the given delegate asynchronously |
245 // with the pre-baked response as parameter. | 245 // with the pre-baked response as parameter. |
246 virtual void Start() OVERRIDE { | 246 virtual void Start() OVERRIDE { |
247 MessageLoop::current()->PostTask( | 247 MessageLoop::current()->PostTask( |
248 FROM_HERE, | 248 FROM_HERE, |
249 method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); | 249 method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); |
250 } | 250 } |
251 | 251 |
252 virtual const GURL& GetUrl() const OVERRIDE { | 252 virtual const GURL& GetURL() const OVERRIDE { |
253 return TestURLFetcher::GetOriginalUrl(); | 253 return TestURLFetcher::GetOriginalURL(); |
254 } | 254 } |
255 | 255 |
256 private: | 256 private: |
257 virtual ~FakeURLFetcher() { | 257 virtual ~FakeURLFetcher() { |
258 } | 258 } |
259 | 259 |
260 // This is the method which actually calls the delegate that is passed in the | 260 // This is the method which actually calls the delegate that is passed in the |
261 // constructor. | 261 // constructor. |
262 void RunDelegate() { | 262 void RunDelegate() { |
263 delegate()->OnURLFetchComplete(this); | 263 delegate()->OnURLFetchComplete(this); |
(...skipping 25 matching lines...) Expand all Loading... |
289 FakeResponseMap::const_iterator it = fake_responses_.find(url); | 289 FakeResponseMap::const_iterator it = fake_responses_.find(url); |
290 if (it == fake_responses_.end()) { | 290 if (it == fake_responses_.end()) { |
291 if (default_factory_ == NULL) { | 291 if (default_factory_ == NULL) { |
292 // If we don't have a baked response for that URL we return NULL. | 292 // If we don't have a baked response for that URL we return NULL. |
293 DLOG(ERROR) << "No baked response for URL: " << url.spec(); | 293 DLOG(ERROR) << "No baked response for URL: " << url.spec(); |
294 return NULL; | 294 return NULL; |
295 } else { | 295 } else { |
296 return default_factory_->CreateURLFetcher(id, url, request_type, d); | 296 return default_factory_->CreateURLFetcher(id, url, request_type, d); |
297 } | 297 } |
298 } | 298 } |
299 return new FakeURLFetcher(url, request_type, d, | 299 return new FakeURLFetcher(url, d, it->second.first, it->second.second); |
300 it->second.first, it->second.second); | |
301 } | 300 } |
302 | 301 |
303 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, | 302 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, |
304 const std::string& response_data, | 303 const std::string& response_data, |
305 bool success) { | 304 bool success) { |
306 // Overwrite existing URL if it already exists. | 305 // Overwrite existing URL if it already exists. |
307 fake_responses_[GURL(url)] = std::make_pair(response_data, success); | 306 fake_responses_[GURL(url)] = std::make_pair(response_data, success); |
308 } | 307 } |
309 | 308 |
310 void FakeURLFetcherFactory::ClearFakeResponses() { | 309 void FakeURLFetcherFactory::ClearFakeResponses() { |
311 fake_responses_.clear(); | 310 fake_responses_.clear(); |
312 } | 311 } |
313 | 312 |
314 URLFetcherFactory::URLFetcherFactory() {} | 313 URLFetcherImplFactory::URLFetcherImplFactory() {} |
315 | 314 |
316 URLFetcherFactory::~URLFetcherFactory() {} | 315 URLFetcherImplFactory::~URLFetcherImplFactory() {} |
317 | 316 |
318 content::URLFetcher* URLFetcherFactory::CreateURLFetcher( | 317 content::URLFetcher* URLFetcherImplFactory::CreateURLFetcher( |
319 int id, | 318 int id, |
320 const GURL& url, | 319 const GURL& url, |
321 content::URLFetcher::RequestType request_type, | 320 content::URLFetcher::RequestType request_type, |
322 content::URLFetcherDelegate* d) { | 321 content::URLFetcherDelegate* d) { |
323 return new URLFetcherImpl(url, request_type, d); | 322 return new URLFetcherImpl(url, request_type, d); |
324 } | 323 } |
OLD | NEW |