| 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/unregistration_request.h" | 11 #include "google_apis/gcm/engine/gcm_unregistration_request_handler.h" | 
|  | 12 #include "google_apis/gcm/engine/instance_id_delete_token_request_handler.h" | 
| 12 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" | 13 #include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" | 
| 13 #include "net/url_request/test_url_fetcher_factory.h" | 14 #include "net/url_request/test_url_fetcher_factory.h" | 
| 14 #include "net/url_request/url_request_test_util.h" | 15 #include "net/url_request/url_request_test_util.h" | 
| 15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" | 
| 16 | 17 | 
| 17 namespace gcm { | 18 namespace gcm { | 
| 18 | 19 | 
| 19 namespace { | 20 namespace { | 
| 20 const uint64 kAndroidId = 42UL; | 21 const uint64 kAndroidId = 42UL; | 
| 21 const char kLoginHeader[] = "AidLogin"; | 22 const char kLoginHeader[] = "AidLogin"; | 
| 22 const char kAppId[] = "TestAppId"; | 23 const char kAppId[] = "TestAppId"; | 
| 23 const char kDeletedAppId[] = "deleted=TestAppId"; | 24 const char kDeletedAppId[] = "deleted=TestAppId"; | 
|  | 25 const char kDeletedToken[] = "token=SomeToken"; | 
| 24 const char kRegistrationURL[] = "http://foo.bar/register"; | 26 const char kRegistrationURL[] = "http://foo.bar/register"; | 
| 25 const uint64 kSecurityToken = 77UL; | 27 const uint64 kSecurityToken = 77UL; | 
|  | 28 const int kGCMVersion = 40; | 
|  | 29 const char kInstanceId[] = "IID1"; | 
|  | 30 const char kDeveloperId[] = "Project1"; | 
|  | 31 const char kScope[] = "GCM"; | 
| 26 | 32 | 
| 27 // Backoff policy for testing registration request. | 33 // Backoff policy for testing registration request. | 
| 28 const net::BackoffEntry::Policy kDefaultBackoffPolicy = { | 34 const net::BackoffEntry::Policy kDefaultBackoffPolicy = { | 
| 29   // Number of initial errors (in sequence) to ignore before applying | 35   // Number of initial errors (in sequence) to ignore before applying | 
| 30   // exponential back-off rules. | 36   // exponential back-off rules. | 
| 31   // Explicitly set to 2 to skip the delay on the first retry, as we are not | 37   // Explicitly set to 2 to skip the delay on the first retry, as we are not | 
| 32   // trying to test the backoff itself, but rather the fact that retry happens. | 38   // trying to test the backoff itself, but rather the fact that retry happens. | 
| 33   1, | 39   1, | 
| 34 | 40 | 
| 35   // Initial delay for exponential back-off in ms. | 41   // Initial delay for exponential back-off in ms. | 
| (...skipping 18 matching lines...) Expand all  Loading... | 
| 54 }; | 60 }; | 
| 55 }  // namespace | 61 }  // namespace | 
| 56 | 62 | 
| 57 class UnregistrationRequestTest : public testing::Test { | 63 class UnregistrationRequestTest : public testing::Test { | 
| 58  public: | 64  public: | 
| 59   UnregistrationRequestTest(); | 65   UnregistrationRequestTest(); | 
| 60   ~UnregistrationRequestTest() override; | 66   ~UnregistrationRequestTest() override; | 
| 61 | 67 | 
| 62   void UnregistrationCallback(UnregistrationRequest::Status status); | 68   void UnregistrationCallback(UnregistrationRequest::Status status); | 
| 63 | 69 | 
| 64   void CreateRequest(); |  | 
| 65   void SetResponseStatusAndString(net::HttpStatusCode status_code, | 70   void SetResponseStatusAndString(net::HttpStatusCode status_code, | 
| 66                                   const std::string& response_body); | 71                                   const std::string& response_body); | 
| 67   void CompleteFetch(); | 72   void CompleteFetch(); | 
| 68 | 73 | 
| 69  protected: | 74  protected: | 
| 70   bool callback_called_; | 75   bool callback_called_; | 
| 71   UnregistrationRequest::Status status_; | 76   UnregistrationRequest::Status status_; | 
| 72   scoped_ptr<UnregistrationRequest> request_; | 77   scoped_ptr<UnregistrationRequest> request_; | 
| 73   base::MessageLoop message_loop_; | 78   base::MessageLoop message_loop_; | 
| 74   net::TestURLFetcherFactory url_fetcher_factory_; | 79   net::TestURLFetcherFactory url_fetcher_factory_; | 
| 75   scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 80   scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_; | 
| 76   FakeGCMStatsRecorder recorder_; | 81   FakeGCMStatsRecorder recorder_; | 
| 77 }; | 82 }; | 
| 78 | 83 | 
| 79 UnregistrationRequestTest::UnregistrationRequestTest() | 84 UnregistrationRequestTest::UnregistrationRequestTest() | 
| 80     : callback_called_(false), | 85     : callback_called_(false), | 
| 81       status_(UnregistrationRequest::UNREGISTRATION_STATUS_COUNT), | 86       status_(UnregistrationRequest::UNREGISTRATION_STATUS_COUNT), | 
| 82       url_request_context_getter_(new net::TestURLRequestContextGetter( | 87       url_request_context_getter_(new net::TestURLRequestContextGetter( | 
| 83           message_loop_.message_loop_proxy())) {} | 88           message_loop_.message_loop_proxy())) {} | 
| 84 | 89 | 
| 85 UnregistrationRequestTest::~UnregistrationRequestTest() {} | 90 UnregistrationRequestTest::~UnregistrationRequestTest() {} | 
| 86 | 91 | 
| 87 void UnregistrationRequestTest::UnregistrationCallback( | 92 void UnregistrationRequestTest::UnregistrationCallback( | 
| 88     UnregistrationRequest::Status status) { | 93     UnregistrationRequest::Status status) { | 
| 89   callback_called_ = true; | 94   callback_called_ = true; | 
| 90   status_ = status; | 95   status_ = status; | 
| 91 } | 96 } | 
| 92 | 97 | 
| 93 void UnregistrationRequestTest::CreateRequest() { |  | 
| 94   request_.reset(new UnregistrationRequest( |  | 
| 95       GURL(kRegistrationURL), |  | 
| 96       UnregistrationRequest::RequestInfo(kAndroidId, |  | 
| 97                                          kSecurityToken, |  | 
| 98                                          kAppId), |  | 
| 99       kDefaultBackoffPolicy, |  | 
| 100       base::Bind(&UnregistrationRequestTest::UnregistrationCallback, |  | 
| 101                  base::Unretained(this)), |  | 
| 102       url_request_context_getter_.get(), |  | 
| 103       &recorder_)); |  | 
| 104 } |  | 
| 105 |  | 
| 106 void UnregistrationRequestTest::SetResponseStatusAndString( | 98 void UnregistrationRequestTest::SetResponseStatusAndString( | 
| 107     net::HttpStatusCode status_code, | 99     net::HttpStatusCode status_code, | 
| 108     const std::string& response_body) { | 100     const std::string& response_body) { | 
| 109   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 101   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 
| 110   ASSERT_TRUE(fetcher); | 102   ASSERT_TRUE(fetcher); | 
| 111   fetcher->set_response_code(status_code); | 103   fetcher->set_response_code(status_code); | 
| 112   fetcher->SetResponseString(response_body); | 104   fetcher->SetResponseString(response_body); | 
| 113 } | 105 } | 
| 114 | 106 | 
| 115 void UnregistrationRequestTest::CompleteFetch() { | 107 void UnregistrationRequestTest::CompleteFetch() { | 
| 116   status_ = UnregistrationRequest::UNREGISTRATION_STATUS_COUNT; | 108   status_ = UnregistrationRequest::UNREGISTRATION_STATUS_COUNT; | 
| 117   callback_called_ = false; | 109   callback_called_ = false; | 
| 118   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 110   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 
| 119   ASSERT_TRUE(fetcher); | 111   ASSERT_TRUE(fetcher); | 
| 120   fetcher->delegate()->OnURLFetchComplete(fetcher); | 112   fetcher->delegate()->OnURLFetchComplete(fetcher); | 
| 121 } | 113 } | 
| 122 | 114 | 
| 123 TEST_F(UnregistrationRequestTest, RequestDataPassedToFetcher) { | 115 class GCMUnregistrationRequestTest : public UnregistrationRequestTest { | 
|  | 116  public: | 
|  | 117   GCMUnregistrationRequestTest(); | 
|  | 118   ~GCMUnregistrationRequestTest() override; | 
|  | 119 | 
|  | 120   void CreateRequest(); | 
|  | 121 }; | 
|  | 122 | 
|  | 123 GCMUnregistrationRequestTest::GCMUnregistrationRequestTest() { | 
|  | 124 } | 
|  | 125 | 
|  | 126 GCMUnregistrationRequestTest::~GCMUnregistrationRequestTest() { | 
|  | 127 } | 
|  | 128 | 
|  | 129 void GCMUnregistrationRequestTest::CreateRequest() { | 
|  | 130   UnregistrationRequest::RequestInfo request_info( | 
|  | 131       kAndroidId, kSecurityToken, kAppId); | 
|  | 132   scoped_ptr<GCMUnregistrationRequestHandler> request_handler( | 
|  | 133       new GCMUnregistrationRequestHandler(kAppId)); | 
|  | 134   request_.reset(new UnregistrationRequest( | 
|  | 135       GURL(kRegistrationURL), | 
|  | 136       request_info, | 
|  | 137       request_handler.Pass(), | 
|  | 138       kDefaultBackoffPolicy, | 
|  | 139       base::Bind(&UnregistrationRequestTest::UnregistrationCallback, | 
|  | 140                  base::Unretained(this)), | 
|  | 141       url_request_context_getter_.get(), | 
|  | 142       &recorder_)); | 
|  | 143 } | 
|  | 144 | 
|  | 145 TEST_F(GCMUnregistrationRequestTest, RequestDataPassedToFetcher) { | 
| 124   CreateRequest(); | 146   CreateRequest(); | 
| 125   request_->Start(); | 147   request_->Start(); | 
| 126 | 148 | 
| 127   // Get data sent by request. | 149   // Get data sent by request. | 
| 128   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 150   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 
| 129   ASSERT_TRUE(fetcher); | 151   ASSERT_TRUE(fetcher); | 
| 130 | 152 | 
| 131   EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); | 153   EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); | 
| 132 | 154 | 
| 133   // Verify that authorization header was put together properly. | 155   // Verify that authorization header was put together properly. | 
| (...skipping 27 matching lines...) Expand all  Loading... | 
| 161     ASSERT_TRUE(iter != expected_pairs.end()) << data_tokenizer.token(); | 183     ASSERT_TRUE(iter != expected_pairs.end()) << data_tokenizer.token(); | 
| 162     ASSERT_TRUE(data_tokenizer.GetNext()) << data_tokenizer.token(); | 184     ASSERT_TRUE(data_tokenizer.GetNext()) << data_tokenizer.token(); | 
| 163     EXPECT_EQ(iter->second, data_tokenizer.token()); | 185     EXPECT_EQ(iter->second, data_tokenizer.token()); | 
| 164     // Ensure that none of the keys appears twice. | 186     // Ensure that none of the keys appears twice. | 
| 165     expected_pairs.erase(iter); | 187     expected_pairs.erase(iter); | 
| 166   } | 188   } | 
| 167 | 189 | 
| 168   EXPECT_EQ(0UL, expected_pairs.size()); | 190   EXPECT_EQ(0UL, expected_pairs.size()); | 
| 169 } | 191 } | 
| 170 | 192 | 
| 171 TEST_F(UnregistrationRequestTest, SuccessfulUnregistration) { | 193 TEST_F(GCMUnregistrationRequestTest, SuccessfulUnregistration) { | 
| 172   CreateRequest(); | 194   CreateRequest(); | 
| 173   request_->Start(); | 195   request_->Start(); | 
| 174 | 196 | 
| 175   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 197   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 
| 176   CompleteFetch(); | 198   CompleteFetch(); | 
| 177 | 199 | 
| 178   EXPECT_TRUE(callback_called_); | 200   EXPECT_TRUE(callback_called_); | 
| 179   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 201   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 
| 180 } | 202 } | 
| 181 | 203 | 
| 182 TEST_F(UnregistrationRequestTest, ResponseHttpStatusNotOK) { | 204 TEST_F(GCMUnregistrationRequestTest, ResponseHttpStatusNotOK) { | 
| 183   CreateRequest(); | 205   CreateRequest(); | 
| 184   request_->Start(); | 206   request_->Start(); | 
| 185 | 207 | 
| 186   SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, ""); | 208   SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, ""); | 
| 187   CompleteFetch(); | 209   CompleteFetch(); | 
| 188 | 210 | 
| 189   EXPECT_TRUE(callback_called_); | 211   EXPECT_TRUE(callback_called_); | 
| 190   EXPECT_EQ(UnregistrationRequest::HTTP_NOT_OK, status_); | 212   EXPECT_EQ(UnregistrationRequest::HTTP_NOT_OK, status_); | 
| 191 } | 213 } | 
| 192 | 214 | 
| 193 TEST_F(UnregistrationRequestTest, ResponseEmpty) { | 215 TEST_F(GCMUnregistrationRequestTest, ResponseEmpty) { | 
| 194   CreateRequest(); | 216   CreateRequest(); | 
| 195   request_->Start(); | 217   request_->Start(); | 
| 196 | 218 | 
| 197   SetResponseStatusAndString(net::HTTP_OK, ""); | 219   SetResponseStatusAndString(net::HTTP_OK, ""); | 
| 198   CompleteFetch(); | 220   CompleteFetch(); | 
| 199 | 221 | 
| 200   EXPECT_FALSE(callback_called_); | 222   EXPECT_FALSE(callback_called_); | 
| 201 | 223 | 
| 202   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 224   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 
| 203   CompleteFetch(); | 225   CompleteFetch(); | 
| 204 | 226 | 
| 205   EXPECT_TRUE(callback_called_); | 227   EXPECT_TRUE(callback_called_); | 
| 206   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 228   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 
| 207 } | 229 } | 
| 208 | 230 | 
| 209 TEST_F(UnregistrationRequestTest, InvalidParametersError) { | 231 TEST_F(GCMUnregistrationRequestTest, InvalidParametersError) { | 
| 210   CreateRequest(); | 232   CreateRequest(); | 
| 211   request_->Start(); | 233   request_->Start(); | 
| 212 | 234 | 
| 213   SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS"); | 235   SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS"); | 
| 214   CompleteFetch(); | 236   CompleteFetch(); | 
| 215 | 237 | 
| 216   EXPECT_TRUE(callback_called_); | 238   EXPECT_TRUE(callback_called_); | 
| 217   EXPECT_EQ(UnregistrationRequest::INVALID_PARAMETERS, status_); | 239   EXPECT_EQ(UnregistrationRequest::INVALID_PARAMETERS, status_); | 
| 218 } | 240 } | 
| 219 | 241 | 
| 220 TEST_F(UnregistrationRequestTest, UnkwnownError) { | 242 TEST_F(GCMUnregistrationRequestTest, UnkwnownError) { | 
| 221   CreateRequest(); | 243   CreateRequest(); | 
| 222   request_->Start(); | 244   request_->Start(); | 
| 223 | 245 | 
| 224   SetResponseStatusAndString(net::HTTP_OK, "Error=XXX"); | 246   SetResponseStatusAndString(net::HTTP_OK, "Error=XXX"); | 
| 225   CompleteFetch(); | 247   CompleteFetch(); | 
| 226 | 248 | 
| 227   EXPECT_TRUE(callback_called_); | 249   EXPECT_TRUE(callback_called_); | 
| 228   EXPECT_EQ(UnregistrationRequest::UNKNOWN_ERROR, status_); | 250   EXPECT_EQ(UnregistrationRequest::UNKNOWN_ERROR, status_); | 
| 229 } | 251 } | 
| 230 | 252 | 
| 231 TEST_F(UnregistrationRequestTest, ServiceUnavailable) { | 253 TEST_F(GCMUnregistrationRequestTest, ServiceUnavailable) { | 
| 232   CreateRequest(); | 254   CreateRequest(); | 
| 233   request_->Start(); | 255   request_->Start(); | 
| 234 | 256 | 
| 235   SetResponseStatusAndString(net::HTTP_SERVICE_UNAVAILABLE, ""); | 257   SetResponseStatusAndString(net::HTTP_SERVICE_UNAVAILABLE, ""); | 
| 236   CompleteFetch(); | 258   CompleteFetch(); | 
| 237 | 259 | 
| 238   EXPECT_FALSE(callback_called_); | 260   EXPECT_FALSE(callback_called_); | 
| 239 | 261 | 
| 240   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 262   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 
| 241   CompleteFetch(); | 263   CompleteFetch(); | 
| 242 | 264 | 
| 243   EXPECT_TRUE(callback_called_); | 265   EXPECT_TRUE(callback_called_); | 
| 244   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 266   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 
| 245 } | 267 } | 
| 246 | 268 | 
| 247 TEST_F(UnregistrationRequestTest, InternalServerError) { | 269 TEST_F(GCMUnregistrationRequestTest, InternalServerError) { | 
| 248   CreateRequest(); | 270   CreateRequest(); | 
| 249   request_->Start(); | 271   request_->Start(); | 
| 250 | 272 | 
| 251   SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, ""); | 273   SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, ""); | 
| 252   CompleteFetch(); | 274   CompleteFetch(); | 
| 253 | 275 | 
| 254   EXPECT_FALSE(callback_called_); | 276   EXPECT_FALSE(callback_called_); | 
| 255 | 277 | 
| 256   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 278   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 
| 257   CompleteFetch(); | 279   CompleteFetch(); | 
| 258 | 280 | 
| 259   EXPECT_TRUE(callback_called_); | 281   EXPECT_TRUE(callback_called_); | 
| 260   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 282   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 
| 261 } | 283 } | 
| 262 | 284 | 
| 263 TEST_F(UnregistrationRequestTest, IncorrectAppId) { | 285 TEST_F(GCMUnregistrationRequestTest, IncorrectAppId) { | 
| 264   CreateRequest(); | 286   CreateRequest(); | 
| 265   request_->Start(); | 287   request_->Start(); | 
| 266 | 288 | 
| 267   SetResponseStatusAndString(net::HTTP_OK, "deleted=OtherTestAppId"); | 289   SetResponseStatusAndString(net::HTTP_OK, "deleted=OtherTestAppId"); | 
| 268   CompleteFetch(); | 290   CompleteFetch(); | 
| 269 | 291 | 
| 270   EXPECT_FALSE(callback_called_); | 292   EXPECT_FALSE(callback_called_); | 
| 271 | 293 | 
| 272   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 294   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 
| 273   CompleteFetch(); | 295   CompleteFetch(); | 
| 274 | 296 | 
| 275   EXPECT_TRUE(callback_called_); | 297   EXPECT_TRUE(callback_called_); | 
| 276   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 298   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 
| 277 } | 299 } | 
| 278 | 300 | 
| 279 TEST_F(UnregistrationRequestTest, ResponseParsingFailed) { | 301 TEST_F(GCMUnregistrationRequestTest, ResponseParsingFailed) { | 
| 280   CreateRequest(); | 302   CreateRequest(); | 
| 281   request_->Start(); | 303   request_->Start(); | 
| 282 | 304 | 
| 283   SetResponseStatusAndString(net::HTTP_OK, "some malformed response"); | 305   SetResponseStatusAndString(net::HTTP_OK, "some malformed response"); | 
| 284   CompleteFetch(); | 306   CompleteFetch(); | 
| 285 | 307 | 
| 286   EXPECT_FALSE(callback_called_); | 308   EXPECT_FALSE(callback_called_); | 
| 287 | 309 | 
| 288   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 310   SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId); | 
| 289   CompleteFetch(); | 311   CompleteFetch(); | 
| 290 | 312 | 
| 291   EXPECT_TRUE(callback_called_); | 313   EXPECT_TRUE(callback_called_); | 
| 292   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 314   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 
| 293 } | 315 } | 
| 294 | 316 | 
|  | 317 class InstaceIDDeleteTokenRequestTest : public UnregistrationRequestTest { | 
|  | 318  public: | 
|  | 319   InstaceIDDeleteTokenRequestTest(); | 
|  | 320   ~InstaceIDDeleteTokenRequestTest() override; | 
|  | 321 | 
|  | 322   void CreateRequest(const std::string& instance_id, | 
|  | 323                      const std::string& authorized_entity, | 
|  | 324                      const std::string& scope); | 
|  | 325 }; | 
|  | 326 | 
|  | 327 InstaceIDDeleteTokenRequestTest::InstaceIDDeleteTokenRequestTest() { | 
|  | 328 } | 
|  | 329 | 
|  | 330 InstaceIDDeleteTokenRequestTest::~InstaceIDDeleteTokenRequestTest() { | 
|  | 331 } | 
|  | 332 | 
|  | 333 void InstaceIDDeleteTokenRequestTest::CreateRequest( | 
|  | 334     const std::string& instance_id, | 
|  | 335     const std::string& authorized_entity, | 
|  | 336     const std::string& scope) { | 
|  | 337   UnregistrationRequest::RequestInfo request_info( | 
|  | 338       kAndroidId, kSecurityToken, kAppId); | 
|  | 339   scoped_ptr<InstanceIDDeleteTokenRequestHandler> request_handler( | 
|  | 340       new InstanceIDDeleteTokenRequestHandler( | 
|  | 341           instance_id, authorized_entity, scope, kGCMVersion)); | 
|  | 342   request_.reset(new UnregistrationRequest( | 
|  | 343       GURL(kRegistrationURL), | 
|  | 344       request_info, | 
|  | 345       request_handler.Pass(), | 
|  | 346       kDefaultBackoffPolicy, | 
|  | 347       base::Bind(&UnregistrationRequestTest::UnregistrationCallback, | 
|  | 348                  base::Unretained(this)), | 
|  | 349       url_request_context_getter_.get(), | 
|  | 350       &recorder_)); | 
|  | 351 } | 
|  | 352 | 
|  | 353 TEST_F(InstaceIDDeleteTokenRequestTest, RequestDataPassedToFetcher) { | 
|  | 354   CreateRequest(kInstanceId, kDeveloperId, kScope); | 
|  | 355   request_->Start(); | 
|  | 356 | 
|  | 357   // Get data sent by request. | 
|  | 358   net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); | 
|  | 359   ASSERT_TRUE(fetcher); | 
|  | 360 | 
|  | 361   EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); | 
|  | 362 | 
|  | 363   // Verify that authorization header was put together properly. | 
|  | 364   net::HttpRequestHeaders headers; | 
|  | 365   fetcher->GetExtraRequestHeaders(&headers); | 
|  | 366   std::string auth_header; | 
|  | 367   headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header); | 
|  | 368   base::StringTokenizer auth_tokenizer(auth_header, " :"); | 
|  | 369   ASSERT_TRUE(auth_tokenizer.GetNext()); | 
|  | 370   EXPECT_EQ(kLoginHeader, auth_tokenizer.token()); | 
|  | 371   ASSERT_TRUE(auth_tokenizer.GetNext()); | 
|  | 372   EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token()); | 
|  | 373   ASSERT_TRUE(auth_tokenizer.GetNext()); | 
|  | 374   EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token()); | 
|  | 375   std::string app_id_header; | 
|  | 376   headers.GetHeader("app", &app_id_header); | 
|  | 377   EXPECT_EQ(kAppId, app_id_header); | 
|  | 378 | 
|  | 379   std::map<std::string, std::string> expected_pairs; | 
|  | 380   expected_pairs["gmsv"] = base::IntToString(kGCMVersion); | 
|  | 381   expected_pairs["app"] = kAppId; | 
|  | 382   expected_pairs["device"] = base::Uint64ToString(kAndroidId); | 
|  | 383   expected_pairs["delete"] = "true"; | 
|  | 384   expected_pairs["appid"] = kInstanceId; | 
|  | 385   expected_pairs["sender"] = kDeveloperId; | 
|  | 386   expected_pairs["scope"] = kScope; | 
|  | 387 | 
|  | 388   // Verify data was formatted properly. | 
|  | 389   std::string upload_data = fetcher->upload_data(); | 
|  | 390   base::StringTokenizer data_tokenizer(upload_data, "&="); | 
|  | 391   while (data_tokenizer.GetNext()) { | 
|  | 392     std::map<std::string, std::string>::iterator iter = | 
|  | 393         expected_pairs.find(data_tokenizer.token()); | 
|  | 394     ASSERT_TRUE(iter != expected_pairs.end()) << data_tokenizer.token(); | 
|  | 395     ASSERT_TRUE(data_tokenizer.GetNext()) << data_tokenizer.token(); | 
|  | 396     EXPECT_EQ(iter->second, data_tokenizer.token()); | 
|  | 397     // Ensure that none of the keys appears twice. | 
|  | 398     expected_pairs.erase(iter); | 
|  | 399   } | 
|  | 400 | 
|  | 401   EXPECT_EQ(0UL, expected_pairs.size()); | 
|  | 402 } | 
|  | 403 | 
|  | 404 TEST_F(InstaceIDDeleteTokenRequestTest, SuccessfulUnregistration) { | 
|  | 405   CreateRequest(kInstanceId, kDeveloperId, kScope); | 
|  | 406   request_->Start(); | 
|  | 407 | 
|  | 408   SetResponseStatusAndString(net::HTTP_OK, kDeletedToken); | 
|  | 409   CompleteFetch(); | 
|  | 410 | 
|  | 411   EXPECT_TRUE(callback_called_); | 
|  | 412   EXPECT_EQ(UnregistrationRequest::SUCCESS, status_); | 
|  | 413 } | 
|  | 414 | 
|  | 415 TEST_F(InstaceIDDeleteTokenRequestTest, ResponseHttpStatusNotOK) { | 
|  | 416   CreateRequest(kInstanceId, kDeveloperId, kScope); | 
|  | 417   request_->Start(); | 
|  | 418 | 
|  | 419   SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, ""); | 
|  | 420   CompleteFetch(); | 
|  | 421 | 
|  | 422   EXPECT_TRUE(callback_called_); | 
|  | 423   EXPECT_EQ(UnregistrationRequest::HTTP_NOT_OK, status_); | 
|  | 424 } | 
|  | 425 | 
| 295 }  // namespace gcm | 426 }  // namespace gcm | 
| OLD | NEW | 
|---|