oauth2_api_call_flow_unittest.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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.
  6. #include "google_apis/gaia/oauth2_api_call_flow.h"
  7. #include <memory>
  8. #include <string>
  9. #include <utility>
  10. #include "base/run_loop.h"
  11. #include "base/test/task_environment.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 "net/base/net_errors.h"
  18. #include "net/http/http_request_headers.h"
  19. #include "net/http/http_status_code.h"
  20. #include "net/traffic_annotation/network_traffic_annotation_test_helper.h"
  21. #include "services/network/public/cpp/weak_wrapper_shared_url_loader_factory.h"
  22. #include "services/network/public/mojom/url_response_head.mojom.h"
  23. #include "services/network/test/test_url_loader_factory.h"
  24. #include "services/network/test/test_utils.h"
  25. #include "testing/gmock/include/gmock/gmock.h"
  26. #include "testing/gtest/include/gtest/gtest.h"
  27. using net::HttpRequestHeaders;
  28. using testing::_;
  29. using testing::ByMove;
  30. using testing::Return;
  31. using testing::StrictMock;
  32. namespace {
  33. const char kAccessToken[] = "access_token";
  34. static std::string CreateBody() {
  35. return "some body";
  36. }
  37. static GURL CreateApiUrl() {
  38. return GURL("https://www.googleapis.com/someapi");
  39. }
  40. class MockApiCallFlow : public OAuth2ApiCallFlow {
  41. public:
  42. MockApiCallFlow() {}
  43. ~MockApiCallFlow() override {}
  44. MOCK_METHOD0(CreateApiCallUrl, GURL());
  45. MOCK_METHOD0(CreateApiCallBody, std::string());
  46. MOCK_METHOD0(CreateApiCallHeaders, net::HttpRequestHeaders());
  47. MOCK_METHOD2(ProcessApiCallSuccess,
  48. void(const network::mojom::URLResponseHead* head,
  49. std::unique_ptr<std::string> body));
  50. MOCK_METHOD3(ProcessApiCallFailure,
  51. void(int net_error,
  52. const network::mojom::URLResponseHead* head,
  53. std::unique_ptr<std::string> body));
  54. MOCK_METHOD1(ProcessNewAccessToken, void(const std::string& access_token));
  55. MOCK_METHOD1(ProcessMintAccessTokenFailure,
  56. void(const GoogleServiceAuthError& error));
  57. net::PartialNetworkTrafficAnnotationTag GetNetworkTrafficAnnotationTag()
  58. override {
  59. return PARTIAL_TRAFFIC_ANNOTATION_FOR_TESTS;
  60. }
  61. };
  62. } // namespace
  63. class OAuth2ApiCallFlowTest : public testing::Test {
  64. protected:
  65. OAuth2ApiCallFlowTest()
  66. : shared_factory_(
  67. base::MakeRefCounted<network::WeakWrapperSharedURLLoaderFactory>(
  68. &test_url_loader_factory_)) {}
  69. void AddFetchResult(const GURL& url,
  70. bool fetch_succeeds,
  71. net::HttpStatusCode response_code,
  72. const std::string& body) {
  73. net::Error error = fetch_succeeds ? net::OK : net::ERR_FAILED;
  74. auto http_head = network::CreateURLResponseHead(response_code);
  75. test_url_loader_factory_.AddResponse(
  76. url, std::move(http_head), body,
  77. network::URLLoaderCompletionStatus(error));
  78. }
  79. protected:
  80. void SetupApiCall(bool succeeds, net::HttpStatusCode status) {
  81. std::string body(CreateBody());
  82. GURL url(CreateApiUrl());
  83. EXPECT_CALL(flow_, CreateApiCallBody()).WillOnce(Return(body));
  84. EXPECT_CALL(flow_, CreateApiCallUrl()).WillOnce(Return(url));
  85. EXPECT_CALL(flow_, CreateApiCallHeaders());
  86. AddFetchResult(url, succeeds, status, std::string());
  87. }
  88. base::test::SingleThreadTaskEnvironment task_environment_;
  89. network::TestURLLoaderFactory test_url_loader_factory_;
  90. scoped_refptr<network::SharedURLLoaderFactory> shared_factory_;
  91. StrictMock<MockApiCallFlow> flow_;
  92. };
  93. TEST_F(OAuth2ApiCallFlowTest, ApiCallSucceedsHttpOk) {
  94. SetupApiCall(true, net::HTTP_OK);
  95. EXPECT_CALL(flow_, ProcessApiCallSuccess(_, _));
  96. flow_.Start(shared_factory_, kAccessToken);
  97. base::RunLoop().RunUntilIdle();
  98. }
  99. TEST_F(OAuth2ApiCallFlowTest, ApiCallSucceedsHttpNoContent) {
  100. SetupApiCall(true, net::HTTP_NO_CONTENT);
  101. EXPECT_CALL(flow_, ProcessApiCallSuccess(_, _));
  102. flow_.Start(shared_factory_, kAccessToken);
  103. base::RunLoop().RunUntilIdle();
  104. }
  105. TEST_F(OAuth2ApiCallFlowTest, ApiCallFailure) {
  106. SetupApiCall(true, net::HTTP_UNAUTHORIZED);
  107. EXPECT_CALL(flow_, ProcessApiCallFailure(_, _, _));
  108. flow_.Start(shared_factory_, kAccessToken);
  109. base::RunLoop().RunUntilIdle();
  110. }
  111. TEST_F(OAuth2ApiCallFlowTest, ExpectedHTTPHeaders) {
  112. std::string body = CreateBody();
  113. GURL url(CreateApiUrl());
  114. SetupApiCall(true, net::HTTP_OK);
  115. // ... never mind the HTTP response part of the setup --- don't want
  116. // TestURLLoaderFactory replying to it just yet as it would prevent examining
  117. // the request headers.
  118. test_url_loader_factory_.ClearResponses();
  119. flow_.Start(shared_factory_, kAccessToken);
  120. const std::vector<network::TestURLLoaderFactory::PendingRequest>& pending =
  121. *test_url_loader_factory_.pending_requests();
  122. ASSERT_EQ(1u, pending.size());
  123. EXPECT_EQ(url, pending[0].request.url);
  124. std::string auth_header;
  125. EXPECT_TRUE(
  126. pending[0].request.headers.GetHeader("Authorization", &auth_header));
  127. EXPECT_EQ("Bearer access_token", auth_header);
  128. EXPECT_EQ(body, network::GetUploadData(pending[0].request));
  129. }
  130. net::HttpRequestHeaders CreateHeaders() {
  131. net::HttpRequestHeaders headers;
  132. headers.SetHeader("Test-Header-Field", "test content");
  133. return headers;
  134. }
  135. TEST_F(OAuth2ApiCallFlowTest, ExpectedMultipleHTTPHeaders) {
  136. std::string body = CreateBody();
  137. GURL url(CreateApiUrl());
  138. SetupApiCall(true, net::HTTP_OK);
  139. // Overwrite EXPECT_CALL default return so that we get multiple headers.
  140. ON_CALL(flow_, CreateApiCallHeaders).WillByDefault(Return(CreateHeaders()));
  141. // ... never mind the HTTP response part of the setup --- don't want
  142. // TestURLLoaderFactory replying to it just yet as it would prevent examining
  143. // the request headers.
  144. test_url_loader_factory_.ClearResponses();
  145. flow_.Start(shared_factory_, kAccessToken);
  146. const std::vector<network::TestURLLoaderFactory::PendingRequest>& pending =
  147. *test_url_loader_factory_.pending_requests();
  148. ASSERT_EQ(1u, pending.size());
  149. EXPECT_EQ(url, pending[0].request.url);
  150. const auto& headers = pending[0].request.headers;
  151. std::string auth_header;
  152. EXPECT_TRUE(headers.GetHeader("Authorization", &auth_header));
  153. EXPECT_EQ("Bearer access_token", auth_header);
  154. std::string test_header_content;
  155. EXPECT_TRUE(headers.GetHeader("Test-Header-Field", &test_header_content));
  156. EXPECT_EQ("test content", test_header_content);
  157. EXPECT_EQ(body, network::GetUploadData(pending[0].request));
  158. }