Chromium Code Reviews| 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 #include "content/test/test_url_fetcher_factory.h" | 5 #include "content/test/test_url_fetcher_factory.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "base/compiler_specific.h" | 9 #include "base/compiler_specific.h" |
| 10 #include "base/message_loop.h" | 10 #include "base/message_loop.h" |
| 11 #include "content/common/net/url_fetcher.h" | |
| 11 #include "content/public/common/url_fetcher_delegate.h" | 12 #include "content/public/common/url_fetcher_delegate.h" |
| 13 #include "net/base/host_port_pair.h" | |
| 12 #include "net/http/http_response_headers.h" | 14 #include "net/http/http_response_headers.h" |
| 13 #include "net/url_request/url_request_status.h" | 15 #include "net/url_request/url_request_status.h" |
| 14 | 16 |
| 15 ScopedURLFetcherFactory::ScopedURLFetcherFactory(URLFetcher::Factory* factory) { | 17 ScopedURLFetcherFactory::ScopedURLFetcherFactory( |
| 18 content::URLFetcherFactory* factory) { | |
| 16 DCHECK(!URLFetcher::factory()); | 19 DCHECK(!URLFetcher::factory()); |
| 17 URLFetcher::set_factory(factory); | 20 URLFetcher::set_factory(factory); |
| 18 } | 21 } |
| 19 | 22 |
| 20 ScopedURLFetcherFactory::~ScopedURLFetcherFactory() { | 23 ScopedURLFetcherFactory::~ScopedURLFetcherFactory() { |
| 21 DCHECK(URLFetcher::factory()); | 24 DCHECK(URLFetcher::factory()); |
| 22 URLFetcher::set_factory(NULL); | 25 URLFetcher::set_factory(NULL); |
| 23 } | 26 } |
| 24 | 27 |
| 25 TestURLFetcher::TestURLFetcher(int id, | 28 TestURLFetcher::TestURLFetcher(int id, |
| 26 const GURL& url, | 29 const GURL& url, |
| 27 URLFetcher::RequestType request_type, | 30 URLFetcher::RequestType request_type, |
| 28 content::URLFetcherDelegate* d) | 31 content::URLFetcherDelegate* d) |
|
wtc
2011/10/27 21:29:16
The |request_type| argument is not used. I am not
jam
2011/10/27 22:34:41
when doing this originally i wasn't sure if it's n
| |
| 29 : URLFetcher(url, request_type, d), | 32 : id_(id), |
| 30 id_(id), | |
| 31 original_url_(url), | 33 original_url_(url), |
| 32 did_receive_last_chunk_(false) { | 34 delegate_(d), |
| 35 did_receive_last_chunk_(false), | |
| 36 fake_load_flags_(0), | |
| 37 fake_response_code_(-1), | |
| 38 fake_response_destination_(STRING), | |
| 39 fake_was_fetched_via_proxy_(false), | |
| 40 fake_max_retries_(0) { | |
| 33 } | 41 } |
| 34 | 42 |
| 35 TestURLFetcher::~TestURLFetcher() { | 43 TestURLFetcher::~TestURLFetcher() { |
| 36 } | 44 } |
| 37 | 45 |
| 46 void TestURLFetcher::SetUploadData(const std::string& upload_content_type, | |
| 47 const std::string& upload_content) { | |
| 48 upload_data_ = upload_content; | |
| 49 } | |
| 50 | |
| 51 void TestURLFetcher::SetChunkedUpload(const std::string& upload_content_type) { | |
| 52 } | |
| 53 | |
| 38 void TestURLFetcher::AppendChunkToUpload(const std::string& data, | 54 void TestURLFetcher::AppendChunkToUpload(const std::string& data, |
| 39 bool is_last_chunk) { | 55 bool is_last_chunk) { |
| 40 DCHECK(!did_receive_last_chunk_); | 56 DCHECK(!did_receive_last_chunk_); |
| 41 did_receive_last_chunk_ = is_last_chunk; | 57 did_receive_last_chunk_ = is_last_chunk; |
| 42 chunks_.push_back(data); | 58 chunks_.push_back(data); |
| 43 } | 59 } |
| 44 | 60 |
| 61 void TestURLFetcher::SetLoadFlags(int load_flags) { | |
| 62 fake_load_flags_= load_flags; | |
| 63 } | |
| 64 | |
| 65 int TestURLFetcher::GetLoadFlags() const { | |
| 66 return fake_load_flags_; | |
| 67 } | |
| 68 | |
| 69 void TestURLFetcher::SetReferrer(const std::string& referrer) { | |
| 70 } | |
| 71 | |
| 72 void TestURLFetcher::SetExtraRequestHeaders( | |
| 73 const std::string& extra_request_headers) { | |
| 74 fake_extra_request_headers_.Clear(); | |
| 75 fake_extra_request_headers_.AddHeadersFromString(extra_request_headers); | |
| 76 } | |
| 77 | |
| 78 void TestURLFetcher::GetExtraRequestHeaders(net::HttpRequestHeaders* headers) { | |
| 79 *headers = fake_extra_request_headers_; | |
| 80 } | |
| 81 | |
| 82 void TestURLFetcher::SetRequestContext( | |
| 83 net::URLRequestContextGetter* request_context_getter) { | |
| 84 } | |
| 85 | |
| 86 void TestURLFetcher::SetAutomaticallyRetryOn5xx(bool retry) { | |
| 87 } | |
| 88 | |
| 89 void TestURLFetcher::SetMaxRetries(int max_retries) { | |
| 90 fake_max_retries_ = max_retries; | |
| 91 } | |
| 92 | |
| 93 int TestURLFetcher::GetMaxRetries() const { | |
| 94 return fake_max_retries_; | |
| 95 } | |
| 96 | |
| 97 base::TimeDelta TestURLFetcher::GetBackoffDelay() const { | |
| 98 return fake_backoff_delay_; | |
| 99 } | |
| 100 | |
| 101 void TestURLFetcher::SaveResponseToTemporaryFile( | |
| 102 scoped_refptr<base::MessageLoopProxy> file_message_loop_proxy) { | |
| 103 } | |
| 104 | |
| 105 net::HttpResponseHeaders* TestURLFetcher::GetResponseHeaders() const { | |
| 106 return fake_response_headers_; | |
| 107 } | |
| 108 | |
| 109 net::HostPortPair TestURLFetcher::GetSocketAddress() const { | |
| 110 NOTIMPLEMENTED(); | |
| 111 return net::HostPortPair(); | |
| 112 } | |
| 113 | |
| 114 bool TestURLFetcher::WasFetchedViaProxy() const { | |
| 115 return fake_was_fetched_via_proxy_; | |
| 116 } | |
| 117 | |
| 118 void TestURLFetcher::Start() { | |
| 119 // Overriden to do nothing. It is assumed the caller will notify the delegate. | |
| 120 } | |
| 121 | |
| 122 void TestURLFetcher::StartWithRequestContextGetter( | |
| 123 net::URLRequestContextGetter* request_context_getter) { | |
| 124 NOTIMPLEMENTED(); | |
| 125 } | |
| 126 | |
| 45 const GURL& TestURLFetcher::GetOriginalUrl() const { | 127 const GURL& TestURLFetcher::GetOriginalUrl() const { |
| 46 return original_url_; | 128 return original_url_; |
| 47 } | 129 } |
| 48 | 130 |
| 49 void TestURLFetcher::set_status(const net::URLRequestStatus& status) { | 131 const GURL& TestURLFetcher::GetUrl() const { |
| 50 fake_status_ = status; | 132 return fake_url_; |
| 51 } | 133 } |
| 52 | 134 |
| 53 void TestURLFetcher::set_was_fetched_via_proxy(bool flag) { | 135 const net::URLRequestStatus& TestURLFetcher::GetStatus() const { |
| 54 URLFetcher::set_was_fetched_via_proxy(flag); | 136 return fake_status_; |
| 55 } | 137 } |
| 56 | 138 |
| 57 void TestURLFetcher::set_response_headers( | 139 int TestURLFetcher::GetResponseCode() const { |
| 58 scoped_refptr<net::HttpResponseHeaders> headers) { | 140 return fake_response_code_; |
| 59 URLFetcher::set_response_headers(headers); | |
| 60 } | 141 } |
| 61 | 142 |
| 62 void TestURLFetcher::SetResponseString(const std::string& response) { | 143 const net::ResponseCookies& TestURLFetcher::GetCookies() const { |
| 63 SetResponseDestinationForTesting(STRING); | 144 return fake_cookies_; |
| 64 fake_response_string_ = response; | |
| 65 } | 145 } |
| 66 | 146 |
| 67 void TestURLFetcher::SetResponseFilePath(const FilePath& path) { | 147 bool TestURLFetcher::FileErrorOccurred( |
| 68 SetResponseDestinationForTesting(TEMP_FILE); | 148 base::PlatformFileError* out_error_code) const { |
| 69 fake_response_file_path_ = path; | 149 NOTIMPLEMENTED(); |
| 150 return false; | |
| 151 } | |
| 152 | |
| 153 void TestURLFetcher::ReceivedContentWasMalformed() { | |
| 70 } | 154 } |
| 71 | 155 |
| 72 bool TestURLFetcher::GetResponseAsString( | 156 bool TestURLFetcher::GetResponseAsString( |
| 73 std::string* out_response_string) const { | 157 std::string* out_response_string) const { |
| 74 if (GetResponseDestinationForTesting() != STRING) | 158 if (fake_response_destination_ != STRING) |
| 75 return false; | 159 return false; |
| 76 | 160 |
| 77 *out_response_string = fake_response_string_; | 161 *out_response_string = fake_response_string_; |
| 78 return true; | 162 return true; |
| 79 } | 163 } |
| 80 | 164 |
| 81 bool TestURLFetcher::GetResponseAsFilePath( | 165 bool TestURLFetcher::GetResponseAsFilePath( |
| 82 bool take_ownership, FilePath* out_response_path) const { | 166 bool take_ownership, FilePath* out_response_path) const { |
| 83 if (GetResponseDestinationForTesting() != TEMP_FILE) | 167 if (fake_response_destination_ != TEMP_FILE) |
| 84 return false; | 168 return false; |
| 85 | 169 |
| 86 *out_response_path = fake_response_file_path_; | 170 *out_response_path = fake_response_file_path_; |
| 87 return true; | 171 return true; |
| 88 } | 172 } |
| 89 | 173 |
| 174 void TestURLFetcher::set_status(const net::URLRequestStatus& status) { | |
| 175 fake_status_ = status; | |
| 176 } | |
| 177 | |
| 178 void TestURLFetcher::set_was_fetched_via_proxy(bool flag) { | |
| 179 fake_was_fetched_via_proxy_ = flag; | |
| 180 } | |
| 181 | |
| 182 void TestURLFetcher::set_response_headers( | |
| 183 scoped_refptr<net::HttpResponseHeaders> headers) { | |
| 184 fake_response_headers_ = headers; | |
| 185 } | |
| 186 | |
| 187 void TestURLFetcher::set_backoff_delay(base::TimeDelta backoff_delay) { | |
| 188 fake_backoff_delay_ = backoff_delay; | |
| 189 } | |
| 190 | |
| 191 void TestURLFetcher::SetResponseString(const std::string& response) { | |
| 192 fake_response_destination_ = STRING; | |
| 193 fake_response_string_ = response; | |
| 194 } | |
| 195 | |
| 196 void TestURLFetcher::SetResponseFilePath(const FilePath& path) { | |
| 197 fake_response_destination_ = TEMP_FILE; | |
| 198 fake_response_file_path_ = path; | |
| 199 } | |
| 200 | |
| 90 TestURLFetcherFactory::TestURLFetcherFactory() | 201 TestURLFetcherFactory::TestURLFetcherFactory() |
| 91 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 202 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 92 } | 203 } |
| 93 | 204 |
| 94 TestURLFetcherFactory::~TestURLFetcherFactory() {} | 205 TestURLFetcherFactory::~TestURLFetcherFactory() {} |
| 95 | 206 |
| 96 URLFetcher* TestURLFetcherFactory::CreateURLFetcher( | 207 content::URLFetcher* TestURLFetcherFactory::CreateURLFetcher( |
| 97 int id, | 208 int id, |
| 98 const GURL& url, | 209 const GURL& url, |
| 99 URLFetcher::RequestType request_type, | 210 content::URLFetcher::RequestType request_type, |
| 100 content::URLFetcherDelegate* d) { | 211 content::URLFetcherDelegate* d) { |
| 101 TestURLFetcher* fetcher = new TestURLFetcher(id, url, request_type, d); | 212 TestURLFetcher* fetcher = new TestURLFetcher(id, url, request_type, d); |
| 102 fetchers_[id] = fetcher; | 213 fetchers_[id] = fetcher; |
| 103 return fetcher; | 214 return fetcher; |
| 104 } | 215 } |
| 105 | 216 |
| 106 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { | 217 TestURLFetcher* TestURLFetcherFactory::GetFetcherByID(int id) const { |
| 107 Fetchers::const_iterator i = fetchers_.find(id); | 218 Fetchers::const_iterator i = fetchers_.find(id); |
| 108 return i == fetchers_.end() ? NULL : i->second; | 219 return i == fetchers_.end() ? NULL : i->second; |
| 109 } | 220 } |
| 110 | 221 |
| 111 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { | 222 void TestURLFetcherFactory::RemoveFetcherFromMap(int id) { |
| 112 Fetchers::iterator i = fetchers_.find(id); | 223 Fetchers::iterator i = fetchers_.find(id); |
| 113 DCHECK(i != fetchers_.end()); | 224 DCHECK(i != fetchers_.end()); |
| 114 fetchers_.erase(i); | 225 fetchers_.erase(i); |
| 115 } | 226 } |
| 116 | 227 |
| 117 const GURL& TestURLFetcher::GetUrl() const { | |
| 118 return fake_url_; | |
| 119 } | |
| 120 | |
| 121 const net::URLRequestStatus& TestURLFetcher::GetStatus() const { | |
| 122 return fake_status_; | |
| 123 } | |
| 124 | |
| 125 int TestURLFetcher::GetResponseCode() const { | |
| 126 return fake_response_code_; | |
| 127 } | |
| 128 | |
| 129 const net::ResponseCookies& TestURLFetcher::GetCookies() const { | |
| 130 return fake_cookies_; | |
| 131 } | |
| 132 | |
| 133 // This class is used by the FakeURLFetcherFactory below. | 228 // This class is used by the FakeURLFetcherFactory below. |
| 134 class FakeURLFetcher : public URLFetcher { | 229 class FakeURLFetcher : public TestURLFetcher { |
| 135 public: | 230 public: |
| 136 // Normal URL fetcher constructor but also takes in a pre-baked response. | 231 // Normal URL fetcher constructor but also takes in a pre-baked response. |
| 137 FakeURLFetcher(const GURL& url, RequestType request_type, | 232 FakeURLFetcher(const GURL& url, RequestType request_type, |
| 138 content::URLFetcherDelegate* d, | 233 content::URLFetcherDelegate* d, |
| 139 const std::string& response_data, bool success) | 234 const std::string& response_data, bool success) |
| 140 : URLFetcher(url, request_type, d), | 235 : TestURLFetcher(0, url, request_type, d), |
| 141 url_(url), | |
| 142 response_data_(response_data), | |
| 143 success_(success), | |
| 144 status_(success ? net::URLRequestStatus::SUCCESS : | |
| 145 net::URLRequestStatus::FAILED, 0), | |
| 146 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 236 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 237 set_status(net::URLRequestStatus( | |
| 238 success ? net::URLRequestStatus::SUCCESS :net::URLRequestStatus::FAILED, | |
|
wtc
2011/10/27 21:29:16
Add a space before net::URLRequestStatus::FAILED.
jam
2011/10/27 22:34:41
Done.
| |
| 239 0)); | |
| 240 set_response_code(success ? 200 : 500); | |
| 241 SetResponseString(response_data); | |
| 147 } | 242 } |
| 148 | 243 |
| 149 // Start the request. This will call the given delegate asynchronously | 244 // Start the request. This will call the given delegate asynchronously |
| 150 // with the pre-baked response as parameter. | 245 // with the pre-baked response as parameter. |
| 151 virtual void Start() OVERRIDE { | 246 virtual void Start() OVERRIDE { |
| 152 MessageLoop::current()->PostTask( | 247 MessageLoop::current()->PostTask( |
| 153 FROM_HERE, | 248 FROM_HERE, |
| 154 method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); | 249 method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); |
| 155 } | 250 } |
| 156 | 251 |
| 157 // These methods are overriden so we can use the version of | |
| 158 // OnURLFetchComplete that only has a single URLFetcher argument. | |
| 159 virtual const net::ResponseCookies& GetCookies() const OVERRIDE { | |
| 160 return cookies_; | |
| 161 } | |
| 162 | |
| 163 virtual bool GetResponseAsString( | |
| 164 std::string* out_response_string) const OVERRIDE { | |
| 165 *out_response_string = response_data_; | |
| 166 return true; | |
| 167 } | |
| 168 | |
| 169 virtual int GetResponseCode() const OVERRIDE { | |
| 170 return success_ ? 200 : 500; | |
| 171 } | |
| 172 | |
| 173 virtual const net::URLRequestStatus& GetStatus() const OVERRIDE { | |
| 174 return status_; | |
| 175 } | |
| 176 | |
| 177 virtual const GURL& GetUrl() const OVERRIDE { | 252 virtual const GURL& GetUrl() const OVERRIDE { |
| 178 return url_; | 253 return TestURLFetcher::GetOriginalUrl(); |
| 179 } | 254 } |
| 180 | 255 |
| 181 private: | 256 private: |
| 182 virtual ~FakeURLFetcher() { | 257 virtual ~FakeURLFetcher() { |
| 183 } | 258 } |
| 184 | 259 |
| 185 // This is the method which actually calls the delegate that is passed in the | 260 // This is the method which actually calls the delegate that is passed in the |
| 186 // constructor. | 261 // constructor. |
| 187 void RunDelegate() { | 262 void RunDelegate() { |
| 188 delegate()->OnURLFetchComplete(this); | 263 delegate()->OnURLFetchComplete(this); |
| 189 } | 264 } |
| 190 | 265 |
| 191 // Pre-baked response data and flag which indicates whether the request should | |
| 192 // be successful or not. | |
| 193 GURL url_; | |
| 194 std::string response_data_; | |
| 195 bool success_; | |
| 196 net::URLRequestStatus status_; | |
| 197 net::ResponseCookies cookies_; | |
| 198 | |
| 199 // Method factory used to run the delegate. | 266 // Method factory used to run the delegate. |
| 200 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; | 267 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; |
| 201 | 268 |
| 202 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | 269 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); |
| 203 }; | 270 }; |
| 204 | 271 |
| 205 FakeURLFetcherFactory::FakeURLFetcherFactory() | 272 FakeURLFetcherFactory::FakeURLFetcherFactory() |
| 206 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 273 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
| 207 } | 274 } |
| 208 | 275 |
| 209 FakeURLFetcherFactory::FakeURLFetcherFactory( | 276 FakeURLFetcherFactory::FakeURLFetcherFactory( |
| 210 URLFetcher::Factory* default_factory) | 277 content::URLFetcherFactory* default_factory) |
| 211 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), | 278 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)), |
| 212 default_factory_(default_factory) { | 279 default_factory_(default_factory) { |
| 213 } | 280 } |
| 214 | 281 |
| 215 FakeURLFetcherFactory::~FakeURLFetcherFactory() {} | 282 FakeURLFetcherFactory::~FakeURLFetcherFactory() {} |
| 216 | 283 |
| 217 URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( | 284 content::URLFetcher* FakeURLFetcherFactory::CreateURLFetcher( |
| 218 int id, | 285 int id, |
| 219 const GURL& url, | 286 const GURL& url, |
| 220 URLFetcher::RequestType request_type, | 287 content::URLFetcher::RequestType request_type, |
| 221 content::URLFetcherDelegate* d) { | 288 content::URLFetcherDelegate* d) { |
| 222 FakeResponseMap::const_iterator it = fake_responses_.find(url); | 289 FakeResponseMap::const_iterator it = fake_responses_.find(url); |
| 223 if (it == fake_responses_.end()) { | 290 if (it == fake_responses_.end()) { |
| 224 if (default_factory_ == NULL) { | 291 if (default_factory_ == NULL) { |
| 225 // If we don't have a baked response for that URL we return NULL. | 292 // If we don't have a baked response for that URL we return NULL. |
| 226 DLOG(ERROR) << "No baked response for URL: " << url.spec(); | 293 DLOG(ERROR) << "No baked response for URL: " << url.spec(); |
| 227 return NULL; | 294 return NULL; |
| 228 } else { | 295 } else { |
| 229 return default_factory_->CreateURLFetcher(id, url, request_type, d); | 296 return default_factory_->CreateURLFetcher(id, url, request_type, d); |
| 230 } | 297 } |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 241 } | 308 } |
| 242 | 309 |
| 243 void FakeURLFetcherFactory::ClearFakeResponses() { | 310 void FakeURLFetcherFactory::ClearFakeResponses() { |
| 244 fake_responses_.clear(); | 311 fake_responses_.clear(); |
| 245 } | 312 } |
| 246 | 313 |
| 247 URLFetcherFactory::URLFetcherFactory() {} | 314 URLFetcherFactory::URLFetcherFactory() {} |
| 248 | 315 |
| 249 URLFetcherFactory::~URLFetcherFactory() {} | 316 URLFetcherFactory::~URLFetcherFactory() {} |
| 250 | 317 |
| 251 URLFetcher* URLFetcherFactory::CreateURLFetcher( | 318 content::URLFetcher* URLFetcherFactory::CreateURLFetcher( |
| 252 int id, | 319 int id, |
| 253 const GURL& url, | 320 const GURL& url, |
| 254 URLFetcher::RequestType request_type, | 321 content::URLFetcher::RequestType request_type, |
| 255 content::URLFetcherDelegate* d) { | 322 content::URLFetcherDelegate* d) { |
| 256 return new URLFetcher(url, request_type, d); | 323 return new URLFetcher(url, request_type, d); |
| 257 } | 324 } |
| OLD | NEW |