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 #ifndef NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ | 5 #ifndef NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
6 #define NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ | 6 #define NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
7 | 7 |
8 #include <list> | 8 #include <list> |
9 #include <map> | 9 #include <map> |
10 #include <string> | 10 #include <string> |
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
74 virtual void OnRequestStart(int fetcher_id) = 0; | 74 virtual void OnRequestStart(int fetcher_id) = 0; |
75 | 75 |
76 // Callback issued correspondingly to the call to |AppendChunkToUpload|. | 76 // Callback issued correspondingly to the call to |AppendChunkToUpload|. |
77 // Uploaded chunks can be retrieved with the |upload_chunks()| getter. | 77 // Uploaded chunks can be retrieved with the |upload_chunks()| getter. |
78 virtual void OnChunkUpload(int fetcher_id) = 0; | 78 virtual void OnChunkUpload(int fetcher_id) = 0; |
79 | 79 |
80 // Callback issued correspondingly to the destructor. | 80 // Callback issued correspondingly to the destructor. |
81 virtual void OnRequestEnd(int fetcher_id) = 0; | 81 virtual void OnRequestEnd(int fetcher_id) = 0; |
82 }; | 82 }; |
83 | 83 |
84 TestURLFetcher(int id, | 84 TestURLFetcher(int id, const GURL& url, URLFetcherDelegate* d); |
85 const GURL& url, | |
86 URLFetcherDelegate* d); | |
87 virtual ~TestURLFetcher(); | 85 virtual ~TestURLFetcher(); |
88 | 86 |
89 // URLFetcher implementation | 87 // URLFetcher implementation |
90 virtual void SetUploadData(const std::string& upload_content_type, | 88 virtual void SetUploadData(const std::string& upload_content_type, |
91 const std::string& upload_content) OVERRIDE; | 89 const std::string& upload_content) OVERRIDE; |
92 virtual void SetUploadFilePath( | 90 virtual void SetUploadFilePath( |
93 const std::string& upload_content_type, | 91 const std::string& upload_content_type, |
94 const base::FilePath& file_path, | 92 const base::FilePath& file_path, |
95 uint64 range_offset, | 93 uint64 range_offset, |
96 uint64 range_length, | 94 uint64 range_length, |
(...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 virtual const GURL& GetOriginalURL() const OVERRIDE; | 138 virtual const GURL& GetOriginalURL() const OVERRIDE; |
141 virtual const GURL& GetURL() const OVERRIDE; | 139 virtual const GURL& GetURL() const OVERRIDE; |
142 virtual const URLRequestStatus& GetStatus() const OVERRIDE; | 140 virtual const URLRequestStatus& GetStatus() const OVERRIDE; |
143 virtual int GetResponseCode() const OVERRIDE; | 141 virtual int GetResponseCode() const OVERRIDE; |
144 virtual const ResponseCookies& GetCookies() const OVERRIDE; | 142 virtual const ResponseCookies& GetCookies() const OVERRIDE; |
145 virtual void ReceivedContentWasMalformed() OVERRIDE; | 143 virtual void ReceivedContentWasMalformed() OVERRIDE; |
146 // Override response access functions to return fake data. | 144 // Override response access functions to return fake data. |
147 virtual bool GetResponseAsString( | 145 virtual bool GetResponseAsString( |
148 std::string* out_response_string) const OVERRIDE; | 146 std::string* out_response_string) const OVERRIDE; |
149 virtual bool GetResponseAsFilePath( | 147 virtual bool GetResponseAsFilePath( |
150 bool take_ownership, base::FilePath* out_response_path) const OVERRIDE; | 148 bool take_ownership, |
| 149 base::FilePath* out_response_path) const OVERRIDE; |
151 | 150 |
152 void GetExtraRequestHeaders(HttpRequestHeaders* headers) const; | 151 void GetExtraRequestHeaders(HttpRequestHeaders* headers) const; |
153 | 152 |
154 // Sets owner of this class. Set it to a non-NULL value if you want | 153 // Sets owner of this class. Set it to a non-NULL value if you want |
155 // to automatically unregister this fetcher from the owning factory | 154 // to automatically unregister this fetcher from the owning factory |
156 // upon destruction. | 155 // upon destruction. |
157 void set_owner(TestURLFetcherFactory* owner) { owner_ = owner; } | 156 void set_owner(TestURLFetcherFactory* owner) { owner_ = owner; } |
158 | 157 |
159 // Unique ID in our factory. | 158 // Unique ID in our factory. |
160 int id() const { return id_; } | 159 int id() const { return id_; } |
(...skipping 23 matching lines...) Expand all Loading... |
184 void SetDelegateForTests(DelegateForTests* delegate_for_tests); | 183 void SetDelegateForTests(DelegateForTests* delegate_for_tests); |
185 | 184 |
186 // Set string data. | 185 // Set string data. |
187 void SetResponseString(const std::string& response); | 186 void SetResponseString(const std::string& response); |
188 | 187 |
189 // Set File data. | 188 // Set File data. |
190 void SetResponseFilePath(const base::FilePath& path); | 189 void SetResponseFilePath(const base::FilePath& path); |
191 | 190 |
192 private: | 191 private: |
193 enum ResponseDestinationType { | 192 enum ResponseDestinationType { |
194 STRING, // Default: In a std::string | 193 STRING, // Default: In a std::string |
195 TEMP_FILE // Write to a temp file | 194 TEMP_FILE // Write to a temp file |
196 }; | 195 }; |
197 | 196 |
198 TestURLFetcherFactory* owner_; | 197 TestURLFetcherFactory* owner_; |
199 const int id_; | 198 const int id_; |
200 const GURL original_url_; | 199 const GURL original_url_; |
201 URLFetcherDelegate* delegate_; | 200 URLFetcherDelegate* delegate_; |
202 DelegateForTests* delegate_for_tests_; | 201 DelegateForTests* delegate_for_tests_; |
203 std::string upload_data_; | 202 std::string upload_data_; |
204 base::FilePath upload_file_path_; | 203 base::FilePath upload_file_path_; |
(...skipping 27 matching lines...) Expand all Loading... |
232 // Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers | 231 // Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers |
233 // are registered in a map by the id passed to the create method. | 232 // are registered in a map by the id passed to the create method. |
234 // Optionally, a fetcher may be automatically unregistered from the map upon | 233 // Optionally, a fetcher may be automatically unregistered from the map upon |
235 // its destruction. | 234 // its destruction. |
236 class TestURLFetcherFactory : public URLFetcherFactory, | 235 class TestURLFetcherFactory : public URLFetcherFactory, |
237 public ScopedURLFetcherFactory { | 236 public ScopedURLFetcherFactory { |
238 public: | 237 public: |
239 TestURLFetcherFactory(); | 238 TestURLFetcherFactory(); |
240 virtual ~TestURLFetcherFactory(); | 239 virtual ~TestURLFetcherFactory(); |
241 | 240 |
242 virtual URLFetcher* CreateURLFetcher( | 241 virtual URLFetcher* CreateURLFetcher(int id, |
243 int id, | 242 const GURL& url, |
244 const GURL& url, | 243 URLFetcher::RequestType request_type, |
245 URLFetcher::RequestType request_type, | 244 URLFetcherDelegate* d) OVERRIDE; |
246 URLFetcherDelegate* d) OVERRIDE; | |
247 TestURLFetcher* GetFetcherByID(int id) const; | 245 TestURLFetcher* GetFetcherByID(int id) const; |
248 void RemoveFetcherFromMap(int id); | 246 void RemoveFetcherFromMap(int id); |
249 void SetDelegateForTests(TestURLFetcherDelegateForTests* delegate_for_tests); | 247 void SetDelegateForTests(TestURLFetcherDelegateForTests* delegate_for_tests); |
250 void set_remove_fetcher_on_delete(bool remove_fetcher_on_delete) { | 248 void set_remove_fetcher_on_delete(bool remove_fetcher_on_delete) { |
251 remove_fetcher_on_delete_ = remove_fetcher_on_delete; | 249 remove_fetcher_on_delete_ = remove_fetcher_on_delete; |
252 } | 250 } |
253 | 251 |
254 private: | 252 private: |
255 // Maps from id passed to create to the returned URLFetcher. | 253 // Maps from id passed to create to the returned URLFetcher. |
256 typedef std::map<int, TestURLFetcher*> Fetchers; | 254 typedef std::map<int, TestURLFetcher*> Fetchers; |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
304 private: | 302 private: |
305 // This is the method which actually calls the delegate that is passed in the | 303 // This is the method which actually calls the delegate that is passed in the |
306 // constructor. | 304 // constructor. |
307 void RunDelegate(); | 305 void RunDelegate(); |
308 | 306 |
309 base::WeakPtrFactory<FakeURLFetcher> weak_factory_; | 307 base::WeakPtrFactory<FakeURLFetcher> weak_factory_; |
310 | 308 |
311 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | 309 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); |
312 }; | 310 }; |
313 | 311 |
314 | |
315 // FakeURLFetcherFactory is a factory for FakeURLFetcher objects. When | 312 // FakeURLFetcherFactory is a factory for FakeURLFetcher objects. When |
316 // instantiated, it sets itself up as the default URLFetcherFactory. Fake | 313 // instantiated, it sets itself up as the default URLFetcherFactory. Fake |
317 // responses for given URLs can be set using SetFakeResponse. | 314 // responses for given URLs can be set using SetFakeResponse. |
318 // | 315 // |
319 // This class is not thread-safe. You should not call SetFakeResponse or | 316 // This class is not thread-safe. You should not call SetFakeResponse or |
320 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is | 317 // ClearFakeResponse at the same time you call CreateURLFetcher. However, it is |
321 // OK to start URLFetcher objects while setting or clearing fake responses | 318 // OK to start URLFetcher objects while setting or clearing fake responses |
322 // since already created URLFetcher objects will not be affected by any changes | 319 // since already created URLFetcher objects will not be affected by any changes |
323 // made to the fake responses (once a URLFetcher object is created you cannot | 320 // made to the fake responses (once a URLFetcher object is created you cannot |
324 // change its fake response). | 321 // change its fake response). |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
362 // Parameters to FakeURLFetcherCreator: url, delegate, response_data, | 359 // Parameters to FakeURLFetcherCreator: url, delegate, response_data, |
363 // response_code | 360 // response_code |
364 // |url| URL for instantiated FakeURLFetcher | 361 // |url| URL for instantiated FakeURLFetcher |
365 // |delegate| Delegate for FakeURLFetcher | 362 // |delegate| Delegate for FakeURLFetcher |
366 // |response_data| response data for FakeURLFetcher | 363 // |response_data| response data for FakeURLFetcher |
367 // |response_code| response code for FakeURLFetcher | 364 // |response_code| response code for FakeURLFetcher |
368 // |status| URL fetch status for FakeURLFetcher | 365 // |status| URL fetch status for FakeURLFetcher |
369 // These arguments should by default be used in instantiating FakeURLFetcher | 366 // These arguments should by default be used in instantiating FakeURLFetcher |
370 // like so: | 367 // like so: |
371 // new FakeURLFetcher(url, delegate, response_data, response_code, status) | 368 // new FakeURLFetcher(url, delegate, response_data, response_code, status) |
372 typedef base::Callback<scoped_ptr<FakeURLFetcher>( | 369 typedef base::Callback<scoped_ptr<FakeURLFetcher>(const GURL&, |
373 const GURL&, | 370 URLFetcherDelegate*, |
374 URLFetcherDelegate*, | 371 const std::string&, |
375 const std::string&, | 372 HttpStatusCode, |
376 HttpStatusCode, | 373 URLRequestStatus::Status)> |
377 URLRequestStatus::Status)> FakeURLFetcherCreator; | 374 FakeURLFetcherCreator; |
378 | 375 |
379 // |default_factory|, which can be NULL, is a URLFetcherFactory that | 376 // |default_factory|, which can be NULL, is a URLFetcherFactory that |
380 // will be used to construct a URLFetcher in case the URL being created | 377 // will be used to construct a URLFetcher in case the URL being created |
381 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be | 378 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be |
382 // created in this case. | 379 // created in this case. |
383 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); | 380 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); |
384 | 381 |
385 // |default_factory|, which can be NULL, is a URLFetcherFactory that | 382 // |default_factory|, which can be NULL, is a URLFetcherFactory that |
386 // will be used to construct a URLFetcher in case the URL being created | 383 // will be used to construct a URLFetcher in case the URL being created |
387 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be | 384 // has no pre-baked response. If it is NULL, a URLFetcherImpl will be |
388 // created in this case. | 385 // created in this case. |
389 // |creator| is a callback that returns will be called to create a | 386 // |creator| is a callback that returns will be called to create a |
390 // FakeURLFetcher if a response is found to a given URL. It can be | 387 // FakeURLFetcher if a response is found to a given URL. It can be |
391 // set to MakeFakeURLFetcher. | 388 // set to MakeFakeURLFetcher. |
392 FakeURLFetcherFactory(URLFetcherFactory* default_factory, | 389 FakeURLFetcherFactory(URLFetcherFactory* default_factory, |
393 const FakeURLFetcherCreator& creator); | 390 const FakeURLFetcherCreator& creator); |
394 | 391 |
395 virtual ~FakeURLFetcherFactory(); | 392 virtual ~FakeURLFetcherFactory(); |
396 | 393 |
397 // If no fake response is set for the given URL this method will delegate the | 394 // If no fake response is set for the given URL this method will delegate the |
398 // call to |default_factory_| if it is not NULL, or return NULL if it is | 395 // call to |default_factory_| if it is not NULL, or return NULL if it is |
399 // NULL. | 396 // NULL. |
400 // Otherwise, it will return a URLFetcher object which will respond with the | 397 // Otherwise, it will return a URLFetcher object which will respond with the |
401 // pre-baked response that the client has set by calling SetFakeResponse(). | 398 // pre-baked response that the client has set by calling SetFakeResponse(). |
402 virtual URLFetcher* CreateURLFetcher( | 399 virtual URLFetcher* CreateURLFetcher(int id, |
403 int id, | 400 const GURL& url, |
404 const GURL& url, | 401 URLFetcher::RequestType request_type, |
405 URLFetcher::RequestType request_type, | 402 URLFetcherDelegate* d) OVERRIDE; |
406 URLFetcherDelegate* d) OVERRIDE; | |
407 | 403 |
408 // Sets the fake response for a given URL. The |response_data| may be empty. | 404 // Sets the fake response for a given URL. The |response_data| may be empty. |
409 // The |response_code| may be any HttpStatusCode. For instance, HTTP_OK will | 405 // The |response_code| may be any HttpStatusCode. For instance, HTTP_OK will |
410 // return an HTTP/200 and HTTP_INTERNAL_SERVER_ERROR will return an HTTP/500. | 406 // return an HTTP/200 and HTTP_INTERNAL_SERVER_ERROR will return an HTTP/500. |
411 // The |status| argument may be any URLRequestStatus::Status value. Typically, | 407 // The |status| argument may be any URLRequestStatus::Status value. Typically, |
412 // requests that return a valid HttpStatusCode have the SUCCESS status, while | 408 // requests that return a valid HttpStatusCode have the SUCCESS status, while |
413 // requests that indicate a failure to connect to the server have the FAILED | 409 // requests that indicate a failure to connect to the server have the FAILED |
414 // status. | 410 // status. |
415 void SetFakeResponse(const GURL& url, | 411 void SetFakeResponse(const GURL& url, |
416 const std::string& response_data, | 412 const std::string& response_data, |
(...skipping 28 matching lines...) Expand all Loading... |
445 // This is an implementation of URLFetcherFactory that will create a | 441 // This is an implementation of URLFetcherFactory that will create a |
446 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in | 442 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in |
447 // integration tests to control the behavior of some requests but execute | 443 // integration tests to control the behavior of some requests but execute |
448 // all the other ones. | 444 // all the other ones. |
449 class URLFetcherImplFactory : public URLFetcherFactory { | 445 class URLFetcherImplFactory : public URLFetcherFactory { |
450 public: | 446 public: |
451 URLFetcherImplFactory(); | 447 URLFetcherImplFactory(); |
452 virtual ~URLFetcherImplFactory(); | 448 virtual ~URLFetcherImplFactory(); |
453 | 449 |
454 // This method will create a real URLFetcher. | 450 // This method will create a real URLFetcher. |
455 virtual URLFetcher* CreateURLFetcher( | 451 virtual URLFetcher* CreateURLFetcher(int id, |
456 int id, | 452 const GURL& url, |
457 const GURL& url, | 453 URLFetcher::RequestType request_type, |
458 URLFetcher::RequestType request_type, | 454 URLFetcherDelegate* d) OVERRIDE; |
459 URLFetcherDelegate* d) OVERRIDE; | |
460 | |
461 }; | 455 }; |
462 | 456 |
463 } // namespace net | 457 } // namespace net |
464 | 458 |
465 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ | 459 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
OLD | NEW |