From 10df23d198e4f56c7906e052131a1947c6260b34 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Thu, 17 Nov 2016 21:18:22 +0100 Subject: [PATCH] Add unit test for request change filter --- .../sone/template/RequestChangeFilterTest.kt | 71 ++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/template/RequestChangeFilterTest.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/template/RequestChangeFilterTest.kt b/src/test/kotlin/net/pterodactylus/sone/template/RequestChangeFilterTest.kt new file mode 100644 index 0000000..d56bdc5 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/template/RequestChangeFilterTest.kt @@ -0,0 +1,71 @@ +package net.pterodactylus.sone.template + +import freenet.support.api.HTTPRequest +import net.pterodactylus.sone.Matchers.matchesRegex +import net.pterodactylus.sone.test.mock +import net.pterodactylus.sone.test.whenever +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.template.TemplateContext +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Before +import org.junit.Test +import org.mockito.ArgumentMatchers.anyString +import java.net.URI + +/** + * Unit test for [RequestChangeFilter]. + */ +class RequestChangeFilterTest { + + private val filter = RequestChangeFilter() + private val templateContext = mock() + private val freenetRequest = mock() + private val httpRequest = mock() + private val parameters = mutableMapOf() + + @Before + fun setupFreenetRequest() { + whenever(freenetRequest.httpRequest).thenReturn(httpRequest) + whenever(freenetRequest.httpRequest.parameterNames).thenAnswer { parameters.keys } + whenever(freenetRequest.httpRequest.getParam(anyString())).thenAnswer { parameters[it.arguments[0]] } + } + + @Test + fun `filter correctly appends parameter to request URL without parameters`() { + whenever(freenetRequest.uri).thenReturn(URI("/some/path.html")) + val uri = filter.format(templateContext, freenetRequest, mapOf("name" to "name", "value" to "value")) as URI + assertThat(uri, equalTo(URI("/some/path.html?name=value"))) + } + + @Test + fun `filter cuts off old query`() { + whenever(freenetRequest.uri).thenReturn(URI("/some/path.html?foo=bar")) + val uri = filter.format(templateContext, freenetRequest, mapOf("name" to "name", "value" to "value")) as URI + assertThat(uri, equalTo(URI("/some/path.html?name=value"))) + } + + @Test + fun `filter correctly appends parameter to request URL with parameters`() { + parameters["foo"] = "bar" + whenever(freenetRequest.uri).thenReturn(URI("/some/path.html")) + val uri = filter.format(templateContext, freenetRequest, mapOf("name" to "name", "value" to "value")) as URI + assertThat(uri.toString(), matchesRegex("/some/path.html\\?(foo=bar&name=value|name=value&foo=bar)")) + } + + @Test + fun `filter overwrites existing parameter value`() { + parameters["name"] = "old" + whenever(freenetRequest.uri).thenReturn(URI("/some/path.html")) + val uri = filter.format(templateContext, freenetRequest, mapOf("name" to "name", "value" to "value")) as URI + assertThat(uri, equalTo(URI("/some/path.html?name=value"))) + } + + @Test + fun `filter correctly encodes characters`() { + whenever(freenetRequest.uri).thenReturn(URI("/some/path.html")) + val uri = filter.format(templateContext, freenetRequest, mapOf("name" to "name", "value" to " välue")) as URI + assertThat(uri, equalTo(URI("/some/path.html?name=+v%C3%A4lue"))) + } + +} -- 2.7.4