|
| 1 | +// Copyright 2025 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/http_header.h" |
| 16 | +#include <gmock/gmock.h> |
| 17 | + |
| 18 | +namespace google { |
| 19 | +namespace cloud { |
| 20 | +namespace rest_internal { |
| 21 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 22 | +namespace { |
| 23 | + |
| 24 | +using ::testing::Eq; |
| 25 | + |
| 26 | +TEST(HttpHeader, ConstructionAndStringFormatting) { |
| 27 | + HttpHeader empty; |
| 28 | + EXPECT_THAT(std::string(empty), Eq("")); |
| 29 | + |
| 30 | + HttpHeader no_value("key"); |
| 31 | + EXPECT_THAT(std::string(no_value), Eq("key:")); |
| 32 | + |
| 33 | + HttpHeader single_value("key", "value"); |
| 34 | + EXPECT_THAT(std::string(single_value), Eq("key: value")); |
| 35 | + |
| 36 | + HttpHeader multi_value("key", std::vector<std::string>{"value1", "value2"}); |
| 37 | + EXPECT_THAT(std::string(multi_value), Eq("key: value1,value2")); |
| 38 | + HttpHeader multi_literal_value("key", {"value1", "value2"}); |
| 39 | + EXPECT_THAT(std::string(multi_literal_value), Eq("key: value1,value2")); |
| 40 | +} |
| 41 | + |
| 42 | +TEST(HttpHeader, Equality) { |
| 43 | + HttpHeader empty; |
| 44 | + // Key field tests |
| 45 | + EXPECT_TRUE(empty == empty); |
| 46 | + EXPECT_FALSE(empty != empty); |
| 47 | + EXPECT_FALSE(empty == HttpHeader("key")); |
| 48 | + EXPECT_FALSE(HttpHeader("key") == empty); |
| 49 | + EXPECT_TRUE(HttpHeader("Key") == HttpHeader("key")); |
| 50 | + EXPECT_TRUE(HttpHeader("key") == HttpHeader("Key")); |
| 51 | + EXPECT_TRUE(HttpHeader("Content-Length") == HttpHeader("content-length")); |
| 52 | + |
| 53 | + // Values field tests |
| 54 | + EXPECT_TRUE(HttpHeader("key", "value") == HttpHeader("key", "value")); |
| 55 | + EXPECT_FALSE(HttpHeader("key", "value") == HttpHeader("key", "Value")); |
| 56 | + EXPECT_FALSE(HttpHeader("key", {"v1", "v2"}) == HttpHeader("Key", "v1")); |
| 57 | + EXPECT_FALSE(HttpHeader("key", {"v1", "v2"}) == HttpHeader("Key", {"v1"})); |
| 58 | + EXPECT_FALSE(HttpHeader("key", {"v1", "v2"}) == |
| 59 | + HttpHeader("Key", {"v1", "V2"})); |
| 60 | + EXPECT_FALSE(HttpHeader("key", {"V1", "v2"}) == |
| 61 | + HttpHeader("Key", {"v1", "V2"})); |
| 62 | + EXPECT_TRUE(HttpHeader("key", {"v1", "v2"}) == |
| 63 | + HttpHeader("Key", {"v1", "v2"})); |
| 64 | + EXPECT_FALSE(HttpHeader("key", {"v1", "v2"}) == |
| 65 | + HttpHeader("Key", {"v2", "v1"})); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(HttpHeader, LessThan) { |
| 69 | + EXPECT_TRUE(HttpHeader("hey") < HttpHeader("key")); |
| 70 | + EXPECT_FALSE(HttpHeader("key") < HttpHeader("key")); |
| 71 | + EXPECT_FALSE(HttpHeader("key") < HttpHeader("hey")); |
| 72 | + EXPECT_TRUE(HttpHeader("Hey") < HttpHeader("key")); |
| 73 | + EXPECT_FALSE(HttpHeader("key") < HttpHeader("Key")); |
| 74 | + EXPECT_FALSE(HttpHeader("key") < HttpHeader("Hey")); |
| 75 | +} |
| 76 | + |
| 77 | +TEST(HttpHeader, IsSameKey) { |
| 78 | + EXPECT_TRUE(HttpHeader("key").IsSameKey("key")); |
| 79 | + EXPECT_TRUE(HttpHeader("Key").IsSameKey("key")); |
| 80 | + EXPECT_TRUE(HttpHeader("Key").IsSameKey("Key")); |
| 81 | + EXPECT_FALSE(HttpHeader("Key").IsSameKey("ey")); |
| 82 | + |
| 83 | + EXPECT_TRUE(HttpHeader("key").IsSameKey(HttpHeader("key"))); |
| 84 | + EXPECT_TRUE(HttpHeader("Key").IsSameKey(HttpHeader("key"))); |
| 85 | + EXPECT_TRUE(HttpHeader("Key").IsSameKey(HttpHeader("Key"))); |
| 86 | + EXPECT_FALSE(HttpHeader("Key").IsSameKey(HttpHeader("ey"))); |
| 87 | +} |
| 88 | + |
| 89 | +TEST(HttpHeader, DebugString) { |
| 90 | + HttpHeader empty; |
| 91 | + EXPECT_THAT(empty.DebugString(), Eq("")); |
| 92 | + |
| 93 | + HttpHeader no_value("key"); |
| 94 | + EXPECT_THAT(no_value.DebugString(), Eq("key:")); |
| 95 | + |
| 96 | + HttpHeader short_value("key", "short"); |
| 97 | + EXPECT_THAT(short_value.DebugString(), Eq("key: short")); |
| 98 | + |
| 99 | + HttpHeader long_value("key", "valuelongerthantruncatelength"); |
| 100 | + EXPECT_THAT(long_value.DebugString(), Eq("key: valuelonge")); |
| 101 | +} |
| 102 | + |
| 103 | +TEST(HttpHeader, MergeHeader) { |
| 104 | + HttpHeader k1_v1("k1", "k1-value1"); |
| 105 | + HttpHeader k2_v1("k2", "k2-value1"); |
| 106 | + EXPECT_THAT(k1_v1.MergeHeader(k2_v1), Eq(HttpHeader("k1", "k1-value1"))); |
| 107 | + EXPECT_THAT(k1_v1.MergeHeader(std::move(k2_v1)), |
| 108 | + Eq(HttpHeader("k1", "k1-value1"))); |
| 109 | + |
| 110 | + HttpHeader k1_v2("k1", "k1-value2"); |
| 111 | + EXPECT_THAT(k1_v1.MergeHeader(k1_v2), |
| 112 | + Eq(HttpHeader("k1", {"k1-value1", "k1-value2"}))); |
| 113 | + EXPECT_THAT(k1_v2, Eq(HttpHeader("k1", "k1-value2"))); |
| 114 | + k1_v1 = HttpHeader("k1", "k1-value1"); |
| 115 | + EXPECT_THAT(k1_v1.MergeHeader(std::move(k1_v2)), |
| 116 | + Eq(HttpHeader("k1", {"k1-value1", "k1-value2"}))); |
| 117 | + |
| 118 | + HttpHeader k1_v3("k1", "k1-value3"); |
| 119 | + k1_v1 = HttpHeader("k1", {"k1-value1"}); |
| 120 | + EXPECT_THAT(k1_v3.MergeHeader(k1_v1), |
| 121 | + Eq(HttpHeader("k1", {"k1-value3", "k1-value1"}))); |
| 122 | + k1_v3 = HttpHeader("k1", "k1-value3"); |
| 123 | + EXPECT_THAT(k1_v3.MergeHeader(std::move(k1_v1)), |
| 124 | + Eq(HttpHeader("k1", {"k1-value3", "k1-value1"}))); |
| 125 | + |
| 126 | + HttpHeader k3_v1("k3", "k3-value10"); |
| 127 | + HttpHeader k3_v5("k3", "k3-value5"); |
| 128 | + EXPECT_THAT(k3_v1.MergeHeader(HttpHeader("k3", {"k3-value2", "k3-value3"})) |
| 129 | + .MergeHeader(k3_v5) |
| 130 | + .MergeHeader((std::move(k3_v5))), |
| 131 | + Eq(HttpHeader("k3", {"k3-value10", "k3-value2", "k3-value3", |
| 132 | + "k3-value5", "k3-value5"}))); |
| 133 | +} |
| 134 | + |
| 135 | +} // namespace |
| 136 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 137 | +} // namespace rest_internal |
| 138 | +} // namespace cloud |
| 139 | +} // namespace google |
0 commit comments