Commit beca0156 authored by noamsml@chromium.org's avatar noamsml@chromium.org

GCD registration ticket creation flow

GCD API flow for registration ticket creation

BUG=

Review URL: https://codereview.chromium.org/307353008

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@274759 0039d316-1c4b-4281-b951-d872f2087c98
parent 6b847d8e
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/local_discovery/gcd_registration_ticket_request.h"
#include "chrome/browser/local_discovery/gcd_constants.h"
#include "chrome/common/cloud_print/cloud_print_constants.h"
#include "components/cloud_devices/common/cloud_devices_urls.h"
namespace local_discovery {
namespace {
const char kUploadData[] = "{ \"userEmail\": \"me\" }";
const char kKindRegistrationTicket[] = "clouddevices#registrationTicket";
const char kGCDKeyId[] = "id";
}
GCDRegistrationTicketRequest::GCDRegistrationTicketRequest(
const ResponseCallback& callback)
: callback_(callback) {
}
GCDRegistrationTicketRequest::~GCDRegistrationTicketRequest() {
}
void GCDRegistrationTicketRequest::GetUploadData(std::string* upload_type,
std::string* upload_data) {
*upload_data = kUploadData;
// TODO(noamsml): Move this constant to cloud_devices component.
*upload_type = cloud_print::kContentTypeJSON;
}
net::URLFetcher::RequestType GCDRegistrationTicketRequest::GetRequestType() {
return net::URLFetcher::POST;
}
void GCDRegistrationTicketRequest::OnGCDAPIFlowError(
GCDApiFlow::Status status) {
callback_.Run(std::string());
}
void GCDRegistrationTicketRequest::OnGCDAPIFlowComplete(
const base::DictionaryValue& value) {
std::string kind;
std::string id;
if (!value.GetString(kGCDKeyKind, &kind) || kind != kKindRegistrationTicket ||
!value.GetString(kGCDKeyId, &id)) {
callback_.Run(std::string());
return;
}
callback_.Run(id);
}
GURL GCDRegistrationTicketRequest::GetURL() {
return cloud_devices::GetCloudDevicesRelativeURL("registrationTickets");
}
} // namespace local_discovery
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef CHROME_BROWSER_LOCAL_DISCOVERY_GCD_REGISTRATION_TICKET_REQUEST_H_
#define CHROME_BROWSER_LOCAL_DISCOVERY_GCD_REGISTRATION_TICKET_REQUEST_H_
#include <string>
#include "base/callback.h"
#include "base/values.h"
#include "chrome/browser/local_discovery/gcd_api_flow.h"
namespace local_discovery {
class GCDRegistrationTicketRequest : public GCDApiFlowRequest {
public:
// |ticket_id| contains the registration ticket ID, or an empty string in case
// of an error.
typedef base::Callback<void(const std::string& ticket_id)> ResponseCallback;
explicit GCDRegistrationTicketRequest(const ResponseCallback& callback);
virtual ~GCDRegistrationTicketRequest();
// GCDApiFlow::Request implementation.
virtual void GetUploadData(std::string* upload_type,
std::string* upload_data) OVERRIDE;
virtual net::URLFetcher::RequestType GetRequestType() OVERRIDE;
virtual void OnGCDAPIFlowError(GCDApiFlow::Status status) OVERRIDE;
virtual void OnGCDAPIFlowComplete(
const base::DictionaryValue& value) OVERRIDE;
virtual GURL GetURL() OVERRIDE;
private:
ResponseCallback callback_;
};
} // namespace local_discovery
#endif // CHROME_BROWSER_LOCAL_DISCOVERY_GCD_REGISTRATION_TICKET_REQUEST_H_
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/local_discovery/gcd_registration_ticket_request.h"
#include "base/json/json_reader.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
using testing::StrictMock;
namespace local_discovery {
namespace {
const char kSampleResponse[] =
"{"
"\"kind\": \"clouddevices#registrationTicket\","
"\"id\": \"SampleTicketID\""
"}";
const char kErrorResponse[] =
"{"
"\"kind\": \"clouddevices#error\""
"}";
TEST(GCDRegistrationTicketRequestTest, Params) {
GCDRegistrationTicketRequest::ResponseCallback null_callback;
GCDRegistrationTicketRequest request(null_callback);
EXPECT_EQ(
GURL("https://www.googleapis.com/clouddevices/v1/registrationTickets"),
request.GetURL());
EXPECT_EQ("https://www.googleapis.com/auth/clouddevices",
request.GetOAuthScope());
EXPECT_EQ(net::URLFetcher::POST, request.GetRequestType());
EXPECT_TRUE(request.GetExtraRequestHeaders().empty());
}
class MockDelegate {
public:
MOCK_METHOD1(Callback, void(const std::string& ticket_id));
};
TEST(GCDRegistrationTicketRequestTest, Parsing) {
StrictMock<MockDelegate> delegate;
GCDRegistrationTicketRequest request(
base::Bind(&MockDelegate::Callback, base::Unretained(&delegate)));
EXPECT_CALL(delegate, Callback("SampleTicketID"));
scoped_ptr<base::Value> value(base::JSONReader::Read(kSampleResponse));
const base::DictionaryValue* dictionary = NULL;
ASSERT_TRUE(value->GetAsDictionary(&dictionary));
request.OnGCDAPIFlowComplete(*dictionary);
EXPECT_CALL(delegate, Callback(""));
value.reset(base::JSONReader::Read(kErrorResponse));
ASSERT_TRUE(value->GetAsDictionary(&dictionary));
request.OnGCDAPIFlowComplete(*dictionary);
}
} // namespace
} // namespace local_discovery
......@@ -964,6 +964,8 @@
'browser/local_discovery/gcd_api_flow.h',
'browser/local_discovery/gcd_constants.cc',
'browser/local_discovery/gcd_constants.h',
'browser/local_discovery/gcd_registration_ticket_request.cc',
'browser/local_discovery/gcd_registration_ticket_request.h',
'browser/local_discovery/privet_confirm_api_flow.cc',
'browser/local_discovery/privet_confirm_api_flow.h',
'browser/local_discovery/privet_constants.cc',
......
......@@ -1049,6 +1049,7 @@
'browser/invalidation/ticl_invalidation_service_unittest.cc',
'browser/invalidation/ticl_profile_settings_provider_unittest.cc',
'browser/local_discovery/gcd_api_flow_unittest.cc',
'browser/local_discovery/gcd_registration_ticket_request_unittest.cc',
'browser/local_discovery/privet_confirm_api_flow_unittest.cc',
'browser/local_discovery/privet_http_unittest.cc',
'browser/local_discovery/privet_url_fetcher_unittest.cc',
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment