Skip to content

verify http response compression #3319

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
50 changes: 50 additions & 0 deletions spring-cloud-gateway-integration-tests/http2/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<description>Spring Cloud Gateway HTTP2 Integration Test</description>

<properties>
<brotli4j.version>1.16.0</brotli4j.version>
</properties>

<parent>
Expand All @@ -37,6 +38,49 @@
<groupId>io.netty</groupId>
<artifactId>netty-tcnative-boringssl-static</artifactId>
</dependency>
<!-- brotli4j is needed for content compression -->
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>brotli4j</artifactId>
<version>${brotli4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-x86_64</artifactId>
<version>${brotli4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-aarch64</artifactId>
<version>${brotli4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-linux-armv7</artifactId>
<version>${brotli4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-osx-x86_64</artifactId>
<version>${brotli4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-osx-aarch64</artifactId>
<version>${brotli4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.aayushatharva.brotli4j</groupId>
<artifactId>native-windows-x86_64</artifactId>
<version>${brotli4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
Expand All @@ -56,6 +100,12 @@
<artifactId>assertj-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.15.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package org.springframework.cloud.gateway.tests.http2;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

Expand All @@ -32,6 +33,7 @@
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
Expand All @@ -52,6 +54,11 @@ public String hello() {
return "Hello";
}

@GetMapping("data")
public String getData(@RequestParam(name = "size", defaultValue = "5000", required = true) Integer size) {
return StringUtils.repeat("a", size);
}

@Bean
public RouteLocator myRouteLocator(RouteLocatorBuilder builder) {
return builder.routes().route(r -> r.path("/myprefix/**").filters(f -> f.stripPrefix(1)).uri("lb://myservice"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ logging:
reactor.netty.http.client: DEBUG

server:
compression:
enabled: true
ssl:
key-store: classpath:sample.jks
key-store-password: secret
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,26 @@

package org.springframework.cloud.gateway.tests.http2;

import com.aayushatharva.brotli4j.decoder.DecoderJNI;
import com.aayushatharva.brotli4j.decoder.DirectDecompress;
import io.netty.handler.codec.compression.Brotli;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.time.Duration;

import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import java.util.List;
import java.util.stream.Stream;
import java.util.zip.GZIPInputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.io.IOUtils;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import org.springframework.http.HttpHeaders;
import reactor.core.publisher.Hooks;
import reactor.core.publisher.Mono;
import reactor.netty.http.Http2SslContextSpec;
Expand Down Expand Up @@ -63,6 +77,75 @@ public void http2Works(CapturedOutput output) {
Assertions.assertThat(output).contains("Negotiated application-level protocol [h2]", "PRI * HTTP/2.0");
}

@ParameterizedTest
@MethodSource("httpContentCompressionParameters")
public void httpContentCompression(String acceptEncoding, String expectedContentEncoding) throws Throwable {
Brotli.ensureAvailability();
final int uncompressedResponseSize = 6_666;
final String expectedUncompressedResponse = StringUtils.repeat('a', uncompressedResponseSize);
WebClient client = WebClient.builder().clientConnector(new ReactorClientHttpConnector(getHttpClient())).build();
Mono<ResponseEntity<byte[]>> responseEntityMono = client.get()
.uri(uriBuilder ->
uriBuilder.host("localhost").path("/myprefix/data").port(port).scheme("https").queryParam("size", uncompressedResponseSize).build())
.header("Accept-Encoding", acceptEncoding)
.retrieve()
.toEntity(byte[].class);
StepVerifier.create(responseEntityMono).assertNext(entity -> {
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
HttpHeaders responseHeaders = entity.getHeaders();
assertThat(responseHeaders.getContentType().toString()).isEqualTo("text/plain;charset=UTF-8");
List<String> contentEncoding = responseHeaders.get("Content-Encoding");
if (expectedContentEncoding == null) {
assertThat(contentEncoding).isNull();
assertThat(entity.hasBody()).isTrue();
assertThat(new String(entity.getBody())).isEqualTo(expectedUncompressedResponse);
assertThat(responseHeaders.getContentLength()).isEqualTo(uncompressedResponseSize);
} else {
assertThat(contentEncoding).containsExactly(expectedContentEncoding);
assertThat(entity.hasBody()).isTrue();
String decompressedResponseBody = new String(decompress(entity));
assertThat(decompressedResponseBody).isEqualTo(expectedUncompressedResponse);
long contentLength = responseHeaders.getContentLength();
assertThat(contentLength).isGreaterThan(0);
assertThat(contentLength).isLessThan(uncompressedResponseSize);
}
}).expectComplete().verify();

}

private static byte[] decompress(ResponseEntity<byte[]> entity) {
if (!entity.hasBody()) {
return null;
}
String contentEncoding = entity.getHeaders().get("Content-Encoding").get(0);
byte[] compressedData = entity.getBody();
try {
if ("br".equals(contentEncoding)) {
DirectDecompress directDecompress = DirectDecompress.decompress(compressedData);
assertThat(directDecompress.getResultStatus()).isEqualTo(DecoderJNI.Status.DONE);
return directDecompress.getDecompressedData();
} else if ("gzip".equals(contentEncoding)) {
ByteArrayInputStream input = new ByteArrayInputStream(compressedData);
GZIPInputStream gzInput = new GZIPInputStream(input);
return IOUtils.toByteArray(gzInput);
} else {
throw new IllegalStateException("unexpected contentEncoding: " + contentEncoding);
}
} catch (IOException ex) {
throw new IllegalStateException(ex);
}
}

public static Stream<Arguments> httpContentCompressionParameters() {
return Stream.of(Arguments.of("", null),
Arguments.of("br", "br"),
Arguments.of("gzip", "gzip"),
Arguments.of("br, gzip", "br"),
Arguments.of("gzip, br", "br"),
Arguments.of("garbage, gzip", "gzip"),
Arguments.of("garbage", null));
}

public static void assertResponse(String uri, String expected) {
WebClient client = WebClient.builder().clientConnector(new ReactorClientHttpConnector(getHttpClient())).build();
Mono<ResponseEntity<String>> responseEntityMono = client.get().uri(uri).retrieve().toEntity(String.class);
Expand Down