Index: google_apis/gcm/engine/registration_request_unittest.cc |
diff --git a/google_apis/gcm/engine/registration_request_unittest.cc b/google_apis/gcm/engine/registration_request_unittest.cc |
index b808f39e68d33a6f5d980dd7c29fa2e9f67e1552..d85eeeaa21679833783d7a0c79770f2aecc3060f 100644 |
--- a/google_apis/gcm/engine/registration_request_unittest.cc |
+++ b/google_apis/gcm/engine/registration_request_unittest.cc |
@@ -8,7 +8,8 @@ |
#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/gcm_registration_request_handler.h" |
+#include "google_apis/gcm/engine/instance_id_get_token_request_handler.h" |
#include "google_apis/gcm/monitoring/fake_gcm_stats_recorder.h" |
#include "net/url_request/test_url_fetcher_factory.h" |
#include "net/url_request/url_request_status.h" |
@@ -24,6 +25,9 @@ const char kDeveloperId[] = "Project1"; |
const char kLoginHeader[] = "AidLogin"; |
const char kRegistrationURL[] = "http://foo.bar/register"; |
const uint64 kSecurityToken = 77UL; |
+const int kGCMVersion = 40; |
+const char kInstanceId[] = "IID1"; |
+const char kScope[] = "GCM"; |
// Backoff policy for testing registration request. |
const net::BackoffEntry::Policy kDefaultBackoffPolicy = { |
@@ -64,7 +68,6 @@ class RegistrationRequestTest : public testing::Test { |
void RegistrationCallback(RegistrationRequest::Status status, |
const std::string& registration_id); |
- void CreateRequest(const std::string& sender_ids); |
void SetResponseStatusAndString(net::HttpStatusCode status_code, |
const std::string& response_body); |
void CompleteFetch(); |
@@ -102,26 +105,6 @@ void RegistrationRequestTest::RegistrationCallback( |
callback_called_ = true; |
} |
-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( |
- GURL(kRegistrationURL), |
- RegistrationRequest::RequestInfo(kAndroidId, |
- kSecurityToken, |
- kAppId, |
- senders), |
- kDefaultBackoffPolicy, |
- base::Bind(&RegistrationRequestTest::RegistrationCallback, |
- base::Unretained(this)), |
- max_retry_count_, |
- url_request_context_getter_.get(), |
- &recorder_)); |
-} |
- |
void RegistrationRequestTest::SetResponseStatusAndString( |
net::HttpStatusCode status_code, |
const std::string& response_body) { |
@@ -141,7 +124,39 @@ void RegistrationRequestTest::CompleteFetch() { |
fetcher->delegate()->OnURLFetchComplete(fetcher); |
} |
-TEST_F(RegistrationRequestTest, RequestSuccessful) { |
+class GCMRegistrationRequestTest : public RegistrationRequestTest { |
+ public: |
+ GCMRegistrationRequestTest(); |
+ ~GCMRegistrationRequestTest() override; |
+ |
+ void CreateRequest(const std::string& sender_ids); |
+}; |
+ |
+GCMRegistrationRequestTest::GCMRegistrationRequestTest() { |
+} |
+ |
+GCMRegistrationRequestTest::~GCMRegistrationRequestTest() { |
+} |
+ |
+void GCMRegistrationRequestTest::CreateRequest(const std::string& sender_ids) { |
+ RegistrationRequest::RequestInfo request_info( |
+ kAndroidId, kSecurityToken, kAppId); |
+ scoped_ptr<GCMRegistrationRequestHandler> request_handler( |
+ new GCMRegistrationRequestHandler(sender_ids)); |
+ request_.reset(new RegistrationRequest( |
+ GURL(kRegistrationURL), |
+ request_info, |
+ request_handler.Pass(), |
+ kDefaultBackoffPolicy, |
+ base::Bind(&RegistrationRequestTest::RegistrationCallback, |
+ base::Unretained(this)), |
+ max_retry_count_, |
+ url_request_context_getter_.get(), |
+ &recorder_, |
+ sender_ids)); |
+} |
+ |
+TEST_F(GCMRegistrationRequestTest, RequestSuccessful) { |
set_max_retry_count(0); |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -154,7 +169,7 @@ TEST_F(RegistrationRequestTest, RequestSuccessful) { |
EXPECT_EQ("2501", registration_id_); |
} |
-TEST_F(RegistrationRequestTest, RequestDataAndURL) { |
+TEST_F(GCMRegistrationRequestTest, RequestDataAndURL) { |
CreateRequest(kDeveloperId); |
request_->Start(); |
@@ -198,7 +213,7 @@ TEST_F(RegistrationRequestTest, RequestDataAndURL) { |
EXPECT_EQ(0UL, expected_pairs.size()); |
} |
-TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) { |
+TEST_F(GCMRegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) { |
CreateRequest("sender1,sender2@gmail.com"); |
request_->Start(); |
@@ -223,7 +238,7 @@ TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) { |
EXPECT_EQ("sender2@gmail.com", sender_tokenizer.token()); |
} |
-TEST_F(RegistrationRequestTest, ResponseParsing) { |
+TEST_F(GCMRegistrationRequestTest, ResponseParsing) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -235,7 +250,7 @@ TEST_F(RegistrationRequestTest, ResponseParsing) { |
EXPECT_EQ("2501", registration_id_); |
} |
-TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) { |
+TEST_F(GCMRegistrationRequestTest, ResponseHttpStatusNotOK) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -252,7 +267,7 @@ TEST_F(RegistrationRequestTest, ResponseHttpStatusNotOK) { |
EXPECT_EQ("2501", registration_id_); |
} |
-TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) { |
+TEST_F(GCMRegistrationRequestTest, ResponseMissingRegistrationId) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -275,7 +290,7 @@ TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) { |
EXPECT_EQ("2501", registration_id_); |
} |
-TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) { |
+TEST_F(GCMRegistrationRequestTest, ResponseDeviceRegistrationError) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -293,7 +308,7 @@ TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) { |
EXPECT_EQ("2501", registration_id_); |
} |
-TEST_F(RegistrationRequestTest, ResponseAuthenticationError) { |
+TEST_F(GCMRegistrationRequestTest, ResponseAuthenticationError) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -312,7 +327,7 @@ TEST_F(RegistrationRequestTest, ResponseAuthenticationError) { |
EXPECT_EQ("2501", registration_id_); |
} |
-TEST_F(RegistrationRequestTest, ResponseInvalidParameters) { |
+TEST_F(GCMRegistrationRequestTest, ResponseInvalidParameters) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -324,7 +339,7 @@ TEST_F(RegistrationRequestTest, ResponseInvalidParameters) { |
EXPECT_EQ(std::string(), registration_id_); |
} |
-TEST_F(RegistrationRequestTest, ResponseInvalidSender) { |
+TEST_F(GCMRegistrationRequestTest, ResponseInvalidSender) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -336,7 +351,7 @@ TEST_F(RegistrationRequestTest, ResponseInvalidSender) { |
EXPECT_EQ(std::string(), registration_id_); |
} |
-TEST_F(RegistrationRequestTest, ResponseInvalidSenderBadRequest) { |
+TEST_F(GCMRegistrationRequestTest, ResponseInvalidSenderBadRequest) { |
CreateRequest("sender1"); |
request_->Start(); |
@@ -348,7 +363,7 @@ TEST_F(RegistrationRequestTest, ResponseInvalidSenderBadRequest) { |
EXPECT_EQ(std::string(), registration_id_); |
} |
-TEST_F(RegistrationRequestTest, RequestNotSuccessful) { |
+TEST_F(GCMRegistrationRequestTest, RequestNotSuccessful) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -371,7 +386,7 @@ TEST_F(RegistrationRequestTest, RequestNotSuccessful) { |
EXPECT_EQ("2501", registration_id_); |
} |
-TEST_F(RegistrationRequestTest, ResponseHttpNotOk) { |
+TEST_F(GCMRegistrationRequestTest, ResponseHttpNotOk) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -389,7 +404,7 @@ TEST_F(RegistrationRequestTest, ResponseHttpNotOk) { |
EXPECT_EQ("2501", registration_id_); |
} |
-TEST_F(RegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) { |
+TEST_F(GCMRegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) { |
set_max_retry_count(0); |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -402,7 +417,7 @@ TEST_F(RegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) { |
EXPECT_EQ(std::string(), registration_id_); |
} |
-TEST_F(RegistrationRequestTest, MaximumAttemptsReached) { |
+TEST_F(GCMRegistrationRequestTest, MaximumAttemptsReached) { |
CreateRequest("sender1,sender2"); |
request_->Start(); |
@@ -424,4 +439,128 @@ TEST_F(RegistrationRequestTest, MaximumAttemptsReached) { |
EXPECT_EQ(std::string(), registration_id_); |
} |
+class InstanceIDGetTokenRequestTest : public RegistrationRequestTest { |
+ public: |
+ InstanceIDGetTokenRequestTest(); |
+ ~InstanceIDGetTokenRequestTest() override; |
+ |
+ void CreateRequest(const std::string& instance_id, |
+ const std::string& authorized_entity, |
+ const std::string& scope, |
+ const std::map<std::string, std::string>& options); |
+}; |
+ |
+InstanceIDGetTokenRequestTest::InstanceIDGetTokenRequestTest() { |
+} |
+ |
+InstanceIDGetTokenRequestTest::~InstanceIDGetTokenRequestTest() { |
+} |
+ |
+void InstanceIDGetTokenRequestTest::CreateRequest( |
+ const std::string& instance_id, |
+ const std::string& authorized_entity, |
+ const std::string& scope, |
+ const std::map<std::string, std::string>& options) { |
+ RegistrationRequest::RequestInfo request_info( |
+ kAndroidId, kSecurityToken, kAppId); |
+ scoped_ptr<InstanceIDGetTokenRequestHandler> request_handler( |
+ new InstanceIDGetTokenRequestHandler( |
+ instance_id, authorized_entity, scope, kGCMVersion, options)); |
+ request_.reset(new RegistrationRequest( |
+ GURL(kRegistrationURL), |
+ request_info, |
+ request_handler.Pass(), |
+ kDefaultBackoffPolicy, |
+ base::Bind(&RegistrationRequestTest::RegistrationCallback, |
+ base::Unretained(this)), |
+ max_retry_count_, |
+ url_request_context_getter_.get(), |
+ &recorder_, |
+ authorized_entity)); |
+} |
+ |
+TEST_F(InstanceIDGetTokenRequestTest, RequestSuccessful) { |
+ std::map<std::string, std::string> options; |
+ options["Foo"] = "Bar"; |
+ |
+ set_max_retry_count(0); |
+ CreateRequest(kInstanceId, kDeveloperId, kScope, options); |
+ request_->Start(); |
+ |
+ SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
+ CompleteFetch(); |
+ |
+ EXPECT_TRUE(callback_called_); |
+ EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
+ EXPECT_EQ("2501", registration_id_); |
+} |
+ |
+TEST_F(InstanceIDGetTokenRequestTest, RequestDataAndURL) { |
+ std::map<std::string, std::string> options; |
+ options["Foo"] = "Bar"; |
+ CreateRequest(kInstanceId, kDeveloperId, kScope, options); |
+ request_->Start(); |
+ |
+ // Get data sent by request. |
+ net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0); |
+ ASSERT_TRUE(fetcher); |
+ |
+ EXPECT_EQ(GURL(kRegistrationURL), fetcher->GetOriginalURL()); |
+ |
+ // Verify that authorization header was put together properly. |
+ net::HttpRequestHeaders headers; |
+ fetcher->GetExtraRequestHeaders(&headers); |
+ std::string auth_header; |
+ headers.GetHeader(net::HttpRequestHeaders::kAuthorization, &auth_header); |
+ base::StringTokenizer auth_tokenizer(auth_header, " :"); |
+ ASSERT_TRUE(auth_tokenizer.GetNext()); |
+ EXPECT_EQ(kLoginHeader, auth_tokenizer.token()); |
+ ASSERT_TRUE(auth_tokenizer.GetNext()); |
+ EXPECT_EQ(base::Uint64ToString(kAndroidId), auth_tokenizer.token()); |
+ ASSERT_TRUE(auth_tokenizer.GetNext()); |
+ EXPECT_EQ(base::Uint64ToString(kSecurityToken), auth_tokenizer.token()); |
+ |
+ std::map<std::string, std::string> expected_pairs; |
+ expected_pairs["gmsv"] = base::IntToString(kGCMVersion); |
+ expected_pairs["app"] = kAppId; |
+ expected_pairs["sender"] = kDeveloperId; |
+ expected_pairs["device"] = base::Uint64ToString(kAndroidId); |
+ expected_pairs["appid"] = kInstanceId; |
+ expected_pairs["scope"] = kScope; |
+ expected_pairs["X-Foo"] = "Bar"; |
+ |
+ // Verify data was formatted properly. |
+ std::string upload_data = fetcher->upload_data(); |
+ base::StringTokenizer data_tokenizer(upload_data, "&="); |
+ 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()); |
+ EXPECT_EQ(iter->second, data_tokenizer.token()); |
+ // Ensure that none of the keys appears twice. |
+ expected_pairs.erase(iter); |
+ } |
+ |
+ EXPECT_EQ(0UL, expected_pairs.size()); |
+} |
+ |
+TEST_F(InstanceIDGetTokenRequestTest, ResponseHttpStatusNotOK) { |
+ std::map<std::string, std::string> options; |
+ CreateRequest(kInstanceId, kDeveloperId, kScope, options); |
+ request_->Start(); |
+ |
+ SetResponseStatusAndString(net::HTTP_UNAUTHORIZED, "token=2501"); |
+ CompleteFetch(); |
+ |
+ EXPECT_FALSE(callback_called_); |
+ |
+ SetResponseStatusAndString(net::HTTP_OK, "token=2501"); |
+ CompleteFetch(); |
+ |
+ EXPECT_TRUE(callback_called_); |
+ EXPECT_EQ(RegistrationRequest::SUCCESS, status_); |
+ EXPECT_EQ("2501", registration_id_); |
+} |
+ |
} // namespace gcm |