@@ -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,48 +45,60 @@ 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+ 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" ))));
4853}
4954
50- TEST (Credentials, AuthenticationHeaderJoinedSuccess ) {
55+ TEST (Credentials, AuthenticationHeaderError ) {
5156 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" ));
57+ EXPECT_CALL (mock, GetToken).WillOnce (Return (UnavailableError (" try-again" )));
58+ auto actual = mock.AuthenticationHeaders (std::chrono::system_clock::now (),
59+ " my-endpoint" );
60+ EXPECT_EQ (actual.status (), UnavailableError (" try-again" ));
5861}
5962
60- TEST (Credentials, AuthenticationHeaderJoinedEmpty ) {
63+ TEST (Credentials, ProjectId ) {
6164 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 ()));
65+ EXPECT_THAT (mock.project_id (), Not (IsOk ()));
66+ EXPECT_THAT (mock.project_id ({}), Not (IsOk ()));
6867}
6968
70- TEST (Credentials, AuthenticationHeaderError ) {
69+ TEST (Credentials, AllowedLocationsSuccess ) {
7170 MockCredentials mock;
72- EXPECT_CALL (mock, GetToken).WillOnce (Return (UnavailableError (" try-again" )));
73- auto actual = mock.AuthenticationHeader (std::chrono::system_clock::now ());
74- EXPECT_EQ (actual.status (), UnavailableError (" try-again" ));
75- }
71+ auto const now = std::chrono::system_clock::now ();
72+ auto const expiration = now + std::chrono::seconds (3600 );
73+ EXPECT_CALL (mock, GetToken)
74+ .WillOnce (Return (AccessToken{" test-token" , expiration}));
75+ EXPECT_CALL (mock, AllowedLocations)
76+ .WillOnce (Return (
77+ rest_internal::HttpHeader (" x-allowed-locations" , " my-location" )));
7678
77- TEST (Credentials, AuthenticationHeaderJoinedError) {
78- 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" ));
79+ auto auth_headers = mock.AuthenticationHeaders (
80+ std::chrono::system_clock::now (), " my-endpoint" );
81+ EXPECT_THAT (
82+ auth_headers,
83+ IsOkAndHolds (::testing::ElementsAre (
84+ rest_internal::HttpHeader (" authorization" , " Bearer test-token" ),
85+ rest_internal::HttpHeader (" x-allowed-locations" , " my-location" ))));
8286}
8387
84- TEST (Credentials, ProjectId ) {
88+ TEST (Credentials, AllowedLocationsFailure ) {
8589 MockCredentials mock;
86- EXPECT_THAT (mock.project_id (), Not (IsOk ()));
87- EXPECT_THAT (mock.project_id ({}), Not (IsOk ()));
90+ auto const now = std::chrono::system_clock::now ();
91+ auto const expiration = now + std::chrono::seconds (3600 );
92+ EXPECT_CALL (mock, GetToken)
93+ .WillOnce (Return (AccessToken{" test-token" , expiration}));
94+ EXPECT_CALL (mock, AllowedLocations)
95+ .WillOnce (Return (internal::DeadlineExceededError (" RPC took too long" )));
96+
97+ auto auth_headers = mock.AuthenticationHeaders (
98+ std::chrono::system_clock::now (), " my-endpoint" );
99+ EXPECT_THAT (auth_headers,
100+ IsOkAndHolds (::testing::ElementsAre (rest_internal::HttpHeader (
101+ " authorization" , " Bearer test-token" ))));
88102}
89103
90104} // namespace
0 commit comments