diff --git a/client/src/main/java/org/asynchttpclient/AsyncHandler.java b/client/src/main/java/org/asynchttpclient/AsyncHandler.java index a912ad9c5..22451fe09 100644 --- a/client/src/main/java/org/asynchttpclient/AsyncHandler.java +++ b/client/src/main/java/org/asynchttpclient/AsyncHandler.java @@ -116,7 +116,8 @@ default State onTrailingHeadersReceived(HttpHeaders headers) throws Exception { * @return T Value that will be returned by the associated {@link Future} * @throws Exception if something wrong happens */ - @Nullable T onCompleted() throws Exception; + @Nullable + T onCompleted() throws Exception; /** * Notify the callback before hostname resolution diff --git a/client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java b/client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java index 484cd0029..12dc93d7d 100644 --- a/client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java +++ b/client/src/main/java/org/asynchttpclient/AsyncHttpClientConfig.java @@ -160,7 +160,8 @@ public interface AsyncHttpClientConfig { * @return the {@link ThreadFactory} an {@link AsyncHttpClient} use for handling asynchronous response. If no {@link ThreadFactory} has been explicitly * provided, this method will return {@code null} */ - @Nullable ThreadFactory getThreadFactory(); + @Nullable + ThreadFactory getThreadFactory(); /** * An instance of {@link ProxyServer} used by an {@link AsyncHttpClient} @@ -174,14 +175,16 @@ public interface AsyncHttpClientConfig { * * @return an instance of {@link SslContext} used for SSL connection. */ - @Nullable SslContext getSslContext(); + @Nullable + SslContext getSslContext(); /** * Return the current {@link Realm} * * @return the current {@link Realm} */ - @Nullable Realm getRealm(); + @Nullable + Realm getRealm(); /** * Return the list of {@link RequestFilter} @@ -260,12 +263,14 @@ public interface AsyncHttpClientConfig { /** * @return the array of enabled protocols */ - @Nullable String[] getEnabledProtocols(); + @Nullable + String[] getEnabledProtocols(); /** * @return the array of enabled cipher suites */ - @Nullable String[] getEnabledCipherSuites(); + @Nullable + String[] getEnabledCipherSuites(); /** * @return if insecure cipher suites must be filtered out (only used when not explicitly passing enabled cipher suites) @@ -294,7 +299,8 @@ public interface AsyncHttpClientConfig { int getHandshakeTimeout(); - @Nullable SslEngineFactory getSslEngineFactory(); + @Nullable + SslEngineFactory getSslEngineFactory(); int getChunkedFileChunkSize(); @@ -310,23 +316,29 @@ public interface AsyncHttpClientConfig { Map, Object> getChannelOptions(); - @Nullable EventLoopGroup getEventLoopGroup(); + @Nullable + EventLoopGroup getEventLoopGroup(); boolean isUseNativeTransport(); boolean isUseOnlyEpollNativeTransport(); - @Nullable Consumer getHttpAdditionalChannelInitializer(); + @Nullable + Consumer getHttpAdditionalChannelInitializer(); - @Nullable Consumer getWsAdditionalChannelInitializer(); + @Nullable + Consumer getWsAdditionalChannelInitializer(); ResponseBodyPartFactory getResponseBodyPartFactory(); - @Nullable ChannelPool getChannelPool(); + @Nullable + ChannelPool getChannelPool(); - @Nullable ConnectionSemaphoreFactory getConnectionSemaphoreFactory(); + @Nullable + ConnectionSemaphoreFactory getConnectionSemaphoreFactory(); - @Nullable Timer getNettyTimer(); + @Nullable + Timer getNettyTimer(); /** * @return the duration between tick of {@link HashedWheelTimer} @@ -358,7 +370,8 @@ public interface AsyncHttpClientConfig { int getSoRcvBuf(); - @Nullable ByteBufAllocator getAllocator(); + @Nullable + ByteBufAllocator getAllocator(); int getIoThreadsCount(); diff --git a/client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClient.java b/client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClient.java index fb5dad6ff..1f616c328 100644 --- a/client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClient.java +++ b/client/src/main/java/org/asynchttpclient/DefaultAsyncHttpClient.java @@ -24,8 +24,8 @@ import org.asynchttpclient.channel.ChannelPool; import org.asynchttpclient.cookie.CookieEvictionTask; import org.asynchttpclient.cookie.CookieStore; -import org.asynchttpclient.filter.FilterContext; import org.asynchttpclient.exception.FilterException; +import org.asynchttpclient.filter.FilterContext; import org.asynchttpclient.filter.RequestFilter; import org.asynchttpclient.handler.resumable.ResumableAsyncHandler; import org.asynchttpclient.netty.channel.ChannelManager; diff --git a/client/src/main/java/org/asynchttpclient/HttpResponseBodyPart.java b/client/src/main/java/org/asynchttpclient/HttpResponseBodyPart.java index 8ab4c9bcc..0df78f7b2 100644 --- a/client/src/main/java/org/asynchttpclient/HttpResponseBodyPart.java +++ b/client/src/main/java/org/asynchttpclient/HttpResponseBodyPart.java @@ -16,6 +16,7 @@ package org.asynchttpclient; import io.netty.buffer.ByteBuf; + import java.nio.ByteBuffer; /** diff --git a/client/src/main/java/org/asynchttpclient/Request.java b/client/src/main/java/org/asynchttpclient/Request.java index e43edc3fe..1d95016b3 100644 --- a/client/src/main/java/org/asynchttpclient/Request.java +++ b/client/src/main/java/org/asynchttpclient/Request.java @@ -67,12 +67,14 @@ public interface Request { /** * @return the InetAddress to be used to bypass uri's hostname resolution */ - @Nullable InetAddress getAddress(); + @Nullable + InetAddress getAddress(); /** * @return the local address to bind from */ - @Nullable InetAddress getLocalAddress(); + @Nullable + InetAddress getLocalAddress(); /** * @return the HTTP headers @@ -92,32 +94,38 @@ public interface Request { /** * @return the request's body array of byte arrays (only non-null if it was set this way) */ - @Nullable List getCompositeByteData(); + @Nullable + List getCompositeByteData(); /** * @return the request's body string (only non-null if it was set this way) */ - @Nullable String getStringData(); + @Nullable + String getStringData(); /** * @return the request's body ByteBuffer (only non-null if it was set this way) */ - @Nullable ByteBuffer getByteBufferData(); + @Nullable + ByteBuffer getByteBufferData(); /** * @return the request's body ByteBuf (only non-null if it was set this way) */ - @Nullable ByteBuf getByteBufData(); + @Nullable + ByteBuf getByteBufData(); /** * @return the request's body InputStream (only non-null if it was set this way) */ - @Nullable InputStream getStreamData(); + @Nullable + InputStream getStreamData(); /** * @return the request's body BodyGenerator (only non-null if it was set this way) */ - @Nullable BodyGenerator getBodyGenerator(); + @Nullable + BodyGenerator getBodyGenerator(); /** * @return the request's form parameters @@ -132,7 +140,8 @@ public interface Request { /** * @return the virtual host to connect to */ - @Nullable String getVirtualHost(); + @Nullable + String getVirtualHost(); /** * @return the query params resolved from the url/uri @@ -142,22 +151,26 @@ public interface Request { /** * @return the proxy server to be used to perform this request (overrides the one defined in config) */ - @Nullable ProxyServer getProxyServer(); + @Nullable + ProxyServer getProxyServer(); /** * @return the realm to be used to perform this request (overrides the one defined in config) */ - @Nullable Realm getRealm(); + @Nullable + Realm getRealm(); /** * @return the file to be uploaded */ - @Nullable File getFile(); + @Nullable + File getFile(); /** * @return if this request is to follow redirects. Non null values means "override config value". */ - @Nullable Boolean getFollowRedirect(); + @Nullable + Boolean getFollowRedirect(); /** * @return the request timeout. Non zero values means "override config value". @@ -177,7 +190,8 @@ public interface Request { /** * @return the charset value used when decoding the request's body. */ - @Nullable Charset getCharset(); + @Nullable + Charset getCharset(); /** * @return the strategy to compute ChannelPool's keys diff --git a/client/src/main/java/org/asynchttpclient/channel/ChannelPool.java b/client/src/main/java/org/asynchttpclient/channel/ChannelPool.java index 1b38f09a3..de30cc576 100755 --- a/client/src/main/java/org/asynchttpclient/channel/ChannelPool.java +++ b/client/src/main/java/org/asynchttpclient/channel/ChannelPool.java @@ -38,7 +38,8 @@ public interface ChannelPool { * @param partitionKey the partition used when invoking offer * @return the channel associated with the uri */ - @Nullable Channel poll(Object partitionKey); + @Nullable + Channel poll(Object partitionKey); /** * Remove all channels from the cache. A channel might have been associated diff --git a/client/src/main/java/org/asynchttpclient/channel/NoopChannelPool.java b/client/src/main/java/org/asynchttpclient/channel/NoopChannelPool.java index 3a5b2e93d..5a7a8ca54 100644 --- a/client/src/main/java/org/asynchttpclient/channel/NoopChannelPool.java +++ b/client/src/main/java/org/asynchttpclient/channel/NoopChannelPool.java @@ -30,7 +30,6 @@ public enum NoopChannelPool implements ChannelPool { INSTANCE; /** - * * @return always false since this is a {@link NoopChannelPool} */ @Override @@ -39,7 +38,6 @@ public boolean offer(Channel channel, Object partitionKey) { } /** - * * @return always null since this is a {@link NoopChannelPool} */ @Override @@ -48,7 +46,6 @@ public boolean offer(Channel channel, Object partitionKey) { } /** - * * @return always false since this is a {@link NoopChannelPool} */ @Override @@ -57,7 +54,6 @@ public boolean removeAll(Channel channel) { } /** - * * @return always true since this is a {@link NoopChannelPool} */ @Override @@ -66,7 +62,6 @@ public boolean isOpen() { } /** - * * Does nothing since this is a {@link NoopChannelPool} */ @Override @@ -74,7 +69,6 @@ public void destroy() { } /** - * * Does nothing since this is a {@link NoopChannelPool} */ @Override @@ -82,7 +76,6 @@ public void flushPartitions(Predicate predicate) { } /** - * * @return always {@link Collections#emptyMap()} since this is a {@link NoopChannelPool} */ @Override diff --git a/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java b/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java index 35a620e31..5832185cc 100644 --- a/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java +++ b/client/src/main/java/org/asynchttpclient/cookie/ThreadSafeCookieStore.java @@ -229,7 +229,7 @@ private void removeExpired() { cookieJar.values() .forEach(cookieMap -> removed[0] |= cookieMap.entrySet() - .removeIf(v -> hasCookieExpired(v.getValue().cookie, v.getValue().createdAt))); + .removeIf(v -> hasCookieExpired(v.getValue().cookie, v.getValue().createdAt))); if (removed[0]) { cookieJar.entrySet().removeIf(entry -> entry.getValue() == null || entry.getValue().isEmpty()); diff --git a/client/src/main/java/org/asynchttpclient/netty/handler/intercept/ResponseFiltersInterceptor.java b/client/src/main/java/org/asynchttpclient/netty/handler/intercept/ResponseFiltersInterceptor.java index 19b41f502..5f905d94f 100644 --- a/client/src/main/java/org/asynchttpclient/netty/handler/intercept/ResponseFiltersInterceptor.java +++ b/client/src/main/java/org/asynchttpclient/netty/handler/intercept/ResponseFiltersInterceptor.java @@ -20,8 +20,8 @@ import org.asynchttpclient.AsyncHandler; import org.asynchttpclient.AsyncHttpClientConfig; import org.asynchttpclient.HttpResponseStatus; -import org.asynchttpclient.filter.FilterContext; import org.asynchttpclient.exception.FilterException; +import org.asynchttpclient.filter.FilterContext; import org.asynchttpclient.filter.ResponseFilter; import org.asynchttpclient.netty.NettyResponseFuture; import org.asynchttpclient.netty.request.NettyRequestSender; diff --git a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java index 2c0314325..afde13643 100755 --- a/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java +++ b/client/src/main/java/org/asynchttpclient/netty/request/NettyRequestSender.java @@ -36,10 +36,10 @@ import org.asynchttpclient.Realm; import org.asynchttpclient.Realm.AuthScheme; import org.asynchttpclient.Request; +import org.asynchttpclient.exception.FilterException; import org.asynchttpclient.exception.PoolAlreadyClosedException; import org.asynchttpclient.exception.RemotelyClosedException; import org.asynchttpclient.filter.FilterContext; -import org.asynchttpclient.exception.FilterException; import org.asynchttpclient.filter.IOExceptionFilter; import org.asynchttpclient.handler.TransferCompletionHandler; import org.asynchttpclient.netty.NettyResponseFuture; diff --git a/client/src/main/java/org/asynchttpclient/ntlm/NtlmEngine.java b/client/src/main/java/org/asynchttpclient/ntlm/NtlmEngine.java index ce7624d2b..c2338c46a 100644 --- a/client/src/main/java/org/asynchttpclient/ntlm/NtlmEngine.java +++ b/client/src/main/java/org/asynchttpclient/ntlm/NtlmEngine.java @@ -1083,7 +1083,8 @@ byte[] getChallenge() { /** * Retrieve the target */ - @Nullable String getTarget() { + @Nullable + String getTarget() { return target; } diff --git a/client/src/main/java/org/asynchttpclient/proxy/ProxyServerSelector.java b/client/src/main/java/org/asynchttpclient/proxy/ProxyServerSelector.java index 46391c804..048f2e78e 100644 --- a/client/src/main/java/org/asynchttpclient/proxy/ProxyServerSelector.java +++ b/client/src/main/java/org/asynchttpclient/proxy/ProxyServerSelector.java @@ -35,5 +35,6 @@ public interface ProxyServerSelector { * @param uri The URI to select a proxy server for. * @return The proxy server to use, if any. May return null. */ - @Nullable ProxyServer select(Uri uri); + @Nullable + ProxyServer select(Uri uri); } diff --git a/client/src/test/java/org/asynchttpclient/AbstractBasicTest.java b/client/src/test/java/org/asynchttpclient/AbstractBasicTest.java index 993f87905..2dcfa859d 100644 --- a/client/src/test/java/org/asynchttpclient/AbstractBasicTest.java +++ b/client/src/test/java/org/asynchttpclient/AbstractBasicTest.java @@ -21,9 +21,7 @@ import org.eclipse.jetty.server.ServerConnector; import org.eclipse.jetty.server.handler.AbstractHandler; import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.TestInstance; import org.junit.jupiter.api.extension.ExtendWith; import org.slf4j.Logger; diff --git a/client/src/test/java/org/asynchttpclient/AutomaticDecompressionTest.java b/client/src/test/java/org/asynchttpclient/AutomaticDecompressionTest.java index 238bb7206..dfd0a9446 100644 --- a/client/src/test/java/org/asynchttpclient/AutomaticDecompressionTest.java +++ b/client/src/test/java/org/asynchttpclient/AutomaticDecompressionTest.java @@ -17,10 +17,16 @@ import com.aayushatharva.brotli4j.encoder.BrotliOutputStream; import com.aayushatharva.brotli4j.encoder.Encoder; +import com.github.luben.zstd.Zstd; import com.sun.net.httpserver.Headers; import com.sun.net.httpserver.HttpExchange; import com.sun.net.httpserver.HttpHandler; import com.sun.net.httpserver.HttpServer; +import io.netty.handler.codec.compression.Brotli; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + import java.io.IOException; import java.io.OutputStream; import java.net.InetSocketAddress; @@ -29,133 +35,129 @@ import java.util.List; import java.util.stream.Collectors; import java.util.zip.GZIPOutputStream; -import org.junit.jupiter.api.AfterAll; -import org.junit.jupiter.api.BeforeAll; -import org.junit.jupiter.api.Test; -import com.github.luben.zstd.Zstd; import static org.junit.jupiter.api.Assertions.assertEquals; public class AutomaticDecompressionTest { - private static final String UNCOMPRESSED_PAYLOAD = "a".repeat(500); - - private static HttpServer HTTP_SERVER; - - private static AsyncHttpClient createClient() { - AsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() - .setEnableAutomaticDecompression(true) - .setCompressionEnforced(true) - .build(); - return new DefaultAsyncHttpClient(config); - } - - @BeforeAll - static void setupServer() throws Exception { - HTTP_SERVER = HttpServer.create(new InetSocketAddress(0), 0); - - HTTP_SERVER.createContext("/br").setHandler(new HttpHandler() { - @Override - public void handle(HttpExchange exchange) - throws IOException { - validateAcceptEncodingHeader(exchange); - exchange.getResponseHeaders().set("Content-Encoding", "br"); - exchange.sendResponseHeaders(200, 0); - OutputStream out = exchange.getResponseBody(); - Encoder.Parameters params = new Encoder.Parameters(); - BrotliOutputStream brotliOutputStream = new BrotliOutputStream(out, params); - brotliOutputStream.write(UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8)); - brotliOutputStream.flush(); - brotliOutputStream.close(); - } - }); - - HTTP_SERVER.createContext("/zstd").setHandler(new HttpHandler() { - @Override - public void handle(HttpExchange exchange) - throws IOException { - validateAcceptEncodingHeader(exchange); - exchange.getResponseHeaders().set("Content-Encoding", "zstd"); - byte[] compressedData = new byte[UNCOMPRESSED_PAYLOAD.length()]; - long n = Zstd.compress(compressedData, UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8), 2, true); - exchange.sendResponseHeaders(200, n); - OutputStream out = exchange.getResponseBody(); - out.write(compressedData, 0, (int) n); - out.flush(); - out.close(); - } - }); - - HTTP_SERVER.createContext("/gzip").setHandler(new HttpHandler() { - @Override - public void handle(HttpExchange exchange) - throws IOException { - validateAcceptEncodingHeader(exchange); - exchange.getResponseHeaders().set("Content-Encoding", "gzip"); - exchange.sendResponseHeaders(200, 0); - OutputStream out = exchange.getResponseBody(); - GZIPOutputStream gzip = new GZIPOutputStream(out); - gzip.write(UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8)); - gzip.flush(); - gzip.close(); - } - }); - - HTTP_SERVER.start(); - } - - private static void validateAcceptEncodingHeader(HttpExchange exchange) { - Headers requestHeaders = exchange.getRequestHeaders(); - List acceptEncodingList = requestHeaders.get("Accept-Encoding") - .stream() - .flatMap(x -> Arrays.asList(x.split(",")).stream()) - .collect(Collectors.toList()); - assertEquals(List.of("gzip", "deflate", "br", "zstd"), acceptEncodingList); - } - - @AfterAll - static void stopServer() { - if (HTTP_SERVER != null) { - HTTP_SERVER.stop(0); + private static final String UNCOMPRESSED_PAYLOAD = "a".repeat(500); + + private static HttpServer HTTP_SERVER; + + private static AsyncHttpClient createClient() { + AsyncHttpClientConfig config = new DefaultAsyncHttpClientConfig.Builder() + .setEnableAutomaticDecompression(true) + .setCompressionEnforced(true) + .build(); + return new DefaultAsyncHttpClient(config); + } + + @BeforeAll + static void setupServer() throws Exception { + HTTP_SERVER = HttpServer.create(new InetSocketAddress(0), 0); + + HTTP_SERVER.createContext("/br").setHandler(new HttpHandler() { + @Override + public void handle(HttpExchange exchange) + throws IOException { + validateAcceptEncodingHeader(exchange); + exchange.getResponseHeaders().set("Content-Encoding", "br"); + exchange.sendResponseHeaders(200, 0); + OutputStream out = exchange.getResponseBody(); + Encoder.Parameters params = new Encoder.Parameters(); + BrotliOutputStream brotliOutputStream = new BrotliOutputStream(out, params); + brotliOutputStream.write(UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8)); + brotliOutputStream.flush(); + brotliOutputStream.close(); + } + }); + + HTTP_SERVER.createContext("/zstd").setHandler(new HttpHandler() { + @Override + public void handle(HttpExchange exchange) + throws IOException { + validateAcceptEncodingHeader(exchange); + exchange.getResponseHeaders().set("Content-Encoding", "zstd"); + byte[] compressedData = new byte[UNCOMPRESSED_PAYLOAD.length()]; + long n = Zstd.compress(compressedData, UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8), 2, true); + exchange.sendResponseHeaders(200, n); + OutputStream out = exchange.getResponseBody(); + out.write(compressedData, 0, (int) n); + out.flush(); + out.close(); + } + }); + + HTTP_SERVER.createContext("/gzip").setHandler(new HttpHandler() { + @Override + public void handle(HttpExchange exchange) + throws IOException { + validateAcceptEncodingHeader(exchange); + exchange.getResponseHeaders().set("Content-Encoding", "gzip"); + exchange.sendResponseHeaders(200, 0); + OutputStream out = exchange.getResponseBody(); + GZIPOutputStream gzip = new GZIPOutputStream(out); + gzip.write(UNCOMPRESSED_PAYLOAD.getBytes(StandardCharsets.UTF_8)); + gzip.flush(); + gzip.close(); + } + }); + + HTTP_SERVER.start(); + } + + private static void validateAcceptEncodingHeader(HttpExchange exchange) { + Headers requestHeaders = exchange.getRequestHeaders(); + List acceptEncodingList = requestHeaders.get("Accept-Encoding") + .stream() + .flatMap(x -> Arrays.asList(x.split(",")).stream()) + .collect(Collectors.toList()); + assertEquals(List.of("gzip", "deflate", "br", "zstd"), acceptEncodingList); } - } - - @Test - void zstd() throws Throwable { - io.netty.handler.codec.compression.Zstd.ensureAvailability(); - try (AsyncHttpClient client = createClient()) { - Request request = new RequestBuilder("GET") - .setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/zstd") - .build(); - Response response = client.executeRequest(request).get(); - assertEquals(200, response.getStatusCode()); - assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody()); + + @AfterAll + static void stopServer() { + if (HTTP_SERVER != null) { + HTTP_SERVER.stop(0); + } } - } - - @Test - void brotli() throws Throwable { - io.netty.handler.codec.compression.Brotli.ensureAvailability(); - try (AsyncHttpClient client = createClient()) { - Request request = new RequestBuilder("GET") - .setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/br") - .build(); - Response response = client.executeRequest(request).get(); - assertEquals(200, response.getStatusCode()); - assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody()); + + @Test + void zstd() throws Throwable { + io.netty.handler.codec.compression.Zstd.ensureAvailability(); + try (AsyncHttpClient client = createClient()) { + Request request = new RequestBuilder("GET") + .setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/zstd") + .build(); + Response response = client.executeRequest(request).get(); + assertEquals(200, response.getStatusCode()); + assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody()); + } } - } - - @Test - void gzip() throws Throwable { - try (AsyncHttpClient client = createClient()) { - Request request = new RequestBuilder("GET") - .setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/gzip") - .build(); - Response response = client.executeRequest(request).get(); - assertEquals(200, response.getStatusCode()); - assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody()); + + @Test + void brotli() throws Throwable { + Brotli.ensureAvailability(); + try (AsyncHttpClient client = createClient()) { + Request request = new RequestBuilder("GET") + .setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/br") + .build(); + Response response = client.executeRequest(request).get(); + assertEquals(200, response.getStatusCode()); + assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody()); + } + } + + @Test + void gzip() throws Throwable { + try (AsyncHttpClient client = createClient()) { + Request request = new RequestBuilder("GET") + .setUrl("http://localhost:" + HTTP_SERVER.getAddress().getPort() + "/gzip") + .build(); + Response response = client.executeRequest(request).get(); + assertEquals(200, response.getStatusCode()); + assertEquals(UNCOMPRESSED_PAYLOAD, response.getResponseBody()); + } } - } } diff --git a/client/src/test/java/org/asynchttpclient/BasicHttpTest.java b/client/src/test/java/org/asynchttpclient/BasicHttpTest.java index cc75c03c1..f83cac80f 100755 --- a/client/src/test/java/org/asynchttpclient/BasicHttpTest.java +++ b/client/src/test/java/org/asynchttpclient/BasicHttpTest.java @@ -79,6 +79,7 @@ import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertThrows; @@ -559,7 +560,7 @@ public void connectFailureNotifiesHandlerWithConnectException() throws Throwable @Override public void onThrowable(Throwable t) { try { - assertTrue(t instanceof ConnectException); + assertInstanceOf(ConnectException.class, t); } finally { l.countDown(); } @@ -962,8 +963,8 @@ public void requestingPlainHttpEndpointOverHttpsThrowsSslException() throws Thro client.prepareGet(getTargetUrl().replace("http", "https")).execute().get(); fail("Request shouldn't succeed"); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof ConnectException, "Cause should be a ConnectException"); - assertTrue(e.getCause().getCause() instanceof SSLException, "Root cause should be a SslException"); + assertInstanceOf(ConnectException.class, e.getCause(), "Cause should be a ConnectException"); + assertInstanceOf(SSLException.class, e.getCause().getCause(), "Root cause should be a SslException"); } })); } diff --git a/client/src/test/java/org/asynchttpclient/PerRequestRelative302Test.java b/client/src/test/java/org/asynchttpclient/PerRequestRelative302Test.java index 4c119779c..ae3eccf85 100644 --- a/client/src/test/java/org/asynchttpclient/PerRequestRelative302Test.java +++ b/client/src/test/java/org/asynchttpclient/PerRequestRelative302Test.java @@ -38,6 +38,7 @@ import static org.asynchttpclient.test.TestUtils.addHttpConnector; import static org.asynchttpclient.test.TestUtils.findFreePort; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -125,7 +126,7 @@ public void redirected302InvalidTest() throws Exception { assertNotNull(e); Throwable cause = e.getCause(); - assertTrue(cause instanceof ConnectException); + assertInstanceOf(ConnectException.class, cause); assertTrue(cause.getMessage().contains(":" + port2)); } diff --git a/client/src/test/java/org/asynchttpclient/PerRequestTimeoutTest.java b/client/src/test/java/org/asynchttpclient/PerRequestTimeoutTest.java index b04f16533..bee7d0b67 100644 --- a/client/src/test/java/org/asynchttpclient/PerRequestTimeoutTest.java +++ b/client/src/test/java/org/asynchttpclient/PerRequestTimeoutTest.java @@ -34,6 +34,7 @@ import static org.asynchttpclient.Dsl.config; import static org.asynchttpclient.util.DateUtils.unpreciseMillisTime; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -73,7 +74,7 @@ public void testRequestTimeout() throws IOException { } catch (InterruptedException e) { fail("Interrupted.", e); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof TimeoutException); + assertInstanceOf(TimeoutException.class, e.getCause()); checkTimeoutMessage(e.getCause().getMessage(), true); } catch (TimeoutException e) { fail("Timeout.", e); @@ -89,7 +90,7 @@ public void testReadTimeout() throws IOException { } catch (InterruptedException e) { fail("Interrupted.", e); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof TimeoutException); + assertInstanceOf(TimeoutException.class, e.getCause()); checkTimeoutMessage(e.getCause().getMessage(), false); } catch (TimeoutException e) { fail("Timeout.", e); @@ -107,7 +108,7 @@ public void testGlobalDefaultPerRequestInfiniteTimeout() throws IOException { } catch (InterruptedException e) { fail("Interrupted.", e); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof TimeoutException); + assertInstanceOf(TimeoutException.class, e.getCause()); checkTimeoutMessage(e.getCause().getMessage(), true); } } @@ -121,7 +122,7 @@ public void testGlobalRequestTimeout() throws IOException { } catch (InterruptedException e) { fail("Interrupted.", e); } catch (ExecutionException e) { - assertTrue(e.getCause() instanceof TimeoutException); + assertInstanceOf(TimeoutException.class, e.getCause()); checkTimeoutMessage(e.getCause().getMessage(), true); } catch (TimeoutException e) { fail("Timeout.", e); diff --git a/client/src/test/java/org/asynchttpclient/Relative302Test.java b/client/src/test/java/org/asynchttpclient/Relative302Test.java index b4d254bfb..074930791 100644 --- a/client/src/test/java/org/asynchttpclient/Relative302Test.java +++ b/client/src/test/java/org/asynchttpclient/Relative302Test.java @@ -39,6 +39,7 @@ import static org.asynchttpclient.test.TestUtils.addHttpConnector; import static org.asynchttpclient.test.TestUtils.findFreePort; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -101,7 +102,7 @@ public void redirected302InvalidTest() throws Exception { assertNotNull(e); Throwable cause = e.getCause(); - assertTrue(cause instanceof ConnectException); + assertInstanceOf(ConnectException.class, cause); assertTrue(cause.getMessage().contains(":" + port2)); } diff --git a/client/src/test/java/org/asynchttpclient/netty/NettyAsyncResponseTest.java b/client/src/test/java/org/asynchttpclient/netty/NettyAsyncResponseTest.java index caadf19b2..5172bae7a 100644 --- a/client/src/test/java/org/asynchttpclient/netty/NettyAsyncResponseTest.java +++ b/client/src/test/java/org/asynchttpclient/netty/NettyAsyncResponseTest.java @@ -22,7 +22,11 @@ import java.nio.charset.StandardCharsets; import java.text.SimpleDateFormat; -import java.util.*; +import java.util.Date; +import java.util.LinkedList; +import java.util.List; +import java.util.Locale; +import java.util.TimeZone; import static io.netty.handler.codec.http.HttpHeaderNames.SET_COOKIE; import static org.junit.jupiter.api.Assertions.assertEquals; diff --git a/client/src/test/java/org/asynchttpclient/netty/NettyTest.java b/client/src/test/java/org/asynchttpclient/netty/NettyTest.java index 7878766f1..f80c0911e 100644 --- a/client/src/test/java/org/asynchttpclient/netty/NettyTest.java +++ b/client/src/test/java/org/asynchttpclient/netty/NettyTest.java @@ -13,43 +13,43 @@ public class NettyTest { @Test - @EnabledOnOs(value = OS.LINUX) + @EnabledOnOs(OS.LINUX) public void epollIsAvailableOnLinux() { assertTrue(Epoll.isAvailable()); } @Test - @EnabledOnOs(value = OS.LINUX) + @EnabledOnOs(OS.LINUX) public void ioUringIsAvailableOnLinux() { assertTrue(IOUring.isAvailable()); } @Test - @EnabledOnOs(value = OS.MAC) + @EnabledOnOs(OS.MAC) public void kqueueIsAvailableOnMac() { assertTrue(KQueue.isAvailable()); } @Test - @EnabledOnOs(value = OS.LINUX) + @EnabledOnOs(OS.LINUX) public void brotliIsAvailableOnLinux() { assertTrue(Brotli.isAvailable()); } @Test - @EnabledOnOs(value = OS.MAC) + @EnabledOnOs(OS.MAC) public void brotliIsAvailableOnMac() { assertTrue(Brotli.isAvailable()); } @Test - @EnabledOnOs(value = OS.LINUX) + @EnabledOnOs(OS.LINUX) public void zstdIsAvailableOnLinux() { assertTrue(Zstd.isAvailable()); } @Test - @EnabledOnOs(value = OS.MAC) + @EnabledOnOs(OS.MAC) public void zstdIsAvailableOnMac() { assertTrue(Zstd.isAvailable()); } diff --git a/client/src/test/java/org/asynchttpclient/ntlm/NtlmTest.java b/client/src/test/java/org/asynchttpclient/ntlm/NtlmTest.java index 93506925e..139274c84 100644 --- a/client/src/test/java/org/asynchttpclient/ntlm/NtlmTest.java +++ b/client/src/test/java/org/asynchttpclient/ntlm/NtlmTest.java @@ -100,7 +100,7 @@ public void testGenerateType1Msg() { @RepeatedIfExceptionsTest(repeats = 5) public void testGenerateType3MsgThrowsExceptionWhenChallengeTooShort() { NtlmEngine engine = new NtlmEngine(); - assertThrows(NtlmEngineException.class, () -> engine.generateType3Msg("username", "password", "localhost", "workstation", + assertThrows(NtlmEngineException.class, () -> NtlmEngine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString("a".getBytes())), "An NtlmEngineException must have occurred as challenge length is too short"); } @@ -108,7 +108,7 @@ public void testGenerateType3MsgThrowsExceptionWhenChallengeTooShort() { @RepeatedIfExceptionsTest(repeats = 5) public void testGenerateType3MsgThrowsExceptionWhenChallengeDoesNotFollowCorrectFormat() { NtlmEngine engine = new NtlmEngine(); - assertThrows(NtlmEngineException.class, () -> engine.generateType3Msg("username", "password", "localhost", "workstation", + assertThrows(NtlmEngineException.class, () -> NtlmEngine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString("challenge".getBytes())), "An NtlmEngineException must have occurred as challenge length is too short"); } @@ -125,7 +125,7 @@ public void testGenerateType3MsgThworsExceptionWhenType2IndicatorNotPresent() th buf.write(0); buf.write("challenge".getBytes()); NtlmEngine engine = new NtlmEngine(); - assertThrows(NtlmEngineException.class, () -> engine.generateType3Msg("username", "password", "localhost", "workstation", + assertThrows(NtlmEngineException.class, () -> NtlmEngine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())), "An NtlmEngineException must have occurred as type 2 indicator is incorrect"); } } @@ -151,7 +151,7 @@ public void testGenerateType3MsgThrowsExceptionWhenUnicodeSupportNotIndicated() buf.write(longToBytes(1L));// challenge NtlmEngine engine = new NtlmEngine(); - assertThrows(NtlmEngineException.class, () -> engine.generateType3Msg("username", "password", "localhost", "workstation", + assertThrows(NtlmEngineException.class, () -> NtlmEngine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())), "An NtlmEngineException must have occurred as unicode support is not indicated"); } @@ -184,7 +184,7 @@ public void testGenerateType3Msg() throws IOException { buf.write(longToBytes(1L));// challenge NtlmEngine engine = new NtlmEngine(); - String type3Msg = engine.generateType3Msg("username", "password", "localhost", "workstation", + String type3Msg = NtlmEngine.generateType3Msg("username", "password", "localhost", "workstation", Base64.getEncoder().encodeToString(buf.toByteArray())); assertEquals(type3Msg, "TlRMTVNTUAADAAAAGAAYAEgAAAAYABgAYAAAABIAEgB4AAAAEAAQAIoAAAAWABYAmgAAAAAAAACwAAAAAQAAAgUBKAoAAAAP1g6lqqN1HZ0wSSxeQ5riQkyh7/UexwVlCPQm0SHU2vsDQm2wM6NbT2zPonPzLJL0TABPAEMAQQBMAEgATwBTAFQAdQBzAGUAcgBuAGEAbQBlAFcATwBSAEsAUwBUAEEAVABJAE8ATgA=", diff --git a/client/src/test/java/org/asynchttpclient/request/body/PutByteBufTest.java b/client/src/test/java/org/asynchttpclient/request/body/PutByteBufTest.java index 8b0f9f230..3260604d3 100644 --- a/client/src/test/java/org/asynchttpclient/request/body/PutByteBufTest.java +++ b/client/src/test/java/org/asynchttpclient/request/body/PutByteBufTest.java @@ -25,6 +25,7 @@ import java.io.IOException; import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; import java.time.Duration; import java.util.Arrays; @@ -52,7 +53,7 @@ public void testPutSmallBody() throws Exception { public void testPutBigBody() throws Exception { byte[] array = new byte[2048]; Arrays.fill(array, (byte) 97); - String longString = new String(array, Charset.forName("UTF-8")); + String longString = new String(array, StandardCharsets.UTF_8); put(longString); } diff --git a/client/src/test/java/org/asynchttpclient/testserver/SocksProxy.java b/client/src/test/java/org/asynchttpclient/testserver/SocksProxy.java index d3b193209..b8e67b79f 100644 --- a/client/src/test/java/org/asynchttpclient/testserver/SocksProxy.java +++ b/client/src/test/java/org/asynchttpclient/testserver/SocksProxy.java @@ -39,15 +39,17 @@ public SocksProxy(int runningTime) throws IOException { Set keys = select.selectedKeys(); for (SelectionKey k : keys) { - if (!k.isValid()) + if (!k.isValid()) { continue; + } // new connection? if (k.isAcceptable() && k.channel() == socks) { // server socket SocketChannel csock = socks.accept(); - if (csock == null) + if (csock == null) { continue; + } addClient(csock); csock.register(select, SelectionKey.OP_READ); } else if (k.isReadable()) { @@ -56,14 +58,16 @@ public SocksProxy(int runningTime) throws IOException { SocksClient cl = clients.get(i); try { if (k.channel() == cl.client) // from client (e.g. socks client) + { cl.newClientData(select); - else if (k.channel() == cl.remote) { // from server client is connected to (e.g. website) + } else if (k.channel() == cl.remote) { // from server client is connected to (e.g. website) cl.newRemoteData(); } } catch (IOException e) { // error occurred - remove client cl.client.close(); - if (cl.remote != null) + if (cl.remote != null) { cl.remote.close(); + } k.cancel(); clients.remove(cl); } @@ -75,10 +79,11 @@ else if (k.channel() == cl.remote) { // from server client is connected to (e.g // client timeout check for (int i = 0; i < clients.size(); i++) { SocksClient cl = clients.get(i); - if ((System.currentTimeMillis() - cl.lastData) > 30000L) { + if (System.currentTimeMillis() - cl.lastData > 30000L) { cl.client.close(); - if (cl.remote != null) + if (cl.remote != null) { cl.remote.close(); + } clients.remove(cl); } } @@ -115,8 +120,9 @@ class SocksClient { void newRemoteData() throws IOException { ByteBuffer buf = ByteBuffer.allocate(1024); - if (remote.read(buf) == -1) + if (remote.read(buf) == -1) { throw new IOException("disconnected"); + } lastData = System.currentTimeMillis(); buf.flip(); client.write(buf); @@ -125,8 +131,9 @@ void newRemoteData() throws IOException { void newClientData(Selector selector) throws IOException { if (!connected) { ByteBuffer inbuf = ByteBuffer.allocate(512); - if (client.read(inbuf) < 1) + if (client.read(inbuf) < 1) { return; + } inbuf.flip(); // read socks header @@ -143,13 +150,15 @@ void newClientData(Selector selector) throws IOException { final int port = inbuf.getShort() & 0xffff; - final byte ip[] = new byte[4]; + final byte[] ip = new byte[4]; // fetch IP inbuf.get(ip); InetAddress remoteAddr = InetAddress.getByAddress(ip); - while ((inbuf.get()) != 0) ; // username + while (inbuf.get() != 0) { + ; // username + } // hostname provided, not IP if (ip[0] == 0 && ip[1] == 0 && ip[2] == 0 && ip[3] != 0) { // host provided @@ -172,8 +181,9 @@ void newClientData(Selector selector) throws IOException { out.flip(); client.write(out); - if (!remote.isConnected()) + if (!remote.isConnected()) { throw new IOException("connect failed"); + } remote.configureBlocking(false); remote.register(selector, SelectionKey.OP_READ); @@ -181,8 +191,9 @@ void newClientData(Selector selector) throws IOException { connected = true; } else { ByteBuffer buf = ByteBuffer.allocate(1024); - if (client.read(buf) == -1) + if (client.read(buf) == -1) { throw new IOException("disconnected"); + } lastData = System.currentTimeMillis(); buf.flip(); remote.write(buf);