| OLD | NEW |
| (Empty) |
| 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_api_flow.h" | |
| 6 | |
| 7 #include <utility> | |
| 8 | |
| 9 #include "base/bind.h" | |
| 10 #include "base/message_loop/message_loop.h" | |
| 11 #include "base/thread_task_runner_handle.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/local_discovery/gcd_api_flow_impl.h" | |
| 14 #include "content/public/test/test_browser_thread.h" | |
| 15 #include "google_apis/gaia/fake_oauth2_token_service.h" | |
| 16 #include "google_apis/gaia/google_service_auth_error.h" | |
| 17 #include "net/base/host_port_pair.h" | |
| 18 #include "net/base/net_errors.h" | |
| 19 #include "net/http/http_request_headers.h" | |
| 20 #include "net/url_request/test_url_fetcher_factory.h" | |
| 21 #include "net/url_request/url_request_test_util.h" | |
| 22 #include "testing/gmock/include/gmock/gmock.h" | |
| 23 #include "testing/gtest/include/gtest/gtest.h" | |
| 24 | |
| 25 using testing::_; | |
| 26 using testing::Return; | |
| 27 | |
| 28 namespace local_discovery { | |
| 29 | |
| 30 namespace { | |
| 31 | |
| 32 const char kSampleConfirmResponse[] = "{}"; | |
| 33 | |
| 34 const char kFailedConfirmResponseBadJson[] = "[]"; | |
| 35 | |
| 36 const char kAccountId[] = "account_id"; | |
| 37 | |
| 38 class MockDelegate : public CloudPrintApiFlowRequest { | |
| 39 public: | |
| 40 MOCK_METHOD1(OnGCDAPIFlowError, void(GCDApiFlow::Status)); | |
| 41 MOCK_METHOD1(OnGCDAPIFlowComplete, void(const base::DictionaryValue&)); | |
| 42 | |
| 43 MOCK_METHOD0(GetURL, GURL()); | |
| 44 }; | |
| 45 | |
| 46 class GCDApiFlowTest : public testing::Test { | |
| 47 public: | |
| 48 GCDApiFlowTest() | |
| 49 : ui_thread_(content::BrowserThread::UI, &loop_), | |
| 50 request_context_(new net::TestURLRequestContextGetter( | |
| 51 base::ThreadTaskRunnerHandle::Get())), | |
| 52 account_id_(kAccountId) {} | |
| 53 | |
| 54 ~GCDApiFlowTest() override {} | |
| 55 | |
| 56 protected: | |
| 57 void SetUp() override { | |
| 58 token_service_.GetFakeOAuth2TokenServiceDelegate()->set_request_context( | |
| 59 request_context_.get()); | |
| 60 token_service_.AddAccount(account_id_); | |
| 61 ui_thread_.Stop(); // HACK: Fake being on the UI thread | |
| 62 | |
| 63 scoped_ptr<MockDelegate> delegate(new MockDelegate); | |
| 64 mock_delegate_ = delegate.get(); | |
| 65 EXPECT_CALL(*mock_delegate_, GetURL()) | |
| 66 .WillRepeatedly(Return( | |
| 67 GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"))); | |
| 68 gcd_flow_.reset(new GCDApiFlowImpl( | |
| 69 request_context_.get(), &token_service_, account_id_)); | |
| 70 gcd_flow_->Start(std::move(delegate)); | |
| 71 } | |
| 72 base::MessageLoopForUI loop_; | |
| 73 content::TestBrowserThread ui_thread_; | |
| 74 scoped_refptr<net::TestURLRequestContextGetter> request_context_; | |
| 75 net::TestURLFetcherFactory fetcher_factory_; | |
| 76 FakeOAuth2TokenService token_service_; | |
| 77 std::string account_id_; | |
| 78 scoped_ptr<GCDApiFlowImpl> gcd_flow_; | |
| 79 MockDelegate* mock_delegate_; | |
| 80 }; | |
| 81 | |
| 82 TEST_F(GCDApiFlowTest, SuccessOAuth2) { | |
| 83 gcd_flow_->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | |
| 84 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 85 | |
| 86 EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"), | |
| 87 fetcher->GetOriginalURL()); | |
| 88 | |
| 89 net::HttpRequestHeaders headers; | |
| 90 fetcher->GetExtraRequestHeaders(&headers); | |
| 91 std::string oauth_header; | |
| 92 std::string proxy; | |
| 93 EXPECT_TRUE(headers.GetHeader("Authorization", &oauth_header)); | |
| 94 EXPECT_EQ("Bearer SomeToken", oauth_header); | |
| 95 EXPECT_TRUE(headers.GetHeader("X-Cloudprint-Proxy", &proxy)); | |
| 96 EXPECT_EQ("Chrome", proxy); | |
| 97 | |
| 98 fetcher->SetResponseString(kSampleConfirmResponse); | |
| 99 fetcher->set_status( | |
| 100 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | |
| 101 fetcher->set_response_code(200); | |
| 102 | |
| 103 EXPECT_CALL(*mock_delegate_, OnGCDAPIFlowComplete(_)); | |
| 104 | |
| 105 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 106 } | |
| 107 | |
| 108 TEST_F(GCDApiFlowTest, BadToken) { | |
| 109 EXPECT_CALL(*mock_delegate_, OnGCDAPIFlowError(GCDApiFlow::ERROR_TOKEN)); | |
| 110 gcd_flow_->OnGetTokenFailure( | |
| 111 NULL, GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP)); | |
| 112 } | |
| 113 | |
| 114 TEST_F(GCDApiFlowTest, BadJson) { | |
| 115 gcd_flow_->OnGetTokenSuccess(NULL, "SomeToken", base::Time()); | |
| 116 net::TestURLFetcher* fetcher = fetcher_factory_.GetFetcherByID(0); | |
| 117 | |
| 118 EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"), | |
| 119 fetcher->GetOriginalURL()); | |
| 120 | |
| 121 fetcher->SetResponseString(kFailedConfirmResponseBadJson); | |
| 122 fetcher->set_status( | |
| 123 net::URLRequestStatus(net::URLRequestStatus::SUCCESS, net::OK)); | |
| 124 fetcher->set_response_code(200); | |
| 125 | |
| 126 EXPECT_CALL(*mock_delegate_, | |
| 127 OnGCDAPIFlowError(GCDApiFlow::ERROR_MALFORMED_RESPONSE)); | |
| 128 | |
| 129 fetcher->delegate()->OnURLFetchComplete(fetcher); | |
| 130 } | |
| 131 | |
| 132 } // namespace | |
| 133 | |
| 134 } // namespace local_discovery | |
| OLD | NEW |