Skip to content

Commit

Permalink
chore(test): More tests for HttpHeadersBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Mar 31, 2022
1 parent 8b743c3 commit 125560b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/io/github/nstdio/http/ext/HttpHeadersBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> ALWAYS_ALLOW = (s, s2) -> true;
private final TreeMap<String, List<String>> headersMap;

HttpHeadersBuilder() {
Expand Down Expand Up @@ -71,7 +73,7 @@ HttpHeadersBuilder set(String name, List<String> value) {

HttpHeadersBuilder remove(String name, String value) {
List<String> values = headersMap.get(name);
if (value != null) {
if (values != null) {
values.remove(value);
if (values.isEmpty()) {
headersMap.remove(name);
Expand All @@ -86,7 +88,11 @@ HttpHeadersBuilder remove(String name) {
}

HttpHeaders build() {
return HttpHeaders.of(headersMap, (s, s2) -> true);
return build(ALWAYS_ALLOW);
}

HttpHeaders build(BiPredicate<String, String> filter) {
return HttpHeaders.of(headersMap, filter);
}

@Override
Expand Down
5 changes: 5 additions & 0 deletions src/test/kotlin/io/github/nstdio/http/ext/Assertions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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")
}
}

0 comments on commit 125560b

Please sign in to comment.