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 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
58 const GURL& url, | 58 const GURL& url, |
59 RequestType request_type, | 59 RequestType request_type, |
60 Delegate* d); | 60 Delegate* 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 | 70 |
70 // Unique ID in our factory. | 71 // Unique ID in our factory. |
71 int id() const { return id_; } | 72 int id() const { return id_; } |
72 | 73 |
73 // 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() |
74 // 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() |
75 // in your tests. | 76 // in your tests. |
76 const GURL& original_url() const { return original_url_; } | 77 virtual const GURL& original_url() const OVERRIDE; |
77 | 78 |
78 // Returns the data uploaded on this URLFetcher. | 79 // Returns the data uploaded on this URLFetcher. |
79 const std::string& upload_data() const { return URLFetcher::upload_data(); } | 80 const std::string& upload_data() const { return URLFetcher::upload_data(); } |
80 | 81 |
81 // Returns the chunks of data uploaded on this URLFetcher. | 82 // Returns the chunks of data uploaded on this URLFetcher. |
82 const std::list<std::string>& upload_chunks() const { return chunks_; } | 83 const std::list<std::string>& upload_chunks() const { return chunks_; } |
83 | 84 |
84 // Returns the delegate installed on the URLFetcher. | 85 // Returns the delegate installed on the URLFetcher. |
85 Delegate* delegate() const { return URLFetcher::delegate(); } | 86 Delegate* delegate() const { return URLFetcher::delegate(); } |
86 | 87 |
87 void set_url(const GURL& url) { fake_url_ = url; } | 88 void set_url(const GURL& url) { fake_url_ = url; } |
88 virtual const GURL& url() const; | 89 virtual const GURL& url() const OVERRIDE; |
89 | 90 |
90 void set_status(const net::URLRequestStatus& status); | 91 void set_status(const net::URLRequestStatus& status); |
91 virtual const net::URLRequestStatus& status() const; | 92 virtual const net::URLRequestStatus& status() const OVERRIDE; |
92 | 93 |
93 void set_response_code(int response_code) { | 94 void set_response_code(int response_code) { |
94 fake_response_code_ = response_code; | 95 fake_response_code_ = response_code; |
95 } | 96 } |
96 virtual int response_code() const; | 97 virtual int response_code() const OVERRIDE; |
97 | 98 |
98 // Set string data. | 99 // Set string data. |
99 void SetResponseString(const std::string& response); | 100 void SetResponseString(const std::string& response); |
100 | 101 |
101 // Set File data. | 102 // Set File data. |
102 void SetResponseFilePath(const FilePath& path); | 103 void SetResponseFilePath(const FilePath& path); |
103 | 104 |
104 // Override response access functions to return fake data. | 105 // Override response access functions to return fake data. |
105 virtual bool GetResponseAsString(std::string* out_response_string) const; | 106 virtual bool GetResponseAsString(std::string* out_response_string) const |
| 107 OVERRIDE; |
106 virtual bool GetResponseAsFilePath(bool take_ownership, | 108 virtual bool GetResponseAsFilePath(bool take_ownership, |
107 FilePath* out_response_path) const; | 109 FilePath* out_response_path) const |
| 110 OVERRIDE; |
108 | 111 |
109 private: | 112 private: |
110 const int id_; | 113 const int id_; |
111 const GURL original_url_; | 114 const GURL original_url_; |
112 std::list<std::string> chunks_; | 115 std::list<std::string> chunks_; |
113 bool did_receive_last_chunk_; | 116 bool did_receive_last_chunk_; |
114 | 117 |
115 // User can use set_* methods to provide values returned by getters. | 118 // User can use set_* methods to provide values returned by getters. |
116 // Setting the real values is not possible, because the real class | 119 // Setting the real values is not possible, because the real class |
117 // has no setters. The data is a private member of a class defined | 120 // has no setters. The data is a private member of a class defined |
(...skipping 11 matching lines...) Expand all Loading... |
129 // are registered in a map by the id passed to the create method. | 132 // are registered in a map by the id passed to the create method. |
130 class TestURLFetcherFactory : public URLFetcher::Factory, | 133 class TestURLFetcherFactory : public URLFetcher::Factory, |
131 public ScopedURLFetcherFactory { | 134 public ScopedURLFetcherFactory { |
132 public: | 135 public: |
133 TestURLFetcherFactory(); | 136 TestURLFetcherFactory(); |
134 virtual ~TestURLFetcherFactory(); | 137 virtual ~TestURLFetcherFactory(); |
135 | 138 |
136 virtual URLFetcher* CreateURLFetcher(int id, | 139 virtual URLFetcher* CreateURLFetcher(int id, |
137 const GURL& url, | 140 const GURL& url, |
138 URLFetcher::RequestType request_type, | 141 URLFetcher::RequestType request_type, |
139 URLFetcher::Delegate* d); | 142 URLFetcher::Delegate* d) OVERRIDE; |
140 TestURLFetcher* GetFetcherByID(int id) const; | 143 TestURLFetcher* GetFetcherByID(int id) const; |
141 void RemoveFetcherFromMap(int id); | 144 void RemoveFetcherFromMap(int id); |
142 | 145 |
143 private: | 146 private: |
144 // Maps from id passed to create to the returned URLFetcher. | 147 // Maps from id passed to create to the returned URLFetcher. |
145 typedef std::map<int, TestURLFetcher*> Fetchers; | 148 typedef std::map<int, TestURLFetcher*> Fetchers; |
146 Fetchers fetchers_; | 149 Fetchers fetchers_; |
147 | 150 |
148 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); | 151 DISALLOW_COPY_AND_ASSIGN(TestURLFetcherFactory); |
149 }; | 152 }; |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
190 virtual ~FakeURLFetcherFactory(); | 193 virtual ~FakeURLFetcherFactory(); |
191 | 194 |
192 // If no fake response is set for the given URL this method will delegate the | 195 // If no fake response is set for the given URL this method will delegate the |
193 // call to |default_factory_| if it is not NULL, or return NULL if it is | 196 // call to |default_factory_| if it is not NULL, or return NULL if it is |
194 // NULL. | 197 // NULL. |
195 // Otherwise, it will return a URLFetcher object which will respond with the | 198 // Otherwise, it will return a URLFetcher object which will respond with the |
196 // pre-baked response that the client has set by calling SetFakeResponse(). | 199 // pre-baked response that the client has set by calling SetFakeResponse(). |
197 virtual URLFetcher* CreateURLFetcher(int id, | 200 virtual URLFetcher* CreateURLFetcher(int id, |
198 const GURL& url, | 201 const GURL& url, |
199 URLFetcher::RequestType request_type, | 202 URLFetcher::RequestType request_type, |
200 URLFetcher::Delegate* d); | 203 URLFetcher::Delegate* d) OVERRIDE; |
201 | 204 |
202 // Sets the fake response for a given URL. If success is true we will serve | 205 // Sets the fake response for a given URL. If success is true we will serve |
203 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. | 206 // an HTTP/200 and an HTTP/500 otherwise. The |response_data| may be empty. |
204 void SetFakeResponse(const std::string& url, | 207 void SetFakeResponse(const std::string& url, |
205 const std::string& response_data, | 208 const std::string& response_data, |
206 bool success); | 209 bool success); |
207 | 210 |
208 // Clear all the fake responses that were previously set via | 211 // Clear all the fake responses that were previously set via |
209 // SetFakeResponse(). | 212 // SetFakeResponse(). |
210 void ClearFakeReponses(); | 213 void ClearFakeReponses(); |
(...skipping 12 matching lines...) Expand all Loading... |
223 // all the other ones. | 226 // all the other ones. |
224 class URLFetcherFactory : public URLFetcher::Factory { | 227 class URLFetcherFactory : public URLFetcher::Factory { |
225 public: | 228 public: |
226 URLFetcherFactory(); | 229 URLFetcherFactory(); |
227 virtual ~URLFetcherFactory(); | 230 virtual ~URLFetcherFactory(); |
228 | 231 |
229 // This method will create a real URLFetcher. | 232 // This method will create a real URLFetcher. |
230 virtual URLFetcher* CreateURLFetcher(int id, | 233 virtual URLFetcher* CreateURLFetcher(int id, |
231 const GURL& url, | 234 const GURL& url, |
232 URLFetcher::RequestType request_type, | 235 URLFetcher::RequestType request_type, |
233 URLFetcher::Delegate* d); | 236 URLFetcher::Delegate* d) OVERRIDE; |
234 | 237 |
235 }; | 238 }; |
236 | 239 |
237 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ | 240 #endif // CONTENT_TEST_TEST_URL_FETCHER_FACTORY_H_ |
OLD | NEW |