From bf496cfa392e27dd45245d00dc845e8e713cf0ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 2 May 2017 22:09:15 +0200 Subject: [PATCH] Add helper to simplify access to request objects --- .../net/pterodactylus/sone/utils/Requests.kt | 21 +++++++ .../net/pterodactylus/sone/utils/RequestsTest.kt | 68 ++++++++++++++++++++-- 2 files changed, 83 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt b/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt index 50f3957..ded275f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt +++ b/src/main/kotlin/net/pterodactylus/sone/utils/Requests.kt @@ -1,8 +1,29 @@ package net.pterodactylus.sone.utils +import freenet.support.api.HTTPRequest +import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.web.Method.GET import net.pterodactylus.util.web.Method.POST import net.pterodactylus.util.web.Request val Request.isGET get() = this.method == GET val Request.isPOST get() = this.method == POST +val HTTPRequest.isGET get() = method == "GET" +val HTTPRequest.isPOST get() = method == "POST" + +val FreenetRequest.parameters get() = Parameters(httpRequest) +val HTTPRequest.parameters get() = Parameters(this) + +class Parameters(private val request: HTTPRequest) { + operator fun get(name: String, maxLength: Int = 1048576) = when { + request.isGET -> request.getParam(name) + request.isPOST -> request.getPartAsStringFailsafe(name, maxLength) + else -> null + } + + operator fun contains(name: String) = when { + request.isGET -> request.isParameterSet(name) + request.isPOST -> request.isPartSet(name) + else -> false + } +} diff --git a/src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt b/src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt index eb0bc43..4e13b7e 100644 --- a/src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/utils/RequestsTest.kt @@ -1,31 +1,87 @@ package net.pterodactylus.sone.utils +import freenet.support.api.HTTPRequest import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.web.page.FreenetRequest import net.pterodactylus.util.web.Method.GET import net.pterodactylus.util.web.Method.POST import net.pterodactylus.util.web.Request import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.equalTo +import org.hamcrest.Matchers.nullValue import org.junit.Test +import org.mockito.ArgumentMatchers.anyInt +import org.mockito.ArgumentMatchers.eq /** * Unit test for the [Request] utilities. */ class RequestsTest { + private val httpGetRequest = mock().apply { whenever(method).thenReturn("GET") } + private val httpPostRequest = mock().apply { whenever(method).thenReturn("POST") } + private val getRequest = mock().apply { whenever(method).thenReturn(GET) } + private val postRequest = mock().apply { whenever(method).thenReturn(POST) } + private val freenetGetRequest = mock().apply { + whenever(method).thenReturn(GET) + whenever(httpRequest).thenReturn(this@RequestsTest.httpGetRequest) + } + private val freenetPostRequest = mock().apply { + whenever(method).thenReturn(POST) + whenever(httpRequest).thenReturn(this@RequestsTest.httpPostRequest) + } + @Test fun `GET request is recognized correctly`() { - val request = mock().apply { whenever(method).thenReturn(GET) } - assertThat(request.isGET, equalTo(true)) - assertThat(request.isPOST, equalTo(false)) + assertThat(getRequest.isGET, equalTo(true)) + assertThat(getRequest.isPOST, equalTo(false)) } @Test fun `POST request is recognized correctly`() { - val request = mock().apply { whenever(method).thenReturn(POST) } - assertThat(request.isGET, equalTo(false)) - assertThat(request.isPOST, equalTo(true)) + assertThat(postRequest.isGET, equalTo(false)) + assertThat(postRequest.isPOST, equalTo(true)) + } + + @Test + fun `correct parameter of GET request is returned`() { + whenever(httpGetRequest.getParam("test-param")).thenAnswer { "test-value" } + assertThat(freenetGetRequest.parameters["test-param"], equalTo("test-value")) + } + + @Test + fun `correct parameter of POST request is returned`() { + whenever(httpPostRequest.getPartAsStringFailsafe(eq("test-param"), anyInt())).thenAnswer { "test-value" } + assertThat(freenetPostRequest.parameters["test-param"], equalTo("test-value")) + } + + @Test + fun `parameter of unknown request is not returned`() { + val request = mock() + val httpRequest = mock() + whenever(request.httpRequest).thenReturn(httpRequest) + assertThat(request.parameters["test-param"], nullValue()) + } + + @Test + fun `parameter of GET request is checked for presence correctly`() { + whenever(httpGetRequest.isParameterSet("test-param")).thenAnswer { true } + assertThat("test-param" in freenetGetRequest.parameters, equalTo(true)) + } + + @Test + fun `parameter of POST request is checked for presence correctly`() { + whenever(httpPostRequest.isPartSet("test-param")).thenAnswer { true } + assertThat("test-param" in freenetPostRequest.parameters, equalTo(true)) + } + + @Test + fun `parameter of unknown request is not set`() { + val request = mock() + val httpRequest = mock() + whenever(request.httpRequest).thenReturn(httpRequest) + assertThat("test-param" in request.parameters, equalTo(false)) } } -- 2.7.4