Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(166)

Side by Side Diff: google_apis/gcm/engine/registration_request_unittest.cc

Issue 1137463003: Support getting and deleting token for Instance ID. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Patch to land Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/gcm_registration_request_handler.h"
12 #include "google_apis/gcm/engine/instance_id_get_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_status.h" 15 #include "net/url_request/url_request_status.h"
15 #include "net/url_request/url_request_test_util.h" 16 #include "net/url_request/url_request_test_util.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
18 namespace gcm { 19 namespace gcm {
19 20
20 namespace { 21 namespace {
21 const uint64 kAndroidId = 42UL; 22 const uint64 kAndroidId = 42UL;
22 const char kAppId[] = "TestAppId"; 23 const char kAppId[] = "TestAppId";
23 const char kDeveloperId[] = "Project1"; 24 const char kDeveloperId[] = "Project1";
24 const char kLoginHeader[] = "AidLogin"; 25 const char kLoginHeader[] = "AidLogin";
25 const char kRegistrationURL[] = "http://foo.bar/register"; 26 const char kRegistrationURL[] = "http://foo.bar/register";
26 const uint64 kSecurityToken = 77UL; 27 const uint64 kSecurityToken = 77UL;
28 const int kGCMVersion = 40;
29 const char kInstanceId[] = "IID1";
30 const char kScope[] = "GCM";
27 31
28 // Backoff policy for testing registration request. 32 // Backoff policy for testing registration request.
29 const net::BackoffEntry::Policy kDefaultBackoffPolicy = { 33 const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
30 // Number of initial errors (in sequence) to ignore before applying 34 // Number of initial errors (in sequence) to ignore before applying
31 // exponential back-off rules. 35 // exponential back-off rules.
32 // Explicitly set to 2 to skip the delay on the first retry, as we are not 36 // Explicitly set to 2 to skip the delay on the first retry, as we are not
33 // trying to test the backoff itself, but rather the fact that retry happens. 37 // trying to test the backoff itself, but rather the fact that retry happens.
34 2, 38 2,
35 39
36 // Initial delay for exponential back-off in ms. 40 // Initial delay for exponential back-off in ms.
(...skipping 20 matching lines...) Expand all
57 } // namespace 61 } // namespace
58 62
59 class RegistrationRequestTest : public testing::Test { 63 class RegistrationRequestTest : public testing::Test {
60 public: 64 public:
61 RegistrationRequestTest(); 65 RegistrationRequestTest();
62 ~RegistrationRequestTest() override; 66 ~RegistrationRequestTest() override;
63 67
64 void RegistrationCallback(RegistrationRequest::Status status, 68 void RegistrationCallback(RegistrationRequest::Status status,
65 const std::string& registration_id); 69 const std::string& registration_id);
66 70
67 void CreateRequest(const std::string& sender_ids);
68 void SetResponseStatusAndString(net::HttpStatusCode status_code, 71 void SetResponseStatusAndString(net::HttpStatusCode status_code,
69 const std::string& response_body); 72 const std::string& response_body);
70 void CompleteFetch(); 73 void CompleteFetch();
71 void set_max_retry_count(int max_retry_count) { 74 void set_max_retry_count(int max_retry_count) {
72 max_retry_count_ = max_retry_count; 75 max_retry_count_ = max_retry_count;
73 } 76 }
74 77
75 protected: 78 protected:
76 int max_retry_count_; 79 int max_retry_count_;
77 RegistrationRequest::Status status_; 80 RegistrationRequest::Status status_;
(...skipping 17 matching lines...) Expand all
95 RegistrationRequestTest::~RegistrationRequestTest() {} 98 RegistrationRequestTest::~RegistrationRequestTest() {}
96 99
97 void RegistrationRequestTest::RegistrationCallback( 100 void RegistrationRequestTest::RegistrationCallback(
98 RegistrationRequest::Status status, 101 RegistrationRequest::Status status,
99 const std::string& registration_id) { 102 const std::string& registration_id) {
100 status_ = status; 103 status_ = status;
101 registration_id_ = registration_id; 104 registration_id_ = registration_id;
102 callback_called_ = true; 105 callback_called_ = true;
103 } 106 }
104 107
105 void RegistrationRequestTest::CreateRequest(const std::string& sender_ids) {
106 std::vector<std::string> senders;
107 base::StringTokenizer tokenizer(sender_ids, ",");
108 while (tokenizer.GetNext())
109 senders.push_back(tokenizer.token());
110
111 request_.reset(new RegistrationRequest(
112 GURL(kRegistrationURL),
113 RegistrationRequest::RequestInfo(kAndroidId,
114 kSecurityToken,
115 kAppId,
116 senders),
117 kDefaultBackoffPolicy,
118 base::Bind(&RegistrationRequestTest::RegistrationCallback,
119 base::Unretained(this)),
120 max_retry_count_,
121 url_request_context_getter_.get(),
122 &recorder_));
123 }
124
125 void RegistrationRequestTest::SetResponseStatusAndString( 108 void RegistrationRequestTest::SetResponseStatusAndString(
126 net::HttpStatusCode status_code, 109 net::HttpStatusCode status_code,
127 const std::string& response_body) { 110 const std::string& response_body) {
128 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 111 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
129 ASSERT_TRUE(fetcher); 112 ASSERT_TRUE(fetcher);
130 fetcher->set_response_code(status_code); 113 fetcher->set_response_code(status_code);
131 fetcher->SetResponseString(response_body); 114 fetcher->SetResponseString(response_body);
132 } 115 }
133 116
134 void RegistrationRequestTest::CompleteFetch() { 117 void RegistrationRequestTest::CompleteFetch() {
135 registration_id_.clear(); 118 registration_id_.clear();
136 status_ = RegistrationRequest::SUCCESS; 119 status_ = RegistrationRequest::SUCCESS;
137 callback_called_ = false; 120 callback_called_ = false;
138 121
139 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 122 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
140 ASSERT_TRUE(fetcher); 123 ASSERT_TRUE(fetcher);
141 fetcher->delegate()->OnURLFetchComplete(fetcher); 124 fetcher->delegate()->OnURLFetchComplete(fetcher);
142 } 125 }
143 126
144 TEST_F(RegistrationRequestTest, RequestSuccessful) { 127 class GCMRegistrationRequestTest : public RegistrationRequestTest {
128 public:
129 GCMRegistrationRequestTest();
130 ~GCMRegistrationRequestTest() override;
131
132 void CreateRequest(const std::string& sender_ids);
133 };
134
135 GCMRegistrationRequestTest::GCMRegistrationRequestTest() {
136 }
137
138 GCMRegistrationRequestTest::~GCMRegistrationRequestTest() {
139 }
140
141 void GCMRegistrationRequestTest::CreateRequest(const std::string& sender_ids) {
142 RegistrationRequest::RequestInfo request_info(
143 kAndroidId, kSecurityToken, kAppId);
144 scoped_ptr<GCMRegistrationRequestHandler> request_handler(
145 new GCMRegistrationRequestHandler(sender_ids));
146 request_.reset(new RegistrationRequest(
147 GURL(kRegistrationURL),
148 request_info,
149 request_handler.Pass(),
150 kDefaultBackoffPolicy,
151 base::Bind(&RegistrationRequestTest::RegistrationCallback,
152 base::Unretained(this)),
153 max_retry_count_,
154 url_request_context_getter_.get(),
155 &recorder_,
156 sender_ids));
157 }
158
159 TEST_F(GCMRegistrationRequestTest, RequestSuccessful) {
145 set_max_retry_count(0); 160 set_max_retry_count(0);
146 CreateRequest("sender1,sender2"); 161 CreateRequest("sender1,sender2");
147 request_->Start(); 162 request_->Start();
148 163
149 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 164 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
150 CompleteFetch(); 165 CompleteFetch();
151 166
152 EXPECT_TRUE(callback_called_); 167 EXPECT_TRUE(callback_called_);
153 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 168 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
154 EXPECT_EQ("2501", registration_id_); 169 EXPECT_EQ("2501", registration_id_);
155 } 170 }
156 171
157 TEST_F(RegistrationRequestTest, RequestDataAndURL) { 172 TEST_F(GCMRegistrationRequestTest, RequestDataAndURL) {
158 CreateRequest(kDeveloperId); 173 CreateRequest(kDeveloperId);
159 request_->Start(); 174 request_->Start();
160 175
161 // Get data sent by request. 176 // Get data sent by request.
162 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 177 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
163 ASSERT_TRUE(fetcher); 178 ASSERT_TRUE(fetcher);
164 179
165 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); 180 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL());
166 181
167 // Verify that authorization header was put together properly. 182 // Verify that authorization header was put together properly.
(...skipping 23 matching lines...) Expand all
191 ASSERT_TRUE(iter != expected_pairs.end()); 206 ASSERT_TRUE(iter != expected_pairs.end());
192 ASSERT_TRUE(data_tokenizer.GetNext()); 207 ASSERT_TRUE(data_tokenizer.GetNext());
193 EXPECT_EQ(iter->second, data_tokenizer.token()); 208 EXPECT_EQ(iter->second, data_tokenizer.token());
194 // Ensure that none of the keys appears twice. 209 // Ensure that none of the keys appears twice.
195 expected_pairs.erase(iter); 210 expected_pairs.erase(iter);
196 } 211 }
197 212
198 EXPECT_EQ(0UL, expected_pairs.size()); 213 EXPECT_EQ(0UL, expected_pairs.size());
199 } 214 }
200 215
201 TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) { 216 TEST_F(GCMRegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) {
202 CreateRequest("sender1,sender2@gmail.com"); 217 CreateRequest("sender1,sender2@gmail.com");
203 request_->Start(); 218 request_->Start();
204 219
205 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 220 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
206 ASSERT_TRUE(fetcher); 221 ASSERT_TRUE(fetcher);
207 222
208 // Verify data was formatted properly. 223 // Verify data was formatted properly.
209 std::string upload_data = fetcher->upload_data(); 224 std::string upload_data = fetcher->upload_data();
210 base::StringTokenizer data_tokenizer(upload_data, "&="); 225 base::StringTokenizer data_tokenizer(upload_data, "&=");
211 226
212 // Skip all tokens until you hit entry for senders. 227 // Skip all tokens until you hit entry for senders.
213 while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender") 228 while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender")
214 continue; 229 continue;
215 230
216 ASSERT_TRUE(data_tokenizer.GetNext()); 231 ASSERT_TRUE(data_tokenizer.GetNext());
217 std::string senders(net::UnescapeURLComponent(data_tokenizer.token(), 232 std::string senders(net::UnescapeURLComponent(data_tokenizer.token(),
218 net::UnescapeRule::URL_SPECIAL_CHARS)); 233 net::UnescapeRule::URL_SPECIAL_CHARS));
219 base::StringTokenizer sender_tokenizer(senders, ","); 234 base::StringTokenizer sender_tokenizer(senders, ",");
220 ASSERT_TRUE(sender_tokenizer.GetNext()); 235 ASSERT_TRUE(sender_tokenizer.GetNext());
221 EXPECT_EQ("sender1", sender_tokenizer.token()); 236 EXPECT_EQ("sender1", sender_tokenizer.token());
222 ASSERT_TRUE(sender_tokenizer.GetNext()); 237 ASSERT_TRUE(sender_tokenizer.GetNext());
223 EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token()); 238 EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token());
224 } 239 }
225 240
226 TEST_F(RegistrationRequestTest, ResponseParsing) { 241 TEST_F(GCMRegistrationRequestTest, ResponseParsing) {
227 CreateRequest("sender1,sender2"); 242 CreateRequest("sender1,sender2");
228 request_->Start(); 243 request_->Start();
229 244
230 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 245 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
231 CompleteFetch(); 246 CompleteFetch();
232 247
233 EXPECT_TRUE(callback_called_); 248 EXPECT_TRUE(callback_called_);
234 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 249 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
235 EXPECT_EQ("2501", registration_id_); 250 EXPECT_EQ("2501", registration_id_);
236 } 251 }
237 252
238 TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) { 253 TEST_F(GCMRegistrationRequestTest, ResponseHttpStatusNotOK) {
239 CreateRequest("sender1,sender2"); 254 CreateRequest("sender1,sender2");
240 request_->Start(); 255 request_->Start();
241 256
242 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501"); 257 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501");
243 CompleteFetch(); 258 CompleteFetch();
244 259
245 EXPECT_FALSE(callback_called_); 260 EXPECT_FALSE(callback_called_);
246 261
247 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 262 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
248 CompleteFetch(); 263 CompleteFetch();
249 264
250 EXPECT_TRUE(callback_called_); 265 EXPECT_TRUE(callback_called_);
251 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 266 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
252 EXPECT_EQ("2501", registration_id_); 267 EXPECT_EQ("2501", registration_id_);
253 } 268 }
254 269
255 TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) { 270 TEST_F(GCMRegistrationRequestTest, ResponseMissingRegistrationId) {
256 CreateRequest("sender1,sender2"); 271 CreateRequest("sender1,sender2");
257 request_->Start(); 272 request_->Start();
258 273
259 SetResponseStatusAndString(net::HTTP_OK, ""); 274 SetResponseStatusAndString(net::HTTP_OK, "");
260 CompleteFetch(); 275 CompleteFetch();
261 276
262 EXPECT_FALSE(callback_called_); 277 EXPECT_FALSE(callback_called_);
263 278
264 SetResponseStatusAndString(net::HTTP_OK, "some error in response"); 279 SetResponseStatusAndString(net::HTTP_OK, "some error in response");
265 CompleteFetch(); 280 CompleteFetch();
266 281
267 EXPECT_FALSE(callback_called_); 282 EXPECT_FALSE(callback_called_);
268 283
269 // Ensuring a retry happened and succeeds. 284 // Ensuring a retry happened and succeeds.
270 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 285 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
271 CompleteFetch(); 286 CompleteFetch();
272 287
273 EXPECT_TRUE(callback_called_); 288 EXPECT_TRUE(callback_called_);
274 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 289 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
275 EXPECT_EQ("2501", registration_id_); 290 EXPECT_EQ("2501", registration_id_);
276 } 291 }
277 292
278 TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) { 293 TEST_F(GCMRegistrationRequestTest, ResponseDeviceRegistrationError) {
279 CreateRequest("sender1,sender2"); 294 CreateRequest("sender1,sender2");
280 request_->Start(); 295 request_->Start();
281 296
282 SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR"); 297 SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR");
283 CompleteFetch(); 298 CompleteFetch();
284 299
285 EXPECT_FALSE(callback_called_); 300 EXPECT_FALSE(callback_called_);
286 301
287 // Ensuring a retry happened and succeeds. 302 // Ensuring a retry happened and succeeds.
288 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 303 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
289 CompleteFetch(); 304 CompleteFetch();
290 305
291 EXPECT_TRUE(callback_called_); 306 EXPECT_TRUE(callback_called_);
292 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 307 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
293 EXPECT_EQ("2501", registration_id_); 308 EXPECT_EQ("2501", registration_id_);
294 } 309 }
295 310
296 TEST_F(RegistrationRequestTest, ResponseAuthenticationError) { 311 TEST_F(GCMRegistrationRequestTest, ResponseAuthenticationError) {
297 CreateRequest("sender1,sender2"); 312 CreateRequest("sender1,sender2");
298 request_->Start(); 313 request_->Start();
299 314
300 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, 315 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED,
301 "Error=AUTHENTICATION_FAILED"); 316 "Error=AUTHENTICATION_FAILED");
302 CompleteFetch(); 317 CompleteFetch();
303 318
304 EXPECT_FALSE(callback_called_); 319 EXPECT_FALSE(callback_called_);
305 320
306 // Ensuring a retry happened and succeeds. 321 // Ensuring a retry happened and succeeds.
307 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 322 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
308 CompleteFetch(); 323 CompleteFetch();
309 324
310 EXPECT_TRUE(callback_called_); 325 EXPECT_TRUE(callback_called_);
311 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 326 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
312 EXPECT_EQ("2501", registration_id_); 327 EXPECT_EQ("2501", registration_id_);
313 } 328 }
314 329
315 TEST_F(RegistrationRequestTest, ResponseInvalidParameters) { 330 TEST_F(GCMRegistrationRequestTest, ResponseInvalidParameters) {
316 CreateRequest("sender1,sender2"); 331 CreateRequest("sender1,sender2");
317 request_->Start(); 332 request_->Start();
318 333
319 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS"); 334 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
320 CompleteFetch(); 335 CompleteFetch();
321 336
322 EXPECT_TRUE(callback_called_); 337 EXPECT_TRUE(callback_called_);
323 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_); 338 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_);
324 EXPECT_EQ(std::string(), registration_id_); 339 EXPECT_EQ(std::string(), registration_id_);
325 } 340 }
326 341
327 TEST_F(RegistrationRequestTest, ResponseInvalidSender) { 342 TEST_F(GCMRegistrationRequestTest, ResponseInvalidSender) {
328 CreateRequest("sender1,sender2"); 343 CreateRequest("sender1,sender2");
329 request_->Start(); 344 request_->Start();
330 345
331 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER"); 346 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER");
332 CompleteFetch(); 347 CompleteFetch();
333 348
334 EXPECT_TRUE(callback_called_); 349 EXPECT_TRUE(callback_called_);
335 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); 350 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
336 EXPECT_EQ(std::string(), registration_id_); 351 EXPECT_EQ(std::string(), registration_id_);
337 } 352 }
338 353
339 TEST_F(RegistrationRequestTest, ResponseInvalidSenderBadRequest) { 354 TEST_F(GCMRegistrationRequestTest, ResponseInvalidSenderBadRequest) {
340 CreateRequest("sender1"); 355 CreateRequest("sender1");
341 request_->Start(); 356 request_->Start();
342 357
343 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER"); 358 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER");
344 CompleteFetch(); 359 CompleteFetch();
345 360
346 EXPECT_TRUE(callback_called_); 361 EXPECT_TRUE(callback_called_);
347 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); 362 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
348 EXPECT_EQ(std::string(), registration_id_); 363 EXPECT_EQ(std::string(), registration_id_);
349 } 364 }
350 365
351 TEST_F(RegistrationRequestTest, RequestNotSuccessful) { 366 TEST_F(GCMRegistrationRequestTest, RequestNotSuccessful) {
352 CreateRequest("sender1,sender2"); 367 CreateRequest("sender1,sender2");
353 request_->Start(); 368 request_->Start();
354 369
355 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1); 370 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1);
356 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 371 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
357 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 372 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
358 ASSERT_TRUE(fetcher); 373 ASSERT_TRUE(fetcher);
359 fetcher->set_status(request_status); 374 fetcher->set_status(request_status);
360 375
361 CompleteFetch(); 376 CompleteFetch();
362 377
363 EXPECT_FALSE(callback_called_); 378 EXPECT_FALSE(callback_called_);
364 379
365 // Ensuring a retry happened and succeeded. 380 // Ensuring a retry happened and succeeded.
366 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 381 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
367 CompleteFetch(); 382 CompleteFetch();
368 383
369 EXPECT_TRUE(callback_called_); 384 EXPECT_TRUE(callback_called_);
370 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 385 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
371 EXPECT_EQ("2501", registration_id_); 386 EXPECT_EQ("2501", registration_id_);
372 } 387 }
373 388
374 TEST_F(RegistrationRequestTest, ResponseHttpNotOk) { 389 TEST_F(GCMRegistrationRequestTest, ResponseHttpNotOk) {
375 CreateRequest("sender1,sender2"); 390 CreateRequest("sender1,sender2");
376 request_->Start(); 391 request_->Start();
377 392
378 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 393 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
379 CompleteFetch(); 394 CompleteFetch();
380 395
381 EXPECT_FALSE(callback_called_); 396 EXPECT_FALSE(callback_called_);
382 397
383 // Ensuring a retry happened and succeeded. 398 // Ensuring a retry happened and succeeded.
384 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 399 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
385 CompleteFetch(); 400 CompleteFetch();
386 401
387 EXPECT_TRUE(callback_called_); 402 EXPECT_TRUE(callback_called_);
388 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 403 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
389 EXPECT_EQ("2501", registration_id_); 404 EXPECT_EQ("2501", registration_id_);
390 } 405 }
391 406
392 TEST_F(RegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) { 407 TEST_F(GCMRegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) {
393 set_max_retry_count(0); 408 set_max_retry_count(0);
394 CreateRequest("sender1,sender2"); 409 CreateRequest("sender1,sender2");
395 request_->Start(); 410 request_->Start();
396 411
397 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 412 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
398 CompleteFetch(); 413 CompleteFetch();
399 414
400 EXPECT_TRUE(callback_called_); 415 EXPECT_TRUE(callback_called_);
401 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); 416 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_);
402 EXPECT_EQ(std::string(), registration_id_); 417 EXPECT_EQ(std::string(), registration_id_);
403 } 418 }
404 419
405 TEST_F(RegistrationRequestTest, MaximumAttemptsReached) { 420 TEST_F(GCMRegistrationRequestTest, MaximumAttemptsReached) {
406 CreateRequest("sender1,sender2"); 421 CreateRequest("sender1,sender2");
407 request_->Start(); 422 request_->Start();
408 423
409 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 424 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
410 CompleteFetch(); 425 CompleteFetch();
411 426
412 EXPECT_FALSE(callback_called_); 427 EXPECT_FALSE(callback_called_);
413 428
414 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 429 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
415 CompleteFetch(); 430 CompleteFetch();
416 431
417 EXPECT_FALSE(callback_called_); 432 EXPECT_FALSE(callback_called_);
418 433
419 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 434 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
420 CompleteFetch(); 435 CompleteFetch();
421 436
422 EXPECT_TRUE(callback_called_); 437 EXPECT_TRUE(callback_called_);
423 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); 438 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_);
424 EXPECT_EQ(std::string(), registration_id_); 439 EXPECT_EQ(std::string(), registration_id_);
425 } 440 }
426 441
442 class InstanceIDGetTokenRequestTest : public RegistrationRequestTest {
443 public:
444 InstanceIDGetTokenRequestTest();
445 ~InstanceIDGetTokenRequestTest() override;
446
447 void CreateRequest(const std::string& instance_id,
448 const std::string& authorized_entity,
449 const std::string& scope,
450 const std::map<std::string, std::string>& options);
451 };
452
453 InstanceIDGetTokenRequestTest::InstanceIDGetTokenRequestTest() {
454 }
455
456 InstanceIDGetTokenRequestTest::~InstanceIDGetTokenRequestTest() {
457 }
458
459 void InstanceIDGetTokenRequestTest::CreateRequest(
460 const std::string& instance_id,
461 const std::string& authorized_entity,
462 const std::string& scope,
463 const std::map<std::string, std::string>& options) {
464 RegistrationRequest::RequestInfo request_info(
465 kAndroidId, kSecurityToken, kAppId);
466 scoped_ptr<InstanceIDGetTokenRequestHandler> request_handler(
467 new InstanceIDGetTokenRequestHandler(
468 instance_id, authorized_entity, scope, kGCMVersion, options));
469 request_.reset(new RegistrationRequest(
470 GURL(kRegistrationURL),
471 request_info,
472 request_handler.Pass(),
473 kDefaultBackoffPolicy,
474 base::Bind(&RegistrationRequestTest::RegistrationCallback,
475 base::Unretained(this)),
476 max_retry_count_,
477 url_request_context_getter_.get(),
478 &recorder_,
479 authorized_entity));
480 }
481
482 TEST_F(InstanceIDGetTokenRequestTest, RequestSuccessful) {
483 std::map<std::string, std::string> options;
484 options["Foo"] = "Bar";
485
486 set_max_retry_count(0);
487 CreateRequest(kInstanceId, kDeveloperId, kScope, options);
488 request_->Start();
489
490 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
491 CompleteFetch();
492
493 EXPECT_TRUE(callback_called_);
494 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
495 EXPECT_EQ("2501", registration_id_);
496 }
497
498 TEST_F(InstanceIDGetTokenRequestTest, RequestDataAndURL) {
499 std::map<std::string, std::string> options;
500 options["Foo"] = "Bar";
501 CreateRequest(kInstanceId, kDeveloperId, kScope, options);
502 request_->Start();
503
504 // Get data sent by request.
505 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
506 ASSERT_TRUE(fetcher);
507
508 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL());
509
510 // Verify that authorization header was put together properly.
511 net::HttpRequestHeaders headers;
512 fetcher->GetExtraRequestHeaders(&headers);
513 std::string auth_header;
514 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header);
515 base::StringTokenizer auth_tokenizer(auth_header, " :");
516 ASSERT_TRUE(auth_tokenizer.GetNext());
517 EXPECT_EQ(kLoginHeader, auth_tokenizer.token());
518 ASSERT_TRUE(auth_tokenizer.GetNext());
519 EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token());
520 ASSERT_TRUE(auth_tokenizer.GetNext());
521 EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token());
522
523 std::map<std::string, std::string> expected_pairs;
524 expected_pairs["gmsv"] = base::IntToString(kGCMVersion);
525 expected_pairs["app"] = kAppId;
526 expected_pairs["sender"] = kDeveloperId;
527 expected_pairs["device"] = base::Uint64ToString(kAndroidId);
528 expected_pairs["appid"] = kInstanceId;
529 expected_pairs["scope"] = kScope;
530 expected_pairs["X-Foo"] = "Bar";
531
532 // Verify data was formatted properly.
533 std::string upload_data = fetcher->upload_data();
534 base::StringTokenizer data_tokenizer(upload_data, "&=");
535 while (data_tokenizer.GetNext()) {
536 std::map<std::string, std::string>::iterator iter =
537 expected_pairs.find(data_tokenizer.token());
538 ASSERT_TRUE(iter != expected_pairs.end());
539 ASSERT_TRUE(data_tokenizer.GetNext());
540 EXPECT_EQ(iter->second, data_tokenizer.token());
541 // Ensure that none of the keys appears twice.
542 expected_pairs.erase(iter);
543 }
544
545 EXPECT_EQ(0UL, expected_pairs.size());
546 }
547
548 TEST_F(InstanceIDGetTokenRequestTest, ResponseHttpStatusNotOK) {
549 std::map<std::string, std::string> options;
550 CreateRequest(kInstanceId, kDeveloperId, kScope, options);
551 request_->Start();
552
553 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501");
554 CompleteFetch();
555
556 EXPECT_FALSE(callback_called_);
557
558 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
559 CompleteFetch();
560
561 EXPECT_TRUE(callback_called_);
562 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
563 EXPECT_EQ("2501", registration_id_);
564 }
565
427 } // namespace gcm 566 } // namespace gcm
OLDNEW
« no previous file with comments | « google_apis/gcm/engine/registration_request.cc ('k') | google_apis/gcm/engine/unregistration_request.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698