1 package net.pterodactylus.sone.utils
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
14 import org.mockito.ArgumentMatchers.anyInt
15 import org.mockito.ArgumentMatchers.eq
18 * Unit test for the [Request] utilities.
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)
30 private val freenetPostRequest = mock<FreenetRequest>().apply {
31 whenever(method).thenReturn(POST)
32 whenever(httpRequest).thenReturn(this@RequestsTest.httpPostRequest)
36 fun `GET request is recognized correctly`() {
37 assertThat(getRequest.isGET, equalTo(true))
38 assertThat(getRequest.isPOST, equalTo(false))
42 fun `POST request is recognized correctly`() {
43 assertThat(postRequest.isGET, equalTo(false))
44 assertThat(postRequest.isPOST, equalTo(true))
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"))
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"))
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())
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))
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))
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))
88 fun `http header of freenet request can be retrieved`() {
89 whenever(httpGetRequest.getHeader("foo")).thenReturn("bar")
90 assertThat(freenetGetRequest.headers["foo"], equalTo("bar"))
94 fun `http header of freenet request is case-insensitive`() {
95 whenever(httpGetRequest.getHeader("foo")).thenReturn("bar")
96 assertThat(freenetGetRequest.headers["FOO"], equalTo("bar"))
100 fun `value of non-existant http header of freenet request is null`() {
101 assertThat(freenetGetRequest.headers["Foo"], nullValue())
105 fun `http header of http request can be retrieved`() {
106 whenever(httpGetRequest.getHeader("foo")).thenReturn("bar")
107 assertThat(httpGetRequest.headers["foo"], equalTo("bar"))
111 fun `http header of http request is case-insensitive`() {
112 whenever(httpGetRequest.getHeader("foo")).thenReturn("bar")
113 assertThat(httpGetRequest.headers["FOO"], equalTo("bar"))
117 fun `value of non-existant http header of http request is null`() {
118 assertThat(httpGetRequest.headers["Foo"], nullValue())