|
| 1 | +// Copyright 2024 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/internal/status_utils.h" |
| 16 | +#include "google/cloud/status.h" |
| 17 | +#include <gmock/gmock.h> |
| 18 | + |
| 19 | +namespace google { |
| 20 | +namespace cloud { |
| 21 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 22 | +namespace internal { |
| 23 | +namespace { |
| 24 | + |
| 25 | +TEST(IsClientOriginTest, OriginatesFromClient) { |
| 26 | + Status cases[] = { |
| 27 | + Status(StatusCode::kCancelled, "cancelled + contains origin metadata", |
| 28 | + ErrorInfo("test-only-reasons", "test-only-domain", |
| 29 | + {{"gl-cpp.error.origin", "client"}})), |
| 30 | + Status(StatusCode::kUnknown, |
| 31 | + "unknown + contains origin metadata + other metadata", |
| 32 | + ErrorInfo("test-only-reasons", "test-only-domain", |
| 33 | + { |
| 34 | + {"some-other-key", "random-value"}, |
| 35 | + {"gl-cpp.error.origin", "client"}, |
| 36 | + }))}; |
| 37 | + |
| 38 | + for (auto const& status : cases) { |
| 39 | + SCOPED_TRACE("Testing status: " + StatusCodeToString(status.code()) + |
| 40 | + " - " + status.message()); |
| 41 | + EXPECT_TRUE(IsClientOrigin(status)); |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +TEST(IsClientOriginTest, DoesNotOriginateFromClient) { |
| 46 | + Status cases[] = { |
| 47 | + Status(StatusCode::kAborted, "no metadata"), |
| 48 | + Status(StatusCode::kCancelled, "incorrect origin value", |
| 49 | + ErrorInfo("test-only-reasons", "test-only-domain", |
| 50 | + {{"gl-cpp.error.origin", "server"}})), |
| 51 | + Status(StatusCode::kOk, "incorrect origin value with client prefix", |
| 52 | + ErrorInfo("test-only-reasons", "test-only-domain", |
| 53 | + {{"gl-cpp.error.origin", "client-maybe"}})), |
| 54 | + Status(StatusCode::kUnknown, "does not contain origin value", |
| 55 | + ErrorInfo("test-only-reasons", "test-only-domain", |
| 56 | + { |
| 57 | + {"some-other-key", "random-value"}, |
| 58 | + })), |
| 59 | + Status(StatusCode::kOk, "success status + contains origin metadata", |
| 60 | + ErrorInfo("test-only-reasons", "test-only-domain", |
| 61 | + {{"gl-cpp.error.origin", "client"}}))}; |
| 62 | + |
| 63 | + for (auto const& status : cases) { |
| 64 | + SCOPED_TRACE("Testing status: " + StatusCodeToString(status.code()) + |
| 65 | + " - " + status.message()); |
| 66 | + EXPECT_FALSE(IsClientOrigin(status)); |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +} // namespace |
| 71 | +} // namespace internal |
| 72 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 73 | +} // namespace cloud |
| 74 | +} // namespace google |
0 commit comments