|
29 | 29 | */ |
30 | 30 | package com.google.api.gax.httpjson; |
31 | 31 |
|
| 32 | +import static com.google.common.truth.Truth.assertThat; |
32 | 33 | import static org.junit.jupiter.api.Assertions.assertEquals; |
33 | 34 | import static org.junit.jupiter.api.Assertions.assertNull; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
34 | 36 |
|
35 | 37 | import com.google.api.client.http.HttpHeaders; |
36 | 38 | import com.google.common.collect.ImmutableList; |
37 | 39 | import com.google.common.collect.ImmutableMap; |
| 40 | +import java.util.Collections; |
| 41 | +import java.util.HashMap; |
38 | 42 | import java.util.Map; |
39 | 43 | import org.junit.jupiter.api.Test; |
40 | 44 |
|
@@ -130,4 +134,56 @@ void testGetUserAgentValue() { |
130 | 134 | headersMap = ImmutableMap.of("Custom-Header", "CustomHeader", "accept", "Accept"); |
131 | 135 | assertNull(HttpHeadersUtils.getUserAgentValue(headersMap)); |
132 | 136 | } |
| 137 | + |
| 138 | + @Test |
| 139 | + void getSingleHeader_success() { |
| 140 | + Map<String, Object> headers = |
| 141 | + ImmutableMap.of( |
| 142 | + "X-Goog-Upload-URL", |
| 143 | + "https://test.googleapis.com/upload", |
| 144 | + "Location", |
| 145 | + ImmutableList.of("https://redirect.url"), |
| 146 | + "Content-Length", |
| 147 | + 1024L); |
| 148 | + |
| 149 | + // Case-insensitive & exact lookup |
| 150 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "X-Goog-Upload-URL")) |
| 151 | + .isEqualTo("https://test.googleapis.com/upload"); |
| 152 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "x-goog-upload-url")) |
| 153 | + .isEqualTo("https://test.googleapis.com/upload"); |
| 154 | + |
| 155 | + // Single-element iterable |
| 156 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "location")) |
| 157 | + .isEqualTo("https://redirect.url"); |
| 158 | + |
| 159 | + // Non-string object conversion |
| 160 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "content-length")).isEqualTo("1024"); |
| 161 | + } |
| 162 | + |
| 163 | + @Test |
| 164 | + void getSingleHeader_multipleElements_throwsIllegalArgumentException() { |
| 165 | + Map<String, Object> headers = |
| 166 | + ImmutableMap.of( |
| 167 | + "Location", ImmutableList.of("https://redirect.url", "https://secondary.url")); |
| 168 | + |
| 169 | + assertThrows( |
| 170 | + IllegalArgumentException.class, |
| 171 | + () -> HttpHeadersUtils.getSingleHeader(headers, "location")); |
| 172 | + } |
| 173 | + |
| 174 | + @Test |
| 175 | + void getSingleHeader_missingOrEmpty_returnsNull() { |
| 176 | + Map<String, Object> headers = new HashMap<>(); |
| 177 | + headers.put("Existing-Header", "value"); |
| 178 | + headers.put("Null-Value-Header", null); |
| 179 | + headers.put("Empty-List-Header", Collections.emptyList()); |
| 180 | + headers.put("Null-First-Header", Collections.singletonList(null)); |
| 181 | + headers.put(null, "some-value"); |
| 182 | + |
| 183 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "non-existent-header")).isNull(); |
| 184 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "null-value-header")).isNull(); |
| 185 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "empty-list-header")).isNull(); |
| 186 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "null-first-header")).isNull(); |
| 187 | + assertThat(HttpHeadersUtils.getSingleHeader(headers, "any-header")).isNull(); |
| 188 | + } |
133 | 189 | } |
0 commit comments