Add some tests and fix names of tests
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / web / ReloadingPageTest.kt
1 package net.pterodactylus.sone.web
2
3 import net.pterodactylus.sone.web.page.FreenetRequest
4 import net.pterodactylus.util.web.Method.GET
5 import org.hamcrest.MatcherAssert.assertThat
6 import org.hamcrest.Matchers.equalTo
7 import org.junit.Rule
8 import org.junit.Test
9 import org.junit.rules.TemporaryFolder
10 import java.nio.file.Files
11 import java.nio.file.Paths
12 import kotlin.text.Charsets.UTF_8
13
14 /**
15  * Unit test for [ReloadingPage].
16  */
17 class ReloadingPageTest : WebPageTest() {
18
19         @Rule @JvmField val tempFolder = TemporaryFolder()
20         private val folder by lazy { tempFolder.newFolder() }
21         private val page by lazy { ReloadingPage<FreenetRequest>("/prefix/", folder.path, "text/plain") }
22
23         @Test
24         fun `page returns correct path prefix`() {
25                 assertThat(page.path, equalTo("/prefix/"))
26         }
27
28         @Test
29         fun `page returns that it’s a prefix page`() {
30                 assertThat(page.isPrefixPage, equalTo(true))
31         }
32
33         @Test
34         fun `requesting invalid file results in 404`() {
35                 request("/prefix/path/file.txt", GET)
36                 page.handleRequest(freenetRequest, response)
37                 assertThat(response.statusCode, equalTo(404))
38         }
39
40         @Test
41         fun `requesting valid file results in 200 and delivers file`() {
42                 Files.write(Paths.get(folder.path, "file.txt"), listOf("Hello", "World"), UTF_8)
43                 request("/prefix/path/file.txt", GET)
44                 page.handleRequest(freenetRequest, response)
45                 assertThat(response.statusCode, equalTo(200))
46                 assertThat(response.contentType, equalTo("text/plain"))
47                 assertThat(responseBytes, equalTo("Hello\nWorld\n".toByteArray()))
48         }
49
50 }