Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(335)

Side by Side Diff: content/test/test_url_fetcher_factory.h

Issue 8395038: Make test URLFetcher implementations not derive from the URLFetcher implementation, since we want... (Closed) Base URL: svn://chrome-svn/chrome/trunk/src/
Patch Set: move factory to its own file and remove Create function from URLFetcher impl Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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>
11 #include <string> 11 #include <string>
12 #include <utility> 12 #include <utility>
13 13
14 #include "base/threading/non_thread_safe.h" 14 #include "base/threading/non_thread_safe.h"
15 #include "content/common/net/url_fetcher.h" 15 #include "content/public/common/url_fetcher_factory.h"
16 #include "googleurl/src/gurl.h" 16 #include "googleurl/src/gurl.h"
17 #include "net/http/http_request_headers.h"
17 #include "net/url_request/url_request_status.h" 18 #include "net/url_request/url_request_status.h"
18 19
19 // Changes URLFetcher's Factory for the lifetime of the object. 20 // Changes URLFetcher's Factory for the lifetime of the object.
20 // Note that this scoper cannot be nested (to make it even harder to misuse). 21 // Note that this scoper cannot be nested (to make it even harder to misuse).
21 class ScopedURLFetcherFactory : public base::NonThreadSafe { 22 class ScopedURLFetcherFactory : public base::NonThreadSafe {
22 public: 23 public:
23 explicit ScopedURLFetcherFactory(URLFetcher::Factory* factory); 24 explicit ScopedURLFetcherFactory(content::URLFetcherFactory* factory);
24 virtual ~ScopedURLFetcherFactory(); 25 virtual ~ScopedURLFetcherFactory();
25 26
26 private: 27 private:
27 DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory); 28 DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory);
28 }; 29 };
29 30
30 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of 31 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of
31 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates 32 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates
wtc 2011/10/27 21:29:16 There are still references to URLFetcher::Factory
jam 2011/10/27 22:34:41 Done.
32 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is 33 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is
33 // expected that you'll grab the delegate from the TestURLFetcher and invoke 34 // expected that you'll grab the delegate from the TestURLFetcher and invoke
34 // the callback method when appropriate. In this way it's easy to mock a 35 // the callback method when appropriate. In this way it's easy to mock a
35 // URLFetcher. 36 // URLFetcher.
36 // Typical usage: 37 // Typical usage:
37 // // TestURLFetcher requires a MessageLoop: 38 // // TestURLFetcher requires a MessageLoop:
38 // MessageLoopForUI message_loop; 39 // MessageLoopForUI message_loop;
39 // // And io_thread to release URLRequestContextGetter in URLFetcher::Core. 40 // // And io_thread to release URLRequestContextGetter in URLFetcher::Core.
40 // BrowserThread io_thread(BrowserThread::IO, &message_loop); 41 // BrowserThread io_thread(BrowserThread::IO, &message_loop);
41 // // Create factory (it automatically sets itself as URLFetcher's factory). 42 // // Create factory (it automatically sets itself as URLFetcher's factory).
42 // TestURLFetcherFactory factory; 43 // TestURLFetcherFactory factory;
43 // // Do something that triggers creation of a URLFetcher. 44 // // Do something that triggers creation of a URLFetcher.
44 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); 45 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id);
45 // DCHECK(fetcher); 46 // DCHECK(fetcher);
46 // // Notify delegate with whatever data you want. 47 // // Notify delegate with whatever data you want.
47 // fetcher->delegate()->OnURLFetchComplete(...); 48 // fetcher->delegate()->OnURLFetchComplete(...);
48 // // Make sure consumer of URLFetcher does the right thing. 49 // // Make sure consumer of URLFetcher does the right thing.
49 // ... 50 // ...
50 // 51 //
51 // Note: if you don't know when your request objects will be created you 52 // Note: if you don't know when your request objects will be created you
52 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes 53 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes
wtc 2011/10/27 21:29:16 Typo: FakeUrlFetcherFactory => FakeURLFetcherFacto
jam 2011/10/27 22:34:41 Done.
53 // below. 54 // below.
54 55
55 class TestURLFetcher : public URLFetcher { 56 class TestURLFetcher : public content::URLFetcher {
56 public: 57 public:
57 TestURLFetcher(int id, 58 TestURLFetcher(int id,
58 const GURL& url, 59 const GURL& url,
59 RequestType request_type, 60 RequestType request_type,
60 content::URLFetcherDelegate* d); 61 content::URLFetcherDelegate* d);
61 virtual ~TestURLFetcher(); 62 virtual ~TestURLFetcher();
62 63
63 // Overriden to do nothing. It is assumed the caller will notify the delegate. 64 // content::URLFetcher implementation
64 virtual void Start() {} 65 virtual void SetUploadData(const std::string& upload_content_type,
65 66 const std::string& upload_content) OVERRIDE;
67 virtual void SetChunkedUpload(
68 const std::string& upload_content_type) OVERRIDE;
66 // Overriden to cache the chunks uploaded. Caller can read back the uploaded 69 // Overriden to cache the chunks uploaded. Caller can read back the uploaded
67 // chunks with the upload_data() accessor. 70 // chunks with the upload_chunks() accessor.
68 virtual void AppendChunkToUpload(const std::string& data, bool is_last_chunk) 71 virtual void AppendChunkToUpload(const std::string& data,
69 OVERRIDE; 72 bool is_last_chunk) OVERRIDE;
70 73 virtual void SetLoadFlags(int load_flags) OVERRIDE;
71 // Unique ID in our factory. 74 virtual int GetLoadFlags() const OVERRIDE;
72 int id() const { return id_; } 75 virtual void SetReferrer(const std::string& referrer) OVERRIDE;
76 virtual void SetExtraRequestHeaders(
77 const std::string& extra_request_headers) OVERRIDE;
78 virtual void GetExtraRequestHeaders(
79 net::HttpRequestHeaders* headers) OVERRIDE;
80 virtual void SetRequestContext(
81 net::URLRequestContextGetter* request_context_getter) OVERRIDE;
82 virtual void SetAutomaticallyRetryOn5xx(bool retry) OVERRIDE;
83 virtual void SetMaxRetries(int max_retries) OVERRIDE;
84 virtual int GetMaxRetries() const OVERRIDE;
85 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE;
86 virtual void SaveResponseToTemporaryFile(
87 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE;
88 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE;
89 virtual net::HostPortPair GetSocketAddress() const OVERRIDE;
90 virtual bool WasFetchedViaProxy() const OVERRIDE;
91 virtual void Start() OVERRIDE;
92 virtual void StartWithRequestContextGetter(
93 net::URLRequestContextGetter* request_context_getter) OVERRIDE;
73 94
74 // URL we were created with. Because of how we're using URLFetcher GetUrl() 95 // URL we were created with. Because of how we're using URLFetcher GetUrl()
75 // always returns an empty URL. Chances are you'll want to use 96 // always returns an empty URL. Chances are you'll want to use
76 // GetOriginalUrl() in your tests. 97 // GetOriginalUrl() in your tests.
77 virtual const GURL& GetOriginalUrl() const OVERRIDE; 98 virtual const GURL& GetOriginalUrl() const OVERRIDE;
99 virtual const GURL& GetUrl() const OVERRIDE;
100 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE;
101 virtual int GetResponseCode() const OVERRIDE;
102 virtual const net::ResponseCookies& GetCookies() const OVERRIDE;
103 virtual bool FileErrorOccurred(
104 base::PlatformFileError* out_error_code) const OVERRIDE;
105 virtual void ReceivedContentWasMalformed() OVERRIDE;
106 // Override response access functions to return fake data.
107 virtual bool GetResponseAsString(
108 std::string* out_response_string) const OVERRIDE;
109 virtual bool GetResponseAsFilePath(
110 bool take_ownership, FilePath* out_response_path) const OVERRIDE;
111
112 // Unique ID in our factory.
113 int id() const { return id_; }
78 114
79 // Returns the data uploaded on this URLFetcher. 115 // Returns the data uploaded on this URLFetcher.
80 const std::string& upload_data() const { return URLFetcher::upload_data(); } 116 const std::string& upload_data() const { return upload_data_; }
81 117
82 // Returns the chunks of data uploaded on this URLFetcher. 118 // Returns the chunks of data uploaded on this URLFetcher.
83 const std::list<std::string>& upload_chunks() const { return chunks_; } 119 const std::list<std::string>& upload_chunks() const { return chunks_; }
84 120
85 // Returns the delegate installed on the URLFetcher. 121 // Returns the delegate installed on the URLFetcher.
86 content::URLFetcherDelegate* delegate() const { 122 content::URLFetcherDelegate* delegate() const { return delegate_; }
87 return URLFetcher::delegate();
88 }
89 123
90 void set_url(const GURL& url) { fake_url_ = url; } 124 void set_url(const GURL& url) { fake_url_ = url; }
91 virtual const GURL& GetUrl() const OVERRIDE;
92
93 void set_status(const net::URLRequestStatus& status); 125 void set_status(const net::URLRequestStatus& status);
94 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE;
95
96 void set_response_code(int response_code) { 126 void set_response_code(int response_code) {
97 fake_response_code_ = response_code; 127 fake_response_code_ = response_code;
98 } 128 }
99 virtual int GetResponseCode() const OVERRIDE;
100
101 void set_cookies(const net::ResponseCookies& c) { fake_cookies_ = c; } 129 void set_cookies(const net::ResponseCookies& c) { fake_cookies_ = c; }
102 virtual const net::ResponseCookies& GetCookies() const OVERRIDE;
103
104 void set_was_fetched_via_proxy(bool flag); 130 void set_was_fetched_via_proxy(bool flag);
105
106 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers); 131 void set_response_headers(scoped_refptr<net::HttpResponseHeaders> headers);
132 void set_backoff_delay(base::TimeDelta backoff_delay);
107 133
108 // Set string data. 134 // Set string data.
109 void SetResponseString(const std::string& response); 135 void SetResponseString(const std::string& response);
110 136
111 // Set File data. 137 // Set File data.
112 void SetResponseFilePath(const FilePath& path); 138 void SetResponseFilePath(const FilePath& path);
113 139
114 // Override response access functions to return fake data. 140 private:
115 virtual bool GetResponseAsString(std::string* out_response_string) const 141 enum ResponseDestinationType {
116 OVERRIDE; 142 STRING, // Default: In a std::string
117 virtual bool GetResponseAsFilePath(bool take_ownership, 143 TEMP_FILE // Write to a temp file
118 FilePath* out_response_path) const 144 };
119 OVERRIDE;
120 145
121 private:
122 const int id_; 146 const int id_;
123 const GURL original_url_; 147 const GURL original_url_;
148 content::URLFetcherDelegate* delegate_;
149 std::string upload_data_;
124 std::list<std::string> chunks_; 150 std::list<std::string> chunks_;
125 bool did_receive_last_chunk_; 151 bool did_receive_last_chunk_;
126 152
127 // User can use set_* methods to provide values returned by getters. 153 // User can use set_* methods to provide values returned by getters.
128 // Setting the real values is not possible, because the real class 154 // Setting the real values is not possible, because the real class
129 // has no setters. The data is a private member of a class defined 155 // has no setters. The data is a private member of a class defined
130 // in a .cc file, so we can't get at it with friendship. 156 // in a .cc file, so we can't get at it with friendship.
157 int fake_load_flags_;
131 GURL fake_url_; 158 GURL fake_url_;
132 net::URLRequestStatus fake_status_; 159 net::URLRequestStatus fake_status_;
133 int fake_response_code_; 160 int fake_response_code_;
134 net::ResponseCookies fake_cookies_; 161 net::ResponseCookies fake_cookies_;
162 ResponseDestinationType fake_response_destination_;
135 std::string fake_response_string_; 163 std::string fake_response_string_;
136 FilePath fake_response_file_path_; 164 FilePath fake_response_file_path_;
165 bool fake_was_fetched_via_proxy_;
166 scoped_refptr<net::HttpResponseHeaders> fake_response_headers_;
167 net::HttpRequestHeaders fake_extra_request_headers_;
168 int fake_max_retries_;
169 base::TimeDelta fake_backoff_delay_;
137 170
138 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); 171 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher);
139 }; 172 };
140 173
141 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers 174 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers
142 // are registered in a map by the id passed to the create method. 175 // are registered in a map by the id passed to the create method.
143 class TestURLFetcherFactory : public URLFetcher::Factory, 176 class TestURLFetcherFactory : public content::URLFetcherFactory,
144 public ScopedURLFetcherFactory { 177 public ScopedURLFetcherFactory {
145 public: 178 public:
146 TestURLFetcherFactory(); 179 TestURLFetcherFactory();
147 virtual ~TestURLFetcherFactory(); 180 virtual ~TestURLFetcherFactory();
148 181
149 virtual URLFetcher* CreateURLFetcher(int id, 182 virtual content::URLFetcher* CreateURLFetcher(
150 const GURL& url, 183 int id,
151 URLFetcher::RequestType request_type, 184 const GURL& url,
152 content::URLFetcherDelegate* d) OVERRIDE; 185 content::URLFetcher::RequestType request_type,
186 content::URLFetcherDelegate* d) OVERRIDE;
153 TestURLFetcher* GetFetcherByID(int id) const; 187 TestURLFetcher* GetFetcherByID(int id) const;
154 void RemoveFetcherFromMap(int id); 188 void RemoveFetcherFromMap(int id);
155 189
156 private: 190 private:
157 // Maps from id passed to create to the returned URLFetcher. 191 // Maps from id passed to create to the returned URLFetcher.
158 typedef std::map<int, TestURLFetcher*> Fetchers; 192 typedef std::map<int, TestURLFetcher*> Fetchers;
159 Fetchers fetchers_; 193 Fetchers fetchers_;
160 194
161 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); 195 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory);
162 }; 196 };
(...skipping 23 matching lines...) Expand all
186 // factory.SetFakeResponse("http://a.com/", "", false); 220 // factory.SetFakeResponse("http://a.com/", "", false);
187 // // But if the service requests http://b.com/asdf you want to respond with 221 // // But if the service requests http://b.com/asdf you want to respond with
188 // // a simple html page and an HTTP/200 code. 222 // // a simple html page and an HTTP/200 code.
189 // factory.SetFakeResponse("http://b.com/asdf", 223 // factory.SetFakeResponse("http://b.com/asdf",
190 // "<html><body>hello world</body></html>", 224 // "<html><body>hello world</body></html>",
191 // true); 225 // true);
192 // 226 //
193 // SomeService service; 227 // SomeService service;
194 // service.Run(); // Will eventually request these two URLs. 228 // service.Run(); // Will eventually request these two URLs.
195 229
196 class FakeURLFetcherFactory : public URLFetcher::Factory, 230 class FakeURLFetcherFactory : public content::URLFetcherFactory,
197 public ScopedURLFetcherFactory { 231 public ScopedURLFetcherFactory {
198 public: 232 public:
199 FakeURLFetcherFactory(); 233 FakeURLFetcherFactory();
200 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown 234 // FakeURLFetcherFactory that will delegate creating URLFetcher for unknown
201 // url to the given factory. 235 // url to the given factory.
202 explicit FakeURLFetcherFactory(URLFetcher::Factory* default_factory); 236 explicit FakeURLFetcherFactory(content::URLFetcherFactory* default_factory);
203 virtual ~FakeURLFetcherFactory(); 237 virtual ~FakeURLFetcherFactory();
204 238
205 // If no fake response is set for the given URL this method will delegate the 239 // If no fake response is set for the given URL this method will delegate the
206 // call to |default_factory_| if it is not NULL, or return NULL if it is 240 // call to |default_factory_| if it is not NULL, or return NULL if it is
207 // NULL. 241 // NULL.
208 // Otherwise, it will return a URLFetcher object which will respond with the 242 // Otherwise, it will return a URLFetcher object which will respond with the
209 // pre-baked response that the client has set by calling SetFakeResponse(). 243 // pre-baked response that the client has set by calling SetFakeResponse().
210 virtual URLFetcher* CreateURLFetcher(int id, 244 virtual content::URLFetcher* CreateURLFetcher(
211 const GURL& url, 245 int id,
212 URLFetcher::RequestType request_type, 246 const GURL& url,
213 content::URLFetcherDelegate* d) OVERRIDE; 247 content::URLFetcher::RequestType request_type,
248 content::URLFetcherDelegate* d) OVERRIDE;
214 249
215 // Sets the fake response for a given URL. If success is true we will serve 250 // Sets the fake response for a given URL. If success is true we will serve
216 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. 251 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty.
217 void SetFakeResponse(const std::string& url, 252 void SetFakeResponse(const std::string& url,
218 const std::string& response_data, 253 const std::string& response_data,
219 bool success); 254 bool success);
220 255
221 // Clear all the fake responses that were previously set via 256 // Clear all the fake responses that were previously set via
222 // SetFakeResponse(). 257 // SetFakeResponse().
223 void ClearFakeResponses(); 258 void ClearFakeResponses();
224 259
225 private: 260 private:
226 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; 261 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap;
227 FakeResponseMap fake_responses_; 262 FakeResponseMap fake_responses_;
228 URLFetcher::Factory* default_factory_; 263 content::URLFetcherFactory* default_factory_;
229 264
230 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); 265 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory);
231 }; 266 };
232 267
233 // This is an implementation of URLFetcher::Factory that will create a real 268 // This is an implementation of URLFetcher::Factory that will create a real
234 // URLFetcher. It can be use in conjunction with a FakeURLFetcherFactory in 269 // URLFetcher. It can be use in conjunction with a FakeURLFetcherFactory in
235 // integration tests to control the behavior of some requests but execute 270 // integration tests to control the behavior of some requests but execute
236 // all the other ones. 271 // all the other ones.
237 class URLFetcherFactory : public URLFetcher::Factory { 272 class URLFetcherFactory : public content::URLFetcherFactory {
wtc 2011/10/27 21:29:16 Although the content namespace disambiguates the t
jam 2011/10/27 22:34:41 sure, good point. i've renamed it to URLFetcherImp
238 public: 273 public:
239 URLFetcherFactory(); 274 URLFetcherFactory();
240 virtual ~URLFetcherFactory(); 275 virtual ~URLFetcherFactory();
241 276
242 // This method will create a real URLFetcher. 277 // This method will create a real URLFetcher.
243 virtual URLFetcher* CreateURLFetcher(int id, 278 virtual content::URLFetcher* CreateURLFetcher(
244 const GURL& url, 279 int id,
245 URLFetcher::RequestType request_type, 280 const GURL& url,
246 content::URLFetcherDelegate* d) OVERRIDE; 281 content::URLFetcher::RequestType request_type,
282 content::URLFetcherDelegate* d) OVERRIDE;
247 283
248 }; 284 };
249 285
250 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ 286 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698