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;
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;
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);
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
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));
}
--- /dev/null
+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)))
+ }
+
+}