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 |
139 private: | 165 private: |
140 virtual ~FakeURLFetcher() { | 166 virtual ~FakeURLFetcher() { |
141 } | 167 } |
142 | 168 |
143 // This is the method which actually calls the delegate that is passed in the | 169 // This is the method which actually calls the delegate that is passed in the |
144 // constructor. | 170 // constructor. |
145 void RunDelegate() { | 171 void RunDelegate() { |
146 net::URLRequestStatus status; | 172 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 } | 173 } |
152 | 174 |
153 // Pre-baked response data and flag which indicates whether the request should | 175 // Pre-baked response data and flag which indicates whether the request should |
154 // be successful or not. | 176 // be successful or not. |
155 GURL url_; | 177 GURL url_; |
156 std::string response_data_; | 178 std::string response_data_; |
157 bool success_; | 179 bool success_; |
| 180 net::URLRequestStatus status_; |
| 181 net::ResponseCookies cookies_; |
158 | 182 |
159 // Method factory used to run the delegate. | 183 // Method factory used to run the delegate. |
160 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; | 184 ScopedRunnableMethodFactory<FakeURLFetcher> method_factory_; |
161 | 185 |
162 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); | 186 DISALLOW_COPY_AND_ASSIGN(FakeURLFetcher); |
163 }; | 187 }; |
164 | 188 |
165 FakeURLFetcherFactory::FakeURLFetcherFactory() | 189 FakeURLFetcherFactory::FakeURLFetcherFactory() |
166 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 190 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
167 } | 191 } |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
208 | 232 |
209 URLFetcherFactory::~URLFetcherFactory() {} | 233 URLFetcherFactory::~URLFetcherFactory() {} |
210 | 234 |
211 URLFetcher* URLFetcherFactory::CreateURLFetcher( | 235 URLFetcher* URLFetcherFactory::CreateURLFetcher( |
212 int id, | 236 int id, |
213 const GURL& url, | 237 const GURL& url, |
214 URLFetcher::RequestType request_type, | 238 URLFetcher::RequestType request_type, |
215 URLFetcher::Delegate* d) { | 239 URLFetcher::Delegate* d) { |
216 return new URLFetcher(url, request_type, d); | 240 return new URLFetcher(url, request_type, d); |
217 } | 241 } |
OLD | NEW |