1 package net.pterodactylus.sone.web.page
3 import freenet.clients.http.*
4 import freenet.clients.http.SessionManager.*
6 import freenet.support.api.*
7 import net.pterodactylus.sone.test.*
8 import net.pterodactylus.util.web.*
9 import org.hamcrest.MatcherAssert.*
10 import org.hamcrest.Matchers.*
13 import org.mockito.Mockito.*
16 class FreenetRequestTest {
18 private val uri = URI(".")
19 private val method = Method.GET
20 private val httpRequest = mock(HTTPRequest::class.java)
21 private val toadletContext = mock(ToadletContext::class.java)
22 private val l10n = mock<BaseL10n>()
23 private val sessionManager = mock<SessionManager>()
24 private val request = FreenetRequest(uri, method, httpRequest, toadletContext, l10n, sessionManager)
27 fun `uri is retained correctly`() {
28 assertThat(request.uri, equalTo(uri))
32 fun `method is retained correctly`() {
33 assertThat(request.method, equalTo(method))
37 fun `http request is retained correctly`() {
38 assertThat(request.httpRequest, equalTo(httpRequest))
42 fun `toadlet context is retained correctly`() {
43 assertThat(request.toadletContext, equalTo(toadletContext))
47 fun `l10n is retained correctly`() {
48 assertThat(request.l10n, equalTo(l10n))
52 fun `null is returned if no session exists`() {
53 assertThat(request.existingSession, nullValue())
57 fun `existing session can be retrieved`() {
58 val session = mock<Session>()
59 whenever(sessionManager.useSession(toadletContext)).thenReturn(session)
60 assertThat(request.existingSession, sameInstance(session))
64 fun `existing session is returned if it exists`() {
65 val session = mock<Session>()
66 whenever(sessionManager.useSession(toadletContext)).thenReturn(session)
67 assertThat(request.session, sameInstance(session))
71 fun `new session is returned if none exists`() {
72 val session = mock<Session>()
73 whenever(sessionManager.createSession(anyString(), ArgumentMatchers.eq(toadletContext))).thenReturn(session)
74 assertThat(request.session, sameInstance(session))