Add unit test for get image page
authorDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 26 Nov 2016 22:32:05 +0000 (23:32 +0100)
committerDavid ‘Bombe’ Roden <bombe@pterodactylus.net>
Sat, 26 Nov 2016 22:32:05 +0000 (23:32 +0100)
src/test/java/net/pterodactylus/sone/web/WebPageTest.java
src/test/kotlin/net/pterodactylus/sone/web/GetImagePageTest.kt [new file with mode: 0644]

index c73bf61..4aef917 100644 (file)
@@ -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<String, String> requestParameters = new HashMap<>();
        protected final Map<String, String> 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<OwnIdentity> ownIdentities = new HashSet<>();
        private final List<Sone> 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 (file)
index 0000000..57f887a
--- /dev/null
@@ -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)))
+       }
+
+}