add a test to make sure brotli compression is supported

This commit is contained in:
Athou
2024-08-28 17:34:24 +02:00
parent 2aee961600
commit 357e4e207f

View File

@@ -1,19 +1,25 @@
package com.commafeed.backend;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger;
import java.net.SocketTimeoutException;
import java.time.Duration;
import java.util.Arrays;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.zip.DeflaterOutputStream;
import java.util.zip.GZIPOutputStream;
import org.apache.commons.io.IOUtils;
import org.apache.hc.client5.http.ConnectTimeoutException;
import org.apache.hc.core5.http.HttpStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
@@ -27,6 +33,7 @@ import org.mockserver.model.HttpRequest;
import org.mockserver.model.HttpResponse;
import org.mockserver.model.MediaType;
import com.aayushatharva.brotli4j.encoder.BrotliOutputStream;
import com.codahale.metrics.MetricRegistry;
import com.commafeed.CommaFeedConfiguration;
import com.commafeed.CommaFeedVersion;
@@ -183,23 +190,6 @@ class HttpGetterTest {
Assertions.assertEquals(2, calls.get());
}
@Test
void supportsCompression() {
this.mockServerClient.when(HttpRequest.request().withMethod("GET")).respond(req -> {
String acceptEncodingHeader = req.getFirstHeader(HttpHeaders.ACCEPT_ENCODING);
if (!acceptEncodingHeader.contains("deflate")) {
throw new Exception("deflate should be in the Accept-Encoding header");
}
if (!acceptEncodingHeader.contains("gzip")) {
throw new Exception("gzip should be in the Accept-Encoding header");
}
return HttpResponse.response().withBody("ok");
});
Assertions.assertDoesNotThrow(() -> getter.getBinary(this.feedUrl));
}
@Test
void largeFeedWithContentLengthHeader() {
byte[] bytes = new byte[100000];
@@ -231,4 +221,51 @@ class HttpGetterTest {
Assertions.assertEquals("ok", new String(result.getContent()));
}
@Nested
class Compression {
@Test
void deflate() throws IOException, NotModifiedException {
supportsCompression("deflate", DeflaterOutputStream::new);
}
@Test
void gzip() throws IOException, NotModifiedException {
supportsCompression("gzip", GZIPOutputStream::new);
}
@Test
void brotli() throws IOException, NotModifiedException {
supportsCompression("br", BrotliOutputStream::new);
}
void supportsCompression(String encoding, CompressionOutputStreamFunction compressionOutputStreamFunction)
throws IOException, NotModifiedException {
String body = "my body";
HttpGetterTest.this.mockServerClient.when(HttpRequest.request().withMethod("GET")).respond(req -> {
String acceptEncodingHeader = req.getFirstHeader(HttpHeaders.ACCEPT_ENCODING);
if (!Set.of(acceptEncodingHeader.split(", ")).contains(encoding)) {
throw new Exception(encoding + " should be in the Accept-Encoding header");
}
ByteArrayOutputStream output = new ByteArrayOutputStream();
try (OutputStream compressionOutputStream = compressionOutputStreamFunction.apply(output)) {
compressionOutputStream.write(body.getBytes());
}
return HttpResponse.response().withBody(output.toByteArray()).withHeader(HttpHeaders.CONTENT_ENCODING, encoding);
});
HttpResult result = getter.getBinary(HttpGetterTest.this.feedUrl);
Assertions.assertEquals(body, new String(result.getContent()));
}
@FunctionalInterface
public interface CompressionOutputStreamFunction {
OutputStream apply(OutputStream input) throws IOException;
}
}
}