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 CONTENT_PUBLIC_TEST_TEST_URL_FETCHER_FACTORY_H_ | 5 #ifndef NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
6 #define CONTENT_PUBLIC_TEST_TEST_URL_FETCHER_FACTORY_H_ | 6 #define NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 // TODO(akalin): Move this to net/. | |
10 | |
11 #include <list> | 9 #include <list> |
12 #include <map> | 10 #include <map> |
13 #include <string> | 11 #include <string> |
14 #include <utility> | 12 #include <utility> |
15 | 13 |
| 14 #include "base/compiler_specific.h" |
16 #include "base/threading/non_thread_safe.h" | 15 #include "base/threading/non_thread_safe.h" |
17 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
18 #include "net/http/http_request_headers.h" | 17 #include "net/http/http_request_headers.h" |
19 #include "net/url_request/url_fetcher_factory.h" | 18 #include "net/url_request/url_fetcher_factory.h" |
20 #include "net/url_request/url_request_status.h" | 19 #include "net/url_request/url_request_status.h" |
21 | 20 |
| 21 namespace net { |
| 22 |
22 // Changes URLFetcher's Factory for the lifetime of the object. | 23 // Changes URLFetcher's Factory for the lifetime of the object. |
23 // Note that this scoper cannot be nested (to make it even harder to misuse). | 24 // Note that this scoper cannot be nested (to make it even harder to misuse). |
24 class ScopedURLFetcherFactory : public base::NonThreadSafe { | 25 class ScopedURLFetcherFactory : public base::NonThreadSafe { |
25 public: | 26 public: |
26 explicit ScopedURLFetcherFactory(net::URLFetcherFactory* factory); | 27 explicit ScopedURLFetcherFactory(URLFetcherFactory* factory); |
27 virtual ~ScopedURLFetcherFactory(); | 28 virtual ~ScopedURLFetcherFactory(); |
28 | 29 |
29 private: | 30 private: |
30 DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory); | 31 DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory); |
31 }; | 32 }; |
32 | 33 |
33 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of | 34 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of |
34 // URLFetcher. TestURLFetcherFactory is a URLFetcherFactory that creates | 35 // URLFetcher. TestURLFetcherFactory is a URLFetcherFactory that creates |
35 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is | 36 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is |
36 // expected that you'll grab the delegate from the TestURLFetcher and invoke | 37 // expected that you'll grab the delegate from the TestURLFetcher and invoke |
(...skipping 12 matching lines...) Expand all Loading... |
49 // DCHECK(fetcher); | 50 // DCHECK(fetcher); |
50 // // Notify delegate with whatever data you want. | 51 // // Notify delegate with whatever data you want. |
51 // fetcher->delegate()->OnURLFetchComplete(...); | 52 // fetcher->delegate()->OnURLFetchComplete(...); |
52 // // Make sure consumer of URLFetcher does the right thing. | 53 // // Make sure consumer of URLFetcher does the right thing. |
53 // ... | 54 // ... |
54 // | 55 // |
55 // Note: if you don't know when your request objects will be created you | 56 // Note: if you don't know when your request objects will be created you |
56 // might want to use the FakeURLFetcher and FakeURLFetcherFactory classes | 57 // might want to use the FakeURLFetcher and FakeURLFetcherFactory classes |
57 // below. | 58 // below. |
58 | 59 |
59 class TestURLFetcher : public net::URLFetcher { | 60 class TestURLFetcher : public URLFetcher { |
60 public: | 61 public: |
61 TestURLFetcher(int id, | 62 TestURLFetcher(int id, |
62 const GURL& url, | 63 const GURL& url, |
63 net::URLFetcherDelegate* d); | 64 URLFetcherDelegate* d); |
64 virtual ~TestURLFetcher(); | 65 virtual ~TestURLFetcher(); |
65 | 66 |
66 // net::URLFetcher implementation | 67 // URLFetcher implementation |
67 virtual void SetUploadData(const std::string& upload_content_type, | 68 virtual void SetUploadData(const std::string& upload_content_type, |
68 const std::string& upload_content) OVERRIDE; | 69 const std::string& upload_content) OVERRIDE; |
69 virtual void SetChunkedUpload( | 70 virtual void SetChunkedUpload( |
70 const std::string& upload_content_type) OVERRIDE; | 71 const std::string& upload_content_type) OVERRIDE; |
71 // Overriden to cache the chunks uploaded. Caller can read back the uploaded | 72 // Overriden to cache the chunks uploaded. Caller can read back the uploaded |
72 // chunks with the upload_chunks() accessor. | 73 // chunks with the upload_chunks() accessor. |
73 virtual void AppendChunkToUpload(const std::string& data, | 74 virtual void AppendChunkToUpload(const std::string& data, |
74 bool is_last_chunk) OVERRIDE; | 75 bool is_last_chunk) OVERRIDE; |
75 virtual void SetLoadFlags(int load_flags) OVERRIDE; | 76 virtual void SetLoadFlags(int load_flags) OVERRIDE; |
76 virtual int GetLoadFlags() const OVERRIDE; | 77 virtual int GetLoadFlags() const OVERRIDE; |
77 virtual void SetReferrer(const std::string& referrer) OVERRIDE; | 78 virtual void SetReferrer(const std::string& referrer) OVERRIDE; |
78 virtual void SetExtraRequestHeaders( | 79 virtual void SetExtraRequestHeaders( |
79 const std::string& extra_request_headers) OVERRIDE; | 80 const std::string& extra_request_headers) OVERRIDE; |
80 virtual void AddExtraRequestHeader(const std::string& header_line) OVERRIDE; | 81 virtual void AddExtraRequestHeader(const std::string& header_line) OVERRIDE; |
81 virtual void GetExtraRequestHeaders( | 82 virtual void GetExtraRequestHeaders( |
82 net::HttpRequestHeaders* headers) const OVERRIDE; | 83 HttpRequestHeaders* headers) const OVERRIDE; |
83 virtual void SetRequestContext( | 84 virtual void SetRequestContext( |
84 net::URLRequestContextGetter* request_context_getter) OVERRIDE; | 85 URLRequestContextGetter* request_context_getter) OVERRIDE; |
85 virtual void SetFirstPartyForCookies( | 86 virtual void SetFirstPartyForCookies( |
86 const GURL& first_party_for_cookies) OVERRIDE; | 87 const GURL& first_party_for_cookies) OVERRIDE; |
87 virtual void SetURLRequestUserData( | 88 virtual void SetURLRequestUserData( |
88 const void* key, | 89 const void* key, |
89 const CreateDataCallback& create_data_callback) OVERRIDE; | 90 const CreateDataCallback& create_data_callback) OVERRIDE; |
90 virtual void SetStopOnRedirect(bool stop_on_redirect) OVERRIDE; | 91 virtual void SetStopOnRedirect(bool stop_on_redirect) OVERRIDE; |
91 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; | 92 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE; |
92 virtual void SetMaxRetries(int max_retries) OVERRIDE; | 93 virtual void SetMaxRetries(int max_retries) OVERRIDE; |
93 virtual int GetMaxRetries() const OVERRIDE; | 94 virtual int GetMaxRetries() const OVERRIDE; |
94 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; | 95 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; |
95 virtual void SaveResponseToFileAtPath( | 96 virtual void SaveResponseToFileAtPath( |
96 const FilePath& file_path, | 97 const FilePath& file_path, |
97 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | 98 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; |
98 virtual void SaveResponseToTemporaryFile( | 99 virtual void SaveResponseToTemporaryFile( |
99 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | 100 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; |
100 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; | 101 virtual HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; |
101 virtual net::HostPortPair GetSocketAddress() const OVERRIDE; | 102 virtual HostPortPair GetSocketAddress() const OVERRIDE; |
102 virtual bool WasFetchedViaProxy() const OVERRIDE; | 103 virtual bool WasFetchedViaProxy() const OVERRIDE; |
103 virtual void Start() OVERRIDE; | 104 virtual void Start() OVERRIDE; |
104 | 105 |
105 // URL we were created with. Because of how we're using URLFetcher GetURL() | 106 // URL we were created with. Because of how we're using URLFetcher GetURL() |
106 // always returns an empty URL. Chances are you'll want to use | 107 // always returns an empty URL. Chances are you'll want to use |
107 // GetOriginalURL() in your tests. | 108 // GetOriginalURL() in your tests. |
108 virtual const GURL& GetOriginalURL() const OVERRIDE; | 109 virtual const GURL& GetOriginalURL() const OVERRIDE; |
109 virtual const GURL& GetURL() const OVERRIDE; | 110 virtual const GURL& GetURL() const OVERRIDE; |
110 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE; | 111 virtual const URLRequestStatus& GetStatus() const OVERRIDE; |
111 virtual int GetResponseCode() const OVERRIDE; | 112 virtual int GetResponseCode() const OVERRIDE; |
112 virtual const net::ResponseCookies& GetCookies() const OVERRIDE; | 113 virtual const ResponseCookies& GetCookies() const OVERRIDE; |
113 virtual bool FileErrorOccurred( | 114 virtual bool FileErrorOccurred( |
114 base::PlatformFileError* out_error_code) const OVERRIDE; | 115 base::PlatformFileError* out_error_code) const OVERRIDE; |
115 virtual void ReceivedContentWasMalformed() OVERRIDE; | 116 virtual void ReceivedContentWasMalformed() OVERRIDE; |
116 // Override response access functions to return fake data. | 117 // Override response access functions to return fake data. |
117 virtual bool GetResponseAsString( | 118 virtual bool GetResponseAsString( |
118 std::string* out_response_string) const OVERRIDE; | 119 std::string* out_response_string) const OVERRIDE; |
119 virtual bool GetResponseAsFilePath( | 120 virtual bool GetResponseAsFilePath( |
120 bool take_ownership, FilePath* out_response_path) const OVERRIDE; | 121 bool take_ownership, FilePath* out_response_path) const OVERRIDE; |
121 | 122 |
122 // Unique ID in our factory. | 123 // Unique ID in our factory. |
123 int id() const { return id_; } | 124 int id() const { return id_; } |
124 | 125 |
125 // Returns the data uploaded on this URLFetcher. | 126 // Returns the data uploaded on this URLFetcher. |
126 const std::string& upload_data() const { return upload_data_; } | 127 const std::string& upload_data() const { return upload_data_; } |
127 | 128 |
128 // Returns the chunks of data uploaded on this URLFetcher. | 129 // Returns the chunks of data uploaded on this URLFetcher. |
129 const std::list<std::string>& upload_chunks() const { return chunks_; } | 130 const std::list<std::string>& upload_chunks() const { return chunks_; } |
130 | 131 |
131 // Returns the delegate installed on the URLFetcher. | 132 // Returns the delegate installed on the URLFetcher. |
132 net::URLFetcherDelegate* delegate() const { return delegate_; } | 133 URLFetcherDelegate* delegate() const { return delegate_; } |
133 | 134 |
134 void set_url(const GURL& url) { fake_url_ = url; } | 135 void set_url(const GURL& url) { fake_url_ = url; } |
135 void set_status(const net::URLRequestStatus& status); | 136 void set_status(const URLRequestStatus& status); |
136 void set_response_code(int response_code) { | 137 void set_response_code(int response_code) { |
137 fake_response_code_ = response_code; | 138 fake_response_code_ = response_code; |
138 } | 139 } |
139 void set_cookies(const net::ResponseCookies& c) { fake_cookies_ = c; } | 140 void set_cookies(const ResponseCookies& c) { fake_cookies_ = c; } |
140 void set_was_fetched_via_proxy(bool flag); | 141 void set_was_fetched_via_proxy(bool flag); |
141 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); | 142 void set_response_headers(scoped_refptr<HttpResponseHeaders> headers); |
142 void set_backoff_delay(base::TimeDelta backoff_delay); | 143 void set_backoff_delay(base::TimeDelta backoff_delay); |
143 | 144 |
144 // Set string data. | 145 // Set string data. |
145 void SetResponseString(const std::string& response); | 146 void SetResponseString(const std::string& response); |
146 | 147 |
147 // Set File data. | 148 // Set File data. |
148 void SetResponseFilePath(const FilePath& path); | 149 void SetResponseFilePath(const FilePath& path); |
149 | 150 |
150 private: | 151 private: |
151 enum ResponseDestinationType { | 152 enum ResponseDestinationType { |
152 STRING, // Default: In a std::string | 153 STRING, // Default: In a std::string |
153 TEMP_FILE // Write to a temp file | 154 TEMP_FILE // Write to a temp file |
154 }; | 155 }; |
155 | 156 |
156 const int id_; | 157 const int id_; |
157 const GURL original_url_; | 158 const GURL original_url_; |
158 net::URLFetcherDelegate* delegate_; | 159 URLFetcherDelegate* delegate_; |
159 std::string upload_data_; | 160 std::string upload_data_; |
160 std::list<std::string> chunks_; | 161 std::list<std::string> chunks_; |
161 bool did_receive_last_chunk_; | 162 bool did_receive_last_chunk_; |
162 | 163 |
163 // User can use set_* methods to provide values returned by getters. | 164 // User can use set_* methods to provide values returned by getters. |
164 // Setting the real values is not possible, because the real class | 165 // Setting the real values is not possible, because the real class |
165 // has no setters. The data is a private member of a class defined | 166 // has no setters. The data is a private member of a class defined |
166 // in a .cc file, so we can't get at it with friendship. | 167 // in a .cc file, so we can't get at it with friendship. |
167 int fake_load_flags_; | 168 int fake_load_flags_; |
168 GURL fake_url_; | 169 GURL fake_url_; |
169 net::URLRequestStatus fake_status_; | 170 URLRequestStatus fake_status_; |
170 int fake_response_code_; | 171 int fake_response_code_; |
171 net::ResponseCookies fake_cookies_; | 172 ResponseCookies fake_cookies_; |
172 ResponseDestinationType fake_response_destination_; | 173 ResponseDestinationType fake_response_destination_; |
173 std::string fake_response_string_; | 174 std::string fake_response_string_; |
174 FilePath fake_response_file_path_; | 175 FilePath fake_response_file_path_; |
175 bool fake_was_fetched_via_proxy_; | 176 bool fake_was_fetched_via_proxy_; |
176 scoped_refptr<net::HttpResponseHeaders> fake_response_headers_; | 177 scoped_refptr<HttpResponseHeaders> fake_response_headers_; |
177 net::HttpRequestHeaders fake_extra_request_headers_; | 178 HttpRequestHeaders fake_extra_request_headers_; |
178 int fake_max_retries_; | 179 int fake_max_retries_; |
179 base::TimeDelta fake_backoff_delay_; | 180 base::TimeDelta fake_backoff_delay_; |
180 | 181 |
181 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); | 182 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); |
182 }; | 183 }; |
183 | 184 |
184 // Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers | 185 // Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers |
185 // are registered in a map by the id passed to the create method. | 186 // are registered in a map by the id passed to the create method. |
186 class TestURLFetcherFactory : public net::URLFetcherFactory, | 187 class TestURLFetcherFactory : public URLFetcherFactory, |
187 public ScopedURLFetcherFactory { | 188 public ScopedURLFetcherFactory { |
188 public: | 189 public: |
189 TestURLFetcherFactory(); | 190 TestURLFetcherFactory(); |
190 virtual ~TestURLFetcherFactory(); | 191 virtual ~TestURLFetcherFactory(); |
191 | 192 |
192 virtual net::URLFetcher* CreateURLFetcher( | 193 virtual URLFetcher* CreateURLFetcher( |
193 int id, | 194 int id, |
194 const GURL& url, | 195 const GURL& url, |
195 net::URLFetcher::RequestType request_type, | 196 URLFetcher::RequestType request_type, |
196 net::URLFetcherDelegate* d) OVERRIDE; | 197 URLFetcherDelegate* d) OVERRIDE; |
197 TestURLFetcher* GetFetcherByID(int id) const; | 198 TestURLFetcher* GetFetcherByID(int id) const; |
198 void RemoveFetcherFromMap(int id); | 199 void RemoveFetcherFromMap(int id); |
199 | 200 |
200 private: | 201 private: |
201 // Maps from id passed to create to the returned URLFetcher. | 202 // Maps from id passed to create to the returned URLFetcher. |
202 typedef std::map<int, TestURLFetcher*> Fetchers; | 203 typedef std::map<int, TestURLFetcher*> Fetchers; |
203 Fetchers fetchers_; | 204 Fetchers fetchers_; |
204 | 205 |
205 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); | 206 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); |
206 }; | 207 }; |
(...skipping 23 matching lines...) Expand all Loading... |
230 // factory.SetFakeResponse("http://a.com/", "", false); | 231 // factory.SetFakeResponse("http://a.com/", "", false); |
231 // // But if the service requests http://b.com/asdf you want to respond with | 232 // // But if the service requests http://b.com/asdf you want to respond with |
232 // // a simple html page and an HTTP/200 code. | 233 // // a simple html page and an HTTP/200 code. |
233 // factory.SetFakeResponse("http://b.com/asdf", | 234 // factory.SetFakeResponse("http://b.com/asdf", |
234 // "<html><body>hello world</body></html>", | 235 // "<html><body>hello world</body></html>", |
235 // true); | 236 // true); |
236 // | 237 // |
237 // SomeService service; | 238 // SomeService service; |
238 // service.Run(); // Will eventually request these two URLs. | 239 // service.Run(); // Will eventually request these two URLs. |
239 | 240 |
240 class FakeURLFetcherFactory : public net::URLFetcherFactory, | 241 class FakeURLFetcherFactory : public URLFetcherFactory, |
241 public ScopedURLFetcherFactory { | 242 public ScopedURLFetcherFactory { |
242 public: | 243 public: |
243 FakeURLFetcherFactory(); | 244 FakeURLFetcherFactory(); |
244 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown | 245 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown |
245 // url to the given factory. | 246 // url to the given factory. |
246 explicit FakeURLFetcherFactory(net::URLFetcherFactory* default_factory); | 247 explicit FakeURLFetcherFactory(URLFetcherFactory* default_factory); |
247 virtual ~FakeURLFetcherFactory(); | 248 virtual ~FakeURLFetcherFactory(); |
248 | 249 |
249 // If no fake response is set for the given URL this method will delegate the | 250 // If no fake response is set for the given URL this method will delegate the |
250 // call to |default_factory_| if it is not NULL, or return NULL if it is | 251 // call to |default_factory_| if it is not NULL, or return NULL if it is |
251 // NULL. | 252 // NULL. |
252 // Otherwise, it will return a URLFetcher object which will respond with the | 253 // Otherwise, it will return a URLFetcher object which will respond with the |
253 // pre-baked response that the client has set by calling SetFakeResponse(). | 254 // pre-baked response that the client has set by calling SetFakeResponse(). |
254 virtual net::URLFetcher* CreateURLFetcher( | 255 virtual URLFetcher* CreateURLFetcher( |
255 int id, | 256 int id, |
256 const GURL& url, | 257 const GURL& url, |
257 net::URLFetcher::RequestType request_type, | 258 URLFetcher::RequestType request_type, |
258 net::URLFetcherDelegate* d) OVERRIDE; | 259 URLFetcherDelegate* d) OVERRIDE; |
259 | 260 |
260 // Sets the fake response for a given URL. If success is true we will serve | 261 // Sets the fake response for a given URL. If success is true we will serve |
261 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. | 262 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. |
262 void SetFakeResponse(const std::string& url, | 263 void SetFakeResponse(const std::string& url, |
263 const std::string& response_data, | 264 const std::string& response_data, |
264 bool success); | 265 bool success); |
265 | 266 |
266 // Clear all the fake responses that were previously set via | 267 // Clear all the fake responses that were previously set via |
267 // SetFakeResponse(). | 268 // SetFakeResponse(). |
268 void ClearFakeResponses(); | 269 void ClearFakeResponses(); |
269 | 270 |
270 private: | 271 private: |
271 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; | 272 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; |
272 FakeResponseMap fake_responses_; | 273 FakeResponseMap fake_responses_; |
273 net::URLFetcherFactory* default_factory_; | 274 URLFetcherFactory* default_factory_; |
274 | 275 |
275 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); | 276 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); |
276 }; | 277 }; |
277 | 278 |
278 // This is an implementation of URLFetcherFactory that will create a | 279 // This is an implementation of URLFetcherFactory that will create a |
279 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in | 280 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in |
280 // integration tests to control the behavior of some requests but execute | 281 // integration tests to control the behavior of some requests but execute |
281 // all the other ones. | 282 // all the other ones. |
282 class URLFetcherImplFactory : public net::URLFetcherFactory { | 283 class URLFetcherImplFactory : public URLFetcherFactory { |
283 public: | 284 public: |
284 URLFetcherImplFactory(); | 285 URLFetcherImplFactory(); |
285 virtual ~URLFetcherImplFactory(); | 286 virtual ~URLFetcherImplFactory(); |
286 | 287 |
287 // This method will create a real URLFetcher. | 288 // This method will create a real URLFetcher. |
288 virtual net::URLFetcher* CreateURLFetcher( | 289 virtual URLFetcher* CreateURLFetcher( |
289 int id, | 290 int id, |
290 const GURL& url, | 291 const GURL& url, |
291 net::URLFetcher::RequestType request_type, | 292 URLFetcher::RequestType request_type, |
292 net::URLFetcherDelegate* d) OVERRIDE; | 293 URLFetcherDelegate* d) OVERRIDE; |
293 | 294 |
294 }; | 295 }; |
295 | 296 |
296 #endif // CONTENT_PUBLIC_TEST_TEST_URL_FETCHER_FACTORY_H_ | 297 } // namespace net |
| 298 |
| 299 #endif // NET_URL_REQUEST_TEST_URL_FETCHER_FACTORY_H_ |
OLD | NEW |