@@ -26,15 +26,17 @@ namespace {
2626using ::google::cloud::internal::UnavailableError;
2727using ::google::cloud::testing_util::IsOk;
2828using ::google::cloud::testing_util::IsOkAndHolds;
29- using ::testing::IsEmpty ;
29+ using ::testing::Contains ;
3030using ::testing::Not;
31- using ::testing::Pair;
3231using ::testing::Return;
3332
3433class MockCredentials : public Credentials {
3534 public:
3635 MOCK_METHOD (StatusOr<AccessToken>, GetToken,
3736 (std::chrono::system_clock::time_point), (override ));
37+ MOCK_METHOD (StatusOr<rest_internal::HttpHeader>, AllowedLocations,
38+ (std::chrono::system_clock::time_point, std::string_view),
39+ (override ));
3840};
3941
4042TEST (Credentials, AuthorizationHeaderSuccess) {
@@ -43,42 +45,30 @@ TEST(Credentials, AuthorizationHeaderSuccess) {
4345 auto const expiration = now + std::chrono::seconds (3600 );
4446 EXPECT_CALL (mock, GetToken (now))
4547 .WillOnce (Return (AccessToken{" test-token" , expiration}));
46- auto actual = mock.AuthenticationHeader (now);
47- EXPECT_THAT (actual, IsOkAndHolds (Pair (" Authorization" , " Bearer test-token" )));
48- }
49-
50- TEST (Credentials, AuthenticationHeaderJoinedSuccess) {
51- MockCredentials mock;
52- auto const now = std::chrono::system_clock::now ();
53- auto const expiration = now + std::chrono::seconds (3600 );
54- EXPECT_CALL (mock, GetToken (now))
55- .WillOnce (Return (AccessToken{" test-token" , expiration}));
56- auto actual = AuthenticationHeaderJoined (mock, now);
57- EXPECT_THAT (actual, IsOkAndHolds (" Authorization: Bearer test-token" ));
58- }
59-
60- TEST (Credentials, AuthenticationHeaderJoinedEmpty) {
61- MockCredentials mock;
62- auto const now = std::chrono::system_clock::now ();
63- auto const expiration = now + std::chrono::seconds (3600 );
64- EXPECT_CALL (mock, GetToken (now))
65- .WillOnce (Return (AccessToken{" " , expiration}));
66- auto actual = AuthenticationHeaderJoined (mock, now);
67- EXPECT_THAT (actual, IsOkAndHolds (IsEmpty ()));
48+ EXPECT_CALL (mock, AllowedLocations)
49+ .WillOnce (Return (rest_internal::HttpHeader{}));
50+ auto actual = mock.AuthenticationHeaders (now, " my-endpoint" );
51+ EXPECT_THAT (actual, IsOkAndHolds (Contains (rest_internal::HttpHeader (
52+ " authorization" , " Bearer test-token" ))));
6853}
6954
7055TEST (Credentials, AuthenticationHeaderError) {
7156 MockCredentials mock;
7257 EXPECT_CALL (mock, GetToken).WillOnce (Return (UnavailableError (" try-again" )));
73- auto actual = mock.AuthenticationHeader (std::chrono::system_clock::now ());
58+ auto actual = mock.AuthenticationHeaders (std::chrono::system_clock::now (),
59+ " my-endpoint" );
7460 EXPECT_EQ (actual.status (), UnavailableError (" try-again" ));
7561}
7662
77- TEST (Credentials, AuthenticationHeaderJoinedError ) {
63+ TEST (Credentials, AuthenticationHeaderEmpty ) {
7864 MockCredentials mock;
79- EXPECT_CALL (mock, GetToken).WillOnce (Return (UnavailableError (" try-again" )));
80- auto actual = AuthenticationHeaderJoined (mock);
81- EXPECT_EQ (actual.status (), UnavailableError (" try-again" ));
65+ EXPECT_CALL (mock, GetToken)
66+ .WillOnce (Return (AccessToken{" " , std::chrono::system_clock::now ()}));
67+ EXPECT_CALL (mock, AllowedLocations)
68+ .WillOnce (Return (rest_internal::HttpHeader ()));
69+ auto actual = mock.AuthenticationHeaders (std::chrono::system_clock::now (),
70+ " my-endpoint" );
71+ EXPECT_THAT (actual, IsOkAndHolds (std::vector<rest_internal::HttpHeader>{}));
8272}
8373
8474TEST (Credentials, ProjectId) {
@@ -87,6 +77,41 @@ TEST(Credentials, ProjectId) {
8777 EXPECT_THAT (mock.project_id ({}), Not (IsOk ()));
8878}
8979
80+ TEST (Credentials, AllowedLocationsSuccess) {
81+ MockCredentials mock;
82+ auto const now = std::chrono::system_clock::now ();
83+ auto const expiration = now + std::chrono::seconds (3600 );
84+ EXPECT_CALL (mock, GetToken)
85+ .WillOnce (Return (AccessToken{" test-token" , expiration}));
86+ EXPECT_CALL (mock, AllowedLocations)
87+ .WillOnce (Return (
88+ rest_internal::HttpHeader (" x-allowed-locations" , " my-location" )));
89+
90+ auto auth_headers = mock.AuthenticationHeaders (
91+ std::chrono::system_clock::now (), " my-endpoint" );
92+ EXPECT_THAT (
93+ auth_headers,
94+ IsOkAndHolds (::testing::ElementsAre (
95+ rest_internal::HttpHeader (" authorization" , " Bearer test-token" ),
96+ rest_internal::HttpHeader (" x-allowed-locations" , " my-location" ))));
97+ }
98+
99+ TEST (Credentials, AllowedLocationsFailure) {
100+ MockCredentials mock;
101+ auto const now = std::chrono::system_clock::now ();
102+ auto const expiration = now + std::chrono::seconds (3600 );
103+ EXPECT_CALL (mock, GetToken)
104+ .WillOnce (Return (AccessToken{" test-token" , expiration}));
105+ EXPECT_CALL (mock, AllowedLocations)
106+ .WillOnce (Return (internal::DeadlineExceededError (" RPC took too long" )));
107+
108+ auto auth_headers = mock.AuthenticationHeaders (
109+ std::chrono::system_clock::now (), " my-endpoint" );
110+ EXPECT_THAT (auth_headers,
111+ IsOkAndHolds (::testing::ElementsAre (rest_internal::HttpHeader (
112+ " authorization" , " Bearer test-token" ))));
113+ }
114+
90115} // namespace
91116GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END
92117} // namespace oauth2_internal
0 commit comments