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
3 // found in the LICENSE file.
9 #include "base/strings/string_number_conversions.h"
10 #include "base/strings/string_tokenizer.h"
11 #include "google_apis/gcm/engine/unregistration_request.h"
12 #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/url_request_test_util.h"
15 #include "testing/gtest/include/gtest/gtest.h"
20 const uint64 kAndroidId
= 42UL;
21 const char kLoginHeader
[] = "AidLogin";
22 const char kAppId
[] = "TestAppId";
23 const char kDeletedAppId
[] = "deleted=TestAppId";
24 const char kRegistrationURL
[] = "http://foo.bar/register";
25 const uint64 kSecurityToken
= 77UL;
27 // Backoff policy for testing registration request.
28 const net::BackoffEntry::Policy kDefaultBackoffPolicy
= {
29 // Number of initial errors (in sequence) to ignore before applying
30 // exponential back-off rules.
31 // 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.
35 // Initial delay for exponential back-off in ms.
38 // Factor by which the waiting time will be multiplied.
41 // Fuzzing percentage. ex: 10% will spread requests randomly
42 // between 90%-100% of the calculated time.
45 // Maximum amount of time we are willing to delay our request in ms.
46 1000 * 60 * 5, // 5 minutes.
48 // Time to keep an entry from being discarded even when it
49 // has no significant state, -1 to never discard.
52 // Don't use initial delay unless the last request was an error.
57 class UnregistrationRequestTest
: public testing::Test
{
59 UnregistrationRequestTest();
60 virtual ~UnregistrationRequestTest();
62 void UnregistrationCallback(UnregistrationRequest::Status status
);
65 void SetResponseStatusAndString(net::HttpStatusCode status_code
,
66 const std::string
& response_body
);
70 bool callback_called_
;
71 UnregistrationRequest::Status status_
;
72 scoped_ptr
<UnregistrationRequest
> request_
;
73 base::MessageLoop message_loop_
;
74 net::TestURLFetcherFactory url_fetcher_factory_
;
75 scoped_refptr
<net::TestURLRequestContextGetter
> url_request_context_getter_
;
76 FakeGCMStatsRecorder recorder_
;
79 UnregistrationRequestTest::UnregistrationRequestTest()
80 : callback_called_(false),
81 status_(UnregistrationRequest::UNREGISTRATION_STATUS_COUNT
),
82 url_request_context_getter_(new net::TestURLRequestContextGetter(
83 message_loop_
.message_loop_proxy())) {}
85 UnregistrationRequestTest::~UnregistrationRequestTest() {}
87 void UnregistrationRequestTest::UnregistrationCallback(
88 UnregistrationRequest::Status status
) {
89 callback_called_
= true;
93 void UnregistrationRequestTest::CreateRequest() {
94 request_
.reset(new UnregistrationRequest(
95 GURL(kRegistrationURL
),
96 UnregistrationRequest::RequestInfo(kAndroidId
,
99 kDefaultBackoffPolicy
,
100 base::Bind(&UnregistrationRequestTest::UnregistrationCallback
,
101 base::Unretained(this)),
102 url_request_context_getter_
.get(),
106 void UnregistrationRequestTest::SetResponseStatusAndString(
107 net::HttpStatusCode status_code
,
108 const std::string
& response_body
) {
109 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
110 ASSERT_TRUE(fetcher
);
111 fetcher
->set_response_code(status_code
);
112 fetcher
->SetResponseString(response_body
);
115 void UnregistrationRequestTest::CompleteFetch() {
116 status_
= UnregistrationRequest::UNREGISTRATION_STATUS_COUNT
;
117 callback_called_
= false;
118 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
119 ASSERT_TRUE(fetcher
);
120 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
123 TEST_F(UnregistrationRequestTest
, RequestDataPassedToFetcher
) {
127 // Get data sent by request.
128 net::TestURLFetcher
* fetcher
= url_fetcher_factory_
.GetFetcherByID(0);
129 ASSERT_TRUE(fetcher
);
131 EXPECT_EQ(GURL(kRegistrationURL
), fetcher
->GetOriginalURL());
133 // Verify that authorization header was put together properly.
134 net::HttpRequestHeaders headers
;
135 fetcher
->GetExtraRequestHeaders(&headers
);
136 std::string auth_header
;
137 headers
.GetHeader(net::HttpRequestHeaders::kAuthorization
, &auth_header
);
138 base::StringTokenizer
auth_tokenizer(auth_header
, " :");
139 ASSERT_TRUE(auth_tokenizer
.GetNext());
140 EXPECT_EQ(kLoginHeader
, auth_tokenizer
.token());
141 ASSERT_TRUE(auth_tokenizer
.GetNext());
142 EXPECT_EQ(base::Uint64ToString(kAndroidId
), auth_tokenizer
.token());
143 ASSERT_TRUE(auth_tokenizer
.GetNext());
144 EXPECT_EQ(base::Uint64ToString(kSecurityToken
), auth_tokenizer
.token());
145 std::string app_id_header
;
146 headers
.GetHeader("app", &app_id_header
);
147 EXPECT_EQ(kAppId
, app_id_header
);
149 std::map
<std::string
, std::string
> expected_pairs
;
150 expected_pairs
["app"] = kAppId
;
151 expected_pairs
["device"] = base::Uint64ToString(kAndroidId
);
152 expected_pairs
["delete"] = "true";
153 expected_pairs
["gcm_unreg_caller"] = "false";
155 // Verify data was formatted properly.
156 std::string upload_data
= fetcher
->upload_data();
157 base::StringTokenizer
data_tokenizer(upload_data
, "&=");
158 while (data_tokenizer
.GetNext()) {
159 std::map
<std::string
, std::string
>::iterator iter
=
160 expected_pairs
.find(data_tokenizer
.token());
161 ASSERT_TRUE(iter
!= expected_pairs
.end()) << data_tokenizer
.token();
162 ASSERT_TRUE(data_tokenizer
.GetNext()) << data_tokenizer
.token();
163 EXPECT_EQ(iter
->second
, data_tokenizer
.token());
164 // Ensure that none of the keys appears twice.
165 expected_pairs
.erase(iter
);
168 EXPECT_EQ(0UL, expected_pairs
.size());
171 TEST_F(UnregistrationRequestTest
, SuccessfulUnregistration
) {
175 SetResponseStatusAndString(net::HTTP_OK
, kDeletedAppId
);
178 EXPECT_TRUE(callback_called_
);
179 EXPECT_EQ(UnregistrationRequest::SUCCESS
, status_
);
182 TEST_F(UnregistrationRequestTest
, ResponseHttpStatusNotOK
) {
186 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED
, "");
189 EXPECT_TRUE(callback_called_
);
190 EXPECT_EQ(UnregistrationRequest::HTTP_NOT_OK
, status_
);
193 TEST_F(UnregistrationRequestTest
, ResponseEmpty
) {
197 SetResponseStatusAndString(net::HTTP_OK
, "");
200 EXPECT_FALSE(callback_called_
);
202 SetResponseStatusAndString(net::HTTP_OK
, kDeletedAppId
);
205 EXPECT_TRUE(callback_called_
);
206 EXPECT_EQ(UnregistrationRequest::SUCCESS
, status_
);
209 TEST_F(UnregistrationRequestTest
, InvalidParametersError
) {
213 SetResponseStatusAndString(net::HTTP_OK
, "Error=INVALID_PARAMETERS");
216 EXPECT_TRUE(callback_called_
);
217 EXPECT_EQ(UnregistrationRequest::INVALID_PARAMETERS
, status_
);
220 TEST_F(UnregistrationRequestTest
, UnkwnownError
) {
224 SetResponseStatusAndString(net::HTTP_OK
, "Error=XXX");
227 EXPECT_TRUE(callback_called_
);
228 EXPECT_EQ(UnregistrationRequest::UNKNOWN_ERROR
, status_
);
231 TEST_F(UnregistrationRequestTest
, ServiceUnavailable
) {
235 SetResponseStatusAndString(net::HTTP_SERVICE_UNAVAILABLE
, "");
238 EXPECT_FALSE(callback_called_
);
240 SetResponseStatusAndString(net::HTTP_OK
, kDeletedAppId
);
243 EXPECT_TRUE(callback_called_
);
244 EXPECT_EQ(UnregistrationRequest::SUCCESS
, status_
);
247 TEST_F(UnregistrationRequestTest
, InternalServerError
) {
251 SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR
, "");
254 EXPECT_FALSE(callback_called_
);
256 SetResponseStatusAndString(net::HTTP_OK
, kDeletedAppId
);
259 EXPECT_TRUE(callback_called_
);
260 EXPECT_EQ(UnregistrationRequest::SUCCESS
, status_
);
263 TEST_F(UnregistrationRequestTest
, IncorrectAppId
) {
267 SetResponseStatusAndString(net::HTTP_OK
, "deleted=OtherTestAppId");
270 EXPECT_FALSE(callback_called_
);
272 SetResponseStatusAndString(net::HTTP_OK
, kDeletedAppId
);
275 EXPECT_TRUE(callback_called_
);
276 EXPECT_EQ(UnregistrationRequest::SUCCESS
, status_
);
279 TEST_F(UnregistrationRequestTest
, ResponseParsingFailed
) {
283 SetResponseStatusAndString(net::HTTP_OK
, "some malformed response");
286 EXPECT_FALSE(callback_called_
);
288 SetResponseStatusAndString(net::HTTP_OK
, kDeletedAppId
);
291 EXPECT_TRUE(callback_called_
);
292 EXPECT_EQ(UnregistrationRequest::SUCCESS
, status_
);