fix tests

This commit is contained in:
Athou
2026-05-21 04:48:43 +02:00
parent a5268f082d
commit a4592e5199

View File

@@ -1,8 +1,6 @@
package com.commafeed.backend; package com.commafeed.backend;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.NoRouteToHostException; import java.net.NoRouteToHostException;
import java.net.SocketTimeoutException; import java.net.SocketTimeoutException;
@@ -13,22 +11,20 @@ import java.util.Arrays;
import java.util.Objects; import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger; 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.commons.io.IOUtils;
import org.apache.hc.client5.http.ConnectTimeoutException; import org.apache.hc.client5.http.ConnectTimeoutException;
import org.apache.hc.core5.http.HttpStatus; import org.apache.hc.core5.http.HttpStatus;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
import org.mockito.Mockito; import org.mockito.Mockito;
import org.mockserver.client.MockServerClient; import org.mockserver.client.MockServerClient;
import org.mockserver.junit.jupiter.MockServerExtension; import org.mockserver.integration.ClientAndServer;
import org.mockserver.model.ConnectionOptions; import org.mockserver.model.ConnectionOptions;
import org.mockserver.model.Delay; import org.mockserver.model.Delay;
import org.mockserver.model.HttpRequest; import org.mockserver.model.HttpRequest;
@@ -46,7 +42,6 @@ import com.google.common.net.HttpHeaders;
import io.quarkus.runtime.configuration.MemorySize; import io.quarkus.runtime.configuration.MemorySize;
@ExtendWith(MockServerExtension.class)
class HttpGetterTest { class HttpGetterTest {
private static final Instant NOW = Instant.now(); private static final Instant NOW = Instant.now();
@@ -61,9 +56,8 @@ class HttpGetterTest {
private HttpGetter getter; private HttpGetter getter;
@BeforeEach @BeforeEach
void init(MockServerClient mockServerClient) throws IOException { void init() throws IOException {
this.mockServerClient = mockServerClient; this.mockServerClient = ClientAndServer.startClientAndServer(0);
this.mockServerClient.reset();
this.feedUrl = "http://localhost:" + this.mockServerClient.getPort() + "/"; this.feedUrl = "http://localhost:" + this.mockServerClient.getPort() + "/";
this.feedContent = IOUtils.toByteArray(Objects.requireNonNull(getClass().getResource("/feed/rss.xml"))); this.feedContent = IOUtils.toByteArray(Objects.requireNonNull(getClass().getResource("/feed/rss.xml")));
@@ -84,6 +78,13 @@ class HttpGetterTest {
this.getter = new HttpGetter(config, () -> NOW, provider, Mockito.mock(MetricRegistry.class)); this.getter = new HttpGetter(config, () -> NOW, provider, Mockito.mock(MetricRegistry.class));
} }
@AfterEach
void tearDown() {
if (this.mockServerClient != null) {
this.mockServerClient.stop();
}
}
@ParameterizedTest @ParameterizedTest
@ValueSource( @ValueSource(
ints = { HttpStatus.SC_UNAUTHORIZED, HttpStatus.SC_FORBIDDEN, HttpStatus.SC_NOT_FOUND, HttpStatus.SC_INTERNAL_SERVER_ERROR }) ints = { HttpStatus.SC_UNAUTHORIZED, HttpStatus.SC_FORBIDDEN, HttpStatus.SC_NOT_FOUND, HttpStatus.SC_INTERNAL_SERVER_ERROR })
@@ -314,15 +315,15 @@ class HttpGetterTest {
@Test @Test
void gzip() throws Exception { void gzip() throws Exception {
supportsCompression("gzip", GZIPOutputStream::new); supportsCompression("gzip");
} }
@Test @Test
void deflate() throws Exception { void deflate() throws Exception {
supportsCompression("deflate", DeflaterOutputStream::new); supportsCompression("deflate");
} }
void supportsCompression(String encoding, CompressionOutputStreamFunction compressionOutputStreamFunction) throws Exception { void supportsCompression(String encoding) throws Exception {
String body = "my body"; String body = "my body";
HttpGetterTest.this.mockServerClient.when(HttpRequest.request().withMethod("GET")).respond(req -> { HttpGetterTest.this.mockServerClient.when(HttpRequest.request().withMethod("GET")).respond(req -> {
@@ -332,23 +333,14 @@ class HttpGetterTest {
acceptEncodingHeader)); acceptEncodingHeader));
} }
ByteArrayOutputStream output = new ByteArrayOutputStream(); // MockServer 6.x automatically compresses the body based on the Content-Encoding header
try (OutputStream compressionOutputStream = compressionOutputStreamFunction.apply(output)) { return HttpResponse.response().withBody(body.getBytes()).withHeader(HttpHeaders.CONTENT_ENCODING, encoding);
compressionOutputStream.write(body.getBytes());
}
return HttpResponse.response().withBody(output.toByteArray()).withHeader(HttpHeaders.CONTENT_ENCODING, encoding);
}); });
HttpResult result = getter.get(HttpGetterTest.this.feedUrl); HttpResult result = getter.get(HttpGetterTest.this.feedUrl);
Assertions.assertEquals(body, new String(result.content())); Assertions.assertEquals(body, new String(result.content()));
} }
@FunctionalInterface
public interface CompressionOutputStreamFunction {
OutputStream apply(OutputStream input) throws IOException;
}
} }
@Nested @Nested