Skip to content

Commit

Permalink
fix: Use InflaterInputStream when compression is deflate.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Sep 5, 2021
1 parent 28d8ab7 commit fee6240
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 17 deletions.
10 changes: 9 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}

Expand Down
4 changes: 3 additions & 1 deletion src/main/java/com/github/nstdio/http/ext/BodyHandlers.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@
* Implementations of {@code BodyHandler}'s.
*/
public final class BodyHandlers {
private BodyHandlers() {
}

/**
* @return
* @return The decompressing body handler.
*/
public static BodyHandler<InputStream> ofDecompressing() {
return new DecompressingBodyHandler();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@
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<InputStream> {

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'";

Expand All @@ -35,7 +36,7 @@ static Function<InputStream, InputStream> decompressionFn(String directive) {
}
};
case "deflate":
return DeflaterInputStream::new;
return InflaterInputStream::new;
case "compress":
case "br":
throw new UnsupportedOperationException(String.format(UNSUPPORTED_DIRECTIVE, directive));
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -41,7 +41,7 @@ void shouldReturnDeflateInputStream() {

//then
assertThat(inputStream)
.isInstanceOf(DeflaterInputStream.class);
.isInstanceOf(InflaterInputStream.class);
}

@ParameterizedTest
Expand Down

0 comments on commit fee6240

Please sign in to comment.