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