Convert get image page test to use new web page test base
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 19 Jun 2017 21:34:27 +0000 (23:34 +0200)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Mon, 19 Jun 2017 21:34:27 +0000 (23:34 +0200)
src/test/kotlin/net/pterodactylus/sone/web/pages/GetImagePageTest.kt
src/test/kotlin/net/pterodactylus/sone/web/pages/WebPageTest2.kt

index 151f1ae..6c34e3f 100644 (file)
@@ -9,9 +9,13 @@ import org.junit.Test
 /**
  * Unit test for [GetImagePage].
  */
-class GetImagePageTest : WebPageTest() {
+class GetImagePageTest {
 
-       private val page = GetImagePage(webInterface)
+       private val scaffolding = WebPageTest2()
+       private val page = GetImagePage(scaffolding.webInterface)
+       private val freenetRequest = scaffolding.freenetRequest
+       private val response = scaffolding.response
+       private val responseContent = scaffolding.responseContent
 
        @Test
        fun `page returns correct path`() {
@@ -34,7 +38,7 @@ class GetImagePageTest : WebPageTest() {
                assertThat(response.statusCode, equalTo(404))
                assertThat(response.statusText, equalTo("Not found."))
                assertThat(response.contentType, equalTo("text/html; charset=utf-8"))
-               assertThat(responseBytes, equalTo(ByteArray(0)))
+               assertThat(responseContent.toByteArray(), equalTo(ByteArray(0)))
        }
 
        @Test
@@ -43,12 +47,12 @@ class GetImagePageTest : WebPageTest() {
                        mimeType = "image/test"
                        imageData = ByteArray(5, Int::toByte)
                }
-               addHttpRequestParameter("image", "temp-id")
-               addTemporaryImage("temp-id", image)
+               scaffolding.addHttpRequestParameter("image", "temp-id")
+               scaffolding.addTemporaryImage("temp-id", image)
                page.handleRequest(freenetRequest, response)
                assertThat(response.statusCode, equalTo(200))
                assertThat(response.contentType, equalTo("image/test"))
-               assertThat(responseBytes, equalTo(ByteArray(5, Int::toByte)))
+               assertThat(responseContent.toByteArray(), equalTo(ByteArray(5, Int::toByte)))
                println(response.headers.map { it.name to it.iterator().asSequence().toList() })
                assertThat(response.headers.map { it.name to it.iterator().asSequence().toList() }, contains(
                                "Content-Disposition" to listOf("attachment; filename=temp-id.test")
index 5ec3ead..69ac20a 100644 (file)
@@ -9,6 +9,7 @@ import net.pterodactylus.sone.data.Image
 import net.pterodactylus.sone.data.Post
 import net.pterodactylus.sone.data.PostReply
 import net.pterodactylus.sone.data.Sone
+import net.pterodactylus.sone.data.TemporaryImage
 import net.pterodactylus.sone.freenet.wot.OwnIdentity
 import net.pterodactylus.sone.test.deepMock
 import net.pterodactylus.sone.test.get
@@ -91,6 +92,7 @@ open class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePa
                whenever(core.getAlbum(anyString())).then { allAlbums[it[0]] }
                whenever(core.getImage(anyString())).then { allImages[it[0]]}
                whenever(core.getImage(anyString(), anyBoolean())).then { allImages[it[0]]}
+               whenever(core.getTemporaryImage(anyString())).thenReturn(null)
        }
 
        private fun setupWebInterface() {
@@ -190,6 +192,10 @@ open class WebPageTest2(pageSupplier: (Template, WebInterface) -> SoneTemplatePa
                translations[key] = value
        }
 
+       fun addTemporaryImage(id: String, temporaryImage: TemporaryImage) {
+               whenever(core.getTemporaryImage(id)).thenReturn(temporaryImage)
+       }
+
        fun verifyNoRedirect(assertions: () -> Unit) {
                var caughtException: Exception? = null
                try {