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. 4 5 #include "chrome/browser/local_discovery/gcd_registration_ticket_request.h" 6 7 #include "base/json/json_reader.h" 8 #include "testing/gmock/include/gmock/gmock.h" 9 #include "testing/gtest/include/gtest/gtest.h" 10 11 using testing::StrictMock; 12 13 namespace local_discovery { 14 15 namespace { 16 17 const char kSampleResponse[] = 18 "{" 19 "\"kind\": \"clouddevices#registrationTicket\"," 20 "\"id\": \"SampleTicketID\"" 21 "}"; 22 23 const char kErrorResponse[] = 24 "{" 25 "\"kind\": \"clouddevices#error\"" 26 "}"; 27 28 TEST(GCDRegistrationTicketRequestTest, Params) { 29 GCDRegistrationTicketRequest::ResponseCallback null_callback; 30 GCDRegistrationTicketRequest request(null_callback); 31 32 EXPECT_EQ( 33 GURL("https://www.googleapis.com/clouddevices/v1/registrationTickets"), 34 request.GetURL()); 35 EXPECT_EQ("https://www.googleapis.com/auth/clouddevices", 36 request.GetOAuthScope()); 37 EXPECT_EQ(net::URLFetcher::POST, request.GetRequestType()); 38 EXPECT_TRUE(request.GetExtraRequestHeaders().empty()); 39 } 40 41 class MockDelegate { 42 public: 43 MOCK_METHOD1(Callback, void(const std::string& ticket_id)); 44 }; 45 46 TEST(GCDRegistrationTicketRequestTest, Parsing) { 47 StrictMock<MockDelegate> delegate; 48 GCDRegistrationTicketRequest request( 49 base::Bind(&MockDelegate::Callback, base::Unretained(&delegate))); 50 51 EXPECT_CALL(delegate, Callback("SampleTicketID")); 52 53 scoped_ptr<base::Value> value(base::JSONReader::Read(kSampleResponse)); 54 const base::DictionaryValue* dictionary = NULL; 55 ASSERT_TRUE(value->GetAsDictionary(&dictionary)); 56 request.OnGCDAPIFlowComplete(*dictionary); 57 58 EXPECT_CALL(delegate, Callback("")); 59 60 value.reset(base::JSONReader::Read(kErrorResponse)); 61 ASSERT_TRUE(value->GetAsDictionary(&dictionary)); 62 request.OnGCDAPIFlowComplete(*dictionary); 63 } 64 65 } // namespace 66 67 } // namespace local_discovery 68