diff --git a/src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java b/src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java index 587e303..b22033a 100644 --- a/src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java +++ b/src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java @@ -22,6 +22,7 @@ import java.net.Authenticator; import java.net.CookieHandler; import java.net.ProxySelector; +import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpRequest.BodyPublishers; @@ -162,8 +163,8 @@ public CompletableFuture> sendAsync(HttpRequest request, Bod } private CompletableFuture> send0(HttpRequest request, BodyHandler bodyHandler, Sender sender) { - if (!allowInsecure && "http".equalsIgnoreCase(request.uri().getScheme())) { - throw new IllegalArgumentException("Client does not allow insecure HTTP requests."); + if (!allowInsecure) { + checkInsecureScheme(request); } Chain chain = buildAndExecute(RequestContext.of(request, bodyHandler)); @@ -176,6 +177,13 @@ private CompletableFuture> send0(HttpRequest request, BodyHa return future.isDone() ? future.handle(handler) : future.handleAsync(handler); } + private void checkInsecureScheme(HttpRequest request) { + URI uri = request.uri(); + if ("http".equalsIgnoreCase(uri.getScheme())) { + throw new IllegalArgumentException("Client does not allow insecure HTTP requests. URI: " + uri); + } + } + private Chain buildAndExecute(RequestContext ctx) { Chain chain = Chain.of(ctx); chain = compressionInterceptor != null ? compressionInterceptor.intercept(chain) : chain; diff --git a/src/test/kotlin/io/github/nstdio/http/ext/ExtendedHttpClientTest.kt b/src/test/kotlin/io/github/nstdio/http/ext/ExtendedHttpClientTest.kt index bca979d..b64bd2b 100644 --- a/src/test/kotlin/io/github/nstdio/http/ext/ExtendedHttpClientTest.kt +++ b/src/test/kotlin/io/github/nstdio/http/ext/ExtendedHttpClientTest.kt @@ -17,6 +17,8 @@ package io.github.nstdio.http.ext import io.github.nstdio.http.ext.ExtendedHttpClient.Builder import io.kotest.assertions.throwables.shouldThrowExactly +import io.kotest.matchers.should +import io.kotest.matchers.string.shouldEndWith import io.kotest.matchers.throwable.shouldHaveCause import io.kotest.matchers.types.shouldBeSameInstanceAs import org.assertj.core.api.Assertions.assertThatExceptionOfType @@ -117,15 +119,18 @@ internal class ExtendedHttpClientTest { val mockBuilderDelegate = mock(HttpClient.Builder::class.java) given(mockBuilderDelegate.build()).willReturn(mockDelegate) + val uri = "HTTP://abc.local" client = Builder(mockBuilderDelegate) .allowInsecure(false) .build() - val request = HttpRequest.newBuilder("HTTP://abc.local".toUri()).build() + val request = HttpRequest.newBuilder(uri.toUri()).build() //when + then shouldThrowExactly { client.send(request, ofString()) + }.should { + it.message.shouldEndWith("URI: $uri") } }