♻️ Refactor WOT connector
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / page / FreenetRequestTest.kt
1 package net.pterodactylus.sone.web.page
2
3 import freenet.clients.http.*
4 import freenet.clients.http.SessionManager.*
5 import freenet.l10n.*
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.*
11 import org.junit.*
12 import org.mockito.*
13 import org.mockito.Mockito.*
14 import java.net.*
15
16 class FreenetRequestTest {
17
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)
25
26         @Test
27         fun `uri is retained correctly`() {
28                 assertThat(request.uri, equalTo(uri))
29         }
30
31         @Test
32         fun `method is retained correctly`() {
33                 assertThat(request.method, equalTo(method))
34         }
35
36         @Test
37         fun `http request is retained correctly`() {
38                 assertThat(request.httpRequest, equalTo(httpRequest))
39         }
40
41         @Test
42         fun `toadlet context is retained correctly`() {
43                 assertThat(request.toadletContext, equalTo(toadletContext))
44         }
45
46         @Test
47         fun `l10n is retained correctly`() {
48                 assertThat(request.l10n, equalTo(l10n))
49         }
50
51         @Test
52         fun `null is returned if no session exists`() {
53                 assertThat(request.existingSession, nullValue())
54         }
55
56         @Test
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))
61         }
62
63         @Test
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))
68         }
69
70         @Test
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))
75         }
76
77 }