Skip to content

Commit 4404dd7

Browse files
committed
Improve default headers test coverage.
1 parent cfbb458 commit 4404dd7

File tree

1 file changed

+21
-17
lines changed

1 file changed

+21
-17
lines changed

src/test/kotlin/io/github/nstdio/http/ext/ExtendedHttpClientIntegrationTest.kt

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,8 @@ import io.github.nstdio.http.ext.Assertions.awaitFor
2020
import io.github.nstdio.http.ext.Compression.deflate
2121
import io.github.nstdio.http.ext.Compression.gzip
2222
import io.kotest.matchers.collections.shouldContainExactly
23-
import io.kotest.matchers.maps.shouldContain
24-
import io.kotest.matchers.nulls.shouldNotBeNull
23+
import io.kotest.matchers.maps.shouldContainAll
2524
import io.kotest.matchers.should
26-
import io.kotest.matchers.shouldBe
2725
import io.kotest.property.Arb
2826
import io.kotest.property.arbitrary.next
2927
import io.kotest.property.arbitrary.string
@@ -37,6 +35,7 @@ import java.net.http.HttpResponse.BodyHandlers
3735
import java.net.http.HttpResponse.BodyHandlers.discarding
3836
import java.util.*
3937
import java.util.concurrent.LinkedBlockingDeque
38+
import java.util.function.Supplier
4039

4140
@MockWebServerTest
4241
internal class ExtendedHttpClientIntegrationTest(private val mockWebServer: MockWebServer) {
@@ -133,11 +132,15 @@ internal class ExtendedHttpClientIntegrationTest(private val mockWebServer: Mock
133132
@Test
134133
fun `Should add default headers`() {
135134
//given
136-
val client: HttpClient = ExtendedHttpClient.newBuilder()
137-
.defaultHeader("X-Testing-Value", "1")
138-
.defaultHeader("X-Testing-Supplier") { "2" }
139-
.build()
135+
val builder = ExtendedHttpClient.newBuilder()
136+
137+
val headers = (0..4).associate { "X-Testing-Value-$it" to it.toString() }
138+
val headersSuppliers = (0..4).associate { "X-Testing-Supplier-$it" to Supplier { it.toString() } }
140139

140+
headers.forEach { (k, v) -> builder.defaultHeader(k, v) }
141+
headersSuppliers.forEach { (k, v) -> builder.defaultHeader(k, v) }
142+
143+
val client = builder.build()
141144
val request = HttpRequest.newBuilder(mockWebServer.url("/test").toUri()).build()
142145
mockWebServer.enqueue(MockResponse().setResponseCode(200))
143146

@@ -146,19 +149,14 @@ internal class ExtendedHttpClientIntegrationTest(private val mockWebServer: Mock
146149

147150
//then
148151
response.request().headers().map().should {
149-
it.shouldContain("X-Testing-Value", listOf("1"))
150-
it.shouldContain("X-Testing-Supplier", listOf("2"))
152+
it.shouldContainAll(headers.toMultimap())
153+
it.shouldContainAll(headersSuppliers.withResolvedValues().toMultimap())
151154
}
152155

153156
val actualRequest = mockWebServer.takeRequest()
154-
actualRequest.headers.should {
155-
it["X-Testing-Value"]
156-
.shouldNotBeNull()
157-
.shouldBe("1")
158-
159-
it["X-Testing-Supplier"]
160-
.shouldNotBeNull()
161-
.shouldBe("2")
157+
actualRequest.headers.toMap().should {
158+
it.shouldContainAll(headers)
159+
it.shouldContainAll(headersSuppliers.withResolvedValues().toMap())
162160
}
163161
}
164162

@@ -186,4 +184,10 @@ internal class ExtendedHttpClientIntegrationTest(private val mockWebServer: Mock
186184
headerValues.shouldContainExactly(requestIds)
187185
}
188186
}
187+
188+
private fun <K, V> Map<K, V>.toMultimap(): Map<K, List<V>> =
189+
asSequence().map { e -> e.key to listOf(e.value) }.toMap()
190+
191+
private fun <K, V> Map<K, Supplier<V>>.withResolvedValues(): Map<K, V> =
192+
asSequence().map { e -> e.key to e.value.get() }.toMap()
189193
}

0 commit comments

Comments
 (0)