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