♻️ Add SoneRequest for Sone-specific request values
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 13 Apr 2019 22:37:39 +0000 (00:37 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 13 Apr 2019 22:37:39 +0000 (00:37 +0200)
src/main/kotlin/net/pterodactylus/sone/web/page/FreenetRequest.kt
src/main/kotlin/net/pterodactylus/sone/web/page/SoneRequest.kt [new file with mode: 0644]
src/main/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePage.kt
src/test/kotlin/net/pterodactylus/sone/web/page/SoneRequestTest.kt [new file with mode: 0644]
src/test/kotlin/net/pterodactylus/sone/web/pages/SoneTemplatePageTest.kt

index 00424ff..914d70f 100644 (file)
@@ -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 (file)
index 0000000..1cc6911
--- /dev/null
@@ -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)
index 54cd5cc..4290c3a 100644 (file)
@@ -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 (file)
index 0000000..dc2b4a5
--- /dev/null
@@ -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<Core>()
+       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))
+       }
+
+}
index 0206053..d227a84 100644 (file)
@@ -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))
+       }
+
 }