| 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 "google_apis/gaia/oauth2_api_call_flow.h" | 7 #include "google_apis/gaia/oauth2_api_call_flow.h" |
| 8 | 8 |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <string> | 10 #include <string> |
| 11 #include <vector> | 11 #include <vector> |
| 12 | 12 |
| 13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
| 14 #include "base/time/time.h" | 14 #include "base/time/time.h" |
| 15 #include "google_apis/gaia/gaia_urls.h" | 15 #include "google_apis/gaia/gaia_urls.h" |
| 16 #include "google_apis/gaia/google_service_auth_error.h" | 16 #include "google_apis/gaia/google_service_auth_error.h" |
| 17 #include "google_apis/gaia/oauth2_access_token_consumer.h" | 17 #include "google_apis/gaia/oauth2_access_token_consumer.h" |
| 18 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" | 18 #include "google_apis/gaia/oauth2_access_token_fetcher_impl.h" |
| 19 #include "net/base/net_errors.h" | 19 #include "net/base/net_errors.h" |
| 20 #include "net/http/http_request_headers.h" | 20 #include "net/http/http_request_headers.h" |
| 21 #include "net/http/http_status_code.h" | 21 #include "net/http/http_status_code.h" |
| 22 #include "net/traffic_annotation/network_traffic_annotation_test_helper.h" |
| 22 #include "net/url_request/test_url_fetcher_factory.h" | 23 #include "net/url_request/test_url_fetcher_factory.h" |
| 23 #include "net/url_request/url_fetcher.h" | 24 #include "net/url_request/url_fetcher.h" |
| 24 #include "net/url_request/url_fetcher_delegate.h" | 25 #include "net/url_request/url_fetcher_delegate.h" |
| 25 #include "net/url_request/url_fetcher_factory.h" | 26 #include "net/url_request/url_fetcher_factory.h" |
| 26 #include "net/url_request/url_request.h" | 27 #include "net/url_request/url_request.h" |
| 27 #include "net/url_request/url_request_status.h" | 28 #include "net/url_request/url_request_status.h" |
| 28 #include "net/url_request/url_request_test_util.h" | 29 #include "net/url_request/url_request_test_util.h" |
| 29 #include "testing/gmock/include/gmock/gmock.h" | 30 #include "testing/gmock/include/gmock/gmock.h" |
| 30 #include "testing/gtest/include/gtest/gtest.h" | 31 #include "testing/gtest/include/gtest/gtest.h" |
| 31 | 32 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 56 // Replaces the global URLFetcher factory so the test can return a custom | 57 // Replaces the global URLFetcher factory so the test can return a custom |
| 57 // URLFetcher to complete requests. | 58 // URLFetcher to complete requests. |
| 58 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, | 59 class MockUrlFetcherFactory : public ScopedURLFetcherFactory, |
| 59 public URLFetcherFactory { | 60 public URLFetcherFactory { |
| 60 public: | 61 public: |
| 61 MockUrlFetcherFactory() | 62 MockUrlFetcherFactory() |
| 62 : ScopedURLFetcherFactory(this) { | 63 : ScopedURLFetcherFactory(this) { |
| 63 } | 64 } |
| 64 virtual ~MockUrlFetcherFactory() {} | 65 virtual ~MockUrlFetcherFactory() {} |
| 65 | 66 |
| 66 MOCK_METHOD4(CreateURLFetcherMock, | 67 MOCK_METHOD5( |
| 67 URLFetcher*(int id, | 68 CreateURLFetcherMock, |
| 68 const GURL& url, | 69 URLFetcher*(int id, |
| 69 URLFetcher::RequestType request_type, | 70 const GURL& url, |
| 70 URLFetcherDelegate* d)); | 71 URLFetcher::RequestType request_type, |
| 72 URLFetcherDelegate* d, |
| 73 net::NetworkTrafficAnnotationTag traffic_annotation)); |
| 71 | 74 |
| 72 std::unique_ptr<URLFetcher> CreateURLFetcher( | 75 std::unique_ptr<URLFetcher> CreateURLFetcher( |
| 73 int id, | 76 int id, |
| 74 const GURL& url, | 77 const GURL& url, |
| 75 URLFetcher::RequestType request_type, | 78 URLFetcher::RequestType request_type, |
| 76 URLFetcherDelegate* d) override { | 79 URLFetcherDelegate* d, |
| 80 net::NetworkTrafficAnnotationTag traffic_annotation) override { |
| 77 return std::unique_ptr<URLFetcher>( | 81 return std::unique_ptr<URLFetcher>( |
| 78 CreateURLFetcherMock(id, url, request_type, d)); | 82 CreateURLFetcherMock(id, url, request_type, d, traffic_annotation)); |
| 79 } | 83 } |
| 80 }; | 84 }; |
| 81 | 85 |
| 82 class MockApiCallFlow : public OAuth2ApiCallFlow { | 86 class MockApiCallFlow : public OAuth2ApiCallFlow { |
| 83 public: | 87 public: |
| 84 MockApiCallFlow() {} | 88 MockApiCallFlow() {} |
| 85 ~MockApiCallFlow() {} | 89 ~MockApiCallFlow() {} |
| 86 | 90 |
| 87 MOCK_METHOD0(CreateApiCallUrl, GURL ()); | 91 MOCK_METHOD0(CreateApiCallUrl, GURL ()); |
| 88 MOCK_METHOD0(CreateApiCallBody, std::string ()); | 92 MOCK_METHOD0(CreateApiCallBody, std::string ()); |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 return url_fetcher; | 124 return url_fetcher; |
| 121 } | 125 } |
| 122 | 126 |
| 123 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { | 127 TestURLFetcher* SetupApiCall(bool succeeds, net::HttpStatusCode status) { |
| 124 std::string body(CreateBody()); | 128 std::string body(CreateBody()); |
| 125 GURL url(CreateApiUrl()); | 129 GURL url(CreateApiUrl()); |
| 126 EXPECT_CALL(flow_, CreateApiCallBody()).WillOnce(Return(body)); | 130 EXPECT_CALL(flow_, CreateApiCallBody()).WillOnce(Return(body)); |
| 127 EXPECT_CALL(flow_, CreateApiCallUrl()).WillOnce(Return(url)); | 131 EXPECT_CALL(flow_, CreateApiCallUrl()).WillOnce(Return(url)); |
| 128 TestURLFetcher* url_fetcher = | 132 TestURLFetcher* url_fetcher = |
| 129 CreateURLFetcher(url, succeeds, status, std::string()); | 133 CreateURLFetcher(url, succeeds, status, std::string()); |
| 130 EXPECT_CALL(factory_, CreateURLFetcherMock(_, url, _, _)) | 134 EXPECT_CALL(factory_, CreateURLFetcherMock(_, url, _, _, _)) |
| 131 .WillOnce(Return(url_fetcher)); | 135 .WillOnce(Return(url_fetcher)); |
| 132 return url_fetcher; | 136 return url_fetcher; |
| 133 } | 137 } |
| 134 | 138 |
| 135 base::MessageLoop message_loop_; | 139 base::MessageLoop message_loop_; |
| 136 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; | 140 scoped_refptr<net::TestURLRequestContextGetter> request_context_getter_; |
| 137 StrictMock<MockApiCallFlow> flow_; | 141 StrictMock<MockApiCallFlow> flow_; |
| 138 MockUrlFetcherFactory factory_; | 142 MockUrlFetcherFactory factory_; |
| 139 }; | 143 }; |
| 140 | 144 |
| (...skipping 25 matching lines...) Expand all Loading... |
| 166 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); | 170 TestURLFetcher* url_fetcher = SetupApiCall(true, net::HTTP_OK); |
| 167 flow_.Start(request_context_getter_.get(), kAccessToken); | 171 flow_.Start(request_context_getter_.get(), kAccessToken); |
| 168 HttpRequestHeaders headers; | 172 HttpRequestHeaders headers; |
| 169 url_fetcher->GetExtraRequestHeaders(&headers); | 173 url_fetcher->GetExtraRequestHeaders(&headers); |
| 170 std::string auth_header; | 174 std::string auth_header; |
| 171 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); | 175 EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header)); |
| 172 EXPECT_EQ("Bearer access_token", auth_header); | 176 EXPECT_EQ("Bearer access_token", auth_header); |
| 173 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); | 177 EXPECT_EQ(url, url_fetcher->GetOriginalURL()); |
| 174 EXPECT_EQ(body, url_fetcher->upload_data()); | 178 EXPECT_EQ(body, url_fetcher->upload_data()); |
| 175 } | 179 } |
| OLD | NEW |