From 742ed9fb2c8e170dc87b71a7c020a2c8c89eb595 Mon Sep 17 00:00:00 2001 From: Oliver Kopp Date: Fri, 20 Sep 2024 00:00:28 +0200 Subject: [PATCH] Add redirect test case --- build.gradle | 3 ++ .../org/jabref/logic/net/URLDownloadTest.java | 36 ++++++++++++++++++- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 9d7221b5025..473051cc45e 100644 --- a/build.gradle +++ b/build.gradle @@ -365,6 +365,9 @@ dependencies { testImplementation "org.testfx:testfx-junit5:4.0.16-alpha" testImplementation "org.hamcrest:hamcrest-library:3.0" + // recommended by https://github.com/wiremock/wiremock/issues/2149#issuecomment-1835775954 + testImplementation 'org.wiremock:wiremock-standalone:3.3.1' + checkstyle 'com.puppycrawl.tools:checkstyle:10.18.1' // xjc needs the runtime as well for the ant task, otherwise it fails xjc group: 'org.glassfish.jaxb', name: 'jaxb-xjc', version: '3.0.2' diff --git a/src/test/java/org/jabref/logic/net/URLDownloadTest.java b/src/test/java/org/jabref/logic/net/URLDownloadTest.java index ff0c221efc1..f1fa4caa49a 100644 --- a/src/test/java/org/jabref/logic/net/URLDownloadTest.java +++ b/src/test/java/org/jabref/logic/net/URLDownloadTest.java @@ -4,6 +4,7 @@ import java.net.MalformedURLException; import java.net.URL; import java.nio.charset.StandardCharsets; +import java.nio.file.Files; import java.nio.file.Path; import org.jabref.logic.importer.FetcherClientException; @@ -11,11 +12,19 @@ import org.jabref.support.DisabledOnCIServer; import org.jabref.testutils.category.FetcherTest; +import com.github.tomakehurst.wiremock.WireMockServer; import kong.unirest.core.UnirestException; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.configureFor; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -134,6 +143,31 @@ void test503ErrorThrowsFetcherServerException() throws Exception { @Test void test429ErrorThrowsFetcherClientException() throws Exception { URLDownload urlDownload = new URLDownload(new URL("http://httpstat.us/429")); - Exception exception = assertThrows(FetcherClientException.class, urlDownload::asString); + assertThrows(FetcherClientException.class, urlDownload::asString); + } + + @Test + void redirectWorks(@TempDir Path tempDir) throws Exception { + WireMockServer wireMockServer = new WireMockServer(2222); + wireMockServer.start(); + configureFor("localhost", 2222); + stubFor(get("/redirect") + .willReturn(aResponse() + .withStatus(302) + .withHeader("Location", "/final"))); + byte[] pdfContent = {0x00}; + stubFor(get(urlEqualTo("/final")) + .willReturn(aResponse() + .withStatus(200) + .withHeader("Content-Type", "application/pdf") + .withBody(pdfContent))); + + URLDownload urlDownload = new URLDownload(new URL("http://localhost:2222/redirect")); + Path downloadedFile = tempDir.resolve("download.pdf"); + urlDownload.toFile(downloadedFile); + byte[] actual = Files.readAllBytes(downloadedFile); + assertArrayEquals(pdfContent, actual); + + wireMockServer.stop(); } }