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 "net/url_request/test_url_fetcher_factory.h"
13 #include "net/url_request/url_request_test_util.h"
14 #include "testing/gtest/include/gtest/gtest.h"
19 const uint64 kAndroidId = 42UL;
20 const char kLoginHeader[] = "AidLogin";
21 const char kAppId[] = "TestAppId";
22 const char kDeletedAppId[] = "deleted=TestAppId";
23 const uint64 kSecurityToken = 77UL;
25 // Backoff policy for testing registration request.
26 const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
27 // Number of initial errors (in sequence) to ignore before applying
28 // exponential back-off rules.
29 // Explicitly set to 2 to skip the delay on the first retry, as we are not
30 // trying to test the backoff itself, but rather the fact that retry happens.
33 // Initial delay for exponential back-off in ms.
36 // Factor by which the waiting time will be multiplied.
39 // Fuzzing percentage. ex: 10% will spread requests randomly
40 // between 90%-100% of the calculated time.
43 // Maximum amount of time we are willing to delay our request in ms.
44 1000 * 60 * 5, // 5 minutes.
46 // Time to keep an entry from being discarded even when it
47 // has no significant state, -1 to never discard.
50 // Don't use initial delay unless the last request was an error.
55 class UnregistrationRequestTest : public testing::Test {
57 UnregistrationRequestTest();
58 virtual ~UnregistrationRequestTest();
60 void UnregistrationCallback(bool success);
63 void SetResponseStatusAndString(net::HttpStatusCode status_code,
64 const std::string& response_body);
68 bool callback_called_;
69 bool unregistration_successful_;
70 scoped_ptr<UnregistrationRequest> request_;
71 base::MessageLoop message_loop_;
72 net::TestURLFetcherFactory url_fetcher_factory_;
73 scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
76 UnregistrationRequestTest::UnregistrationRequestTest()
77 : callback_called_(false),
78 unregistration_successful_(false),
79 url_request_context_getter_(new net::TestURLRequestContextGetter(
80 message_loop_.message_loop_proxy())) {}
82 UnregistrationRequestTest::~UnregistrationRequestTest() {}
84 void UnregistrationRequestTest::UnregistrationCallback(bool success) {
85 callback_called_ = true;
86 unregistration_successful_ = success;
89 void UnregistrationRequestTest::CreateRequest() {
90 request_.reset(new UnregistrationRequest(
91 UnregistrationRequest::RequestInfo(kAndroidId,
94 kDefaultBackoffPolicy,
95 base::Bind(&UnregistrationRequestTest::UnregistrationCallback,
96 base::Unretained(this)),
97 url_request_context_getter_.get()));
100 void UnregistrationRequestTest::SetResponseStatusAndString(
101 net::HttpStatusCode status_code,
102 const std::string& response_body) {
103 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
104 ASSERT_TRUE(fetcher);
105 fetcher->set_response_code(status_code);
106 fetcher->SetResponseString(response_body);
109 void UnregistrationRequestTest::CompleteFetch() {
110 unregistration_successful_ = false;
111 callback_called_ = false;
112 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
113 ASSERT_TRUE(fetcher);
114 fetcher->delegate()->OnURLFetchComplete(fetcher);
117 TEST_F(UnregistrationRequestTest, RequestDataPassedToFetcher) {
121 // Get data sent by request.
122 net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
123 ASSERT_TRUE(fetcher);
125 // Verify that authorization header was put together properly.
126 net::HttpRequestHeaders headers;
127 fetcher->GetExtraRequestHeaders(&headers);
128 std::string auth_header;
129 headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header);
130 base::StringTokenizer auth_tokenizer(auth_header, " :");
131 ASSERT_TRUE(auth_tokenizer.GetNext());
132 EXPECT_EQ(kLoginHeader, auth_tokenizer.token());
133 ASSERT_TRUE(auth_tokenizer.GetNext());
134 EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token());
135 ASSERT_TRUE(auth_tokenizer.GetNext());
136 EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token());
137 std::string app_id_header;
138 headers.GetHeader("app", &app_id_header);
139 EXPECT_EQ(kAppId, app_id_header);
141 std::map<std::string, std::string> expected_pairs;
142 expected_pairs["app"] = kAppId;
143 expected_pairs["device"] = base::Uint64ToString(kAndroidId);
144 expected_pairs["delete"] = "true";
145 expected_pairs["gcm_unreg_caller"] = "false";
147 // Verify data was formatted properly.
148 std::string upload_data = fetcher->upload_data();
149 base::StringTokenizer data_tokenizer(upload_data, "&=");
150 while (data_tokenizer.GetNext()) {
151 std::map<std::string, std::string>::iterator iter =
152 expected_pairs.find(data_tokenizer.token());
153 ASSERT_TRUE(iter != expected_pairs.end()) << data_tokenizer.token();
154 ASSERT_TRUE(data_tokenizer.GetNext()) << data_tokenizer.token();
155 EXPECT_EQ(iter->second, data_tokenizer.token());
156 // Ensure that none of the keys appears twice.
157 expected_pairs.erase(iter);
160 EXPECT_EQ(0UL, expected_pairs.size());
163 TEST_F(UnregistrationRequestTest, SuccessfulUnregistration) {
167 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
170 EXPECT_TRUE(callback_called_);
171 EXPECT_TRUE(unregistration_successful_);
174 TEST_F(UnregistrationRequestTest, ResponseHttpStatusNotOK) {
178 SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "");
181 EXPECT_TRUE(callback_called_);
182 EXPECT_FALSE(unregistration_successful_);
185 TEST_F(UnregistrationRequestTest, ResponseEmpty) {
189 SetResponseStatusAndString(net::HTTP_OK, "");
192 EXPECT_FALSE(callback_called_);
193 EXPECT_FALSE(unregistration_successful_);
195 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
198 EXPECT_TRUE(callback_called_);
199 EXPECT_TRUE(unregistration_successful_);
202 TEST_F(UnregistrationRequestTest, InvalidParametersError) {
206 SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
209 EXPECT_TRUE(callback_called_);
210 EXPECT_FALSE(unregistration_successful_);
213 TEST_F(UnregistrationRequestTest, UnkwnownError) {
217 SetResponseStatusAndString(net::HTTP_OK, "Error=XXX");
220 EXPECT_TRUE(callback_called_);
221 EXPECT_FALSE(unregistration_successful_);
224 TEST_F(UnregistrationRequestTest, ServiceUnavailable) {
228 SetResponseStatusAndString(net::HTTP_SERVICE_UNAVAILABLE, "");
231 EXPECT_FALSE(callback_called_);
233 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
236 EXPECT_TRUE(callback_called_);
237 EXPECT_TRUE(unregistration_successful_);
240 TEST_F(UnregistrationRequestTest, InternalServerError) {
244 SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, "");
247 EXPECT_FALSE(callback_called_);
249 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
252 EXPECT_TRUE(callback_called_);
253 EXPECT_TRUE(unregistration_successful_);
256 TEST_F(UnregistrationRequestTest, IncorrectAppId) {
260 SetResponseStatusAndString(net::HTTP_OK, "deleted=OtherTestAppId");
263 EXPECT_FALSE(callback_called_);
265 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
268 EXPECT_TRUE(callback_called_);
269 EXPECT_TRUE(unregistration_successful_);
272 TEST_F(UnregistrationRequestTest, ResponseParsingFailed) {
276 SetResponseStatusAndString(net::HTTP_OK, "some malformed response");
279 EXPECT_FALSE(callback_called_);
281 SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
284 EXPECT_TRUE(callback_called_);
285 EXPECT_TRUE(unregistration_successful_);