Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / google_apis / gaia / oauth2_api_call_flow_unittest.cc
blobd537bb7f08bb27420953f2b631e717984f9ba1e5
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/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;
38 using testing::_;
39 using testing::Return;
40 using testing::StrictMock;
42 namespace {
44 const char kAccessToken[] = "access_token";
46 static std::string CreateBody() {
47 return "some body";
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 {
58 public:
59 MockUrlFetcherFactory()
60 : ScopedURLFetcherFactory(this) {
62 virtual ~MockUrlFetcherFactory() {}
64 MOCK_METHOD4(CreateURLFetcherMock,
65 URLFetcher*(int id,
66 const GURL& url,
67 URLFetcher::RequestType request_type,
68 URLFetcherDelegate* d));
70 scoped_ptr<URLFetcher> CreateURLFetcher(int id,
71 const GURL& url,
72 URLFetcher::RequestType request_type,
73 URLFetcherDelegate* d) override {
74 return scoped_ptr<URLFetcher>(
75 CreateURLFetcherMock(id, url, request_type, d));
79 class MockApiCallFlow : public OAuth2ApiCallFlow {
80 public:
81 MockApiCallFlow() {}
82 ~MockApiCallFlow() {}
84 MOCK_METHOD0(CreateApiCallUrl, GURL ());
85 MOCK_METHOD0(CreateApiCallBody, std::string ());
86 MOCK_METHOD1(ProcessApiCallSuccess,
87 void (const URLFetcher* source));
88 MOCK_METHOD1(ProcessApiCallFailure,
89 void (const URLFetcher* source));
90 MOCK_METHOD1(ProcessNewAccessToken,
91 void (const std::string& access_token));
92 MOCK_METHOD1(ProcessMintAccessTokenFailure,
93 void (const GoogleServiceAuthError& error));
96 } // namespace
98 class OAuth2ApiCallFlowTest : public testing::Test {
99 protected:
100 OAuth2ApiCallFlowTest()
101 : request_context_getter_(new net::TestURLRequestContextGetter(
102 message_loop_.task_runner())) {}
104 TestURLFetcher* CreateURLFetcher(
105 const GURL& url, bool fetch_succeeds,
106 int response_code, const std::string& body) {
107 TestURLFetcher* url_fetcher = new TestURLFetcher(0, url, &flow_);
108 URLRequestStatus::Status status =
109 fetch_succeeds ? URLRequestStatus::SUCCESS : URLRequestStatus::FAILED;
110 url_fetcher->set_status(URLRequestStatus(status, 0));
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());