Add utilities for HTTP headers
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 4 Jun 2017 17:10:32 +0000 (19:10 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sun, 4 Jun 2017 17:10:32 +0000 (19:10 +0200)
src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt
src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt

index ded275f..7b7957b 100644 (file)
@@ -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())
+}
index 4e13b7e..f2a97f0 100644 (file)
@@ -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())
+       }
+
 }