From f7e6c9dbec3fdcb77a54e245c9f2b7a9a851b5b0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Sat, 26 Nov 2016 23:32:05 +0100 Subject: [PATCH] Add unit test for get image page --- .../net/pterodactylus/sone/web/WebPageTest.java | 31 +++++++++++++ .../net/pterodactylus/sone/web/GetImagePageTest.kt | 51 ++++++++++++++++++++++ 2 files changed, 82 insertions(+) create mode 100644 src/test/kotlin/net/pterodactylus/sone/web/GetImagePageTest.kt diff --git a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java index c73bf61..4aef917 100644 --- a/src/test/java/net/pterodactylus/sone/web/WebPageTest.java +++ b/src/test/java/net/pterodactylus/sone/web/WebPageTest.java @@ -8,6 +8,10 @@ import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PipedInputStream; +import java.io.PipedOutputStream; import java.net.URI; import java.net.URISyntaxException; import java.util.ArrayList; @@ -27,18 +31,21 @@ import net.pterodactylus.sone.data.Image; import net.pterodactylus.sone.data.Post; import net.pterodactylus.sone.data.Sone; import net.pterodactylus.sone.data.SoneOptions.DefaultSoneOptions; +import net.pterodactylus.sone.data.TemporaryImage; import net.pterodactylus.sone.freenet.wot.OwnIdentity; import net.pterodactylus.sone.web.page.FreenetRequest; import net.pterodactylus.util.notify.Notification; import net.pterodactylus.util.template.Template; import net.pterodactylus.util.template.TemplateContext; import net.pterodactylus.util.web.Method; +import net.pterodactylus.util.web.Response; import freenet.clients.http.ToadletContext; import freenet.support.api.HTTPRequest; import com.google.common.base.Optional; import com.google.common.eventbus.EventBus; +import com.google.common.io.ByteStreams; import org.junit.Before; import org.junit.Rule; import org.junit.rules.ExpectedException; @@ -67,11 +74,22 @@ public abstract class WebPageTest { protected final Map requestParameters = new HashMap<>(); protected final Map requestHeaders = new HashMap<>(); protected final FreenetRequest freenetRequest = mock(FreenetRequest.class); + private final PipedOutputStream responseOutputStream = new PipedOutputStream(); + private final PipedInputStream responseInputStream; + protected final Response response = new Response(responseOutputStream); protected final ToadletContext toadletContext = mock(ToadletContext.class); private final Set ownIdentities = new HashSet<>(); private final List localSones = new ArrayList<>(); + protected WebPageTest() { + try { + responseInputStream = new PipedInputStream(responseOutputStream); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + @Before public final void setupFreenetRequest() { when(freenetRequest.getToadletContext()).thenReturn(toadletContext); @@ -130,6 +148,7 @@ public abstract class WebPageTest { when(core.getAlbum(anyString())).thenReturn(null); when(core.getImage(anyString())).thenReturn(null); when(core.getImage(anyString(), anyBoolean())).thenReturn(null); + when(core.getTemporaryImage(anyString())).thenReturn(null); } @Before @@ -198,6 +217,18 @@ public abstract class WebPageTest { when(core.getImage(eq(imageId), anyBoolean())).thenReturn(image); } + protected void addTemporaryImage(String imageId, TemporaryImage temporaryImage) { + when(core.getTemporaryImage(eq(imageId))).thenReturn(temporaryImage); + } + + protected byte[] getResponseBytes() throws IOException { + response.getContent().close(); + try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { + ByteStreams.copy(responseInputStream, outputStream); + return outputStream.toByteArray(); + } + } + protected void addNotification(String notificationId, Notification notification) { when(webInterface.getNotification(eq(notificationId))).thenReturn(Optional.of(notification)); } diff --git a/src/test/kotlin/net/pterodactylus/sone/web/GetImagePageTest.kt b/src/test/kotlin/net/pterodactylus/sone/web/GetImagePageTest.kt new file mode 100644 index 0000000..57f887a --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/web/GetImagePageTest.kt @@ -0,0 +1,51 @@ +package net.pterodactylus.sone.web + +import net.pterodactylus.sone.data.TemporaryImage +import org.hamcrest.MatcherAssert.assertThat +import org.hamcrest.Matchers.equalTo +import org.junit.Test + +/** + * Unit test for [GetImagePage]. + */ +class GetImagePageTest : WebPageTest() { + + private val page = GetImagePage(webInterface) + + @Test + fun `page returns correct path`() { + assertThat(page.path, equalTo("getImage.html")) + } + + @Test + fun `page is not a prefix page`() { + assertThat(page.isPrefixPage, equalTo(false)) + } + + @Test + fun `page is not link-excepted`() { + assertThat(page.isLinkExcepted(null), equalTo(false)) + } + + @Test + fun `invalid image returns 404 response`() { + page.handleRequest(freenetRequest, response) + assertThat(response.statusCode, equalTo(404)) + assertThat(responseBytes, equalTo(ByteArray(0))) + } + + @Test + fun `valid image returns response with correct data`() { + val image = TemporaryImage("temp-id").apply { + mimeType = "image/test" + imageData = ByteArray(5, Int::toByte) + } + addHttpRequestParameter("image", "temp-id") + 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))) + } + +} -- 2.7.4