OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/command_line.h" | 5 #include "base/command_line.h" |
6 #include "base/memory/ref_counted.h" | 6 #include "base/memory/ref_counted.h" |
7 #include "base/message_loop_proxy.h" | 7 #include "base/message_loop_proxy.h" |
8 #include "base/synchronization/waitable_event.h" | 8 #include "base/synchronization/waitable_event.h" |
9 #include "base/threading/thread.h" | 9 #include "base/threading/thread.h" |
10 #include "base/values.h" | 10 #include "base/values.h" |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
87 class CloudPrintURLFetcherTest : public testing::Test, | 87 class CloudPrintURLFetcherTest : public testing::Test, |
88 public CloudPrintURLFetcherDelegate { | 88 public CloudPrintURLFetcherDelegate { |
89 public: | 89 public: |
90 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { } | 90 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { } |
91 | 91 |
92 // Creates a URLFetcher, using the program's main thread to do IO. | 92 // Creates a URLFetcher, using the program's main thread to do IO. |
93 virtual void CreateFetcher(const GURL& url, int max_retries); | 93 virtual void CreateFetcher(const GURL& url, int max_retries); |
94 | 94 |
95 // CloudPrintURLFetcher::Delegate | 95 // CloudPrintURLFetcher::Delegate |
96 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( | 96 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( |
97 const content::URLFetcher* source, | 97 const net::URLFetcher* source, |
98 const GURL& url, | 98 const GURL& url, |
99 const net::URLRequestStatus& status, | 99 const net::URLRequestStatus& status, |
100 int response_code, | 100 int response_code, |
101 const net::ResponseCookies& cookies, | 101 const net::ResponseCookies& cookies, |
102 const std::string& data); | 102 const std::string& data); |
103 | 103 |
104 virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() { | 104 virtual CloudPrintURLFetcher::ResponseAction OnRequestAuthError() { |
105 ADD_FAILURE(); | 105 ADD_FAILURE(); |
106 return CloudPrintURLFetcher::STOP_PROCESSING; | 106 return CloudPrintURLFetcher::STOP_PROCESSING; |
107 } | 107 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 Time start_time_; | 140 Time start_time_; |
141 scoped_refptr<TestCloudPrintURLFetcher> fetcher_; | 141 scoped_refptr<TestCloudPrintURLFetcher> fetcher_; |
142 }; | 142 }; |
143 | 143 |
144 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { | 144 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { |
145 public: | 145 public: |
146 CloudPrintURLFetcherBasicTest() | 146 CloudPrintURLFetcherBasicTest() |
147 : handle_raw_response_(false), handle_raw_data_(false) { } | 147 : handle_raw_response_(false), handle_raw_data_(false) { } |
148 // CloudPrintURLFetcher::Delegate | 148 // CloudPrintURLFetcher::Delegate |
149 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( | 149 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( |
150 const content::URLFetcher* source, | 150 const net::URLFetcher* source, |
151 const GURL& url, | 151 const GURL& url, |
152 const net::URLRequestStatus& status, | 152 const net::URLRequestStatus& status, |
153 int response_code, | 153 int response_code, |
154 const net::ResponseCookies& cookies, | 154 const net::ResponseCookies& cookies, |
155 const std::string& data); | 155 const std::string& data); |
156 | 156 |
157 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( | 157 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
158 const content::URLFetcher* source, | 158 const net::URLFetcher* source, |
159 const GURL& url, | 159 const GURL& url, |
160 const std::string& data); | 160 const std::string& data); |
161 | 161 |
162 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( | 162 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( |
163 const content::URLFetcher* source, | 163 const net::URLFetcher* source, |
164 const GURL& url, | 164 const GURL& url, |
165 DictionaryValue* json_data, | 165 DictionaryValue* json_data, |
166 bool succeeded); | 166 bool succeeded); |
167 | 167 |
168 void SetHandleRawResponse(bool handle_raw_response) { | 168 void SetHandleRawResponse(bool handle_raw_response) { |
169 handle_raw_response_ = handle_raw_response; | 169 handle_raw_response_ = handle_raw_response; |
170 } | 170 } |
171 void SetHandleRawData(bool handle_raw_data) { | 171 void SetHandleRawData(bool handle_raw_data) { |
172 handle_raw_data_ = handle_raw_data; | 172 handle_raw_data_ = handle_raw_data; |
173 } | 173 } |
174 private: | 174 private: |
175 bool handle_raw_response_; | 175 bool handle_raw_response_; |
176 bool handle_raw_data_; | 176 bool handle_raw_data_; |
177 }; | 177 }; |
178 | 178 |
179 // Version of CloudPrintURLFetcherTest that tests overload protection. | 179 // Version of CloudPrintURLFetcherTest that tests overload protection. |
180 class CloudPrintURLFetcherOverloadTest : public CloudPrintURLFetcherTest { | 180 class CloudPrintURLFetcherOverloadTest : public CloudPrintURLFetcherTest { |
181 public: | 181 public: |
182 CloudPrintURLFetcherOverloadTest() : response_count_(0) { | 182 CloudPrintURLFetcherOverloadTest() : response_count_(0) { |
183 } | 183 } |
184 | 184 |
185 // CloudPrintURLFetcher::Delegate | 185 // CloudPrintURLFetcher::Delegate |
186 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( | 186 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
187 const content::URLFetcher* source, | 187 const net::URLFetcher* source, |
188 const GURL& url, | 188 const GURL& url, |
189 const std::string& data); | 189 const std::string& data); |
190 | 190 |
191 private: | 191 private: |
192 int response_count_; | 192 int response_count_; |
193 }; | 193 }; |
194 | 194 |
195 // Version of CloudPrintURLFetcherTest that tests backoff protection. | 195 // Version of CloudPrintURLFetcherTest that tests backoff protection. |
196 class CloudPrintURLFetcherRetryBackoffTest : public CloudPrintURLFetcherTest { | 196 class CloudPrintURLFetcherRetryBackoffTest : public CloudPrintURLFetcherTest { |
197 public: | 197 public: |
198 CloudPrintURLFetcherRetryBackoffTest() : response_count_(0) { | 198 CloudPrintURLFetcherRetryBackoffTest() : response_count_(0) { |
199 } | 199 } |
200 | 200 |
201 // CloudPrintURLFetcher::Delegate | 201 // CloudPrintURLFetcher::Delegate |
202 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( | 202 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
203 const content::URLFetcher* source, | 203 const net::URLFetcher* source, |
204 const GURL& url, | 204 const GURL& url, |
205 const std::string& data); | 205 const std::string& data); |
206 | 206 |
207 virtual void OnRequestGiveUp(); | 207 virtual void OnRequestGiveUp(); |
208 | 208 |
209 private: | 209 private: |
210 int response_count_; | 210 int response_count_; |
211 }; | 211 }; |
212 | 212 |
213 | 213 |
214 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) { | 214 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) { |
215 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy()); | 215 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy()); |
216 | 216 |
217 // Registers an entry for test url. It only allows 3 requests to be sent | 217 // Registers an entry for test url. It only allows 3 requests to be sent |
218 // in 200 milliseconds. | 218 // in 200 milliseconds. |
219 scoped_refptr<net::URLRequestThrottlerEntry> entry( | 219 scoped_refptr<net::URLRequestThrottlerEntry> entry( |
220 new net::URLRequestThrottlerEntry( | 220 new net::URLRequestThrottlerEntry( |
221 fetcher_->throttler_manager(), "", 200, 3, 1, 2.0, 0.0, 256)); | 221 fetcher_->throttler_manager(), "", 200, 3, 1, 2.0, 0.0, 256)); |
222 fetcher_->throttler_manager()->OverrideEntryForTests(url, entry); | 222 fetcher_->throttler_manager()->OverrideEntryForTests(url, entry); |
223 | 223 |
224 max_retries_ = max_retries; | 224 max_retries_ = max_retries; |
225 start_time_ = Time::Now(); | 225 start_time_ = Time::Now(); |
226 fetcher_->StartGetRequest(url, this, max_retries_, std::string()); | 226 fetcher_->StartGetRequest(url, this, max_retries_, std::string()); |
227 } | 227 } |
228 | 228 |
229 CloudPrintURLFetcher::ResponseAction | 229 CloudPrintURLFetcher::ResponseAction |
230 CloudPrintURLFetcherTest::HandleRawResponse( | 230 CloudPrintURLFetcherTest::HandleRawResponse( |
231 const content::URLFetcher* source, | 231 const net::URLFetcher* source, |
232 const GURL& url, | 232 const GURL& url, |
233 const net::URLRequestStatus& status, | 233 const net::URLRequestStatus& status, |
234 int response_code, | 234 int response_code, |
235 const net::ResponseCookies& cookies, | 235 const net::ResponseCookies& cookies, |
236 const std::string& data) { | 236 const std::string& data) { |
237 EXPECT_TRUE(status.is_success()); | 237 EXPECT_TRUE(status.is_success()); |
238 EXPECT_EQ(200, response_code); // HTTP OK | 238 EXPECT_EQ(200, response_code); // HTTP OK |
239 EXPECT_FALSE(data.empty()); | 239 EXPECT_FALSE(data.empty()); |
240 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 240 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
241 } | 241 } |
242 | 242 |
243 CloudPrintURLFetcher::ResponseAction | 243 CloudPrintURLFetcher::ResponseAction |
244 CloudPrintURLFetcherBasicTest::HandleRawResponse( | 244 CloudPrintURLFetcherBasicTest::HandleRawResponse( |
245 const content::URLFetcher* source, | 245 const net::URLFetcher* source, |
246 const GURL& url, | 246 const GURL& url, |
247 const net::URLRequestStatus& status, | 247 const net::URLRequestStatus& status, |
248 int response_code, | 248 int response_code, |
249 const net::ResponseCookies& cookies, | 249 const net::ResponseCookies& cookies, |
250 const std::string& data) { | 250 const std::string& data) { |
251 EXPECT_TRUE(status.is_success()); | 251 EXPECT_TRUE(status.is_success()); |
252 EXPECT_EQ(200, response_code); // HTTP OK | 252 EXPECT_EQ(200, response_code); // HTTP OK |
253 EXPECT_FALSE(data.empty()); | 253 EXPECT_FALSE(data.empty()); |
254 | 254 |
255 if (handle_raw_response_) { | 255 if (handle_raw_response_) { |
256 // If the current message loop is not the IO loop, it will be shut down when | 256 // If the current message loop is not the IO loop, it will be shut down when |
257 // the main loop returns and this thread subsequently goes out of scope. | 257 // the main loop returns and this thread subsequently goes out of scope. |
258 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 258 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
259 return CloudPrintURLFetcher::STOP_PROCESSING; | 259 return CloudPrintURLFetcher::STOP_PROCESSING; |
260 } | 260 } |
261 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 261 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
262 } | 262 } |
263 | 263 |
264 CloudPrintURLFetcher::ResponseAction | 264 CloudPrintURLFetcher::ResponseAction |
265 CloudPrintURLFetcherBasicTest::HandleRawData( | 265 CloudPrintURLFetcherBasicTest::HandleRawData( |
266 const content::URLFetcher* source, | 266 const net::URLFetcher* source, |
267 const GURL& url, | 267 const GURL& url, |
268 const std::string& data) { | 268 const std::string& data) { |
269 // We should never get here if we returned true in HandleRawResponse | 269 // We should never get here if we returned true in HandleRawResponse |
270 EXPECT_FALSE(handle_raw_response_); | 270 EXPECT_FALSE(handle_raw_response_); |
271 if (handle_raw_data_) { | 271 if (handle_raw_data_) { |
272 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 272 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
273 return CloudPrintURLFetcher::STOP_PROCESSING; | 273 return CloudPrintURLFetcher::STOP_PROCESSING; |
274 } | 274 } |
275 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 275 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
276 } | 276 } |
277 | 277 |
278 CloudPrintURLFetcher::ResponseAction | 278 CloudPrintURLFetcher::ResponseAction |
279 CloudPrintURLFetcherBasicTest::HandleJSONData( | 279 CloudPrintURLFetcherBasicTest::HandleJSONData( |
280 const content::URLFetcher* source, | 280 const net::URLFetcher* source, |
281 const GURL& url, | 281 const GURL& url, |
282 DictionaryValue* json_data, | 282 DictionaryValue* json_data, |
283 bool succeeded) { | 283 bool succeeded) { |
284 // We should never get here if we returned true in one of the above methods. | 284 // We should never get here if we returned true in one of the above methods. |
285 EXPECT_FALSE(handle_raw_response_); | 285 EXPECT_FALSE(handle_raw_response_); |
286 EXPECT_FALSE(handle_raw_data_); | 286 EXPECT_FALSE(handle_raw_data_); |
287 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 287 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
288 return CloudPrintURLFetcher::STOP_PROCESSING; | 288 return CloudPrintURLFetcher::STOP_PROCESSING; |
289 } | 289 } |
290 | 290 |
291 CloudPrintURLFetcher::ResponseAction | 291 CloudPrintURLFetcher::ResponseAction |
292 CloudPrintURLFetcherOverloadTest::HandleRawData( | 292 CloudPrintURLFetcherOverloadTest::HandleRawData( |
293 const content::URLFetcher* source, | 293 const net::URLFetcher* source, |
294 const GURL& url, | 294 const GURL& url, |
295 const std::string& data) { | 295 const std::string& data) { |
296 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 296 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
297 response_count_++; | 297 response_count_++; |
298 if (response_count_ < 20) { | 298 if (response_count_ < 20) { |
299 fetcher_->StartGetRequest(url, | 299 fetcher_->StartGetRequest(url, |
300 this, | 300 this, |
301 max_retries_, | 301 max_retries_, |
302 std::string()); | 302 std::string()); |
303 } else { | 303 } else { |
304 // We have already sent 20 requests continuously. And we expect that | 304 // We have already sent 20 requests continuously. And we expect that |
305 // it takes more than 1 second due to the overload protection settings. | 305 // it takes more than 1 second due to the overload protection settings. |
306 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 306 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
307 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); | 307 io_message_loop_proxy()->PostTask(FROM_HERE, MessageLoop::QuitClosure()); |
308 } | 308 } |
309 return CloudPrintURLFetcher::STOP_PROCESSING; | 309 return CloudPrintURLFetcher::STOP_PROCESSING; |
310 } | 310 } |
311 | 311 |
312 CloudPrintURLFetcher::ResponseAction | 312 CloudPrintURLFetcher::ResponseAction |
313 CloudPrintURLFetcherRetryBackoffTest::HandleRawData( | 313 CloudPrintURLFetcherRetryBackoffTest::HandleRawData( |
314 const content::URLFetcher* source, | 314 const net::URLFetcher* source, |
315 const GURL& url, | 315 const GURL& url, |
316 const std::string& data) { | 316 const std::string& data) { |
317 response_count_++; | 317 response_count_++; |
318 // First attempt + 11 retries = 12 total responses. | 318 // First attempt + 11 retries = 12 total responses. |
319 EXPECT_LE(response_count_, 12); | 319 EXPECT_LE(response_count_, 12); |
320 return CloudPrintURLFetcher::RETRY_REQUEST; | 320 return CloudPrintURLFetcher::RETRY_REQUEST; |
321 } | 321 } |
322 | 322 |
323 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() { | 323 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() { |
324 // It takes more than 200 ms to finish all 11 requests. | 324 // It takes more than 200 ms to finish all 11 requests. |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
369 FilePath(kDocRoot)); | 369 FilePath(kDocRoot)); |
370 ASSERT_TRUE(test_server.Start()); | 370 ASSERT_TRUE(test_server.Start()); |
371 | 371 |
372 GURL url(test_server.GetURL("defaultresponse")); | 372 GURL url(test_server.GetURL("defaultresponse")); |
373 CreateFetcher(url, 11); | 373 CreateFetcher(url, 11); |
374 | 374 |
375 MessageLoop::current()->Run(); | 375 MessageLoop::current()->Run(); |
376 } | 376 } |
377 | 377 |
378 } // namespace. | 378 } // namespace. |
OLD | NEW |