This source file includes following definitions.
- set_max_retry_count
- url_request_context_getter_
- RegistrationCallback
- CreateRequest
- SetResponseStatusAndString
- CompleteFetch
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
- TEST_F
#include <map>
#include <string>
#include <vector>
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_tokenizer.h"
#include "google_apis/gcm/engine/registration_request.h"
#include "net/url_request/test_url_fetcher_factory.h"
#include "net/url_request/url_request_status.h"
#include "net/url_request/url_request_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace gcm {
namespace {
const uint64 kAndroidId = 42UL;
const char kAppId[] = "TestAppId";
const char kDeveloperId[] = "Project1";
const char kLoginHeader[] = "AidLogin";
const uint64 kSecurityToken = 77UL;
const net::BackoffEntry::Policy kDefaultBackoffPolicy = {
2,
15000,
2,
0.5,
1000 * 60 * 5,
-1,
false,
};
}
class RegistrationRequestTest : public testing::Test {
public:
RegistrationRequestTest();
virtual ~RegistrationRequestTest();
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();
void set_max_retry_count(int max_retry_count) {
max_retry_count_ = max_retry_count;
}
protected:
int max_retry_count_;
RegistrationRequest::Status status_;
std::string registration_id_;
bool callback_called_;
std::map<std::string, std::string> extras_;
scoped_ptr<RegistrationRequest> request_;
base::MessageLoop message_loop_;
net::TestURLFetcherFactory url_fetcher_factory_;
scoped_refptr<net::TestURLRequestContextGetter> url_request_context_getter_;
};
RegistrationRequestTest::RegistrationRequestTest()
: max_retry_count_(2),
status_(RegistrationRequest::SUCCESS),
callback_called_(false),
url_request_context_getter_(new net::TestURLRequestContextGetter(
message_loop_.message_loop_proxy())) {}
RegistrationRequestTest::~RegistrationRequestTest() {}
void RegistrationRequestTest::RegistrationCallback(
RegistrationRequest::Status status,
const std::string& registration_id) {
status_ = status;
registration_id_ = registration_id;
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(
RegistrationRequest::RequestInfo(kAndroidId,
kSecurityToken,
kAppId,
senders),
kDefaultBackoffPolicy,
base::Bind(&RegistrationRequestTest::RegistrationCallback,
base::Unretained(this)),
max_retry_count_,
url_request_context_getter_.get()));
}
void RegistrationRequestTest::SetResponseStatusAndString(
net::HttpStatusCode status_code,
const std::string& response_body) {
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->set_response_code(status_code);
fetcher->SetResponseString(response_body);
}
void RegistrationRequestTest::CompleteFetch() {
registration_id_.clear();
status_ = RegistrationRequest::SUCCESS;
callback_called_ = false;
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->delegate()->OnURLFetchComplete(fetcher);
}
TEST_F(RegistrationRequestTest, RequestSuccessful) {
set_max_retry_count(0);
CreateRequest("sender1,sender2");
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(RegistrationRequestTest, RequestDataPassedToFetcher) {
CreateRequest(kDeveloperId);
request_->Start();
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
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["app"] = kAppId;
expected_pairs["sender"] = kDeveloperId;
expected_pairs["device"] = base::Uint64ToString(kAndroidId);
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());
expected_pairs.erase(iter);
}
EXPECT_EQ(0UL, expected_pairs.size());
}
TEST_F(RegistrationRequestTest, RequestRegistrationWithMultipleSenderIds) {
CreateRequest("sender1,sender2@gmail.com");
request_->Start();
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
std::string upload_data = fetcher->upload_data();
base::StringTokenizer data_tokenizer(upload_data, "&=");
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());
}
TEST_F(RegistrationRequestTest, ResponseParsing) {
CreateRequest("sender1,sender2");
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(RegistrationRequestTest, ResponseHttpStatusNotOK) {
CreateRequest("sender1,sender2");
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_);
}
TEST_F(RegistrationRequestTest, ResponseMissingRegistrationId) {
CreateRequest("sender1,sender2");
request_->Start();
SetResponseStatusAndString(net::HTTP_OK, "");
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponseStatusAndString(net::HTTP_OK, "some error in response");
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_);
}
TEST_F(RegistrationRequestTest, ResponseDeviceRegistrationError) {
CreateRequest("sender1,sender2");
request_->Start();
SetResponseStatusAndString(net::HTTP_OK, "Error=PHONE_REGISTRATION_ERROR");
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_);
}
TEST_F(RegistrationRequestTest, ResponseAuthenticationError) {
CreateRequest("sender1,sender2");
request_->Start();
SetResponseStatusAndString(net::HTTP_UNAUTHORIZED,
"Error=AUTHENTICATION_FAILED");
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_);
}
TEST_F(RegistrationRequestTest, ResponseInvalidParameters) {
CreateRequest("sender1,sender2");
request_->Start();
SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_PARAMETERS");
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(RegistrationRequest::INVALID_PARAMETERS, status_);
EXPECT_EQ(std::string(), registration_id_);
}
TEST_F(RegistrationRequestTest, ResponseInvalidSender) {
CreateRequest("sender1,sender2");
request_->Start();
SetResponseStatusAndString(net::HTTP_OK, "Error=INVALID_SENDER");
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
EXPECT_EQ(std::string(), registration_id_);
}
TEST_F(RegistrationRequestTest, ResponseInvalidSenderBadRequest) {
CreateRequest("sender1");
request_->Start();
SetResponseStatusAndString(net::HTTP_BAD_REQUEST, "Error=INVALID_SENDER");
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(RegistrationRequest::INVALID_SENDER, status_);
EXPECT_EQ(std::string(), registration_id_);
}
TEST_F(RegistrationRequestTest, RequestNotSuccessful) {
CreateRequest("sender1,sender2");
request_->Start();
net::URLRequestStatus request_status(net::URLRequestStatus::FAILED, 1);
SetResponseStatusAndString(net::HTTP_OK, "token=2501");
net::TestURLFetcher* fetcher = url_fetcher_factory_.GetFetcherByID(0);
ASSERT_TRUE(fetcher);
fetcher->set_status(request_status);
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_);
}
TEST_F(RegistrationRequestTest, ResponseHttpNotOk) {
CreateRequest("sender1,sender2");
request_->Start();
SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "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_);
}
TEST_F(RegistrationRequestTest, MaximumAttemptsReachedWithZeroRetries) {
set_max_retry_count(0);
CreateRequest("sender1,sender2");
request_->Start();
SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_);
EXPECT_EQ(std::string(), registration_id_);
}
TEST_F(RegistrationRequestTest, MaximumAttemptsReached) {
CreateRequest("sender1,sender2");
request_->Start();
SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
CompleteFetch();
EXPECT_FALSE(callback_called_);
SetResponseStatusAndString(net::HTTP_GATEWAY_TIMEOUT, "token=2501");
CompleteFetch();
EXPECT_TRUE(callback_called_);
EXPECT_EQ(RegistrationRequest::REACHED_MAX_RETRIES, status_);
EXPECT_EQ(std::string(), registration_id_);
}
}