Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 <map> | 5 #include <map> |
| 6 #include <string> | 6 #include <string> |
| 7 #include <vector> | 7 #include <vector> |
| 8 | 8 |
| 9 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/strings/string_tokenizer.h" | 10 #include "base/strings/string_tokenizer.h" |
| 11 #include "google_apis/gcm/engine/registration_request.h" | 11 #include "google_apis/gcm/engine/registration_request.h" |
| 12 #include "net/url_request/test_url_fetcher_factory.h" | 12 #include "net/url_request/test_url_fetcher_factory.h" |
| 13 #include "net/url_request/url_request_status.h" | |
| 13 #include "net/url_request/url_request_test_util.h" | 14 #include "net/url_request/url_request_test_util.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" | 15 #include "testing/gtest/include/gtest/gtest.h" |
| 15 | 16 |
| 16 namespace gcm { | 17 namespace gcm { |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 const uint64 kAndroidId = 42UL; | 20 const uint64 kAndroidId = 42UL; |
| 20 const char kCert[] = "0DEADBEEF420"; | 21 const char kCert[] = "0DEADBEEF420"; |
| 21 const char kDeveloperId[] = "Project1"; | 22 const char kDeveloperId[] = "Project1"; |
| 22 const char kLoginHeader[] = "AidLogin"; | 23 const char kLoginHeader[] = "AidLogin"; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 public: | 59 public: |
| 59 RegistrationRequestTest(); | 60 RegistrationRequestTest(); |
| 60 virtual ~RegistrationRequestTest(); | 61 virtual ~RegistrationRequestTest(); |
| 61 | 62 |
| 62 void RegistrationCallback(RegistrationRequest::Status status, | 63 void RegistrationCallback(RegistrationRequest::Status status, |
| 63 const std::string& registration_id); | 64 const std::string& registration_id); |
| 64 | 65 |
| 65 void CreateRequest(const std::string& sender_ids); | 66 void CreateRequest(const std::string& sender_ids); |
| 66 void SetResponseStatusAndString(net::HttpStatusCode status_code, | 67 void SetResponseStatusAndString(net::HttpStatusCode status_code, |
| 67 const std::string& response_body); | 68 const std::string& response_body); |
| 69 void SetResponseStatusAndString(net::HttpStatusCode status_code, | |
|
fgorski
2014/02/14 06:18:04
In Chromium you are not suppose to overload the me
juyik
2014/02/18 20:11:01
Done.
juyik
2014/02/18 20:11:01
Done.
| |
| 70 const net::URLRequestStatus& request_status, | |
| 71 const std::string& response_body); | |
| 68 void CompleteFetch(); | 72 void CompleteFetch(); |
| 69 | 73 |
| 70 protected: | 74 protected: |
| 71 RegistrationRequest::Status status_; | 75 RegistrationRequest::Status status_; |
| 72 std::string registration_id_; | 76 std::string registration_id_; |
| 73 bool callback_called_; | 77 bool callback_called_; |
| 74 std::map<std::string, std::string> extras_; | 78 std::map<std::string, std::string> extras_; |
| 75 scoped_ptr<RegistrationRequest> request_; | 79 scoped_ptr<RegistrationRequest> request_; |
| 76 base::MessageLoop message_loop_; | 80 base::MessageLoop message_loop_; |
| 77 net::TestURLFetcherFactory url_fetcher_factory_; | 81 net::TestURLFetcherFactory url_fetcher_factory_; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 114 | 118 |
| 115 void RegistrationRequestTest::SetResponseStatusAndString( | 119 void RegistrationRequestTest::SetResponseStatusAndString( |
| 116 net::HttpStatusCode status_code, | 120 net::HttpStatusCode status_code, |
| 117 const std::string& response_body) { | 121 const std::string& response_body) { |
| 118 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 122 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 119 ASSERT_TRUE(fetcher); | 123 ASSERT_TRUE(fetcher); |
| 120 fetcher->set_response_code(status_code); | 124 fetcher->set_response_code(status_code); |
| 121 fetcher->SetResponseString(response_body); | 125 fetcher->SetResponseString(response_body); |
| 122 } | 126 } |
| 123 | 127 |
| 128 void RegistrationRequestTest::SetResponseStatusAndString( | |
| 129 net::HttpStatusCode status_code, | |
| 130 const net::URLRequestStatus& request_status, | |
| 131 const std::string& response_body) { | |
| 132 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | |
| 133 ASSERT_TRUE(fetcher); | |
| 134 fetcher->set_response_code(status_code); | |
| 135 fetcher->set_status(request_status); | |
| 136 fetcher->SetResponseString(response_body); | |
| 137 } | |
| 138 | |
| 124 void RegistrationRequestTest::CompleteFetch() { | 139 void RegistrationRequestTest::CompleteFetch() { |
| 125 registration_id_.clear(); | 140 registration_id_.clear(); |
| 126 status_ = RegistrationRequest::SUCCESS; | 141 status_ = RegistrationRequest::SUCCESS; |
| 127 callback_called_ = false; | 142 callback_called_ = false; |
| 128 | 143 |
| 129 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 144 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
| 130 ASSERT_TRUE(fetcher); | 145 ASSERT_TRUE(fetcher); |
| 131 fetcher->delegate()->OnURLFetchComplete(fetcher); | 146 fetcher->delegate()->OnURLFetchComplete(fetcher); |
| 132 } | 147 } |
| 133 | 148 |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 304 request_->Start(); | 319 request_->Start(); |
| 305 | 320 |
| 306 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER"); | 321 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER"); |
| 307 CompleteFetch(); | 322 CompleteFetch(); |
| 308 | 323 |
| 309 EXPECT_TRUE(callback_called_); | 324 EXPECT_TRUE(callback_called_); |
| 310 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); | 325 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); |
| 311 EXPECT_EQ(std::string(), registration_id_); | 326 EXPECT_EQ(std::string(), registration_id_); |
| 312 } | 327 } |
| 313 | 328 |
| 329 TEST_F(RegistrationRequestTest, RequestNotSucessful) { | |
| 330 CreateRequest("sender1,sender2"); | |
| 331 request_->Start(); | |
| 332 | |
| 333 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1); | |
| 334 SetResponseStatusAndString(net::HTTP_OK, request_status, "token=2501"); | |
| 335 CompleteFetch(); | |
| 336 | |
| 337 EXPECT_FALSE(callback_called_); | |
| 338 | |
| 339 // Ensuring a retry happened and succeeds. | |
| 340 request_status.set_status(net::URLRequestStatus::SUCCESS); | |
| 341 SetResponseStatusAndString(net::HTTP_OK, request_status, "token=2501"); | |
| 342 CompleteFetch(); | |
| 343 | |
| 344 EXPECT_TRUE(callback_called_); | |
| 345 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | |
| 346 EXPECT_EQ("2501", registration_id_); | |
| 347 } | |
| 348 | |
| 349 TEST_F(RegistrationRequestTest, ResponseHTTPNotOK) { | |
|
fgorski
2014/02/14 06:18:04
ResponseHttpNotOk
juyik
2014/02/18 20:11:01
Done.
| |
| 350 CreateRequest("sender1,sender2"); | |
| 351 request_->Start(); | |
| 352 | |
| 353 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); | |
| 354 CompleteFetch(); | |
| 355 | |
| 356 EXPECT_FALSE(callback_called_); | |
| 357 | |
| 358 // Ensuring a retry happened and succeeds. | |
| 359 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); | |
| 360 CompleteFetch(); | |
| 361 | |
| 362 EXPECT_TRUE(callback_called_); | |
| 363 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); | |
| 364 EXPECT_EQ("2501", registration_id_); | |
| 365 } | |
| 366 | |
| 314 } // namespace gcm | 367 } // namespace gcm |
| OLD | NEW |