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/values.h"
10 #include "chrome/browser/local_discovery/gcd_api_flow_impl.h"
11 #include "content/public/test/test_browser_thread.h"
12 #include "google_apis/gaia/fake_oauth2_token_service.h"
13 #include "google_apis/gaia/google_service_auth_error.h"
14 #include "net/base/host_port_pair.h"
15 #include "net/base/net_errors.h"
16 #include "net/http/http_request_headers.h"
17 #include "net/url_request/test_url_fetcher_factory.h"
18 #include "net/url_request/url_request_test_util.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
23 using testing::Return
;
25 namespace local_discovery
{
29 const char kSampleConfirmResponse
[] = "{}";
31 const char kFailedConfirmResponseBadJson
[] = "[]";
33 const char kAccountId
[] = "account_id";
35 class MockDelegate
: public CloudPrintApiFlowRequest
{
37 MOCK_METHOD1(OnGCDAPIFlowError
, void(GCDApiFlow::Status
));
38 MOCK_METHOD1(OnGCDAPIFlowComplete
, void(const base::DictionaryValue
&));
40 MOCK_METHOD0(GetURL
, GURL());
43 class GCDApiFlowTest
: public testing::Test
{
46 : ui_thread_(content::BrowserThread::UI
, &loop_
),
47 request_context_(new net::TestURLRequestContextGetter(
48 base::MessageLoopProxy::current())),
49 account_id_(kAccountId
) {}
51 ~GCDApiFlowTest() override
{}
54 void SetUp() override
{
55 token_service_
.set_request_context(request_context_
.get());
56 token_service_
.AddAccount(account_id_
);
57 ui_thread_
.Stop(); // HACK: Fake being on the UI thread
59 scoped_ptr
<MockDelegate
> delegate(new MockDelegate
);
60 mock_delegate_
= delegate
.get();
61 EXPECT_CALL(*mock_delegate_
, GetURL())
62 .WillRepeatedly(Return(
63 GURL("https://www.google.com/cloudprint/confirm?token=SomeToken")));
64 gcd_flow_
.reset(new GCDApiFlowImpl(
65 request_context_
.get(), &token_service_
, account_id_
));
66 gcd_flow_
->Start(delegate
.Pass());
68 base::MessageLoopForUI loop_
;
69 content::TestBrowserThread ui_thread_
;
70 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_
;
71 net::TestURLFetcherFactory fetcher_factory_
;
72 FakeOAuth2TokenService token_service_
;
73 std::string account_id_
;
74 scoped_ptr
<GCDApiFlowImpl
> gcd_flow_
;
75 MockDelegate
* mock_delegate_
;
78 TEST_F(GCDApiFlowTest
, SuccessOAuth2
) {
79 gcd_flow_
->OnGetTokenSuccess(NULL
, "SomeToken", base::Time());
80 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
82 EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"),
83 fetcher
->GetOriginalURL());
85 net::HttpRequestHeaders headers
;
86 fetcher
->GetExtraRequestHeaders(&headers
);
87 std::string oauth_header
;
89 EXPECT_TRUE(headers
.GetHeader("Authorization", &oauth_header
));
90 EXPECT_EQ("Bearer SomeToken", oauth_header
);
91 EXPECT_TRUE(headers
.GetHeader("X-Cloudprint-Proxy", &proxy
));
92 EXPECT_EQ("Chrome", proxy
);
94 fetcher
->SetResponseString(kSampleConfirmResponse
);
96 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, net::OK
));
97 fetcher
->set_response_code(200);
99 EXPECT_CALL(*mock_delegate_
, OnGCDAPIFlowComplete(_
));
101 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
104 TEST_F(GCDApiFlowTest
, BadToken
) {
105 EXPECT_CALL(*mock_delegate_
, OnGCDAPIFlowError(GCDApiFlow::ERROR_TOKEN
));
106 gcd_flow_
->OnGetTokenFailure(
107 NULL
, GoogleServiceAuthError(GoogleServiceAuthError::USER_NOT_SIGNED_UP
));
110 TEST_F(GCDApiFlowTest
, BadJson
) {
111 gcd_flow_
->OnGetTokenSuccess(NULL
, "SomeToken", base::Time());
112 net::TestURLFetcher
* fetcher
= fetcher_factory_
.GetFetcherByID(0);
114 EXPECT_EQ(GURL("https://www.google.com/cloudprint/confirm?token=SomeToken"),
115 fetcher
->GetOriginalURL());
117 fetcher
->SetResponseString(kFailedConfirmResponseBadJson
);
119 net::URLRequestStatus(net::URLRequestStatus::SUCCESS
, net::OK
));
120 fetcher
->set_response_code(200);
122 EXPECT_CALL(*mock_delegate_
,
123 OnGCDAPIFlowError(GCDApiFlow::ERROR_MALFORMED_RESPONSE
));
125 fetcher
->delegate()->OnURLFetchComplete(fetcher
);
130 } // namespace local_discovery