| 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 "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 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 class CloudPrintURLFetcherTest : public testing::Test, | 70 class CloudPrintURLFetcherTest : public testing::Test, |
| 71 public CloudPrintURLFetcherDelegate { | 71 public CloudPrintURLFetcherDelegate { |
| 72 public: | 72 public: |
| 73 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { } | 73 CloudPrintURLFetcherTest() : max_retries_(0), fetcher_(NULL) { } |
| 74 | 74 |
| 75 // Creates a URLFetcher, using the program's main thread to do IO. | 75 // Creates a URLFetcher, using the program's main thread to do IO. |
| 76 virtual void CreateFetcher(const GURL& url, int max_retries); | 76 virtual void CreateFetcher(const GURL& url, int max_retries); |
| 77 | 77 |
| 78 // CloudPrintURLFetcher::Delegate | 78 // CloudPrintURLFetcher::Delegate |
| 79 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( | 79 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( |
| 80 const URLFetcher* source, | 80 const content::URLFetcher* source, |
| 81 const GURL& url, | 81 const GURL& url, |
| 82 const net::URLRequestStatus& status, | 82 const net::URLRequestStatus& status, |
| 83 int response_code, | 83 int response_code, |
| 84 const net::ResponseCookies& cookies, | 84 const net::ResponseCookies& cookies, |
| 85 const std::string& data); | 85 const std::string& data); |
| 86 | 86 |
| 87 virtual void OnRequestAuthError() { | 87 virtual void OnRequestAuthError() { |
| 88 ADD_FAILURE(); | 88 ADD_FAILURE(); |
| 89 } | 89 } |
| 90 | 90 |
| (...skipping 27 matching lines...) Expand all Loading... |
| 118 Time start_time_; | 118 Time start_time_; |
| 119 scoped_refptr<CloudPrintURLFetcher> fetcher_; | 119 scoped_refptr<CloudPrintURLFetcher> fetcher_; |
| 120 }; | 120 }; |
| 121 | 121 |
| 122 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { | 122 class CloudPrintURLFetcherBasicTest : public CloudPrintURLFetcherTest { |
| 123 public: | 123 public: |
| 124 CloudPrintURLFetcherBasicTest() | 124 CloudPrintURLFetcherBasicTest() |
| 125 : handle_raw_response_(false), handle_raw_data_(false) { } | 125 : handle_raw_response_(false), handle_raw_data_(false) { } |
| 126 // CloudPrintURLFetcher::Delegate | 126 // CloudPrintURLFetcher::Delegate |
| 127 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( | 127 virtual CloudPrintURLFetcher::ResponseAction HandleRawResponse( |
| 128 const URLFetcher* source, | 128 const content::URLFetcher* source, |
| 129 const GURL& url, | 129 const GURL& url, |
| 130 const net::URLRequestStatus& status, | 130 const net::URLRequestStatus& status, |
| 131 int response_code, | 131 int response_code, |
| 132 const net::ResponseCookies& cookies, | 132 const net::ResponseCookies& cookies, |
| 133 const std::string& data); | 133 const std::string& data); |
| 134 | 134 |
| 135 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( | 135 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
| 136 const URLFetcher* source, | 136 const content::URLFetcher* source, |
| 137 const GURL& url, | 137 const GURL& url, |
| 138 const std::string& data); | 138 const std::string& data); |
| 139 | 139 |
| 140 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( | 140 virtual CloudPrintURLFetcher::ResponseAction HandleJSONData( |
| 141 const URLFetcher* source, | 141 const content::URLFetcher* source, |
| 142 const GURL& url, | 142 const GURL& url, |
| 143 DictionaryValue* json_data, | 143 DictionaryValue* json_data, |
| 144 bool succeeded); | 144 bool succeeded); |
| 145 | 145 |
| 146 void SetHandleRawResponse(bool handle_raw_response) { | 146 void SetHandleRawResponse(bool handle_raw_response) { |
| 147 handle_raw_response_ = handle_raw_response; | 147 handle_raw_response_ = handle_raw_response; |
| 148 } | 148 } |
| 149 void SetHandleRawData(bool handle_raw_data) { | 149 void SetHandleRawData(bool handle_raw_data) { |
| 150 handle_raw_data_ = handle_raw_data; | 150 handle_raw_data_ = handle_raw_data; |
| 151 } | 151 } |
| 152 private: | 152 private: |
| 153 bool handle_raw_response_; | 153 bool handle_raw_response_; |
| 154 bool handle_raw_data_; | 154 bool handle_raw_data_; |
| 155 }; | 155 }; |
| 156 | 156 |
| 157 // Version of CloudPrintURLFetcherTest that tests overload protection. | 157 // Version of CloudPrintURLFetcherTest that tests overload protection. |
| 158 class CloudPrintURLFetcherOverloadTest : public CloudPrintURLFetcherTest { | 158 class CloudPrintURLFetcherOverloadTest : public CloudPrintURLFetcherTest { |
| 159 public: | 159 public: |
| 160 CloudPrintURLFetcherOverloadTest() : response_count_(0) { | 160 CloudPrintURLFetcherOverloadTest() : response_count_(0) { |
| 161 } | 161 } |
| 162 | 162 |
| 163 // CloudPrintURLFetcher::Delegate | 163 // CloudPrintURLFetcher::Delegate |
| 164 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( | 164 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
| 165 const URLFetcher* source, | 165 const content::URLFetcher* source, |
| 166 const GURL& url, | 166 const GURL& url, |
| 167 const std::string& data); | 167 const std::string& data); |
| 168 | 168 |
| 169 private: | 169 private: |
| 170 int response_count_; | 170 int response_count_; |
| 171 }; | 171 }; |
| 172 | 172 |
| 173 // Version of CloudPrintURLFetcherTest that tests backoff protection. | 173 // Version of CloudPrintURLFetcherTest that tests backoff protection. |
| 174 class CloudPrintURLFetcherRetryBackoffTest : public CloudPrintURLFetcherTest { | 174 class CloudPrintURLFetcherRetryBackoffTest : public CloudPrintURLFetcherTest { |
| 175 public: | 175 public: |
| 176 CloudPrintURLFetcherRetryBackoffTest() : response_count_(0) { | 176 CloudPrintURLFetcherRetryBackoffTest() : response_count_(0) { |
| 177 } | 177 } |
| 178 | 178 |
| 179 // CloudPrintURLFetcher::Delegate | 179 // CloudPrintURLFetcher::Delegate |
| 180 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( | 180 virtual CloudPrintURLFetcher::ResponseAction HandleRawData( |
| 181 const URLFetcher* source, | 181 const content::URLFetcher* source, |
| 182 const GURL& url, | 182 const GURL& url, |
| 183 const std::string& data); | 183 const std::string& data); |
| 184 | 184 |
| 185 virtual void OnRequestGiveUp(); | 185 virtual void OnRequestGiveUp(); |
| 186 | 186 |
| 187 private: | 187 private: |
| 188 int response_count_; | 188 int response_count_; |
| 189 }; | 189 }; |
| 190 | 190 |
| 191 | 191 |
| 192 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) { | 192 void CloudPrintURLFetcherTest::CreateFetcher(const GURL& url, int max_retries) { |
| 193 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy()); | 193 fetcher_ = new TestCloudPrintURLFetcher(io_message_loop_proxy()); |
| 194 max_retries_ = max_retries; | 194 max_retries_ = max_retries; |
| 195 start_time_ = Time::Now(); | 195 start_time_ = Time::Now(); |
| 196 fetcher_->StartGetRequest(url, this, max_retries_, std::string()); | 196 fetcher_->StartGetRequest(url, this, max_retries_, std::string()); |
| 197 } | 197 } |
| 198 | 198 |
| 199 CloudPrintURLFetcher::ResponseAction | 199 CloudPrintURLFetcher::ResponseAction |
| 200 CloudPrintURLFetcherTest::HandleRawResponse( | 200 CloudPrintURLFetcherTest::HandleRawResponse( |
| 201 const URLFetcher* source, | 201 const content::URLFetcher* source, |
| 202 const GURL& url, | 202 const GURL& url, |
| 203 const net::URLRequestStatus& status, | 203 const net::URLRequestStatus& status, |
| 204 int response_code, | 204 int response_code, |
| 205 const net::ResponseCookies& cookies, | 205 const net::ResponseCookies& cookies, |
| 206 const std::string& data) { | 206 const std::string& data) { |
| 207 EXPECT_TRUE(status.is_success()); | 207 EXPECT_TRUE(status.is_success()); |
| 208 EXPECT_EQ(200, response_code); // HTTP OK | 208 EXPECT_EQ(200, response_code); // HTTP OK |
| 209 EXPECT_FALSE(data.empty()); | 209 EXPECT_FALSE(data.empty()); |
| 210 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 210 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
| 211 } | 211 } |
| 212 | 212 |
| 213 CloudPrintURLFetcher::ResponseAction | 213 CloudPrintURLFetcher::ResponseAction |
| 214 CloudPrintURLFetcherBasicTest::HandleRawResponse( | 214 CloudPrintURLFetcherBasicTest::HandleRawResponse( |
| 215 const URLFetcher* source, | 215 const content::URLFetcher* source, |
| 216 const GURL& url, | 216 const GURL& url, |
| 217 const net::URLRequestStatus& status, | 217 const net::URLRequestStatus& status, |
| 218 int response_code, | 218 int response_code, |
| 219 const net::ResponseCookies& cookies, | 219 const net::ResponseCookies& cookies, |
| 220 const std::string& data) { | 220 const std::string& data) { |
| 221 EXPECT_TRUE(status.is_success()); | 221 EXPECT_TRUE(status.is_success()); |
| 222 EXPECT_EQ(200, response_code); // HTTP OK | 222 EXPECT_EQ(200, response_code); // HTTP OK |
| 223 EXPECT_FALSE(data.empty()); | 223 EXPECT_FALSE(data.empty()); |
| 224 | 224 |
| 225 if (handle_raw_response_) { | 225 if (handle_raw_response_) { |
| 226 // If the current message loop is not the IO loop, it will be shut down when | 226 // If the current message loop is not the IO loop, it will be shut down when |
| 227 // the main loop returns and this thread subsequently goes out of scope. | 227 // the main loop returns and this thread subsequently goes out of scope. |
| 228 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 228 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 229 return CloudPrintURLFetcher::STOP_PROCESSING; | 229 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 230 } | 230 } |
| 231 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 231 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
| 232 } | 232 } |
| 233 | 233 |
| 234 CloudPrintURLFetcher::ResponseAction | 234 CloudPrintURLFetcher::ResponseAction |
| 235 CloudPrintURLFetcherBasicTest::HandleRawData( | 235 CloudPrintURLFetcherBasicTest::HandleRawData( |
| 236 const URLFetcher* source, | 236 const content::URLFetcher* source, |
| 237 const GURL& url, | 237 const GURL& url, |
| 238 const std::string& data) { | 238 const std::string& data) { |
| 239 // We should never get here if we returned true in HandleRawResponse | 239 // We should never get here if we returned true in HandleRawResponse |
| 240 EXPECT_FALSE(handle_raw_response_); | 240 EXPECT_FALSE(handle_raw_response_); |
| 241 if (handle_raw_data_) { | 241 if (handle_raw_data_) { |
| 242 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 242 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 243 return CloudPrintURLFetcher::STOP_PROCESSING; | 243 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 244 } | 244 } |
| 245 return CloudPrintURLFetcher::CONTINUE_PROCESSING; | 245 return CloudPrintURLFetcher::CONTINUE_PROCESSING; |
| 246 } | 246 } |
| 247 | 247 |
| 248 CloudPrintURLFetcher::ResponseAction | 248 CloudPrintURLFetcher::ResponseAction |
| 249 CloudPrintURLFetcherBasicTest::HandleJSONData( | 249 CloudPrintURLFetcherBasicTest::HandleJSONData( |
| 250 const URLFetcher* source, | 250 const content::URLFetcher* source, |
| 251 const GURL& url, | 251 const GURL& url, |
| 252 DictionaryValue* json_data, | 252 DictionaryValue* json_data, |
| 253 bool succeeded) { | 253 bool succeeded) { |
| 254 // We should never get here if we returned true in one of the above methods. | 254 // We should never get here if we returned true in one of the above methods. |
| 255 EXPECT_FALSE(handle_raw_response_); | 255 EXPECT_FALSE(handle_raw_response_); |
| 256 EXPECT_FALSE(handle_raw_data_); | 256 EXPECT_FALSE(handle_raw_data_); |
| 257 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 257 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 258 return CloudPrintURLFetcher::STOP_PROCESSING; | 258 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 259 } | 259 } |
| 260 | 260 |
| 261 CloudPrintURLFetcher::ResponseAction | 261 CloudPrintURLFetcher::ResponseAction |
| 262 CloudPrintURLFetcherOverloadTest::HandleRawData(const URLFetcher* source, | 262 CloudPrintURLFetcherOverloadTest::HandleRawData( |
| 263 const GURL& url, | 263 const content::URLFetcher* source, |
| 264 const std::string& data) { | 264 const GURL& url, |
| 265 const std::string& data) { |
| 265 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); | 266 const TimeDelta one_second = TimeDelta::FromMilliseconds(1000); |
| 266 response_count_++; | 267 response_count_++; |
| 267 if (response_count_ < 20) { | 268 if (response_count_ < 20) { |
| 268 fetcher_->StartGetRequest(url, | 269 fetcher_->StartGetRequest(url, |
| 269 this, | 270 this, |
| 270 max_retries_, | 271 max_retries_, |
| 271 std::string()); | 272 std::string()); |
| 272 } else { | 273 } else { |
| 273 // We have already sent 20 requests continuously. And we expect that | 274 // We have already sent 20 requests continuously. And we expect that |
| 274 // it takes more than 1 second due to the overload protection settings. | 275 // it takes more than 1 second due to the overload protection settings. |
| 275 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); | 276 EXPECT_TRUE(Time::Now() - start_time_ >= one_second); |
| 276 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 277 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| 277 } | 278 } |
| 278 return CloudPrintURLFetcher::STOP_PROCESSING; | 279 return CloudPrintURLFetcher::STOP_PROCESSING; |
| 279 } | 280 } |
| 280 | 281 |
| 281 CloudPrintURLFetcher::ResponseAction | 282 CloudPrintURLFetcher::ResponseAction |
| 282 CloudPrintURLFetcherRetryBackoffTest::HandleRawData(const URLFetcher* source, | 283 CloudPrintURLFetcherRetryBackoffTest::HandleRawData( |
| 283 const GURL& url, | 284 const content::URLFetcher* source, |
| 284 const std::string& data) { | 285 const GURL& url, |
| 286 const std::string& data) { |
| 285 response_count_++; | 287 response_count_++; |
| 286 // First attempt + 11 retries = 12 total responses. | 288 // First attempt + 11 retries = 12 total responses. |
| 287 EXPECT_LE(response_count_, 12); | 289 EXPECT_LE(response_count_, 12); |
| 288 return CloudPrintURLFetcher::RETRY_REQUEST; | 290 return CloudPrintURLFetcher::RETRY_REQUEST; |
| 289 } | 291 } |
| 290 | 292 |
| 291 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() { | 293 void CloudPrintURLFetcherRetryBackoffTest::OnRequestGiveUp() { |
| 292 // It takes more than 200 ms to finish all 11 requests. | 294 // It takes more than 200 ms to finish all 11 requests. |
| 293 EXPECT_TRUE(Time::Now() - start_time_ >= TimeDelta::FromMilliseconds(200)); | 295 EXPECT_TRUE(Time::Now() - start_time_ >= TimeDelta::FromMilliseconds(200)); |
| 294 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); | 296 io_message_loop_proxy()->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 353 manager->OverrideEntryForTests(url, entry); | 355 manager->OverrideEntryForTests(url, entry); |
| 354 | 356 |
| 355 CreateFetcher(url, 11); | 357 CreateFetcher(url, 11); |
| 356 | 358 |
| 357 MessageLoop::current()->Run(); | 359 MessageLoop::current()->Run(); |
| 358 | 360 |
| 359 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); | 361 net::URLRequestThrottlerManager::GetInstance()->EraseEntryForTests(url); |
| 360 } | 362 } |
| 361 | 363 |
| 362 } // namespace. | 364 } // namespace. |
| OLD | NEW |