🎨 Use equalTo instead of is
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / utils / RequestsTest.kt
1 package net.pterodactylus.sone.utils
2
3 import freenet.support.api.HTTPRequest
4 import net.pterodactylus.sone.test.mock
5 import net.pterodactylus.sone.test.whenever
6 import net.pterodactylus.sone.web.page.FreenetRequest
7 import net.pterodactylus.util.web.Method.GET
8 import net.pterodactylus.util.web.Method.POST
9 import net.pterodactylus.util.web.Request
10 import org.hamcrest.MatcherAssert.assertThat
11 import org.hamcrest.Matchers.equalTo
12 import org.hamcrest.Matchers.nullValue
13 import org.junit.Test
14 import org.mockito.ArgumentMatchers.anyInt
15 import org.mockito.ArgumentMatchers.eq
16
17 /**
18  * Unit test for the [Request] utilities.
19  */
20 class RequestsTest {
21
22         private val httpGetRequest = mock<HTTPRequest>().apply { whenever(method).thenReturn("GET") }
23         private val httpPostRequest = mock<HTTPRequest>().apply { whenever(method).thenReturn("POST") }
24         private val getRequest = mock<Request>().apply { whenever(method).thenReturn(GET) }
25         private val postRequest = mock<Request>().apply { whenever(method).thenReturn(POST) }
26         private val freenetGetRequest = mock<FreenetRequest>().apply {
27                 whenever(method).thenReturn(GET)
28                 whenever(httpRequest).thenReturn(this@RequestsTest.httpGetRequest)
29         }
30         private val freenetPostRequest = mock<FreenetRequest>().apply {
31                 whenever(method).thenReturn(POST)
32                 whenever(httpRequest).thenReturn(this@RequestsTest.httpPostRequest)
33         }
34
35         @Test
36         fun `GET request is recognized correctly`() {
37                 assertThat(getRequest.isGET, equalTo(true))
38                 assertThat(getRequest.isPOST, equalTo(false))
39         }
40
41         @Test
42         fun `POST request is recognized correctly`() {
43                 assertThat(postRequest.isGET, equalTo(false))
44                 assertThat(postRequest.isPOST, equalTo(true))
45         }
46
47         @Test
48         fun `correct parameter of GET request is returned`() {
49                 whenever(httpGetRequest.getParam("test-param")).thenAnswer { "test-value" }
50                 assertThat(freenetGetRequest.parameters["test-param"], equalTo("test-value"))
51         }
52
53         @Test
54         fun `correct parameter of POST request is returned`() {
55                 whenever(httpPostRequest.getPartAsStringFailsafe(eq("test-param"), anyInt())).thenAnswer { "test-value" }
56                 assertThat(freenetPostRequest.parameters["test-param"], equalTo("test-value"))
57         }
58
59         @Test
60         fun `parameter of unknown request is not returned`() {
61                 val request = mock<FreenetRequest>()
62                 val httpRequest = mock<HTTPRequest>()
63                 whenever(request.httpRequest).thenReturn(httpRequest)
64                 assertThat(request.parameters["test-param"], nullValue())
65         }
66
67         @Test
68         fun `parameter of GET request is checked for presence correctly`() {
69                 whenever(httpGetRequest.isParameterSet("test-param")).thenAnswer { true }
70                 assertThat("test-param" in freenetGetRequest.parameters, equalTo(true))
71         }
72
73         @Test
74         fun `parameter of POST request is checked for presence correctly`() {
75                 whenever(httpPostRequest.isPartSet("test-param")).thenAnswer { true }
76                 assertThat("test-param" in freenetPostRequest.parameters, equalTo(true))
77         }
78
79         @Test
80         fun `parameter of unknown request is not set`() {
81                 val request = mock<FreenetRequest>()
82                 val httpRequest = mock<HTTPRequest>()
83                 whenever(request.httpRequest).thenReturn(httpRequest)
84                 assertThat("test-param" in request.parameters, equalTo(false))
85         }
86
87         @Test
88         fun `http header of freenet request can be retrieved`() {
89                 whenever(httpGetRequest.getHeader("foo")).thenReturn("bar")
90                 assertThat(freenetGetRequest.headers["foo"], equalTo("bar"))
91         }
92
93         @Test
94         fun `http header of freenet request is case-insensitive`() {
95                 whenever(httpGetRequest.getHeader("foo")).thenReturn("bar")
96                 assertThat(freenetGetRequest.headers["FOO"], equalTo("bar"))
97         }
98
99         @Test
100         fun `value of non-existant http header of freenet request is null`() {
101                 assertThat(freenetGetRequest.headers["Foo"], nullValue())
102         }
103
104         @Test
105         fun `http header of http request can be retrieved`() {
106                 whenever(httpGetRequest.getHeader("foo")).thenReturn("bar")
107                 assertThat(httpGetRequest.headers["foo"], equalTo("bar"))
108         }
109
110         @Test
111         fun `http header of http request is case-insensitive`() {
112                 whenever(httpGetRequest.getHeader("foo")).thenReturn("bar")
113                 assertThat(httpGetRequest.headers["FOO"], equalTo("bar"))
114         }
115
116         @Test
117         fun `value of non-existant http header of http request is null`() {
118                 assertThat(httpGetRequest.headers["Foo"], nullValue())
119         }
120
121 }