1 // Copyright (c) 2012 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 // A complete set of unit tests for OAuth2MintTokenFlow.
10 #include "base/memory/scoped_ptr.h"
11 #include "base/message_loop/message_loop.h"
12 #include "base/time/time.h"
13 #include "google_apis/gaia/gaia_urls.h"
14 #include "google_apis/gaia/google_service_auth_error.h"
15 #include "google_apis/gaia/oauth2_access_token_consumer.h"
16 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h"
17 #include "google_apis/gaia/oauth2_api_call_flow.h"
18 #include "net/http/http_request_headers.h"
19 #include "net/http/http_status_code.h"
20 #include "net/url_request/test_url_fetcher_factory.h"
21 #include "net/url_request/url_fetcher.h"
22 #include "net/url_request/url_fetcher_delegate.h"
23 #include "net/url_request/url_fetcher_factory.h"
24 #include "net/url_request/url_request.h"
25 #include "net/url_request/url_request_status.h"
26 #include "net/url_request/url_request_test_util.h"
27 #include "testing/gmock/include/gmock/gmock.h"
28 #include "testing/gtest/include/gtest/gtest.h"
30 using net::HttpRequestHeaders
;
31 using net::ScopedURLFetcherFactory
;
32 using net::TestURLFetcher
;
33 using net::URLFetcher
;
34 using net::URLFetcherDelegate
;
35 using net::URLFetcherFactory
;
36 using net::URLRequestContextGetter
;
37 using net::URLRequestStatus
;
39 using testing::Return
;
40 using testing::StrictMock
;
44 const char kAccessToken
[] = "access_token";
46 static std::string
CreateBody() {
50 static GURL
CreateApiUrl() {
51 return GURL("https://www.googleapis.com/someapi");
54 // Replaces the global URLFetcher factory so the test can return a custom
55 // URLFetcher to complete requests.
56 class MockUrlFetcherFactory
: public ScopedURLFetcherFactory
,
57 public URLFetcherFactory
{
59 MockUrlFetcherFactory()
60 : ScopedURLFetcherFactory(this) {
62 virtual ~MockUrlFetcherFactory() {}
68 URLFetcher::RequestType request_type
,
69 URLFetcherDelegate
* d
));
72 class MockApiCallFlow
: public OAuth2ApiCallFlow
{
77 MOCK_METHOD0(CreateApiCallUrl
, GURL ());
78 MOCK_METHOD0(CreateApiCallBody
, std::string ());
79 MOCK_METHOD1(ProcessApiCallSuccess
,
80 void (const URLFetcher
* source
));
81 MOCK_METHOD1(ProcessApiCallFailure
,
82 void (const URLFetcher
* source
));
83 MOCK_METHOD1(ProcessNewAccessToken
,
84 void (const std::string
& access_token
));
85 MOCK_METHOD1(ProcessMintAccessTokenFailure
,
86 void (const GoogleServiceAuthError
& error
));
91 class OAuth2ApiCallFlowTest
: public testing::Test
{
93 OAuth2ApiCallFlowTest()
94 : request_context_getter_(new net::TestURLRequestContextGetter(
95 message_loop_
.message_loop_proxy())) {}
97 TestURLFetcher
* CreateURLFetcher(
98 const GURL
& url
, bool fetch_succeeds
,
99 int response_code
, const std::string
& body
) {
100 TestURLFetcher
* url_fetcher
= new TestURLFetcher(0, url
, &flow_
);
101 URLRequestStatus::Status status
=
102 fetch_succeeds
? URLRequestStatus::SUCCESS
: URLRequestStatus::FAILED
;
103 url_fetcher
->set_status(URLRequestStatus(status
, 0));
105 if (response_code
!= 0)
106 url_fetcher
->set_response_code(response_code
);
109 url_fetcher
->SetResponseString(body
);
114 TestURLFetcher
* SetupApiCall(bool succeeds
, net::HttpStatusCode status
) {
115 std::string
body(CreateBody());
116 GURL
url(CreateApiUrl());
117 EXPECT_CALL(flow_
, CreateApiCallBody()).WillOnce(Return(body
));
118 EXPECT_CALL(flow_
, CreateApiCallUrl()).WillOnce(Return(url
));
119 TestURLFetcher
* url_fetcher
=
120 CreateURLFetcher(url
, succeeds
, status
, std::string());
121 EXPECT_CALL(factory_
, CreateURLFetcher(_
, url
, _
, _
))
122 .WillOnce(Return(url_fetcher
));
126 base::MessageLoop message_loop_
;
127 scoped_refptr
<net::TestURLRequestContextGetter
> request_context_getter_
;
128 StrictMock
<MockApiCallFlow
> flow_
;
129 MockUrlFetcherFactory factory_
;
132 TEST_F(OAuth2ApiCallFlowTest
, ApiCallSucceedsHttpOk
) {
133 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_OK
);
134 EXPECT_CALL(flow_
, ProcessApiCallSuccess(url_fetcher
));
135 flow_
.Start(request_context_getter_
.get(), kAccessToken
);
136 flow_
.OnURLFetchComplete(url_fetcher
);
139 TEST_F(OAuth2ApiCallFlowTest
, ApiCallSucceedsHttpNoContent
) {
140 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_NO_CONTENT
);
141 EXPECT_CALL(flow_
, ProcessApiCallSuccess(url_fetcher
));
142 flow_
.Start(request_context_getter_
.get(), kAccessToken
);
143 flow_
.OnURLFetchComplete(url_fetcher
);
146 TEST_F(OAuth2ApiCallFlowTest
, ApiCallFailure
) {
147 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_UNAUTHORIZED
);
148 EXPECT_CALL(flow_
, ProcessApiCallFailure(url_fetcher
));
149 flow_
.Start(request_context_getter_
.get(), kAccessToken
);
150 flow_
.OnURLFetchComplete(url_fetcher
);
153 TEST_F(OAuth2ApiCallFlowTest
, ExpectedHTTPHeaders
) {
154 std::string body
= CreateBody();
155 GURL
url(CreateApiUrl());
157 TestURLFetcher
* url_fetcher
= SetupApiCall(true, net::HTTP_OK
);
158 flow_
.Start(request_context_getter_
.get(), kAccessToken
);
159 HttpRequestHeaders headers
;
160 url_fetcher
->GetExtraRequestHeaders(&headers
);
161 std::string auth_header
;
162 EXPECT_TRUE(headers
.GetHeader("Authorization", &auth_header
));
163 EXPECT_EQ("Bearer access_token", auth_header
);
164 EXPECT_EQ(url
, url_fetcher
->GetOriginalURL());
165 EXPECT_EQ(body
, url_fetcher
->upload_data());