1 package net.pterodactylus.sone.web
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
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
15 * Unit test for [ReloadingPage].
17 class ReloadingPageTest : WebPageTest() {
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") }
24 fun `page returns correct path prefix`() {
25 assertThat(page.path, equalTo("/prefix/"))
29 fun `page returns that it’s a prefix page`() {
30 assertThat(page.isPrefixPage, equalTo(true))
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))
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()))