From 331ddfab1cbad04e73d8b92171f3320400ee4664 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Tue, 6 Dec 2016 21:11:42 +0100 Subject: [PATCH] Add unit test for reloading page --- .../pterodactylus/sone/web/ReloadingPageTest.kt | 50 ++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/ReloadingPageTest.kt diff --git a/src/test/kotlin/net/pterodactylus/sone/web/ReloadingPageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/ReloadingPageTest.kt new file mode 100644 index 0000000..a9cfd46 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/ReloadingPageTest.kt @@ -0,0 +1,50 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.web.page.FreenetRequest +import net.pterodactylus.util.web.Method.GET +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder +import java.nio.file.Files +import java.nio.file.Paths +import kotlin.text.Charsets.UTF_8 + +/** + * Unit test for [ReloadingPage]. + */ +class ReloadingPageTest : WebPageTest() { + + @Rule @JvmField val tempFolder = TemporaryFolder() + private val folder by lazy { tempFolder.newFolder() } + private val page by lazy { ReloadingPage("/prefix/", folder.path, "text/plain") } + + @Test + fun `page returns correct path prefix`() { + assertThat(page.path, equalTo("/prefix/")) + } + + @Test + fun `page returns that it’s a prefix page`() { + assertThat(page.isPrefixPage, equalTo(true)) + } + + @Test + fun `requesting invalid file results in 404`() { + request("/prefix/path/file.txt", GET) + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(404)) + } + + @Test + fun `requesting valid file results in 200 and delivers file`() { + Files.write(Paths.get(folder.path, "file.txt"), listOf("Hello", "World"), UTF_8) + request("/prefix/path/file.txt", GET) + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(200)) + assertThat(response.contentType, equalTo("text/plain")) + assertThat(responseBytes, equalTo("Hello\nWorld\n".toByteArray())) + } + +} -- 2.7.4