OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 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 CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ | 5 #ifndef CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ |
6 #define CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ | 6 #define CHROME_COMMON_NET_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 "chrome/common/net/url_fetcher.h" | 14 #include "chrome/common/net/url_fetcher.h" |
| 15 #include "net/url_request/url_request_status.h" |
15 #include "googleurl/src/gurl.h" | 16 #include "googleurl/src/gurl.h" |
16 | 17 |
17 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of | 18 // TestURLFetcher and TestURLFetcherFactory are used for testing consumers of |
18 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates | 19 // URLFetcher. TestURLFetcherFactory is a URLFetcher::Factory that creates |
19 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is | 20 // TestURLFetchers. TestURLFetcher::Start is overriden to do nothing. It is |
20 // expected that you'll grab the delegate from the TestURLFetcher and invoke | 21 // expected that you'll grab the delegate from the TestURLFetcher and invoke |
21 // the callback method when appropriate. In this way it's easy to mock a | 22 // the callback method when appropriate. In this way it's easy to mock a |
22 // URLFetcher. | 23 // URLFetcher. |
23 // Typical usage: | 24 // Typical usage: |
24 // // TestURLFetcher requires a MessageLoop: | 25 // // TestURLFetcher requires a MessageLoop: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
67 | 68 |
68 // Returns the data uploaded on this URLFetcher. | 69 // Returns the data uploaded on this URLFetcher. |
69 const std::string& upload_data() const { return URLFetcher::upload_data(); } | 70 const std::string& upload_data() const { return URLFetcher::upload_data(); } |
70 | 71 |
71 // Returns the chunks of data uploaded on this URLFetcher. | 72 // Returns the chunks of data uploaded on this URLFetcher. |
72 const std::list<std::string>& upload_chunks() const { return chunks_; } | 73 const std::list<std::string>& upload_chunks() const { return chunks_; } |
73 | 74 |
74 // Returns the delegate installed on the URLFetcher. | 75 // Returns the delegate installed on the URLFetcher. |
75 Delegate* delegate() const { return URLFetcher::delegate(); } | 76 Delegate* delegate() const { return URLFetcher::delegate(); } |
76 | 77 |
| 78 void set_url(const GURL& url) { fake_url_ = url; } |
| 79 const GURL& url() const { return fake_url_; } |
| 80 |
| 81 void set_status(const net::URLRequestStatus& status); |
| 82 const net::URLRequestStatus& status() const { return fake_status_; } |
| 83 |
| 84 void set_response_code(int response_code) { |
| 85 fake_response_code_ = response_code; |
| 86 } |
| 87 virtual int response_code() const { return fake_response_code_; } |
| 88 |
| 89 // Set string data. |
| 90 void SetResponseString(const std::string& response); |
| 91 |
| 92 // Set File data. |
| 93 void SetResponseFilePath(const FilePath& path); |
| 94 |
| 95 // Override response access functions to return fake data. |
| 96 virtual bool GetResponseAsString(std::string* out_response_string) const; |
| 97 virtual bool GetResponseAsFilePath(bool take_ownership, |
| 98 FilePath* out_response_path) const; |
| 99 |
77 private: | 100 private: |
78 const int id_; | 101 const int id_; |
79 const GURL original_url_; | 102 const GURL original_url_; |
80 std::list<std::string> chunks_; | 103 std::list<std::string> chunks_; |
81 bool did_receive_last_chunk_; | 104 bool did_receive_last_chunk_; |
82 | 105 |
| 106 // User can use set_* methods to provide values returned by getters. |
| 107 // Setting the real values is not possible, because the real class |
| 108 // has no setters. The data is a private member of a class defined |
| 109 // in a .cc file, so we can't get at it with friendship. |
| 110 GURL fake_url_; |
| 111 net::URLRequestStatus fake_status_; |
| 112 int fake_response_code_; |
| 113 std::string fake_response_string_; |
| 114 FilePath fake_response_file_path_; |
| 115 |
83 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); | 116 DISALLOW_COPY_AND_ASSIGN(TestURLFetcher); |
84 }; | 117 }; |
85 | 118 |
86 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers | 119 // Simple URLFetcher::Factory method that creates TestURLFetchers. All fetchers |
87 // are registered in a map by the id passed to the create method. | 120 // are registered in a map by the id passed to the create method. |
88 class TestURLFetcherFactory : public URLFetcher::Factory { | 121 class TestURLFetcherFactory : public URLFetcher::Factory { |
89 public: | 122 public: |
90 TestURLFetcherFactory(); | 123 TestURLFetcherFactory(); |
91 virtual ~TestURLFetcherFactory(); | 124 virtual ~TestURLFetcherFactory(); |
92 | 125 |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 void ClearFakeReponses(); | 195 void ClearFakeReponses(); |
163 | 196 |
164 private: | 197 private: |
165 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; | 198 typedef std::map<GURL, std::pair<std::string, bool> > FakeResponseMap; |
166 FakeResponseMap fake_responses_; | 199 FakeResponseMap fake_responses_; |
167 | 200 |
168 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); | 201 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcherFactory); |
169 }; | 202 }; |
170 | 203 |
171 #endif // CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ | 204 #endif // CHROME_COMMON_NET_TEST_URL_FETCHER_FACTORY_H_ |
OLD | NEW |