privet_confirm_api_flow_unittest.cc revision a3f7b4e666c476898878fa745f637129375cd889
1// Copyright 2013 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 "base/bind.h" 6#include "base/message_loop/message_loop.h" 7#include "chrome/browser/local_discovery/privet_confirm_api_flow.h" 8#include "content/public/test/test_browser_thread.h" 9#include "google_apis/gaia/google_service_auth_error.h" 10#include "net/base/host_port_pair.h" 11#include "net/base/net_errors.h" 12#include "net/http/http_request_headers.h" 13#include "net/url_request/test_url_fetcher_factory.h" 14#include "net/url_request/url_request_test_util.h" 15#include "testing/gmock/include/gmock/gmock.h" 16#include "testing/gtest/include/gtest/gtest.h" 17 18using testing::NiceMock; 19 20namespace local_discovery { 21 22namespace { 23 24const char kSampleConfirmResponse[] = "{" 25 " \"success\": true" 26 "}"; 27 28const char kFailedConfirmResponse[] = "{" 29 " \"success\": false" 30 "}"; 31 32 33const char kFailedConfirmResponseBadJson[] = "[" 34 " \"success\"" 35 "]"; 36 37class TestOAuth2TokenService : public OAuth2TokenService { 38 public: 39 explicit TestOAuth2TokenService(net::URLRequestContextGetter* request_context) 40 : request_context_(request_context) { 41 } 42 protected: 43 virtual std::string GetRefreshToken() OVERRIDE { 44 return "SampleToken"; 45 } 46 47 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE { 48 return request_context_.get(); 49 } 50 51 private: 52 scoped_refptr<net::URLRequestContextGetter> request_context_; 53}; 54 55class MockableConfirmCallback { 56 public: 57 MOCK_METHOD1(ConfirmCallback, void(PrivetConfirmApiCallFlow::Status)); 58 59 PrivetConfirmApiCallFlow::ResponseCallback callback() { 60 return base::Bind(&MockableConfirmCallback::ConfirmCallback, 61 base::Unretained(this)); 62 } 63}; 64 65class PrivetConfirmApiFlowTest : public testing::Test { 66 public: 67 PrivetConfirmApiFlowTest() 68 : ui_thread_(content::BrowserThread::UI, 69 &loop_), 70 request_context_(new net::TestURLRequestContextGetter( 71 base::MessageLoopProxy::current())), 72 token_service_(request_context_.get()) { 73 ui_thread_.Stop(); // HACK: Fake being on the UI thread 74 } 75 76 virtual ~PrivetConfirmApiFlowTest() { 77 } 78 79 protected: 80 base::MessageLoopForUI loop_; 81 content::TestBrowserThread ui_thread_; 82 scoped_refptr<net::TestURLRequestContextGetter> request_context_; 83 net::TestURLFetcherFactory fetcher_factory_; 84 TestOAuth2TokenService token_service_; 85 MockableConfirmCallback callback_; 86}; 87 88TEST_F(PrivetConfirmApiFlowTest, Success) { 89 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 90 &token_service_, 91 GURL("http://SoMeUrL.com"), 92 callback_.callback()); 93 94 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 95 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 96 97 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 98 99 net::HttpRequestHeaders headers; 100 fetcher->GetExtraRequestHeaders(&headers); 101 std::string oauth_header; 102 std::string proxy; 103 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header)); 104 EXPECT_EQ("Bearer SomeToken", oauth_header); 105 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy)); 106 EXPECT_EQ("Chrome", proxy); 107 108 fetcher->SetResponseString(kSampleConfirmResponse); 109 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 110 net::OK)); 111 fetcher->set_response_code(200); 112 113 EXPECT_CALL(callback_, ConfirmCallback(PrivetConfirmApiCallFlow::SUCCESS)); 114 115 fetcher->delegate()->OnURLFetchComplete(fetcher); 116} 117 118TEST_F(PrivetConfirmApiFlowTest, BadToken) { 119 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 120 &token_service_, 121 GURL("http://SoMeUrL.com"), 122 callback_.callback()); 123 124 EXPECT_CALL(callback_, 125 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_TOKEN)); 126 confirm_flow.OnGetTokenFailure(NULL, GoogleServiceAuthError( 127 GoogleServiceAuthError::USER_NOT_SIGNED_UP)); 128} 129 130TEST_F(PrivetConfirmApiFlowTest, ServerFailure) { 131 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 132 &token_service_, 133 GURL("http://SoMeUrL.com"), 134 callback_.callback()); 135 136 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 137 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 138 139 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 140 141 fetcher->SetResponseString(kFailedConfirmResponse); 142 fetcher->set_status(net::URLRequestStatus(net::URLRequestStatus::SUCCESS, 143 net::OK)); 144 fetcher->set_response_code(200); 145 146 EXPECT_CALL(callback_, 147 ConfirmCallback(PrivetConfirmApiCallFlow::ERROR_FROM_SERVER)); 148 149 fetcher->delegate()->OnURLFetchComplete(fetcher); 150} 151 152TEST_F(PrivetConfirmApiFlowTest, BadJson) { 153 PrivetConfirmApiCallFlow confirm_flow(request_context_.get(), 154 &token_service_, 155 GURL("http://SoMeUrL.com"), 156 callback_.callback()); 157 158 confirm_flow.OnGetTokenSuccess(NULL, "SomeToken", base::Time()); 159 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); 160 161 EXPECT_EQ(GURL("http://SoMeUrL.com"), fetcher->GetOriginalURL()); 162 163 fetcher->SetResponseString(kFailedConfirmResponseBadJson); 164 fetcher->set_status(net::URLRequestStatus( 165 net::URLRequestStatus::SUCCESS, 166 net::OK)); 167 fetcher->set_response_code(200); 168 169 EXPECT_CALL(callback_, ConfirmCallback 170 (PrivetConfirmApiCallFlow::ERROR_MALFORMED_RESPONSE)); 171 172 fetcher->delegate()->OnURLFetchComplete(fetcher); 173} 174 175} // namespace 176 177} // namespace local_discovery 178