11package com .retailsvc .http ;
22
3+ import static java .net .http .HttpClient .Version .HTTP_1_1 ;
34import static java .util .Collections .emptyMap ;
5+ import static java .util .concurrent .Executors .newVirtualThreadPerTaskExecutor ;
46import static org .assertj .core .api .Assertions .assertThat ;
57import static org .assertj .core .api .Assertions .assertThatThrownBy ;
68import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
1012import java .net .HttpURLConnection ;
1113import java .net .InetAddress ;
1214import java .net .URI ;
15+ import java .net .http .HttpClient ;
16+ import java .net .http .HttpRequest ;
17+ import java .net .http .HttpResponse ;
1318import java .util .List ;
1419import java .util .Map ;
1520import org .junit .jupiter .api .Test ;
@@ -59,23 +64,28 @@ void testExceptionIsThrownOnInvalidHttpPort() {
5964 }
6065
6166 @ Test
62- void shouldBindOnlyToLoopbackWhenBindAddressIsLoopback () throws IOException {
67+ void shouldBindOnlyToLoopbackWhenBindAddressIsLoopback () throws Exception {
6368 try (var server =
6469 OpenApiServer .builder ()
6570 .spec (testSpec ())
6671 .handlers (emptyMap ())
6772 .port (0 )
6873 .bindAddress (InetAddress .getLoopbackAddress ())
6974 .build ()) {
75+ assertThat (server .bindAddress ().isLoopbackAddress ()).isTrue ();
7076 int port = server .listenPort ();
71- HttpURLConnection conn =
72- (HttpURLConnection )
73- URI .create ("http://127.0.0.1:" + port + "/api/missing" ).toURL ().openConnection ();
74- try {
75- assertThat (conn .getResponseCode ()).isEqualTo (HttpURLConnection .HTTP_NOT_FOUND );
76- } finally {
77- conn .disconnect ();
78- }
77+ HttpClient client =
78+ HttpClient .newBuilder ()
79+ .executor (newVirtualThreadPerTaskExecutor ())
80+ .version (HTTP_1_1 )
81+ .build ();
82+ HttpRequest request =
83+ HttpRequest .newBuilder ()
84+ .uri (URI .create ("http://127.0.0.1:" + port + "/api/missing" ))
85+ .GET ()
86+ .build ();
87+ HttpResponse <Void > response = client .send (request , HttpResponse .BodyHandlers .discarding ());
88+ assertThat (response .statusCode ()).isEqualTo (HttpURLConnection .HTTP_NOT_FOUND );
7989 }
8090 }
8191
@@ -87,19 +97,6 @@ void shouldBindToWildcardWhenBindAddressIsUnset() throws IOException {
8797 }
8898 }
8999
90- @ Test
91- void shouldBindToWildcardWhenBindAddressIsExplicitlyNull () throws IOException {
92- try (var server =
93- OpenApiServer .builder ()
94- .spec (testSpec ())
95- .handlers (emptyMap ())
96- .port (0 )
97- .bindAddress (null )
98- .build ()) {
99- assertThat (server .bindAddress ().isAnyLocalAddress ()).isTrue ();
100- }
101- }
102-
103100 private Spec testSpec () {
104101 Map <String , Object > raw =
105102 Map .of (
0 commit comments