From: David ‘Bombe’ Roden Date: Sun, 4 Jun 2017 17:10:32 +0000 (+0200) Subject: Add utilities for HTTP headers X-Git-Tag: 0.9.7^2~169 X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=42e8b77f804273b8d6b99101b11cbdf82a1cf75f;p=Sone.git Add utilities for HTTP headers --- diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt index ded275f..7b7957b 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt @@ -27,3 +27,10 @@ class Parameters(private val request: HTTPRequest) { else -> false } } + +val FreenetRequest.headers get() = Headers(httpRequest) +val HTTPRequest.headers get() = Headers(this) + +class Headers(private val request: HTTPRequest) { + operator fun get(name: String): String? = request.getHeader(name.toLowerCase()) +} diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt index 4e13b7e..f2a97f0 100644 --- a/src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt @@ -84,4 +84,38 @@ class RequestsTest { assertThat("test-param" in request.parameters, equalTo(false)) } + @Test + fun `http header of freenet request can be retrieved`() { + whenever(httpGetRequest.getHeader("foo")).thenReturn("bar") + assertThat(freenetGetRequest.headers["foo"], equalTo("bar")) + } + + @Test + fun `http header of freenet request is case-insensitive`() { + whenever(httpGetRequest.getHeader("foo")).thenReturn("bar") + assertThat(freenetGetRequest.headers["FOO"], equalTo("bar")) + } + + @Test + fun `value of non-existant http header of freenet request is null`() { + assertThat(freenetGetRequest.headers["Foo"], nullValue()) + } + + @Test + fun `http header of http request can be retrieved`() { + whenever(httpGetRequest.getHeader("foo")).thenReturn("bar") + assertThat(httpGetRequest.headers["foo"], equalTo("bar")) + } + + @Test + fun `http header of http request is case-insensitive`() { + whenever(httpGetRequest.getHeader("foo")).thenReturn("bar") + assertThat(httpGetRequest.headers["FOO"], equalTo("bar")) + } + + @Test + fun `value of non-existant http header of http request is null`() { + assertThat(httpGetRequest.headers["Foo"], nullValue()) + } + }