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 11 matching lines...) Expand all Loading... | |
22 class ScopedURLFetcherFactory : public base::NonThreadSafe { | 22 class ScopedURLFetcherFactory : public base::NonThreadSafe { |
23 public: | 23 public: |
24 explicit ScopedURLFetcherFactory(content::URLFetcherFactory* factory); | 24 explicit ScopedURLFetcherFactory(content::URLFetcherFactory* factory); |
25 virtual ~ScopedURLFetcherFactory(); | 25 virtual ~ScopedURLFetcherFactory(); |
26 | 26 |
27 private: | 27 private: |
28 DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory); | 28 DISALLOW_COPY_AND_ASSIGN(ScopedURLFetcherFactory); |
29 }; | 29 }; |
30 | 30 |
31 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of | 31 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of |
32 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates | 32 // URLFetcher. TestURLFetcherFactory is a URLFetcherFactory that creates |
33 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is | 33 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is |
34 // 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 |
35 // 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 |
36 // URLFetcher. | 36 // URLFetcher. |
37 // Typical usage: | 37 // Typical usage: |
38 // // TestURLFetcher requires a MessageLoop: | 38 // // TestURLFetcher requires a MessageLoop: |
39 // MessageLoopForUI message_loop; | 39 // MessageLoopForUI message_loop; |
40 // // And io_thread to release URLRequestContextGetter in URLFetcher::Core. | 40 // // And io_thread to release URLRequestContextGetter in URLFetcher::Core. |
41 // BrowserThread io_thread(BrowserThread::IO, &message_loop); | 41 // BrowserThread io_thread(BrowserThread::IO, &message_loop); |
42 // // Create factory (it automatically sets itself as URLFetcher's factory). | 42 // // Create factory (it automatically sets itself as URLFetcher's factory). |
43 // TestURLFetcherFactory factory; | 43 // TestURLFetcherFactory factory; |
44 // // Do something that triggers creation of a URLFetcher. | 44 // // Do something that triggers creation of a URLFetcher. |
45 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); | 45 // TestURLFetcher* fetcher = factory.GetFetcherByID(expected_id); |
46 // DCHECK(fetcher); | 46 // DCHECK(fetcher); |
47 // // Notify delegate with whatever data you want. | 47 // // Notify delegate with whatever data you want. |
48 // fetcher->delegate()->OnURLFetchComplete(...); | 48 // fetcher->delegate()->OnURLFetchComplete(...); |
49 // // Make sure consumer of URLFetcher does the right thing. | 49 // // Make sure consumer of URLFetcher does the right thing. |
50 // ... | 50 // ... |
51 // | 51 // |
52 // 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 |
53 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes | 53 // might want to use the FakeUrlFetcher and FakeUrlFetcherFactory classes |
wtc
2011/10/27 23:56:50
Fix these two typos: FakeUrlFetcher and FakeUrlFet
| |
54 // below. | 54 // below. |
55 | 55 |
56 class TestURLFetcher : public content::URLFetcher { | 56 class TestURLFetcher : public content::URLFetcher { |
57 public: | 57 public: |
58 TestURLFetcher(int id, | 58 TestURLFetcher(int id, |
59 const GURL& url, | 59 const GURL& url, |
60 RequestType request_type, | |
61 content::URLFetcherDelegate* d); | 60 content::URLFetcherDelegate* d); |
62 virtual ~TestURLFetcher(); | 61 virtual ~TestURLFetcher(); |
63 | 62 |
64 // content::URLFetcher implementation | 63 // content::URLFetcher implementation |
65 virtual void SetUploadData(const std::string& upload_content_type, | 64 virtual void SetUploadData(const std::string& upload_content_type, |
66 const std::string& upload_content) OVERRIDE; | 65 const std::string& upload_content) OVERRIDE; |
67 virtual void SetChunkedUpload( | 66 virtual void SetChunkedUpload( |
68 const std::string& upload_content_type) OVERRIDE; | 67 const std::string& upload_content_type) OVERRIDE; |
69 // Overriden to cache the chunks uploaded. Caller can read back the uploaded | 68 // Overriden to cache the chunks uploaded. Caller can read back the uploaded |
70 // chunks with the upload_chunks() accessor. | 69 // chunks with the upload_chunks() accessor. |
(...skipping 14 matching lines...) Expand all Loading... | |
85 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; | 84 virtual base::TimeDelta GetBackoffDelay() const OVERRIDE; |
86 virtual void SaveResponseToTemporaryFile( | 85 virtual void SaveResponseToTemporaryFile( |
87 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; | 86 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) OVERRIDE; |
88 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; | 87 virtual net::HttpResponseHeaders* GetResponseHeaders() const OVERRIDE; |
89 virtual net::HostPortPair GetSocketAddress() const OVERRIDE; | 88 virtual net::HostPortPair GetSocketAddress() const OVERRIDE; |
90 virtual bool WasFetchedViaProxy() const OVERRIDE; | 89 virtual bool WasFetchedViaProxy() const OVERRIDE; |
91 virtual void Start() OVERRIDE; | 90 virtual void Start() OVERRIDE; |
92 virtual void StartWithRequestContextGetter( | 91 virtual void StartWithRequestContextGetter( |
93 net::URLRequestContextGetter* request_context_getter) OVERRIDE; | 92 net::URLRequestContextGetter* request_context_getter) OVERRIDE; |
94 | 93 |
95 // URL we were created with. Because of how we're using URLFetcher GetUrl() | 94 // URL we were created with. Because of how we're using URLFetcher GetURL() |
96 // always returns an empty URL. Chances are you'll want to use | 95 // always returns an empty URL. Chances are you'll want to use |
97 // GetOriginalUrl() in your tests. | 96 // GetOriginalURL() in your tests. |
98 virtual const GURL& GetOriginalUrl() const OVERRIDE; | 97 virtual const GURL& GetOriginalURL() const OVERRIDE; |
99 virtual const GURL& GetUrl() const OVERRIDE; | 98 virtual const GURL& GetURL() const OVERRIDE; |
100 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE; | 99 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE; |
101 virtual int GetResponseCode() const OVERRIDE; | 100 virtual int GetResponseCode() const OVERRIDE; |
102 virtual const net::ResponseCookies& GetCookies() const OVERRIDE; | 101 virtual const net::ResponseCookies& GetCookies() const OVERRIDE; |
103 virtual bool FileErrorOccurred( | 102 virtual bool FileErrorOccurred( |
104 base::PlatformFileError* out_error_code) const OVERRIDE; | 103 base::PlatformFileError* out_error_code) const OVERRIDE; |
105 virtual void ReceivedContentWasMalformed() OVERRIDE; | 104 virtual void ReceivedContentWasMalformed() OVERRIDE; |
106 // Override response access functions to return fake data. | 105 // Override response access functions to return fake data. |
107 virtual bool GetResponseAsString( | 106 virtual bool GetResponseAsString( |
108 std::string* out_response_string) const OVERRIDE; | 107 std::string* out_response_string) const OVERRIDE; |
109 virtual bool GetResponseAsFilePath( | 108 virtual bool GetResponseAsFilePath( |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
164 FilePath fake_response_file_path_; | 163 FilePath fake_response_file_path_; |
165 bool fake_was_fetched_via_proxy_; | 164 bool fake_was_fetched_via_proxy_; |
166 scoped_refptr<net::HttpResponseHeaders> fake_response_headers_; | 165 scoped_refptr<net::HttpResponseHeaders> fake_response_headers_; |
167 net::HttpRequestHeaders fake_extra_request_headers_; | 166 net::HttpRequestHeaders fake_extra_request_headers_; |
168 int fake_max_retries_; | 167 int fake_max_retries_; |
169 base::TimeDelta fake_backoff_delay_; | 168 base::TimeDelta fake_backoff_delay_; |
170 | 169 |
171 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); | 170 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); |
172 }; | 171 }; |
173 | 172 |
174 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers | 173 // Simple URLFetcherFactory method that creates TestURLFetchers. All fetchers |
175 // are registered in a map by the id passed to the create method. | 174 // are registered in a map by the id passed to the create method. |
176 class TestURLFetcherFactory : public content::URLFetcherFactory, | 175 class TestURLFetcherFactory : public content::URLFetcherFactory, |
177 public ScopedURLFetcherFactory { | 176 public ScopedURLFetcherFactory { |
178 public: | 177 public: |
179 TestURLFetcherFactory(); | 178 TestURLFetcherFactory(); |
180 virtual ~TestURLFetcherFactory(); | 179 virtual ~TestURLFetcherFactory(); |
181 | 180 |
182 virtual content::URLFetcher* CreateURLFetcher( | 181 virtual content::URLFetcher* CreateURLFetcher( |
183 int id, | 182 int id, |
184 const GURL& url, | 183 const GURL& url, |
185 content::URLFetcher::RequestType request_type, | 184 content::URLFetcher::RequestType request_type, |
186 content::URLFetcherDelegate* d) OVERRIDE; | 185 content::URLFetcherDelegate* d) OVERRIDE; |
187 TestURLFetcher* GetFetcherByID(int id) const; | 186 TestURLFetcher* GetFetcherByID(int id) const; |
188 void RemoveFetcherFromMap(int id); | 187 void RemoveFetcherFromMap(int id); |
189 | 188 |
190 private: | 189 private: |
191 // Maps from id passed to create to the returned URLFetcher. | 190 // Maps from id passed to create to the returned URLFetcher. |
192 typedef std::map<int, TestURLFetcher*> Fetchers; | 191 typedef std::map<int, TestURLFetcher*> Fetchers; |
193 Fetchers fetchers_; | 192 Fetchers fetchers_; |
194 | 193 |
195 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); | 194 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); |
196 }; | 195 }; |
197 | 196 |
198 // The FakeUrlFetcher and FakeUrlFetcherFactory classes are similar to the | 197 // The FakeURLFetcher and FakeURLFetcherFactory classes are similar to the |
199 // ones above but don't require you to know when exactly the URLFetcher objects | 198 // ones above but don't require you to know when exactly the URLFetcher objects |
200 // will be created. | 199 // will be created. |
201 // | 200 // |
202 // These classes let you set pre-baked HTTP responses for particular URLs. | 201 // These classes let you set pre-baked HTTP responses for particular URLs. |
203 // E.g., if the user requests http://a.com/ then respond with an HTTP/500. | 202 // E.g., if the user requests http://a.com/ then respond with an HTTP/500. |
204 // | 203 // |
205 // We assume that the thread that is calling Start() on the URLFetcher object | 204 // We assume that the thread that is calling Start() on the URLFetcher object |
206 // has a message loop running. | 205 // has a message loop running. |
207 // | 206 // |
208 // This class is not thread-safe. You should not call SetFakeResponse or | 207 // This class is not thread-safe. You should not call SetFakeResponse or |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
258 void ClearFakeResponses(); | 257 void ClearFakeResponses(); |
259 | 258 |
260 private: | 259 private: |
261 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; | 260 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; |
262 FakeResponseMap fake_responses_; | 261 FakeResponseMap fake_responses_; |
263 content::URLFetcherFactory* default_factory_; | 262 content::URLFetcherFactory* default_factory_; |
264 | 263 |
265 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); | 264 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); |
266 }; | 265 }; |
267 | 266 |
268 // This is an implementation of URLFetcher::Factory that will create a real | 267 // This is an implementation of URLFetcherFactory that will create a |
269 // URLFetcher. It can be use in conjunction with a FakeURLFetcherFactory in | 268 // URLFetcherImpl. It can be use in conjunction with a FakeURLFetcherFactory in |
270 // integration tests to control the behavior of some requests but execute | 269 // integration tests to control the behavior of some requests but execute |
271 // all the other ones. | 270 // all the other ones. |
272 class URLFetcherFactory : public content::URLFetcherFactory { | 271 class URLFetcherImplFactory : public content::URLFetcherFactory { |
273 public: | 272 public: |
274 URLFetcherFactory(); | 273 URLFetcherImplFactory(); |
275 virtual ~URLFetcherFactory(); | 274 virtual ~URLFetcherImplFactory(); |
276 | 275 |
277 // This method will create a real URLFetcher. | 276 // This method will create a real URLFetcher. |
278 virtual content::URLFetcher* CreateURLFetcher( | 277 virtual content::URLFetcher* CreateURLFetcher( |
279 int id, | 278 int id, |
280 const GURL& url, | 279 const GURL& url, |
281 content::URLFetcher::RequestType request_type, | 280 content::URLFetcher::RequestType request_type, |
282 content::URLFetcherDelegate* d) OVERRIDE; | 281 content::URLFetcherDelegate* d) OVERRIDE; |
283 | 282 |
284 }; | 283 }; |
285 | 284 |
286 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ | 285 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |
OLD | NEW |