Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Use InflaterInputStream when compression is deflate. #4

Merged
merged 1 commit into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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<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 +35,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