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

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: Fix trybots Created 5 years, 7 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.h"
12 #include "google_apis/gcm/engine/instance_id_get_token_request.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 std::vector<std::string> senders;
143 base::StringTokenizer tokenizer(sender_ids, ",");
144 while (tokenizer.GetNext())
145 senders.push_back(tokenizer.token());
146
147 RegistrationRequest::RequestInfo request_info(
148 kAndroidId, kSecurityToken, kAppId);
149 GCMRegistrationRequest::ExtraRequestInfo extra_request_info(
150 senders);
151 request_.reset(new GCMRegistrationRequest(
152 GURL(kRegistrationURL),
153 request_info,
154 extra_request_info,
155 kDefaultBackoffPolicy,
156 base::Bind(&RegistrationRequestTest::RegistrationCallback,
157 base::Unretained(this)),
158 max_retry_count_,
159 url_request_context_getter_.get(),
160 &recorder_));
161 }
162
163 TEST_F(GCMRegistrationRequestTest, RequestSuccessful) {
145 set_max_retry_count(0); 164 set_max_retry_count(0);
146 CreateRequest("sender1,sender2"); 165 CreateRequest("sender1,sender2");
147 request_->Start(); 166 request_->Start();
148 167
149 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 168 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
150 CompleteFetch(); 169 CompleteFetch();
151 170
152 EXPECT_TRUE(callback_called_); 171 EXPECT_TRUE(callback_called_);
153 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 172 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
154 EXPECT_EQ("2501", registration_id_); 173 EXPECT_EQ("2501", registration_id_);
155 } 174 }
156 175
157 TEST_F(RegistrationRequestTest, RequestDataAndURL) { 176 TEST_F(GCMRegistrationRequestTest, RequestDataAndURL) {
158 CreateRequest(kDeveloperId); 177 CreateRequest(kDeveloperId);
159 request_->Start(); 178 request_->Start();
160 179
161 // Get data sent by request. 180 // Get data sent by request.
162 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 181 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
163 ASSERT_TRUE(fetcher); 182 ASSERT_TRUE(fetcher);
164 183
165 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); 184 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL());
166 185
167 // Verify that authorization header was put together properly. 186 // Verify that authorization header was put together properly.
(...skipping 23 matching lines...) Expand all
191 ASSERT_TRUE(iter != expected_pairs.end()); 210 ASSERT_TRUE(iter != expected_pairs.end());
192 ASSERT_TRUE(data_tokenizer.GetNext()); 211 ASSERT_TRUE(data_tokenizer.GetNext());
193 EXPECT_EQ(iter->second, data_tokenizer.token()); 212 EXPECT_EQ(iter->second, data_tokenizer.token());
194 // Ensure that none of the keys appears twice. 213 // Ensure that none of the keys appears twice.
195 expected_pairs.erase(iter); 214 expected_pairs.erase(iter);
196 } 215 }
197 216
198 EXPECT_EQ(0UL, expected_pairs.size()); 217 EXPECT_EQ(0UL, expected_pairs.size());
199 } 218 }
200 219
201 TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) { 220 TEST_F(GCMRegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) {
202 CreateRequest("sender1,sender2@gmail.com"); 221 CreateRequest("sender1,sender2@gmail.com");
203 request_->Start(); 222 request_->Start();
204 223
205 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 224 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
206 ASSERT_TRUE(fetcher); 225 ASSERT_TRUE(fetcher);
207 226
208 // Verify data was formatted properly. 227 // Verify data was formatted properly.
209 std::string upload_data = fetcher->upload_data(); 228 std::string upload_data = fetcher->upload_data();
210 base::StringTokenizer data_tokenizer(upload_data, "&="); 229 base::StringTokenizer data_tokenizer(upload_data, "&=");
211 230
212 // Skip all tokens until you hit entry for senders. 231 // Skip all tokens until you hit entry for senders.
213 while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender") 232 while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender")
214 continue; 233 continue;
215 234
216 ASSERT_TRUE(data_tokenizer.GetNext()); 235 ASSERT_TRUE(data_tokenizer.GetNext());
217 std::string senders(net::UnescapeURLComponent(data_tokenizer.token(), 236 std::string senders(net::UnescapeURLComponent(data_tokenizer.token(),
218 net::UnescapeRule::URL_SPECIAL_CHARS)); 237 net::UnescapeRule::URL_SPECIAL_CHARS));
219 base::StringTokenizer sender_tokenizer(senders, ","); 238 base::StringTokenizer sender_tokenizer(senders, ",");
220 ASSERT_TRUE(sender_tokenizer.GetNext()); 239 ASSERT_TRUE(sender_tokenizer.GetNext());
221 EXPECT_EQ("sender1", sender_tokenizer.token()); 240 EXPECT_EQ("sender1", sender_tokenizer.token());
222 ASSERT_TRUE(sender_tokenizer.GetNext()); 241 ASSERT_TRUE(sender_tokenizer.GetNext());
223 EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token()); 242 EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token());
224 } 243 }
225 244
226 TEST_F(RegistrationRequestTest, ResponseParsing) { 245 TEST_F(GCMRegistrationRequestTest, ResponseParsing) {
227 CreateRequest("sender1,sender2"); 246 CreateRequest("sender1,sender2");
228 request_->Start(); 247 request_->Start();
229 248
230 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 249 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
231 CompleteFetch(); 250 CompleteFetch();
232 251
233 EXPECT_TRUE(callback_called_); 252 EXPECT_TRUE(callback_called_);
234 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 253 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
235 EXPECT_EQ("2501", registration_id_); 254 EXPECT_EQ("2501", registration_id_);
236 } 255 }
237 256
238 TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) { 257 TEST_F(GCMRegistrationRequestTest, ResponseHttpStatusNotOK) {
239 CreateRequest("sender1,sender2"); 258 CreateRequest("sender1,sender2");
240 request_->Start(); 259 request_->Start();
241 260
242 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501"); 261 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501");
243 CompleteFetch(); 262 CompleteFetch();
244 263
245 EXPECT_FALSE(callback_called_); 264 EXPECT_FALSE(callback_called_);
246 265
247 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 266 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
248 CompleteFetch(); 267 CompleteFetch();
249 268
250 EXPECT_TRUE(callback_called_); 269 EXPECT_TRUE(callback_called_);
251 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 270 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
252 EXPECT_EQ("2501", registration_id_); 271 EXPECT_EQ("2501", registration_id_);
253 } 272 }
254 273
255 TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) { 274 TEST_F(GCMRegistrationRequestTest, ResponseMissingRegistrationId) {
256 CreateRequest("sender1,sender2"); 275 CreateRequest("sender1,sender2");
257 request_->Start(); 276 request_->Start();
258 277
259 SetResponseStatusAndString(net::HTTP_OK, ""); 278 SetResponseStatusAndString(net::HTTP_OK, "");
260 CompleteFetch(); 279 CompleteFetch();
261 280
262 EXPECT_FALSE(callback_called_); 281 EXPECT_FALSE(callback_called_);
263 282
264 SetResponseStatusAndString(net::HTTP_OK, "some error in response"); 283 SetResponseStatusAndString(net::HTTP_OK, "some error in response");
265 CompleteFetch(); 284 CompleteFetch();
266 285
267 EXPECT_FALSE(callback_called_); 286 EXPECT_FALSE(callback_called_);
268 287
269 // Ensuring a retry happened and succeeds. 288 // Ensuring a retry happened and succeeds.
270 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 289 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
271 CompleteFetch(); 290 CompleteFetch();
272 291
273 EXPECT_TRUE(callback_called_); 292 EXPECT_TRUE(callback_called_);
274 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 293 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
275 EXPECT_EQ("2501", registration_id_); 294 EXPECT_EQ("2501", registration_id_);
276 } 295 }
277 296
278 TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) { 297 TEST_F(GCMRegistrationRequestTest, ResponseDeviceRegistrationError) {
279 CreateRequest("sender1,sender2"); 298 CreateRequest("sender1,sender2");
280 request_->Start(); 299 request_->Start();
281 300
282 SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR"); 301 SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR");
283 CompleteFetch(); 302 CompleteFetch();
284 303
285 EXPECT_FALSE(callback_called_); 304 EXPECT_FALSE(callback_called_);
286 305
287 // Ensuring a retry happened and succeeds. 306 // Ensuring a retry happened and succeeds.
288 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 307 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
289 CompleteFetch(); 308 CompleteFetch();
290 309
291 EXPECT_TRUE(callback_called_); 310 EXPECT_TRUE(callback_called_);
292 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 311 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
293 EXPECT_EQ("2501", registration_id_); 312 EXPECT_EQ("2501", registration_id_);
294 } 313 }
295 314
296 TEST_F(RegistrationRequestTest, ResponseAuthenticationError) { 315 TEST_F(GCMRegistrationRequestTest, ResponseAuthenticationError) {
297 CreateRequest("sender1,sender2"); 316 CreateRequest("sender1,sender2");
298 request_->Start(); 317 request_->Start();
299 318
300 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, 319 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED,
301 "Error=AUTHENTICATION_FAILED"); 320 "Error=AUTHENTICATION_FAILED");
302 CompleteFetch(); 321 CompleteFetch();
303 322
304 EXPECT_FALSE(callback_called_); 323 EXPECT_FALSE(callback_called_);
305 324
306 // Ensuring a retry happened and succeeds. 325 // Ensuring a retry happened and succeeds.
307 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 326 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
308 CompleteFetch(); 327 CompleteFetch();
309 328
310 EXPECT_TRUE(callback_called_); 329 EXPECT_TRUE(callback_called_);
311 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 330 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
312 EXPECT_EQ("2501", registration_id_); 331 EXPECT_EQ("2501", registration_id_);
313 } 332 }
314 333
315 TEST_F(RegistrationRequestTest, ResponseInvalidParameters) { 334 TEST_F(GCMRegistrationRequestTest, ResponseInvalidParameters) {
316 CreateRequest("sender1,sender2"); 335 CreateRequest("sender1,sender2");
317 request_->Start(); 336 request_->Start();
318 337
319 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS"); 338 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
320 CompleteFetch(); 339 CompleteFetch();
321 340
322 EXPECT_TRUE(callback_called_); 341 EXPECT_TRUE(callback_called_);
323 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_); 342 EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_);
324 EXPECT_EQ(std::string(), registration_id_); 343 EXPECT_EQ(std::string(), registration_id_);
325 } 344 }
326 345
327 TEST_F(RegistrationRequestTest, ResponseInvalidSender) { 346 TEST_F(GCMRegistrationRequestTest, ResponseInvalidSender) {
328 CreateRequest("sender1,sender2"); 347 CreateRequest("sender1,sender2");
329 request_->Start(); 348 request_->Start();
330 349
331 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER"); 350 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER");
332 CompleteFetch(); 351 CompleteFetch();
333 352
334 EXPECT_TRUE(callback_called_); 353 EXPECT_TRUE(callback_called_);
335 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); 354 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
336 EXPECT_EQ(std::string(), registration_id_); 355 EXPECT_EQ(std::string(), registration_id_);
337 } 356 }
338 357
339 TEST_F(RegistrationRequestTest, ResponseInvalidSenderBadRequest) { 358 TEST_F(GCMRegistrationRequestTest, ResponseInvalidSenderBadRequest) {
340 CreateRequest("sender1"); 359 CreateRequest("sender1");
341 request_->Start(); 360 request_->Start();
342 361
343 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER"); 362 SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER");
344 CompleteFetch(); 363 CompleteFetch();
345 364
346 EXPECT_TRUE(callback_called_); 365 EXPECT_TRUE(callback_called_);
347 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_); 366 EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
348 EXPECT_EQ(std::string(), registration_id_); 367 EXPECT_EQ(std::string(), registration_id_);
349 } 368 }
350 369
351 TEST_F(RegistrationRequestTest, RequestNotSuccessful) { 370 TEST_F(GCMRegistrationRequestTest, RequestNotSuccessful) {
352 CreateRequest("sender1,sender2"); 371 CreateRequest("sender1,sender2");
353 request_->Start(); 372 request_->Start();
354 373
355 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1); 374 net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1);
356 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 375 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
357 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); 376 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
358 ASSERT_TRUE(fetcher); 377 ASSERT_TRUE(fetcher);
359 fetcher->set_status(request_status); 378 fetcher->set_status(request_status);
360 379
361 CompleteFetch(); 380 CompleteFetch();
362 381
363 EXPECT_FALSE(callback_called_); 382 EXPECT_FALSE(callback_called_);
364 383
365 // Ensuring a retry happened and succeeded. 384 // Ensuring a retry happened and succeeded.
366 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 385 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
367 CompleteFetch(); 386 CompleteFetch();
368 387
369 EXPECT_TRUE(callback_called_); 388 EXPECT_TRUE(callback_called_);
370 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 389 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
371 EXPECT_EQ("2501", registration_id_); 390 EXPECT_EQ("2501", registration_id_);
372 } 391 }
373 392
374 TEST_F(RegistrationRequestTest, ResponseHttpNotOk) { 393 TEST_F(GCMRegistrationRequestTest, ResponseHttpNotOk) {
375 CreateRequest("sender1,sender2"); 394 CreateRequest("sender1,sender2");
376 request_->Start(); 395 request_->Start();
377 396
378 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 397 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
379 CompleteFetch(); 398 CompleteFetch();
380 399
381 EXPECT_FALSE(callback_called_); 400 EXPECT_FALSE(callback_called_);
382 401
383 // Ensuring a retry happened and succeeded. 402 // Ensuring a retry happened and succeeded.
384 SetResponseStatusAndString(net::HTTP_OK, "token=2501"); 403 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
385 CompleteFetch(); 404 CompleteFetch();
386 405
387 EXPECT_TRUE(callback_called_); 406 EXPECT_TRUE(callback_called_);
388 EXPECT_EQ(RegistrationRequest::SUCCESS, status_); 407 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
389 EXPECT_EQ("2501", registration_id_); 408 EXPECT_EQ("2501", registration_id_);
390 } 409 }
391 410
392 TEST_F(RegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) { 411 TEST_F(GCMRegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) {
393 set_max_retry_count(0); 412 set_max_retry_count(0);
394 CreateRequest("sender1,sender2"); 413 CreateRequest("sender1,sender2");
395 request_->Start(); 414 request_->Start();
396 415
397 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 416 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
398 CompleteFetch(); 417 CompleteFetch();
399 418
400 EXPECT_TRUE(callback_called_); 419 EXPECT_TRUE(callback_called_);
401 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); 420 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_);
402 EXPECT_EQ(std::string(), registration_id_); 421 EXPECT_EQ(std::string(), registration_id_);
403 } 422 }
404 423
405 TEST_F(RegistrationRequestTest, MaximumAttemptsReached) { 424 TEST_F(GCMRegistrationRequestTest, MaximumAttemptsReached) {
406 CreateRequest("sender1,sender2"); 425 CreateRequest("sender1,sender2");
407 request_->Start(); 426 request_->Start();
408 427
409 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 428 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
410 CompleteFetch(); 429 CompleteFetch();
411 430
412 EXPECT_FALSE(callback_called_); 431 EXPECT_FALSE(callback_called_);
413 432
414 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 433 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
415 CompleteFetch(); 434 CompleteFetch();
416 435
417 EXPECT_FALSE(callback_called_); 436 EXPECT_FALSE(callback_called_);
418 437
419 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501"); 438 SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
420 CompleteFetch(); 439 CompleteFetch();
421 440
422 EXPECT_TRUE(callback_called_); 441 EXPECT_TRUE(callback_called_);
423 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_); 442 EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_);
424 EXPECT_EQ(std::string(), registration_id_); 443 EXPECT_EQ(std::string(), registration_id_);
425 } 444 }
426 445
446 class InstanceIDGetTokenRequestTest : public RegistrationRequestTest {
447 public:
448 InstanceIDGetTokenRequestTest();
449 ~InstanceIDGetTokenRequestTest() override;
450
451 void CreateRequest(const std::string& instance_id,
452 const std::string& authorized_entity,
453 const std::string& scope,
454 const std::map<std::string, std::string>& options);
455 };
456
457 InstanceIDGetTokenRequestTest::InstanceIDGetTokenRequestTest() {
458 }
459
460 InstanceIDGetTokenRequestTest::~InstanceIDGetTokenRequestTest() {
461 }
462
463 void InstanceIDGetTokenRequestTest::CreateRequest(
464 const std::string& instance_id,
465 const std::string& authorized_entity,
466 const std::string& scope,
467 const std::map<std::string, std::string>& options) {
468 RegistrationRequest::RequestInfo request_info(
469 kAndroidId, kSecurityToken, kAppId);
470 InstanceIDGetTokenRequest::ExtraRequestInfo extra_request_info(
471 instance_id, authorized_entity, scope, kGCMVersion, options);
472 request_.reset(new InstanceIDGetTokenRequest(
473 GURL(kRegistrationURL),
474 request_info,
475 extra_request_info,
476 kDefaultBackoffPolicy,
477 base::Bind(&RegistrationRequestTest::RegistrationCallback,
478 base::Unretained(this)),
479 max_retry_count_,
480 url_request_context_getter_.get(),
481 &recorder_));
482 }
483
484 TEST_F(InstanceIDGetTokenRequestTest, RequestSuccessful) {
485 std::map<std::string, std::string> options;
486 options["Foo"] = "Bar";
487
488 set_max_retry_count(0);
489 CreateRequest(kInstanceId, kDeveloperId, kScope, options);
490 request_->Start();
491
492 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
493 CompleteFetch();
494
495 EXPECT_TRUE(callback_called_);
496 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
497 EXPECT_EQ("2501", registration_id_);
498 }
499
500 TEST_F(InstanceIDGetTokenRequestTest, RequestDataAndURL) {
501 std::map<std::string, std::string> options;
502 options["Foo"] = "Bar";
503 CreateRequest(kInstanceId, kDeveloperId, kScope, options);
504 request_->Start();
505
506 // Get data sent by request.
507 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
508 ASSERT_TRUE(fetcher);
509
510 EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL());
511
512 // Verify that authorization header was put together properly.
513 net::HttpRequestHeaders headers;
514 fetcher->GetExtraRequestHeaders(&headers);
515 std::string auth_header;
516 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header);
517 base::StringTokenizer auth_tokenizer(auth_header, " :");
518 ASSERT_TRUE(auth_tokenizer.GetNext());
519 EXPECT_EQ(kLoginHeader, auth_tokenizer.token());
520 ASSERT_TRUE(auth_tokenizer.GetNext());
521 EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token());
522 ASSERT_TRUE(auth_tokenizer.GetNext());
523 EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token());
524
525 std::map<std::string, std::string> expected_pairs;
526 expected_pairs["gmsv"] = base::IntToString(kGCMVersion);
527 expected_pairs["app"] = kAppId;
528 expected_pairs["sender"] = kDeveloperId;
529 expected_pairs["device"] = base::Uint64ToString(kAndroidId);
530 expected_pairs["appid"] = kInstanceId;
531 expected_pairs["scope"] = kScope;
532 expected_pairs["X-Foo"] = "Bar";
533
534 // Verify data was formatted properly.
535 std::string upload_data = fetcher->upload_data();
536 base::StringTokenizer data_tokenizer(upload_data, "&=");
537 while (data_tokenizer.GetNext()) {
538 std::map<std::string, std::string>::iterator iter =
539 expected_pairs.find(data_tokenizer.token());
540 ASSERT_TRUE(iter != expected_pairs.end());
541 ASSERT_TRUE(data_tokenizer.GetNext());
542 EXPECT_EQ(iter->second, data_tokenizer.token());
543 // Ensure that none of the keys appears twice.
544 expected_pairs.erase(iter);
545 }
546
547 EXPECT_EQ(0UL, expected_pairs.size());
548 }
549
550 TEST_F(InstanceIDGetTokenRequestTest, ResponseHttpStatusNotOK) {
551 std::map<std::string, std::string> options;
552 CreateRequest(kInstanceId, kDeveloperId, kScope, options);
553 request_->Start();
554
555 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501");
556 CompleteFetch();
557
558 EXPECT_FALSE(callback_called_);
559
560 SetResponseStatusAndString(net::HTTP_OK, "token=2501");
561 CompleteFetch();
562
563 EXPECT_TRUE(callback_called_);
564 EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
565 EXPECT_EQ("2501", registration_id_);
566 }
567
427 } // namespace gcm 568 } // namespace gcm
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698