diff --git a/build.gradle b/build.gradle index 2a05491..554d00b 100644 --- a/build.gradle +++ b/build.gradle @@ -17,13 +17,21 @@ repositories { def isCI = Boolean.parseBoolean(System.getenv("CI")) ext { junitVersion = '5.8.0-RC1' + commonIoVersion = '1.3.2' + assertJVersion = '3.20.2' lombokDependency = 'org.projectlombok:lombok:1.18.20' } dependencies { + /** AssertJ & Friends */ + testImplementation "org.assertj:assertj-core:$assertJVersion" + testImplementation group: 'com.jayway.jsonpath', name: 'json-path-assert', version: '2.6.0' + + testImplementation "org.apache.commons:commons-io:$commonIoVersion" + + /** Jupiter */ testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion" testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion" - testImplementation 'org.assertj:assertj-core:3.20.2' testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion" } diff --git a/src/main/java/com/github/nstdio/http/ext/BodyHandlers.java b/src/main/java/com/github/nstdio/http/ext/BodyHandlers.java index 600e4fa..23e06dd 100644 --- a/src/main/java/com/github/nstdio/http/ext/BodyHandlers.java +++ b/src/main/java/com/github/nstdio/http/ext/BodyHandlers.java @@ -7,9 +7,11 @@ * Implementations of {@code BodyHandler}'s. */ public final class BodyHandlers { + private BodyHandlers() { + } /** - * @return + * @return The decompressing body handler. */ public static BodyHandler ofDecompressing() { return new DecompressingBodyHandler(); diff --git a/src/main/java/com/github/nstdio/http/ext/DecompressingBodyHandler.java b/src/main/java/com/github/nstdio/http/ext/DecompressingBodyHandler.java index c6da715..8711278 100644 --- a/src/main/java/com/github/nstdio/http/ext/DecompressingBodyHandler.java +++ b/src/main/java/com/github/nstdio/http/ext/DecompressingBodyHandler.java @@ -12,13 +12,13 @@ import java.net.http.HttpResponse.ResponseInfo; import java.util.function.Function; import java.util.regex.Pattern; -import java.util.zip.DeflaterInputStream; import java.util.zip.GZIPInputStream; +import java.util.zip.InflaterInputStream; final class DecompressingBodyHandler implements BodyHandler { private static final String HEADER_CONTENT_ENCODING = "Content-Encoding"; - private static final Pattern COMMA_PATTERN = Pattern.compile(","); + private static final Pattern COMMA_PATTERN = Pattern.compile(",", Pattern.LITERAL); private static final String UNSUPPORTED_DIRECTIVE = "Compression directive '%s' is not supported"; private static final String UNKNOWN_DIRECTIVE = "Unknown compression directive '%s'"; @@ -35,7 +35,7 @@ static Function decompressionFn(String directive) { } }; case "deflate": - return DeflaterInputStream::new; + return InflaterInputStream::new; case "compress": case "br": throw new UnsupportedOperationException(String.format(UNSUPPORTED_DIRECTIVE, directive)); diff --git a/src/test/java/com/github/nstdio/http/ext/DecompressingBodyHandlerIntegrationTest.java b/src/test/java/com/github/nstdio/http/ext/DecompressingBodyHandlerIntegrationTest.java index 3a23613..1f35699 100644 --- a/src/test/java/com/github/nstdio/http/ext/DecompressingBodyHandlerIntegrationTest.java +++ b/src/test/java/com/github/nstdio/http/ext/DecompressingBodyHandlerIntegrationTest.java @@ -1,27 +1,34 @@ package com.github.nstdio.http.ext; -import org.junit.jupiter.api.Test; +import static com.github.nstdio.http.ext.BodyHandlers.ofDecompressing; +import static com.jayway.jsonpath.matchers.JsonPathMatchers.*; +import static org.hamcrest.MatcherAssert.assertThat; + +import org.apache.commons.io.IOUtils; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; -import java.net.http.HttpResponse; -import java.time.Duration; class DecompressingBodyHandlerIntegrationTest { - private final HttpClient httpClient = HttpClient.newBuilder() - .connectTimeout(Duration.ofSeconds(2)) - .build(); + private final HttpClient httpClient = HttpClient.newHttpClient(); private final URI baseUri = URI.create("https://httpbin.org/"); - @Test - void shouldCreate() throws Exception { + @ParameterizedTest + @ValueSource(strings = {"gzip", "deflate"}) + void shouldCreate(String compression) throws Exception { //given - var request = HttpRequest.newBuilder(baseUri.resolve("gzip")) + var request = HttpRequest.newBuilder(baseUri.resolve(compression)) .build(); - var body = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body(); - var body2 = httpClient.send(request, BodyHandlers.ofDecompressing()).body(); + //when + var body = httpClient.send(request, ofDecompressing()).body(); + var json = IOUtils.toString(body); + + //then + assertThat(json, isJson()); } } diff --git a/src/test/java/com/github/nstdio/http/ext/DecompressingBodyHandlerTest.java b/src/test/java/com/github/nstdio/http/ext/DecompressingBodyHandlerTest.java index 1b27fc7..1a2c608 100644 --- a/src/test/java/com/github/nstdio/http/ext/DecompressingBodyHandlerTest.java +++ b/src/test/java/com/github/nstdio/http/ext/DecompressingBodyHandlerTest.java @@ -12,8 +12,8 @@ import org.junit.jupiter.params.provider.ValueSource; import java.io.ByteArrayInputStream; -import java.util.zip.DeflaterInputStream; import java.util.zip.GZIPInputStream; +import java.util.zip.InflaterInputStream; class DecompressingBodyHandlerTest { @@ -41,7 +41,7 @@ void shouldReturnDeflateInputStream() { //then assertThat(inputStream) - .isInstanceOf(DeflaterInputStream.class); + .isInstanceOf(InflaterInputStream.class); } @ParameterizedTest