| 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 #ifndef CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ | 5 #ifndef CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |
| 6 #define CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ | 6 #define CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <list> | 9 #include <list> |
| 10 #include <map> | 10 #include <map> |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 // | 50 // |
| 51 // Note: if you don't know when your request objects will be created you | 51 // Note: if you don't know when your request objects will be created you |
| 52 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes | 52 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes |
| 53 // below. | 53 // below. |
| 54 | 54 |
| 55 class TestURLFetcher : public URLFetcher { | 55 class TestURLFetcher : public URLFetcher { |
| 56 public: | 56 public: |
| 57 TestURLFetcher(int id, | 57 TestURLFetcher(int id, |
| 58 const GURL& url, | 58 const GURL& url, |
| 59 RequestType request_type, | 59 RequestType request_type, |
| 60 Delegate* d); | 60 content::URLFetcherDelegate* d); |
| 61 virtual ~TestURLFetcher(); | 61 virtual ~TestURLFetcher(); |
| 62 | 62 |
| 63 // Overriden to do nothing. It is assumed the caller will notify the delegate. | 63 // Overriden to do nothing. It is assumed the caller will notify the delegate. |
| 64 virtual void Start() {} | 64 virtual void Start() {} |
| 65 | 65 |
| 66 // Overriden to cache the chunks uploaded. Caller can read back the uploaded | 66 // Overriden to cache the chunks uploaded. Caller can read back the uploaded |
| 67 // chunks with the upload_data() accessor. | 67 // chunks with the upload_data() accessor. |
| 68 virtual void AppendChunkToUpload(const std::string& data, bool is_last_chunk) | 68 virtual void AppendChunkToUpload(const std::string& data, bool is_last_chunk) |
| 69 OVERRIDE; | 69 OVERRIDE; |
| 70 | 70 |
| 71 // Unique ID in our factory. | 71 // Unique ID in our factory. |
| 72 int id() const { return id_; } | 72 int id() const { return id_; } |
| 73 | 73 |
| 74 // URL we were created with. Because of how we're using URLFetcher url() | 74 // URL we were created with. Because of how we're using URLFetcher url() |
| 75 // always returns an empty URL. Chances are you'll want to use original_url() | 75 // always returns an empty URL. Chances are you'll want to use original_url() |
| 76 // in your tests. | 76 // in your tests. |
| 77 virtual const GURL& original_url() const OVERRIDE; | 77 virtual const GURL& original_url() const OVERRIDE; |
| 78 | 78 |
| 79 // Returns the data uploaded on this URLFetcher. | 79 // Returns the data uploaded on this URLFetcher. |
| 80 const std::string& upload_data() const { return URLFetcher::upload_data(); } | 80 const std::string& upload_data() const { return URLFetcher::upload_data(); } |
| 81 | 81 |
| 82 // Returns the chunks of data uploaded on this URLFetcher. | 82 // Returns the chunks of data uploaded on this URLFetcher. |
| 83 const std::list<std::string>& upload_chunks() const { return chunks_; } | 83 const std::list<std::string>& upload_chunks() const { return chunks_; } |
| 84 | 84 |
| 85 // Returns the delegate installed on the URLFetcher. | 85 // Returns the delegate installed on the URLFetcher. |
| 86 Delegate* delegate() const { return URLFetcher::delegate(); } | 86 content::URLFetcherDelegate* delegate() const { |
| 87 return URLFetcher::delegate(); |
| 88 } |
| 87 | 89 |
| 88 void set_url(const GURL& url) { fake_url_ = url; } | 90 void set_url(const GURL& url) { fake_url_ = url; } |
| 89 virtual const GURL& url() const OVERRIDE; | 91 virtual const GURL& url() const OVERRIDE; |
| 90 | 92 |
| 91 void set_status(const net::URLRequestStatus& status); | 93 void set_status(const net::URLRequestStatus& status); |
| 92 virtual const net::URLRequestStatus& status() const OVERRIDE; | 94 virtual const net::URLRequestStatus& status() const OVERRIDE; |
| 93 | 95 |
| 94 void set_response_code(int response_code) { | 96 void set_response_code(int response_code) { |
| 95 fake_response_code_ = response_code; | 97 fake_response_code_ = response_code; |
| 96 } | 98 } |
| 97 virtual int response_code() const OVERRIDE; | 99 virtual int response_code() const OVERRIDE; |
| 98 | 100 |
| 101 void set_cookies(const net::ResponseCookies& c) { fake_cookies_ = c; } |
| 102 virtual const net::ResponseCookies& cookies() const OVERRIDE; |
| 103 |
| 99 void set_was_fetched_via_proxy(bool flag); | 104 void set_was_fetched_via_proxy(bool flag); |
| 100 | 105 |
| 101 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); | 106 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); |
| 102 | 107 |
| 103 // Set string data. | 108 // Set string data. |
| 104 void SetResponseString(const std::string& response); | 109 void SetResponseString(const std::string& response); |
| 105 | 110 |
| 106 // Set File data. | 111 // Set File data. |
| 107 void SetResponseFilePath(const FilePath& path); | 112 void SetResponseFilePath(const FilePath& path); |
| 108 | 113 |
| 109 // Override response access functions to return fake data. | 114 // Override response access functions to return fake data. |
| 110 virtual const std::string& GetResponseStringRef() const OVERRIDE; | |
| 111 virtual bool GetResponseAsString(std::string* out_response_string) const | 115 virtual bool GetResponseAsString(std::string* out_response_string) const |
| 112 OVERRIDE; | 116 OVERRIDE; |
| 113 virtual bool GetResponseAsFilePath(bool take_ownership, | 117 virtual bool GetResponseAsFilePath(bool take_ownership, |
| 114 FilePath* out_response_path) const | 118 FilePath* out_response_path) const |
| 115 OVERRIDE; | 119 OVERRIDE; |
| 116 | 120 |
| 117 private: | 121 private: |
| 118 const int id_; | 122 const int id_; |
| 119 const GURL original_url_; | 123 const GURL original_url_; |
| 120 std::list<std::string> chunks_; | 124 std::list<std::string> chunks_; |
| 121 bool did_receive_last_chunk_; | 125 bool did_receive_last_chunk_; |
| 122 | 126 |
| 123 // User can use set_* methods to provide values returned by getters. | 127 // User can use set_* methods to provide values returned by getters. |
| 124 // Setting the real values is not possible, because the real class | 128 // Setting the real values is not possible, because the real class |
| 125 // has no setters. The data is a private member of a class defined | 129 // has no setters. The data is a private member of a class defined |
| 126 // in a .cc file, so we can't get at it with friendship. | 130 // in a .cc file, so we can't get at it with friendship. |
| 127 GURL fake_url_; | 131 GURL fake_url_; |
| 128 net::URLRequestStatus fake_status_; | 132 net::URLRequestStatus fake_status_; |
| 129 int fake_response_code_; | 133 int fake_response_code_; |
| 134 net::ResponseCookies fake_cookies_; |
| 130 std::string fake_response_string_; | 135 std::string fake_response_string_; |
| 131 FilePath fake_response_file_path_; | 136 FilePath fake_response_file_path_; |
| 132 | 137 |
| 133 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); | 138 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); |
| 134 }; | 139 }; |
| 135 | 140 |
| 136 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers | 141 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers |
| 137 // are registered in a map by the id passed to the create method. | 142 // are registered in a map by the id passed to the create method. |
| 138 class TestURLFetcherFactory : public URLFetcher::Factory, | 143 class TestURLFetcherFactory : public URLFetcher::Factory, |
| 139 public ScopedURLFetcherFactory { | 144 public ScopedURLFetcherFactory { |
| 140 public: | 145 public: |
| 141 TestURLFetcherFactory(); | 146 TestURLFetcherFactory(); |
| 142 virtual ~TestURLFetcherFactory(); | 147 virtual ~TestURLFetcherFactory(); |
| 143 | 148 |
| 144 virtual URLFetcher* CreateURLFetcher(int id, | 149 virtual URLFetcher* CreateURLFetcher(int id, |
| 145 const GURL& url, | 150 const GURL& url, |
| 146 URLFetcher::RequestType request_type, | 151 URLFetcher::RequestType request_type, |
| 147 URLFetcher::Delegate* d) OVERRIDE; | 152 content::URLFetcherDelegate* d) OVERRIDE; |
| 148 TestURLFetcher* GetFetcherByID(int id) const; | 153 TestURLFetcher* GetFetcherByID(int id) const; |
| 149 void RemoveFetcherFromMap(int id); | 154 void RemoveFetcherFromMap(int id); |
| 150 | 155 |
| 151 private: | 156 private: |
| 152 // Maps from id passed to create to the returned URLFetcher. | 157 // Maps from id passed to create to the returned URLFetcher. |
| 153 typedef std::map<int, TestURLFetcher*> Fetchers; | 158 typedef std::map<int, TestURLFetcher*> Fetchers; |
| 154 Fetchers fetchers_; | 159 Fetchers fetchers_; |
| 155 | 160 |
| 156 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); | 161 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); |
| 157 }; | 162 }; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 virtual ~FakeURLFetcherFactory(); | 203 virtual ~FakeURLFetcherFactory(); |
| 199 | 204 |
| 200 // If no fake response is set for the given URL this method will delegate the | 205 // If no fake response is set for the given URL this method will delegate the |
| 201 // call to |default_factory_| if it is not NULL, or return NULL if it is | 206 // call to |default_factory_| if it is not NULL, or return NULL if it is |
| 202 // NULL. | 207 // NULL. |
| 203 // Otherwise, it will return a URLFetcher object which will respond with the | 208 // Otherwise, it will return a URLFetcher object which will respond with the |
| 204 // pre-baked response that the client has set by calling SetFakeResponse(). | 209 // pre-baked response that the client has set by calling SetFakeResponse(). |
| 205 virtual URLFetcher* CreateURLFetcher(int id, | 210 virtual URLFetcher* CreateURLFetcher(int id, |
| 206 const GURL& url, | 211 const GURL& url, |
| 207 URLFetcher::RequestType request_type, | 212 URLFetcher::RequestType request_type, |
| 208 URLFetcher::Delegate* d) OVERRIDE; | 213 content::URLFetcherDelegate* d) OVERRIDE; |
| 209 | 214 |
| 210 // Sets the fake response for a given URL. If success is true we will serve | 215 // Sets the fake response for a given URL. If success is true we will serve |
| 211 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. | 216 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. |
| 212 void SetFakeResponse(const std::string& url, | 217 void SetFakeResponse(const std::string& url, |
| 213 const std::string& response_data, | 218 const std::string& response_data, |
| 214 bool success); | 219 bool success); |
| 215 | 220 |
| 216 // Clear all the fake responses that were previously set via | 221 // Clear all the fake responses that were previously set via |
| 217 // SetFakeResponse(). | 222 // SetFakeResponse(). |
| 218 void ClearFakeResponses(); | 223 void ClearFakeResponses(); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 231 // all the other ones. | 236 // all the other ones. |
| 232 class URLFetcherFactory : public URLFetcher::Factory { | 237 class URLFetcherFactory : public URLFetcher::Factory { |
| 233 public: | 238 public: |
| 234 URLFetcherFactory(); | 239 URLFetcherFactory(); |
| 235 virtual ~URLFetcherFactory(); | 240 virtual ~URLFetcherFactory(); |
| 236 | 241 |
| 237 // This method will create a real URLFetcher. | 242 // This method will create a real URLFetcher. |
| 238 virtual URLFetcher* CreateURLFetcher(int id, | 243 virtual URLFetcher* CreateURLFetcher(int id, |
| 239 const GURL& url, | 244 const GURL& url, |
| 240 URLFetcher::RequestType request_type, | 245 URLFetcher::RequestType request_type, |
| 241 URLFetcher::Delegate* d) OVERRIDE; | 246 content::URLFetcherDelegate* d) OVERRIDE; |
| 242 | 247 |
| 243 }; | 248 }; |
| 244 | 249 |
| 245 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ | 250 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |
| OLD | NEW |