Infobar material design refresh: bg color
[chromium-blink-merge.git] / google_apis / gaia / oauth2_api_call_flow_unittest.cc
blobbf4192c19f646262bfed9e4c32baac933c07d1e2
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.
4 //
5 // A complete set of unit tests for OAuth2MintTokenFlow.
7 #include <string>
8 #include <vector>
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/base/net_errors.h"
19 #include "net/http/http_request_headers.h"
20 #include "net/http/http_status_code.h"
21 #include "net/url_request/test_url_fetcher_factory.h"
22 #include "net/url_request/url_fetcher.h"
23 #include "net/url_request/url_fetcher_delegate.h"
24 #include "net/url_request/url_fetcher_factory.h"
25 #include "net/url_request/url_request.h"
26 #include "net/url_request/url_request_status.h"
27 #include "net/url_request/url_request_test_util.h"
28 #include "testing/gmock/include/gmock/gmock.h"
29 #include "testing/gtest/include/gtest/gtest.h"
31 using net::HttpRequestHeaders;
32 using net::ScopedURLFetcherFactory;
33 using net::TestURLFetcher;
34 using net::URLFetcher;
35 using net::URLFetcherDelegate;
36 using net::URLFetcherFactory;
37 using net::URLRequestContextGetter;
38 using net::URLRequestStatus;
39 using testing::_;
40 using testing::Return;
41 using testing::StrictMock;
43 namespace {
45 const char kAccessToken[] = "access_token";
47 static std::string CreateBody() {
48 return "some body";
51 static GURL CreateApiUrl() {
52 return GURL("https://www.googleapis.com/someapi");
55 // Replaces the global URLFetcher factory so the test can return a custom
56 // URLFetcher to complete requests.
57 class MockUrlFetcherFactory : public ScopedURLFetcherFactory,
58 public URLFetcherFactory {
59 public:
60 MockUrlFetcherFactory()
61 : ScopedURLFetcherFactory(this) {
63 virtual ~MockUrlFetcherFactory() {}
65 MOCK_METHOD4(CreateURLFetcherMock,
66 URLFetcher*(int id,
67 const GURL& url,
68 URLFetcher::RequestType request_type,
69 URLFetcherDelegate* d));
71 scoped_ptr<URLFetcher> CreateURLFetcher(int id,
72 const GURL& url,
73 URLFetcher::RequestType request_type,
74 URLFetcherDelegate* d) override {
75 return scoped_ptr<URLFetcher>(
76 CreateURLFetcherMock(id, url, request_type, d));
80 class MockApiCallFlow : public OAuth2ApiCallFlow {
81 public:
82 MockApiCallFlow() {}
83 ~MockApiCallFlow() {}
85 MOCK_METHOD0(CreateApiCallUrl, GURL ());
86 MOCK_METHOD0(CreateApiCallBody, std::string ());
87 MOCK_METHOD1(ProcessApiCallSuccess,
88 void (const URLFetcher* source));
89 MOCK_METHOD1(ProcessApiCallFailure,
90 void (const URLFetcher* source));
91 MOCK_METHOD1(ProcessNewAccessToken,
92 void (const std::string& access_token));
93 MOCK_METHOD1(ProcessMintAccessTokenFailure,
94 void (const GoogleServiceAuthError& error));
97 } // namespace
99 class OAuth2ApiCallFlowTest : public testing::Test {
100 protected:
101 OAuth2ApiCallFlowTest()
102 : request_context_getter_(new net::TestURLRequestContextGetter(
103 message_loop_.task_runner())) {}
105 TestURLFetcher* CreateURLFetcher(
106 const GURL& url, bool fetch_succeeds,
107 int response_code, const std::string& body) {
108 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &flow_);
109 net::Error error = fetch_succeeds ? net::OK : net::ERR_FAILED;
110 url_fetcher->set_status(URLRequestStatus::FromError(error));
112 if (response_code != 0)
113 url_fetcher->set_response_code(response_code);
115 if (!body.empty())
116 url_fetcher->SetResponseString(body);
118 return url_fetcher;
121 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) {
122 std::string body(CreateBody());
123 GURL url(CreateApiUrl());
124 EXPECT_CALL(flow_, CreateApiCallBody()).WillOnce(Return(body));
125 EXPECT_CALL(flow_, CreateApiCallUrl()).WillOnce(Return(url));
126 TestURLFetcher* url_fetcher =
127 CreateURLFetcher(url, succeeds, status, std::string());
128 EXPECT_CALL(factory_, CreateURLFetcherMock(_, url, _, _))
129 .WillOnce(Return(url_fetcher));
130 return url_fetcher;
133 base::MessageLoop message_loop_;
134 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_;
135 StrictMock<MockApiCallFlow> flow_;
136 MockUrlFetcherFactory factory_;
139 TEST_F(OAuth2ApiCallFlowTest, ApiCallSucceedsHttpOk) {
140 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK);
141 EXPECT_CALL(flow_, ProcessApiCallSuccess(url_fetcher));
142 flow_.Start(request_context_getter_.get(), kAccessToken);
143 flow_.OnURLFetchComplete(url_fetcher);
146 TEST_F(OAuth2ApiCallFlowTest, ApiCallSucceedsHttpNoContent) {
147 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_NO_CONTENT);
148 EXPECT_CALL(flow_, ProcessApiCallSuccess(url_fetcher));
149 flow_.Start(request_context_getter_.get(), kAccessToken);
150 flow_.OnURLFetchComplete(url_fetcher);
153 TEST_F(OAuth2ApiCallFlowTest, ApiCallFailure) {
154 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_UNAUTHORIZED);
155 EXPECT_CALL(flow_, ProcessApiCallFailure(url_fetcher));
156 flow_.Start(request_context_getter_.get(), kAccessToken);
157 flow_.OnURLFetchComplete(url_fetcher);
160 TEST_F(OAuth2ApiCallFlowTest, ExpectedHTTPHeaders) {
161 std::string body = CreateBody();
162 GURL url(CreateApiUrl());
164 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK);
165 flow_.Start(request_context_getter_.get(), kAccessToken);
166 HttpRequestHeaders headers;
167 url_fetcher->GetExtraRequestHeaders(&headers);
168 std::string auth_header;
169 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header));
170 EXPECT_EQ("Bearer access_token", auth_header);
171 EXPECT_EQ(url, url_fetcher->GetOriginalURL());
172 EXPECT_EQ(body, url_fetcher->upload_data());