OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 // | 4 // |
5 // A complete set of unit tests for OAuth2MintTokenFlow. | 5 // A complete set of unit tests for OAuth2MintTokenFlow. |
6 | 6 |
7 #include <string> | 7 #include <string> |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/memory/scoped_ptr.h" | 10 #include "base/memory/scoped_ptr.h" |
11 #include "chrome/common/net/gaia/gaia_urls.h" | 11 #include "chrome/common/net/gaia/gaia_urls.h" |
12 #include "chrome/common/net/gaia/google_service_auth_error.h" | 12 #include "chrome/common/net/gaia/google_service_auth_error.h" |
13 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" | 13 #include "chrome/common/net/gaia/oauth2_access_token_consumer.h" |
14 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" | 14 #include "chrome/common/net/gaia/oauth2_access_token_fetcher.h" |
15 #include "chrome/common/net/gaia/oauth2_api_call_flow.h" | 15 #include "chrome/common/net/gaia/oauth2_api_call_flow.h" |
16 #include "chrome/test/base/testing_profile.h" | 16 #include "chrome/test/base/testing_profile.h" |
17 #include "content/public/common/url_fetcher.h" | 17 #include "content/public/common/url_fetcher.h" |
18 #include "content/public/common/url_fetcher_delegate.h" | 18 #include "content/public/common/url_fetcher_delegate.h" |
19 #include "content/public/common/url_fetcher_factory.h" | 19 #include "content/public/common/url_fetcher_factory.h" |
20 #include "content/test/test_url_fetcher_factory.h" | 20 #include "content/test/test_url_fetcher_factory.h" |
| 21 #include "net/http/http_request_headers.h" |
21 #include "net/http/http_status_code.h" | 22 #include "net/http/http_status_code.h" |
22 #include "net/url_request/url_request.h" | 23 #include "net/url_request/url_request.h" |
23 #include "net/url_request/url_request_status.h" | 24 #include "net/url_request/url_request_status.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 25 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
26 | 27 |
27 using content::URLFetcher; | 28 using content::URLFetcher; |
28 using content::URLFetcherDelegate; | 29 using content::URLFetcherDelegate; |
29 using content::URLFetcherFactory; | 30 using content::URLFetcherFactory; |
| 31 using net::HttpRequestHeaders; |
30 using net::URLRequestStatus; | 32 using net::URLRequestStatus; |
31 using testing::_; | 33 using testing::_; |
32 using testing::Return; | 34 using testing::Return; |
33 | 35 |
34 namespace { | 36 namespace { |
| 37 static std::string CreateBody() { |
| 38 return "some body"; |
| 39 } |
35 | 40 |
36 static GURL CreateApiUrl() { | 41 static GURL CreateApiUrl() { |
37 return GURL("https://www.googleapis.com/someapi"); | 42 return GURL("https://www.googleapis.com/someapi"); |
38 } | 43 } |
39 | 44 |
40 static std::vector<std::string> CreateTestScopes() { | 45 static std::vector<std::string> CreateTestScopes() { |
41 std::vector<std::string> scopes; | 46 std::vector<std::string> scopes; |
42 scopes.push_back("scope1"); | 47 scopes.push_back("scope1"); |
43 scopes.push_back("scope2"); | 48 scopes.push_back("scope2"); |
44 return scopes; | 49 return scopes; |
45 } | 50 } |
46 | 51 |
47 } // namespace | 52 } // namespace |
48 | 53 |
49 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, | 54 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, |
50 public URLFetcherFactory { | 55 public URLFetcherFactory { |
51 public: | 56 public: |
52 MockUrlFetcherFactory() | 57 MockUrlFetcherFactory() |
53 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { | 58 : ScopedURLFetcherFactory(ALLOW_THIS_IN_INITIALIZER_LIST(this)) { |
54 } | 59 } |
55 virtual ~MockUrlFetcherFactory() {} | 60 virtual ~MockUrlFetcherFactory() {} |
56 | 61 |
57 MOCK_METHOD4( | 62 MOCK_METHOD4( |
58 CreateURLFetcher, | 63 CreateURLFetcher, |
59 URLFetcher* (int id, | 64 URLFetcher* (int id, |
60 const GURL& url, | 65 const GURL& url, |
61 URLFetcher::RequestType request_type, | 66 URLFetcher::RequestType request_type, |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
138 flow_.reset(new MockApiCallFlow( | 143 flow_.reset(new MockApiCallFlow( |
139 profile_.GetRequestContext(), | 144 profile_.GetRequestContext(), |
140 refresh_token, | 145 refresh_token, |
141 access_token, | 146 access_token, |
142 scopes)); | 147 scopes)); |
143 access_token_fetcher_.reset(new MockAccessTokenFetcher( | 148 access_token_fetcher_.reset(new MockAccessTokenFetcher( |
144 flow_.get(), profile_.GetRequestContext())); | 149 flow_.get(), profile_.GetRequestContext())); |
145 } | 150 } |
146 | 151 |
147 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { | 152 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { |
| 153 std::string body(CreateBody()); |
148 GURL url(CreateApiUrl()); | 154 GURL url(CreateApiUrl()); |
149 EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return("")); | 155 EXPECT_CALL(*flow_, CreateApiCallBody()).WillOnce(Return(body)); |
150 EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url)); | 156 EXPECT_CALL(*flow_, CreateApiCallUrl()).WillOnce(Return(url)); |
151 TestURLFetcher* url_fetcher = CreateURLFetcher( | 157 TestURLFetcher* url_fetcher = CreateURLFetcher( |
152 url, succeeds, status, ""); | 158 url, succeeds, status, ""); |
153 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) | 159 EXPECT_CALL(factory_, CreateURLFetcher(_, url, _, _)) |
154 .WillOnce(Return(url_fetcher)); | 160 .WillOnce(Return(url_fetcher)); |
155 return url_fetcher; | 161 return url_fetcher; |
156 } | 162 } |
157 | 163 |
158 MockUrlFetcherFactory factory_; | 164 MockUrlFetcherFactory factory_; |
159 scoped_ptr<MockApiCallFlow> flow_; | 165 scoped_ptr<MockApiCallFlow> flow_; |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
255 std::vector<std::string> scopes(CreateTestScopes()); | 261 std::vector<std::string> scopes(CreateTestScopes()); |
256 | 262 |
257 CreateFlow(rt, "", scopes); | 263 CreateFlow(rt, "", scopes); |
258 SetupAccessTokenFetcher(rt, scopes); | 264 SetupAccessTokenFetcher(rt, scopes); |
259 GoogleServiceAuthError error( | 265 GoogleServiceAuthError error( |
260 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); | 266 GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS); |
261 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); | 267 EXPECT_CALL(*flow_, ProcessMintAccessTokenFailure(error)); |
262 flow_->Start(); | 268 flow_->Start(); |
263 flow_->OnGetTokenFailure(error); | 269 flow_->OnGetTokenFailure(error); |
264 } | 270 } |
| 271 |
| 272 TEST_F(OAuth2ApiCallFlowTest, CreateURLFetcher) { |
| 273 std::string rt = "refresh_token"; |
| 274 std::string at = "access_token"; |
| 275 std::vector<std::string> scopes(CreateTestScopes()); |
| 276 std::string body = CreateBody(); |
| 277 GURL url(CreateApiUrl()); |
| 278 |
| 279 CreateFlow(rt, at, scopes); |
| 280 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); |
| 281 flow_->CreateURLFetcher(); |
| 282 HttpRequestHeaders headers; |
| 283 url_fetcher->GetExtraRequestHeaders(&headers); |
| 284 std::string auth_header; |
| 285 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); |
| 286 EXPECT_EQ("Bearer access_token", auth_header); |
| 287 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); |
| 288 EXPECT_EQ(body, url_fetcher->upload_data()); |
| 289 } |
OLD | NEW |