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" |
(...skipping 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 // This class is used by the FakeURLFetcherFactory below. | 118 // This class is used by the FakeURLFetcherFactory below. |
119 class FakeURLFetcher : public URLFetcher { | 119 class FakeURLFetcher : public URLFetcher { |
120 public: | 120 public: |
121 // Normal URL fetcher constructor but also takes in a pre-baked response. | 121 // Normal URL fetcher constructor but also takes in a pre-baked response. |
122 FakeURLFetcher(const GURL& url, RequestType request_type, Delegate* d, | 122 FakeURLFetcher(const GURL& url, RequestType request_type, Delegate* d, |
123 const std::string& response_data, bool success) | 123 const std::string& response_data, bool success) |
124 : URLFetcher(url, request_type, d), | 124 : URLFetcher(url, request_type, d), |
125 url_(url), | 125 url_(url), |
126 response_data_(response_data), | 126 response_data_(response_data), |
127 success_(success), | 127 success_(success), |
| 128 status_(success ? net::URLRequestStatus::SUCCESS : |
| 129 net::URLRequestStatus::FAILED, 0), |
128 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | 130 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
129 } | 131 } |
130 | 132 |
131 // Start the request. This will call the given delegate asynchronously | 133 // Start the request. This will call the given delegate asynchronously |
132 // with the pre-baked response as parameter. | 134 // with the pre-baked response as parameter. |
133 virtual void Start() { | 135 virtual void Start() OVERRIDE { |
134 MessageLoop::current()->PostTask( | 136 MessageLoop::current()->PostTask( |
135 FROM_HERE, | 137 FROM_HERE, |
136 method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); | 138 method_factory_.NewRunnableMethod(&FakeURLFetcher::RunDelegate)); |
137 } | 139 } |
138 | 140 |
| 141 // These methods are overriden so we can use the version of |
| 142 // OnURLFetchComplete that only has a single URLFetcher argument. |
| 143 virtual const net::ResponseCookies& cookies() const OVERRIDE { |
| 144 return cookies_; |
| 145 } |
| 146 |
| 147 virtual const std::string& GetResponseStringRef() const OVERRIDE { |
| 148 return response_data_; |
| 149 } |
| 150 |
| 151 virtual bool GetResponseAsString( |
| 152 std::string* out_response_string) const OVERRIDE { |
| 153 *out_response_string = response_data_; |
| 154 return true; |
| 155 } |
| 156 |
| 157 virtual int response_code() const OVERRIDE { |
| 158 return success_ ? 200 : 500; |
| 159 } |
| 160 |
| 161 virtual const net::URLRequestStatus& status() const OVERRIDE { |
| 162 return status_; |
| 163 } |
| 164 |
| 165 virtual const GURL& url() const OVERRIDE { |
| 166 return url_; |
| 167 } |
| 168 |
139 private: | 169 private: |
140 virtual ~FakeURLFetcher() { | 170 virtual ~FakeURLFetcher() { |
141 } | 171 } |
142 | 172 |
143 // This is the method which actually calls the delegate that is passed in the | 173 // This is the method which actually calls the delegate that is passed in the |
144 // constructor. | 174 // constructor. |
145 void RunDelegate() { | 175 void RunDelegate() { |
146 net::URLRequestStatus status; | 176 delegate()->OnURLFetchComplete(this); |
147 status.set_status(success_ ? net::URLRequestStatus::SUCCESS : | |
148 net::URLRequestStatus::FAILED); | |
149 delegate()->OnURLFetchComplete(this, url_, status, success_ ? 200 : 500, | |
150 net::ResponseCookies(), response_data_); | |
151 } | 177 } |
152 | 178 |
153 // Pre-baked response data and flag which indicates whether the request should | 179 // Pre-baked response data and flag which indicates whether the request should |
154 // be successful or not. | 180 // be successful or not. |
155 GURL url_; | 181 GURL url_; |
156 std::string response_data_; | 182 std::string response_data_; |
157 bool success_; | 183 bool success_; |
| 184 net::URLRequestStatus status_; |
| 185 net::ResponseCookies cookies_; |
158 | 186 |
159 // Method factory used to run the delegate. | 187 // Method factory used to run the delegate. |
160 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; | 188 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; |
161 | 189 |
162 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | 190 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); |
163 }; | 191 }; |
164 | 192 |
165 FakeURLFetcherFactory::FakeURLFetcherFactory() | 193 FakeURLFetcherFactory::FakeURLFetcherFactory() |
166 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 194 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
167 } | 195 } |
(...skipping 25 matching lines...) Expand all Loading... |
193 it->second.first, it->second.second); | 221 it->second.first, it->second.second); |
194 } | 222 } |
195 | 223 |
196 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, | 224 void FakeURLFetcherFactory::SetFakeResponse(const std::string& url, |
197 const std::string& response_data, | 225 const std::string& response_data, |
198 bool success) { | 226 bool success) { |
199 // Overwrite existing URL if it already exists. | 227 // Overwrite existing URL if it already exists. |
200 fake_responses_[GURL(url)] = std::make_pair(response_data, success); | 228 fake_responses_[GURL(url)] = std::make_pair(response_data, success); |
201 } | 229 } |
202 | 230 |
203 void FakeURLFetcherFactory::ClearFakeReponses() { | 231 void FakeURLFetcherFactory::ClearFakeResponses() { |
204 fake_responses_.clear(); | 232 fake_responses_.clear(); |
205 } | 233 } |
206 | 234 |
207 URLFetcherFactory::URLFetcherFactory() {} | 235 URLFetcherFactory::URLFetcherFactory() {} |
208 | 236 |
209 URLFetcherFactory::~URLFetcherFactory() {} | 237 URLFetcherFactory::~URLFetcherFactory() {} |
210 | 238 |
211 URLFetcher* URLFetcherFactory::CreateURLFetcher( | 239 URLFetcher* URLFetcherFactory::CreateURLFetcher( |
212 int id, | 240 int id, |
213 const GURL& url, | 241 const GURL& url, |
214 URLFetcher::RequestType request_type, | 242 URLFetcher::RequestType request_type, |
215 URLFetcher::Delegate* d) { | 243 URLFetcher::Delegate* d) { |
216 return new URLFetcher(url, request_type, d); | 244 return new URLFetcher(url, request_type, d); |
217 } | 245 } |
OLD | NEW |