From 88d2bb8b2b7d1786d299ff5cf43cf87c273e5fd5 Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Sat, 7 Jan 2023 20:09:07 +0400 Subject: [PATCH] chore: Use empty headers when there is no value in builder. --- .../java/io/github/nstdio/http/ext/Headers.java | 2 +- .../github/nstdio/http/ext/HttpHeadersBuilder.java | 5 +++-- .../nstdio/http/ext/HttpHeadersBuilderTest.kt | 13 +++++++++++++ 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main/java/io/github/nstdio/http/ext/Headers.java b/src/main/java/io/github/nstdio/http/ext/Headers.java index 26efd90..3589ee1 100644 --- a/src/main/java/io/github/nstdio/http/ext/Headers.java +++ b/src/main/java/io/github/nstdio/http/ext/Headers.java @@ -51,7 +51,7 @@ class Headers { static final String HEADER_LAST_MODIFIED = "Last-Modified"; static final String HEADER_WARNING = "Warning"; static final BiPredicate ALLOW_ALL = (s, s2) -> true; - private static final HttpHeaders EMPTY_HEADERS = HttpHeaders.of(Map.of(), ALLOW_ALL); + static final HttpHeaders EMPTY_HEADERS = HttpHeaders.of(Map.of(), ALLOW_ALL); private static final DateTimeFormatter ASCTIME_DATE_TIME = new DateTimeFormatterBuilder() .appendPattern("EEE MMM") .appendLiteral(' ') diff --git a/src/main/java/io/github/nstdio/http/ext/HttpHeadersBuilder.java b/src/main/java/io/github/nstdio/http/ext/HttpHeadersBuilder.java index 5a1b546..52b1813 100644 --- a/src/main/java/io/github/nstdio/http/ext/HttpHeadersBuilder.java +++ b/src/main/java/io/github/nstdio/http/ext/HttpHeadersBuilder.java @@ -24,6 +24,7 @@ import java.util.function.BiPredicate; import static io.github.nstdio.http.ext.Headers.ALLOW_ALL; +import static io.github.nstdio.http.ext.Headers.EMPTY_HEADERS; class HttpHeadersBuilder { private final TreeMap> headersMap; @@ -47,7 +48,7 @@ private void copyTo(HttpHeadersBuilder builder, Map> source private List values(String name, int capacity) { return headersMap.computeIfAbsent(name, k -> new ArrayList<>(capacity)); } - + private List values(String name) { return values(name, 1); } @@ -118,7 +119,7 @@ HttpHeaders build() { } HttpHeaders build(BiPredicate filter) { - return HttpHeaders.of(headersMap, filter); + return headersMap.isEmpty() ? EMPTY_HEADERS : HttpHeaders.of(headersMap, filter); } @Override diff --git a/src/test/kotlin/io/github/nstdio/http/ext/HttpHeadersBuilderTest.kt b/src/test/kotlin/io/github/nstdio/http/ext/HttpHeadersBuilderTest.kt index d2b02e8..2a7a8b1 100644 --- a/src/test/kotlin/io/github/nstdio/http/ext/HttpHeadersBuilderTest.kt +++ b/src/test/kotlin/io/github/nstdio/http/ext/HttpHeadersBuilderTest.kt @@ -16,6 +16,8 @@ package io.github.nstdio.http.ext import io.github.nstdio.http.ext.Assertions.assertThat +import io.kotest.matchers.maps.shouldBeEmpty +import io.kotest.matchers.types.shouldBeSameInstanceAs import org.junit.jupiter.api.BeforeEach import org.junit.jupiter.api.Test @@ -126,4 +128,15 @@ internal class HttpHeadersBuilderTest { assertThat(builder.build()) .isEmpty() } + + @Test + fun `Should not create new empty instances`() { + //when + val h1 = builder.build() + val h2 = builder.build() + + //then + h1 shouldBeSameInstanceAs h2 + h1.map().shouldBeEmpty() + } } \ No newline at end of file