|
18 | 18 | import java.net.http.HttpResponse; |
19 | 19 | import java.net.http.WebSocket; |
20 | 20 | import java.time.Duration; |
| 21 | +import java.util.Arrays; |
21 | 22 | import java.util.List; |
22 | 23 | import java.util.Map; |
23 | 24 | import java.util.Optional; |
|
39 | 40 | import static org.junit.jupiter.api.Assertions.assertNull; |
40 | 41 | import static org.junit.jupiter.api.Assertions.assertThrows; |
41 | 42 | import static org.junit.jupiter.api.Assertions.assertTrue; |
| 43 | +import static org.junit.jupiter.api.Assumptions.assumeFalse; |
42 | 44 | import static org.mockserver.model.HttpRequest.request; |
43 | 45 | import static org.mockserver.model.HttpResponse.response; |
44 | 46 |
|
@@ -69,6 +71,34 @@ public void testDefaultConstructorCreatesUsableClient() throws Exception { |
69 | 71 | assertEquals("ok", response.body()); |
70 | 72 | } |
71 | 73 |
|
| 74 | + @Test |
| 75 | + public void testCustomHostHeaderRejectedByJdkHttpClient() { |
| 76 | + // Documents a JDK-wide restriction (not specific to this SDK): java.net.http.HttpClient |
| 77 | + // rejects an explicit "Host" header unless the JVM is started with |
| 78 | + // -Djdk.httpclient.allowRestrictedHeaders=host. Callers who need to send a custom Host |
| 79 | + // header should use that JVM property or a non-JDK A2AHttpClient implementation, e.g. |
| 80 | + // VertxA2AHttpClient from the a2a-java-sdk-http-client-vertx extra. |
| 81 | + // |
| 82 | + // Skip if the JVM running this test already allows a restricted "Host" header: in that |
| 83 | + // (documented, supported) configuration the JDK client legitimately accepts it instead. |
| 84 | + assumeFalse(jdkAllowsRestrictedHostHeader(), "JVM allows a restricted Host header " |
| 85 | + + "via jdk.httpclient.allowRestrictedHeaders; the JDK client no longer rejects it"); |
| 86 | + |
| 87 | + JdkA2AHttpClient client = new JdkA2AHttpClient(); |
| 88 | + |
| 89 | + assertThrows(IllegalArgumentException.class, () -> client.createGet() |
| 90 | + .url("http://localhost:1/unused") |
| 91 | + .addHeader("Host", "custom.example.com") |
| 92 | + .get()); |
| 93 | + } |
| 94 | + |
| 95 | + private static boolean jdkAllowsRestrictedHostHeader() { |
| 96 | + String allowed = System.getProperty("jdk.httpclient.allowRestrictedHeaders", ""); |
| 97 | + return Arrays.stream(allowed.split(",")) |
| 98 | + .map(String::trim) |
| 99 | + .anyMatch("host"::equalsIgnoreCase); |
| 100 | + } |
| 101 | + |
72 | 102 | @Test |
73 | 103 | public void testConstructorUsesProvidedHttpClient() throws Exception { |
74 | 104 | server = ClientAndServer.startClientAndServer(0); |
|
0 commit comments