Home | History | Annotate | Download | only in remoting
      1 // Copyright (c) 2011 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/remoting/directory_add_request.h"
      6 #include "chrome/test/testing_profile.h"
      7 #include "net/url_request/url_request_status.h"
      8 #include "testing/gmock/include/gmock/gmock.h"
      9 #include "testing/gtest/include/gtest/gtest.h"
     10 
     11 namespace remoting {
     12 
     13 namespace {
     14 class MockDoneCallback {
     15  public:
     16   MOCK_METHOD2(OnDone,
     17                void(DirectoryAddRequest::Result,
     18                     const std::string& message));
     19 };
     20 }  // namespace
     21 
     22 class DirectoryAddRequestTest : public testing::Test {
     23  protected:
     24   virtual void SetUp() {
     25     target_.reset(new DirectoryAddRequest(profile_.GetRequestContext()));
     26   }
     27 
     28   void TestResult(int response_code, const std::string& data,
     29                   DirectoryAddRequest::Result expected_result,
     30                   const std::string& expected_message) {
     31     MockDoneCallback callback;
     32     EXPECT_CALL(callback, OnDone(expected_result, expected_message))
     33         .Times(1);
     34 
     35     target_->done_callback_.reset(
     36         NewCallback(&callback, &MockDoneCallback::OnDone));
     37 
     38     GURL url;
     39     net::URLRequestStatus status_ok;
     40     ResponseCookies cookies;
     41     target_->OnURLFetchComplete(NULL, url, status_ok, response_code,
     42                                cookies, data);
     43   }
     44 
     45   TestingProfile profile_;
     46   scoped_ptr<DirectoryAddRequest> target_;
     47 };
     48 
     49 TEST_F(DirectoryAddRequestTest, Success) {
     50   TestResult(200, "{\"data\":{\"kind\":\"chromoting#host\","
     51              "\"hostId\":\"e64906c9-fdc9-4921-80cb-563cf7f564f3\","
     52              "\"hostName\":\"host_name\",\"publicKey\":\"PUBLIC+KEY\"}}",
     53              DirectoryAddRequest::SUCCESS, "");
     54 }
     55 
     56 TEST_F(DirectoryAddRequestTest, Duplicate) {
     57   TestResult(400, "{\"error\":{\"errors\":[{\"domain\":\"global\","
     58              "\"reason\":\"invalid\",\"message\":\"Attempt to register "
     59              "a duplicate host.\"}],\"code\":400,\"message\":\"Attempt to "
     60              "register a duplicate host.\"}}",
     61              DirectoryAddRequest::ERROR_EXISTS,
     62              "Attempt to register a duplicate host.");
     63 }
     64 
     65 TEST_F(DirectoryAddRequestTest, InvalidRequest) {
     66   TestResult(400, "{\"error\":{\"errors\":[{\"domain\":\"global\","
     67              "\"reason\":\"invalid\",\"message\":\"Invalid Value\"}],"
     68              "\"code\":400,\"message\":\"Invalid Value\"}}",
     69              DirectoryAddRequest::ERROR_INVALID_REQUEST,
     70              "Invalid Value");
     71 }
     72 
     73 TEST_F(DirectoryAddRequestTest, InvalidToken) {
     74   TestResult(401, "{\"error\":{\"errors\":[{\"domain\":\"global\","
     75              "\"reason\":\"invalid\",\"message\":\"Token invalid\","
     76              "\"locationType\":\"header\",\"location\":\"Authorization\"}],"
     77              "\"code\":401,\"message\":\"Token invalid\"}}",
     78              DirectoryAddRequest::ERROR_AUTH,
     79              "Token invalid");
     80 }
     81 
     82 }  // namespace remoting
     83