Skip to content

Commit

Permalink
chore: Improve insecure request exception message.
Browse files Browse the repository at this point in the history
  • Loading branch information
nstdio committed Sep 3, 2022
1 parent b3d5687 commit 5a8cb08
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/main/java/io/github/nstdio/http/ext/ExtendedHttpClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -162,8 +163,8 @@ public <T> CompletableFuture<HttpResponse<T>> sendAsync(HttpRequest request, Bod
}

private <T> CompletableFuture<HttpResponse<T>> send0(HttpRequest request, BodyHandler<T> bodyHandler, Sender<T> sender) {
if (!allowInsecure && "http".equalsIgnoreCase(request.uri().getScheme())) {
throw new IllegalArgumentException("Client does not allow insecure HTTP requests.");
if (!allowInsecure) {
checkInsecureScheme(request);
}

Chain<T> chain = buildAndExecute(RequestContext.of(request, bodyHandler));
Expand All @@ -176,6 +177,13 @@ private <T> CompletableFuture<HttpResponse<T>> 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 <T> Chain<T> buildAndExecute(RequestContext ctx) {
Chain<T> chain = Chain.of(ctx);
chain = compressionInterceptor != null ? compressionInterceptor.intercept(chain) : chain;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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<IllegalArgumentException> {
client.send(request, ofString())
}.should {
it.message.shouldEndWith("URI: $uri")
}
}

Expand Down

0 comments on commit 5a8cb08

Please sign in to comment.