From 125560b3a7ed29a17514fcec0804a80337bbf176 Mon Sep 17 00:00:00 2001 From: Edgar Asatryan Date: Thu, 31 Mar 2022 22:40:54 +0400 Subject: [PATCH] chore(test): More tests for HttpHeadersBuilder. --- .../nstdio/http/ext/HttpHeadersBuilder.java | 10 ++- .../io/github/nstdio/http/ext/Assertions.kt | 5 ++ .../nstdio/http/ext/HttpHeadersBuilderTest.kt | 87 +++++++++++++++++++ 3 files changed, 100 insertions(+), 2 deletions(-) create mode 100644 src/test/kotlin/io/github/nstdio/http/ext/HttpHeadersBuilderTest.kt 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 ccbb29c..f18cca9 100644 --- a/src/main/java/io/github/nstdio/http/ext/HttpHeadersBuilder.java +++ b/src/main/java/io/github/nstdio/http/ext/HttpHeadersBuilder.java @@ -20,8 +20,10 @@ import java.util.List; import java.util.Map; import java.util.TreeMap; +import java.util.function.BiPredicate; class HttpHeadersBuilder { + private static final BiPredicate ALWAYS_ALLOW = (s, s2) -> true; private final TreeMap> headersMap; HttpHeadersBuilder() { @@ -71,7 +73,7 @@ HttpHeadersBuilder set(String name, List value) { HttpHeadersBuilder remove(String name, String value) { List values = headersMap.get(name); - if (value != null) { + if (values != null) { values.remove(value); if (values.isEmpty()) { headersMap.remove(name); @@ -86,7 +88,11 @@ HttpHeadersBuilder remove(String name) { } HttpHeaders build() { - return HttpHeaders.of(headersMap, (s, s2) -> true); + return build(ALWAYS_ALLOW); + } + + HttpHeaders build(BiPredicate filter) { + return HttpHeaders.of(headersMap, filter); } @Override diff --git a/src/test/kotlin/io/github/nstdio/http/ext/Assertions.kt b/src/test/kotlin/io/github/nstdio/http/ext/Assertions.kt index 7569699..5470286 100644 --- a/src/test/kotlin/io/github/nstdio/http/ext/Assertions.kt +++ b/src/test/kotlin/io/github/nstdio/http/ext/Assertions.kt @@ -96,6 +96,11 @@ object Assertions { return this } + fun isEmpty(): HttpHeadersAssertion { + assertThat(actual!!.map()).isEmpty() + return this + } + fun hasHeaderWithOnlyValue(header: String?, value: String?): HttpHeadersAssertion { assertThat(actual!!.allValues(header)) .containsExactly(value) diff --git a/src/test/kotlin/io/github/nstdio/http/ext/HttpHeadersBuilderTest.kt b/src/test/kotlin/io/github/nstdio/http/ext/HttpHeadersBuilderTest.kt new file mode 100644 index 0000000..3ea4ce6 --- /dev/null +++ b/src/test/kotlin/io/github/nstdio/http/ext/HttpHeadersBuilderTest.kt @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2022 Edgar Asatryan + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package io.github.nstdio.http.ext + +import io.github.nstdio.http.ext.Assertions.assertThat +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test + +internal class HttpHeadersBuilderTest { + private lateinit var builder: HttpHeadersBuilder + + @BeforeEach + fun setUp() { + builder = HttpHeadersBuilder() + } + + @Test + fun shouldSetHeader() { + //when + builder.add("abc", "1") + builder.set("abc", "2") + + //then + assertThat(builder.build()) + .hasHeaderWithOnlyValue("abc", "2") + } + + @Test + fun `Should remove header single value`() { + //when + builder.add("abc", "1") + builder.add("abc", "2") + builder.remove("abc", "1") + + //then + assertThat(builder.build()) + .hasHeaderWithOnlyValue("abc", "2") + } + + @Test + fun `Should safely remove not existing header`() { + //when + builder.remove("abc", "1") + + //then + assertThat(builder.build()) + .isEmpty() + } + + @Test + fun `Should remove all entry if all removed single`() { + //when + builder.add("abc", "1") + builder.remove("abc", "1") + + //then + assertThat(builder.build()) + .isEmpty() + } + + @Test + fun `Should remove all entry if all removed`() { + //when + builder.add("abc", "1") + builder.add("abc", "2") + builder.add("abcd", "2") + builder.remove("abc") + + //then + assertThat(builder.build()) + .hasNoHeader("abc") + .hasHeaderWithOnlyValue("abcd", "2") + } +} \ No newline at end of file