| Index: google_apis/gcm/engine/unregistration_request_unittest.cc
|
| diff --git a/google_apis/gcm/engine/registration_request_unittest.cc b/google_apis/gcm/engine/unregistration_request_unittest.cc
|
| similarity index 50%
|
| copy from google_apis/gcm/engine/registration_request_unittest.cc
|
| copy to google_apis/gcm/engine/unregistration_request_unittest.cc
|
| index 178c135583950f4225513b0d5b0cbe38ee1c9001..ebd2cadae7506c79af4cab83c0b3be65d43c0c82 100644
|
| --- a/google_apis/gcm/engine/registration_request_unittest.cc
|
| +++ b/google_apis/gcm/engine/unregistration_request_unittest.cc
|
| @@ -8,7 +8,7 @@
|
|
|
| #include "base/strings/string_number_conversions.h"
|
| #include "base/strings/string_tokenizer.h"
|
| -#include "google_apis/gcm/engine/registration_request.h"
|
| +#include "google_apis/gcm/engine/unregistration_request.h"
|
| #include "net/url_request/test_url_fetcher_factory.h"
|
| #include "net/url_request/url_request_test_util.h"
|
| #include "testing/gtest/include/gtest/gtest.h"
|
| @@ -17,10 +17,9 @@ namespace gcm {
|
|
|
| namespace {
|
| const uint64 kAndroidId = 42UL;
|
| -const char kCert[] = "0DEADBEEF420";
|
| -const char kDeveloperId[] = "Project1";
|
| const char kLoginHeader[] = "AidLogin";
|
| const char kAppId[] = "TestAppId";
|
| +const char kDeletedAppId[] = "deleted=TestAppId";
|
| const uint64 kSecurityToken = 77UL;
|
|
|
| // Backoff policy for testing registration request.
|
| @@ -29,7 +28,7 @@ const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
|
| // exponential back-off rules.
|
| // Explicitly set to 2 to skip the delay on the first retry, as we are not
|
| // trying to test the backoff itself, but rather the fact that retry happens.
|
| - 2,
|
| + 1,
|
|
|
| // Initial delay for exponential back-off in ms.
|
| 15000, // 15 seconds.
|
| @@ -51,68 +50,54 @@ const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
|
| // Don't use initial delay unless the last request was an error.
|
| false,
|
| };
|
| -
|
| } // namespace
|
|
|
| -class RegistrationRequestTest : public testing::Test {
|
| +class UnregistrationRequestTest : public testing::Test {
|
| public:
|
| - RegistrationRequestTest();
|
| - virtual ~RegistrationRequestTest();
|
| + UnregistrationRequestTest();
|
| + virtual ~UnregistrationRequestTest();
|
|
|
| - void RegistrationCallback(RegistrationRequest::Status status,
|
| - const std::string& registration_id);
|
| + void UnregistrationCallback(bool success);
|
|
|
| - void CreateRequest(const std::string& sender_ids);
|
| + void CreateRequest();
|
| void SetResponseStatusAndString(net::HttpStatusCode status_code,
|
| const std::string& response_body);
|
| void CompleteFetch();
|
|
|
| protected:
|
| - RegistrationRequest::Status status_;
|
| - std::string registration_id_;
|
| bool callback_called_;
|
| - std::map<std::string, std::string> extras_;
|
| - scoped_ptr<RegistrationRequest> request_;
|
| + bool unregistration_successful_;
|
| + scoped_ptr<UnregistrationRequest> request_;
|
| base::MessageLoop message_loop_;
|
| net::TestURLFetcherFactory url_fetcher_factory_;
|
| scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
|
| };
|
|
|
| -RegistrationRequestTest::RegistrationRequestTest()
|
| - : status_(RegistrationRequest::SUCCESS),
|
| - callback_called_(false),
|
| +UnregistrationRequestTest::UnregistrationRequestTest()
|
| + : callback_called_(false),
|
| + unregistration_successful_(false),
|
| url_request_context_getter_(new net::TestURLRequestContextGetter(
|
| message_loop_.message_loop_proxy())) {}
|
|
|
| -RegistrationRequestTest::~RegistrationRequestTest() {}
|
| +UnregistrationRequestTest::~UnregistrationRequestTest() {}
|
|
|
| -void RegistrationRequestTest::RegistrationCallback(
|
| - RegistrationRequest::Status status,
|
| - const std::string& registration_id) {
|
| - status_ = status;
|
| - registration_id_ = registration_id;
|
| +void UnregistrationRequestTest::UnregistrationCallback(bool success) {
|
| callback_called_ = true;
|
| + unregistration_successful_ = success;
|
| }
|
|
|
| -void RegistrationRequestTest::CreateRequest(const std::string& sender_ids) {
|
| - std::vector<std::string> senders;
|
| - base::StringTokenizer tokenizer(sender_ids, ",");
|
| - while (tokenizer.GetNext())
|
| - senders.push_back(tokenizer.token());
|
| -
|
| - request_.reset(new RegistrationRequest(
|
| - RegistrationRequest::RequestInfo(kAndroidId,
|
| - kSecurityToken,
|
| - kAppId,
|
| - kCert,
|
| - senders),
|
| +void UnregistrationRequestTest::CreateRequest() {
|
| + request_.reset(new UnregistrationRequest(
|
| + UnregistrationRequest::RequestInfo(kAndroidId,
|
| + kSecurityToken,
|
| + kAppId),
|
| kDefaultBackoffPolicy,
|
| - base::Bind(&RegistrationRequestTest::RegistrationCallback,
|
| + base::Bind(&UnregistrationRequestTest::UnregistrationCallback,
|
| base::Unretained(this)),
|
| url_request_context_getter_.get()));
|
| }
|
|
|
| -void RegistrationRequestTest::SetResponseStatusAndString(
|
| +void UnregistrationRequestTest::SetResponseStatusAndString(
|
| net::HttpStatusCode status_code,
|
| const std::string& response_body) {
|
| net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
|
| @@ -121,18 +106,16 @@ void RegistrationRequestTest::SetResponseStatusAndString(
|
| fetcher->SetResponseString(response_body);
|
| }
|
|
|
| -void RegistrationRequestTest::CompleteFetch() {
|
| - registration_id_.clear();
|
| - status_ = RegistrationRequest::SUCCESS;
|
| +void UnregistrationRequestTest::CompleteFetch() {
|
| + unregistration_successful_ = false;
|
| callback_called_ = false;
|
| -
|
| net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
|
| ASSERT_TRUE(fetcher);
|
| fetcher->delegate()->OnURLFetchComplete(fetcher);
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, RequestDataPassedToFetcher) {
|
| - CreateRequest(kDeveloperId);
|
| +TEST_F(UnregistrationRequestTest, RequestDataPassedToFetcher) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| // Get data sent by request.
|
| @@ -151,12 +134,15 @@ TEST_F(RegistrationRequestTest, RequestDataPassedToFetcher) {
|
| EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token());
|
| ASSERT_TRUE(auth_tokenizer.GetNext());
|
| EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token());
|
| + std::string app_id_header;
|
| + headers.GetHeader("app", &app_id_header);
|
| + EXPECT_EQ(kAppId, app_id_header);
|
|
|
| std::map<std::string, std::string> expected_pairs;
|
| expected_pairs["app"] = kAppId;
|
| - expected_pairs["sender"] = kDeveloperId;
|
| - expected_pairs["cert"] = kCert;
|
| expected_pairs["device"] = base::Uint64ToString(kAndroidId);
|
| + expected_pairs["delete"] = "true";
|
| + expected_pairs["gcm_unreg_caller"] = "false";
|
|
|
| // Verify data was formatted properly.
|
| std::string upload_data = fetcher->upload_data();
|
| @@ -164,8 +150,8 @@ TEST_F(RegistrationRequestTest, RequestDataPassedToFetcher) {
|
| while (data_tokenizer.GetNext()) {
|
| std::map<std::string, std::string>::iterator iter =
|
| expected_pairs.find(data_tokenizer.token());
|
| - ASSERT_TRUE(iter != expected_pairs.end());
|
| - ASSERT_TRUE(data_tokenizer.GetNext());
|
| + ASSERT_TRUE(iter != expected_pairs.end()) << data_tokenizer.token();
|
| + ASSERT_TRUE(data_tokenizer.GetNext()) << data_tokenizer.token();
|
| EXPECT_EQ(iter->second, data_tokenizer.token());
|
| // Ensure that none of the keys appears twice.
|
| expected_pairs.erase(iter);
|
| @@ -174,141 +160,129 @@ TEST_F(RegistrationRequestTest, RequestDataPassedToFetcher) {
|
| EXPECT_EQ(0UL, expected_pairs.size());
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) {
|
| - CreateRequest("sender1,sender2@gmail.com");
|
| +TEST_F(UnregistrationRequestTest, SuccessfulUnregistration) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| - net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
|
| - ASSERT_TRUE(fetcher);
|
| -
|
| - // Verify data was formatted properly.
|
| - std::string upload_data = fetcher->upload_data();
|
| - base::StringTokenizer data_tokenizer(upload_data, "&=");
|
| + SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
|
| + CompleteFetch();
|
|
|
| - // Skip all tokens until you hit entry for senders.
|
| - while (data_tokenizer.GetNext() && data_tokenizer.token() != "sender")
|
| - continue;
|
| -
|
| - ASSERT_TRUE(data_tokenizer.GetNext());
|
| - std::string senders(net::UnescapeURLComponent(data_tokenizer.token(),
|
| - net::UnescapeRule::URL_SPECIAL_CHARS));
|
| - base::StringTokenizer sender_tokenizer(senders, ",");
|
| - ASSERT_TRUE(sender_tokenizer.GetNext());
|
| - EXPECT_EQ("sender1", sender_tokenizer.token());
|
| - ASSERT_TRUE(sender_tokenizer.GetNext());
|
| - EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token());
|
| + EXPECT_TRUE(callback_called_);
|
| + EXPECT_TRUE(unregistration_successful_);
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, ResponseParsing) {
|
| - CreateRequest("sender1,sender2");
|
| +TEST_F(UnregistrationRequestTest, ResponseHttpStatusNotOK) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| - SetResponseStatusAndString(net::HTTP_OK, "token=2501");
|
| + SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "");
|
| CompleteFetch();
|
|
|
| EXPECT_TRUE(callback_called_);
|
| - EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
|
| - EXPECT_EQ("2501", registration_id_);
|
| + EXPECT_FALSE(unregistration_successful_);
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) {
|
| - CreateRequest("sender1,sender2");
|
| +TEST_F(UnregistrationRequestTest, ResponseEmpty) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| - SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501");
|
| + SetResponseStatusAndString(net::HTTP_OK, "");
|
| CompleteFetch();
|
|
|
| EXPECT_FALSE(callback_called_);
|
| + EXPECT_FALSE(unregistration_successful_);
|
|
|
| - SetResponseStatusAndString(net::HTTP_OK, "token=2501");
|
| + SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
|
| CompleteFetch();
|
|
|
| EXPECT_TRUE(callback_called_);
|
| - EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
|
| - EXPECT_EQ("2501", registration_id_);
|
| + EXPECT_TRUE(unregistration_successful_);
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) {
|
| - CreateRequest("sender1,sender2");
|
| +TEST_F(UnregistrationRequestTest, InvalidParametersError) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| - SetResponseStatusAndString(net::HTTP_OK, "");
|
| + SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
|
| CompleteFetch();
|
|
|
| - EXPECT_FALSE(callback_called_);
|
| -
|
| - SetResponseStatusAndString(net::HTTP_OK, "some error in response");
|
| - CompleteFetch();
|
| + EXPECT_TRUE(callback_called_);
|
| + EXPECT_FALSE(unregistration_successful_);
|
| +}
|
|
|
| - EXPECT_FALSE(callback_called_);
|
| +TEST_F(UnregistrationRequestTest, UnkwnownError) {
|
| + CreateRequest();
|
| + request_->Start();
|
|
|
| - // Ensuring a retry happened and succeeds.
|
| - SetResponseStatusAndString(net::HTTP_OK, "token=2501");
|
| + SetResponseStatusAndString(net::HTTP_OK, "Error=XXX");
|
| CompleteFetch();
|
|
|
| EXPECT_TRUE(callback_called_);
|
| - EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
|
| - EXPECT_EQ("2501", registration_id_);
|
| + EXPECT_FALSE(unregistration_successful_);
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) {
|
| - CreateRequest("sender1,sender2");
|
| +TEST_F(UnregistrationRequestTest, ServiceUnavailable) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| - SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR");
|
| + SetResponseStatusAndString(net::HTTP_SERVICE_UNAVAILABLE, "");
|
| CompleteFetch();
|
|
|
| EXPECT_FALSE(callback_called_);
|
|
|
| - // Ensuring a retry happened and succeeds.
|
| - SetResponseStatusAndString(net::HTTP_OK, "token=2501");
|
| + SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
|
| CompleteFetch();
|
|
|
| EXPECT_TRUE(callback_called_);
|
| - EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
|
| - EXPECT_EQ("2501", registration_id_);
|
| + EXPECT_TRUE(unregistration_successful_);
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, ResponseAuthenticationError) {
|
| - CreateRequest("sender1,sender2");
|
| +TEST_F(UnregistrationRequestTest, InternalServerError) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| - SetResponseStatusAndString(net::HTTP_OK, "Error=AUTHENTICATION_FAILED");
|
| + SetResponseStatusAndString(net::HTTP_INTERNAL_SERVER_ERROR, "");
|
| CompleteFetch();
|
|
|
| EXPECT_FALSE(callback_called_);
|
|
|
| - // Ensuring a retry happened and succeeds.
|
| - SetResponseStatusAndString(net::HTTP_OK, "token=2501");
|
| + SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
|
| CompleteFetch();
|
|
|
| EXPECT_TRUE(callback_called_);
|
| - EXPECT_EQ(RegistrationRequest::SUCCESS, status_);
|
| - EXPECT_EQ("2501", registration_id_);
|
| + EXPECT_TRUE(unregistration_successful_);
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, ResponseInvalidParameters) {
|
| - CreateRequest("sender1,sender2");
|
| +TEST_F(UnregistrationRequestTest, IncorrectAppId) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| - SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
|
| + SetResponseStatusAndString(net::HTTP_OK, "deleted=OtherTestAppId");
|
| + CompleteFetch();
|
| +
|
| + EXPECT_FALSE(callback_called_);
|
| +
|
| + SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
|
| CompleteFetch();
|
|
|
| EXPECT_TRUE(callback_called_);
|
| - EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_);
|
| - EXPECT_EQ(std::string(), registration_id_);
|
| + EXPECT_TRUE(unregistration_successful_);
|
| }
|
|
|
| -TEST_F(RegistrationRequestTest, ResponseInvalidSender) {
|
| - CreateRequest("sender1,sender2");
|
| +TEST_F(UnregistrationRequestTest, ResponseParsingFailed) {
|
| + CreateRequest();
|
| request_->Start();
|
|
|
| - SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER");
|
| + SetResponseStatusAndString(net::HTTP_OK, "some malformed response");
|
| + CompleteFetch();
|
| +
|
| + EXPECT_FALSE(callback_called_);
|
| +
|
| + SetResponseStatusAndString(net::HTTP_OK, kDeletedAppId);
|
| CompleteFetch();
|
|
|
| EXPECT_TRUE(callback_called_);
|
| - EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
|
| - EXPECT_EQ(std::string(), registration_id_);
|
| + EXPECT_TRUE(unregistration_successful_);
|
| }
|
|
|
| } // namespace gcm
|
|
|