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)
--- /dev/null
+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)
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
}
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? {
--- /dev/null
+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))
+ }
+
+}
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
*/
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 ->
@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(
}
}
+ @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))
+ }
+
}