Skip to content

Commit

Permalink
Add redirect test case
Browse files Browse the repository at this point in the history
  • Loading branch information
koppor committed Sep 19, 2024
1 parent 7dcceb9 commit 742ed9f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
36 changes: 35 additions & 1 deletion src/test/java/org/jabref/logic/net/URLDownloadTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,27 @@
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;
import org.jabref.logic.importer.FetcherServerException;
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;
Expand Down Expand Up @@ -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();
}
}

0 comments on commit 742ed9f

Please sign in to comment.