Skip to content

Commit 800ba4f

Browse files
committed
test: jetty: Close HttpClient
1 parent 89bc18d commit 800ba4f

File tree

1 file changed

+48
-44
lines changed

1 file changed

+48
-44
lines changed

junixsocket-jetty/src/test/java/org/newsclub/net/unix/jetty/UnixDomainTest.java

+48-44
Original file line numberDiff line numberDiff line change
@@ -158,14 +158,15 @@ public boolean handle(Request request, Response response, Callback callback)
158158
// ClientConnector clientConnector = ClientConnector.forUnixDomain(unixDomainPath);
159159
ClientConnector clientConnector = AFSocketClientConnector.withSocketAddress(AFUNIXSocketAddress
160160
.of(unixDomainPath));
161-
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
162-
httpClient.start();
163-
try {
164-
ContentResponse response = httpClient.newRequest(uri).timeout(5, TimeUnit.SECONDS).send();
161+
try (HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector))) {
162+
httpClient.start();
163+
try {
164+
ContentResponse response = httpClient.newRequest(uri).timeout(5, TimeUnit.SECONDS).send();
165165

166-
assertEquals(HttpStatus.OK_200, response.getStatus());
167-
} finally {
168-
httpClient.stop();
166+
assertEquals(HttpStatus.OK_200, response.getStatus());
167+
} finally {
168+
httpClient.stop();
169+
}
169170
}
170171
}
171172

@@ -199,16 +200,17 @@ public boolean handle(Request request, Response response, Callback callback)
199200
fakeProxyPort), null, new Origin.Protocol(List.of("http/1.1"), false), AFSocketTransport
200201
.withSocketChannel(unixDomainAddress)), null);
201202

202-
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
203-
httpClient.getProxyConfiguration().addProxy(proxy);
204-
httpClient.start();
205-
try {
206-
ContentResponse response = httpClient.newRequest("localhost", fakeServerPort).timeout(5,
207-
TimeUnit.SECONDS).send();
203+
try (HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector))) {
204+
httpClient.getProxyConfiguration().addProxy(proxy);
205+
httpClient.start();
206+
try {
207+
ContentResponse response = httpClient.newRequest("localhost", fakeServerPort).timeout(5,
208+
TimeUnit.SECONDS).send();
208209

209-
assertEquals(HttpStatus.OK_200, response.getStatus());
210-
} finally {
211-
httpClient.stop();
210+
assertEquals(HttpStatus.OK_200, response.getStatus());
211+
} finally {
212+
httpClient.stop();
213+
}
212214
}
213215
}
214216

@@ -256,25 +258,26 @@ public boolean handle(Request request, Response response, Callback callback)
256258
ClientConnector clientConnector = AFSocketClientConnector.withSocketAddress(AFUNIXSocketAddress
257259
.of(unixDomainPath));
258260

259-
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
260-
httpClient.start();
261-
try {
262-
// Try PROXYv1 with the PROXY information retrieved from the EndPoint.
263-
// PROXYv1 does not support the UNIX family.
264-
ContentResponse response1 = httpClient.newRequest("localhost", 0).path("/v1").tag(
265-
new V1.Tag()).timeout(5, TimeUnit.SECONDS).send();
266-
267-
assertEquals(HttpStatus.OK_200, response1.getStatus());
268-
269-
// Try PROXYv2 with explicit PROXY information.
270-
V2.Tag tag = new V2.Tag(V2.Tag.Command.PROXY, V2.Tag.Family.UNIX, V2.Tag.Protocol.STREAM,
271-
srcAddr, 0, dstAddr, 0, null);
272-
ContentResponse response2 = httpClient.newRequest("localhost", 0).path("/v2").tag(tag)
273-
.timeout(5, TimeUnit.SECONDS).send();
274-
275-
assertEquals(HttpStatus.OK_200, response2.getStatus());
276-
} finally {
277-
httpClient.stop();
261+
try (HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector))) {
262+
httpClient.start();
263+
try {
264+
// Try PROXYv1 with the PROXY information retrieved from the EndPoint.
265+
// PROXYv1 does not support the UNIX family.
266+
ContentResponse response1 = httpClient.newRequest("localhost", 0).path("/v1").tag(
267+
new V1.Tag()).timeout(5, TimeUnit.SECONDS).send();
268+
269+
assertEquals(HttpStatus.OK_200, response1.getStatus());
270+
271+
// Try PROXYv2 with explicit PROXY information.
272+
V2.Tag tag = new V2.Tag(V2.Tag.Command.PROXY, V2.Tag.Family.UNIX, V2.Tag.Protocol.STREAM,
273+
srcAddr, 0, dstAddr, 0, null);
274+
ContentResponse response2 = httpClient.newRequest("localhost", 0).path("/v2").tag(tag)
275+
.timeout(5, TimeUnit.SECONDS).send();
276+
277+
assertEquals(HttpStatus.OK_200, response2.getStatus());
278+
} finally {
279+
httpClient.stop();
280+
}
278281
}
279282
}
280283

@@ -351,16 +354,17 @@ public boolean handle(Request request, Response response, Callback callback)
351354
// ClientConnector clientConnector = ClientConnector.forUnixDomain(unixDomainPath);
352355
ClientConnector clientConnector = AFSocketClientConnector.withSocketAddress(AFUNIXSocketAddress
353356
.of(unixDomainPath));
354-
HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector));
355-
httpClient.start();
356-
try {
357-
ContentResponse response = httpClient.newRequest(uri).timeout(5, TimeUnit.SECONDS).send();
357+
try (HttpClient httpClient = new HttpClient(new HttpClientTransportDynamic(clientConnector))) {
358+
httpClient.start();
359+
try {
360+
ContentResponse response = httpClient.newRequest(uri).timeout(5, TimeUnit.SECONDS).send();
358361

359-
assertEquals(HttpStatus.OK_200, response.getStatus());
360-
byte[] data = response.getContent();
361-
assertArrayEquals(payload, data);
362-
} finally {
363-
httpClient.stop();
362+
assertEquals(HttpStatus.OK_200, response.getStatus());
363+
byte[] data = response.getContent();
364+
assertArrayEquals(payload, data);
365+
} finally {
366+
httpClient.stop();
367+
}
364368
}
365369
}
366370
}

0 commit comments

Comments
 (0)