From dc47bafb2d0c4a2a440389158039993d10cbfd08 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sun, 14 Apr 2019 00:37:39 +0200 Subject: [PATCH] =?utf8?q?=E2=99=BB=EF=B8=8F=20Add=20SoneRequest=20for=20S?= =?utf8?q?one-specific=20request=20values?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../pterodactylus/sone/web/page/FreenetRequest.kt | 2 +- .../net/pterodactylus/sone/web/page/SoneRequest.kt | 11 +++++ .../sone/web/pages/SoneTemplatePage.kt | 7 +++- .../pterodactylus/sone/web/page/SoneRequestTest.kt | 47 ++++++++++++++++++++++ .../sone/web/pages/SoneTemplatePageTest.kt | 20 +++++++-- 5 files changed, 81 insertions(+), 6 deletions(-) create mode 100644 src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt index 00424ff..914d70f 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt @@ -22,4 +22,4 @@ import freenet.support.api.* import net.pterodactylus.util.web.* import java.net.* -class FreenetRequest(uri: URI, method: Method, val httpRequest: HTTPRequest, val toadletContext: ToadletContext) : Request(uri, method) +open class FreenetRequest(uri: URI, method: Method, val httpRequest: HTTPRequest, val toadletContext: ToadletContext) : Request(uri, method) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt b/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt new file mode 100644 index 0000000..1cc6911 --- /dev/null +++ b/src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt @@ -0,0 +1,11 @@ +package net.pterodactylus.sone.web.page + +import freenet.clients.http.* +import freenet.support.api.* +import net.pterodactylus.sone.core.* +import net.pterodactylus.util.web.* +import java.net.* + +class SoneRequest(uri: URI, method: Method, httpRequest: HTTPRequest, toadletContext: ToadletContext, val core: Core) : FreenetRequest(uri, method, httpRequest, toadletContext) + +fun FreenetRequest.toSoneRequest(core: Core) = SoneRequest(uri, method, httpRequest, toadletContext, core) diff --git a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt index 54cd5cc..4290c3a 100644 --- a/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt +++ b/src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt @@ -6,8 +6,7 @@ import net.pterodactylus.sone.main.SonePlugin import net.pterodactylus.sone.utils.emptyToNull import net.pterodactylus.sone.web.SessionProvider import net.pterodactylus.sone.web.WebInterface -import net.pterodactylus.sone.web.page.FreenetRequest -import net.pterodactylus.sone.web.page.FreenetTemplatePage +import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.template.Template import net.pterodactylus.util.template.TemplateContext @@ -70,6 +69,10 @@ open class SoneTemplatePage @JvmOverloads constructor( } internal open fun handleRequest(freenetRequest: FreenetRequest, templateContext: TemplateContext) { + handleRequest(freenetRequest.toSoneRequest(core), templateContext) + } + + open fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { } override public fun getRedirectTarget(freenetRequest: FreenetRequest): String? { diff --git a/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt new file mode 100644 index 0000000..dc2b4a5 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt @@ -0,0 +1,47 @@ +package net.pterodactylus.sone.web.page + +import freenet.clients.http.* +import freenet.support.api.* +import net.pterodactylus.sone.core.* +import net.pterodactylus.sone.test.* +import net.pterodactylus.util.web.* +import org.hamcrest.MatcherAssert.* +import org.hamcrest.Matchers.* +import org.junit.* +import org.mockito.* +import java.net.* + +class SoneRequestTest { + + private val uri = URI(".") + private val method = Method.GET + private val httpRequest = Mockito.mock(HTTPRequest::class.java) + private val toadletContext = Mockito.mock(ToadletContext::class.java) + private val core = mock() + private val soneRequest = SoneRequest(uri, method, httpRequest, toadletContext, core) + + @Test + fun `freenet request properties are retained correctly`() { + assertThat(soneRequest.uri, equalTo(uri)) + assertThat(soneRequest.method, equalTo(method)) + assertThat(soneRequest.httpRequest, equalTo(httpRequest)) + assertThat(soneRequest.toadletContext, equalTo(toadletContext)) + } + + @Test + fun `core is retained correctly`() { + assertThat(soneRequest.core, sameInstance(core)) + } + + @Test + fun `freenet request is wrapped correctly`() { + val freenetRequest = FreenetRequest(uri, method, httpRequest, toadletContext) + val wrappedSoneRequest = freenetRequest.toSoneRequest(core) + assertThat(wrappedSoneRequest.uri, equalTo(uri)) + assertThat(wrappedSoneRequest.method, equalTo(method)) + assertThat(wrappedSoneRequest.httpRequest, equalTo(httpRequest)) + assertThat(wrappedSoneRequest.toadletContext, equalTo(toadletContext)) + assertThat(wrappedSoneRequest.core, sameInstance(core)) + } + +} diff --git a/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt index 0206053..d227a84 100644 --- a/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt +++ b/src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt @@ -4,7 +4,7 @@ import net.pterodactylus.sone.data.Sone import net.pterodactylus.sone.main.SonePlugin import net.pterodactylus.sone.test.mock import net.pterodactylus.sone.test.whenever -import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.sone.web.page.* import net.pterodactylus.util.notify.Notification import net.pterodactylus.util.template.TemplateContext import net.pterodactylus.util.version.Version @@ -22,6 +22,10 @@ import org.junit.Test */ class SoneTemplatePageTest : WebPageTest({ template, webInterface -> object : SoneTemplatePage("path.html", webInterface, template, requiresLogin = true) {} }) { + init { + request("index.html") + } + @Test fun `page title is empty string if no page title key was given`() { SoneTemplatePage("path.html", webInterface, template, requiresLogin = false).let { page -> @@ -167,14 +171,12 @@ class SoneTemplatePageTest : WebPageTest({ template, webInterface -> object : So @Test fun `redirect does happen if sone is not logged in`() { unsetCurrentSone() - request("index.html") assertThat(page.getRedirectTarget(freenetRequest), equalTo("login.html?target=index.html")) } @Test fun `redirect does happen with parameters encoded correctly if sone is not logged in`() { unsetCurrentSone() - request("index.html") addHttpRequestParameter("foo", "b=r") addHttpRequestParameter("baz", "q&o") assertThat(page.getRedirectTarget(freenetRequest), anyOf( @@ -214,4 +216,16 @@ class SoneTemplatePageTest : WebPageTest({ template, webInterface -> object : So } } + @Test + fun `handle request with sone request is called`() { + var called = false + val page = object : SoneTemplatePage("path.html", webInterface, template) { + override fun handleRequest(soneRequest: SoneRequest, templateContext: TemplateContext) { + called = true + } + } + page.processTemplate(freenetRequest, templateContext) + assertThat(called, equalTo(true)) + } + } -- 2.7.4