diff --git a/carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java b/carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java
index 0fd29a378..3d1fe24ef 100644
--- a/carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java
+++ b/carapace-server/src/test/java/org/carapaceproxy/BigUploadTest.java
@@ -19,6 +19,8 @@
*/
package org.carapaceproxy;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
@@ -35,11 +37,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;
import org.apache.commons.io.IOUtils;
-import org.carapaceproxy.core.EndpointKey;
import org.carapaceproxy.core.HttpProxyServer;
import org.carapaceproxy.utils.TestEndpointMapper;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -172,7 +171,6 @@ public void testConnectionResetByPeerDuringWriteToEndpoint() throws Exception {
mockServer.start();
TestEndpointMapper mapper = new TestEndpointMapper("localhost", mockServer.getPort());
- EndpointKey key = new EndpointKey("localhost", mockServer.getPort());
int size = 20_000_000;
@@ -203,16 +201,16 @@ public void testConnectionResetByPeerDuringWriteToEndpoint() throws Exception {
@Test
public void testBlockingServerWorks() throws Exception {
-
- try (SimpleBlockingTcpServer mockServer =
- new SimpleBlockingTcpServer(() -> {
- return new StaticResponseHandler("HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\n\r\nit works!\r\n".getBytes(StandardCharsets.US_ASCII));
- })) {
-
+ SimpleBlockingTcpServer mockServer = new SimpleBlockingTcpServer(() -> new StaticResponseHandler("""
+ HTTP/1.1 200 OK\r
+ Content-Type: text/plain\r
+ \r
+ it works!\r
+ """.getBytes(StandardCharsets.US_ASCII)));
+ try (mockServer) {
mockServer.start();
TestEndpointMapper mapper = new TestEndpointMapper("localhost", mockServer.getPort());
- EndpointKey key = new EndpointKey("localhost", mockServer.getPort());
try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
server.start();
diff --git a/carapace-server/src/test/java/org/carapaceproxy/ConcurrentClientsTest.java b/carapace-server/src/test/java/org/carapaceproxy/ConcurrentClientsTest.java
index 53682fd45..207161a3b 100644
--- a/carapace-server/src/test/java/org/carapaceproxy/ConcurrentClientsTest.java
+++ b/carapace-server/src/test/java/org/carapaceproxy/ConcurrentClientsTest.java
@@ -23,6 +23,9 @@
import static com.github.tomakehurst.wiremock.client.WireMock.get;
import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
import com.github.tomakehurst.wiremock.junit.WireMockRule;
import java.net.URI;
import java.nio.charset.StandardCharsets;
@@ -32,12 +35,8 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;
import org.apache.commons.io.IOUtils;
-import org.carapaceproxy.core.EndpointKey;
import org.carapaceproxy.core.HttpProxyServer;
import org.carapaceproxy.utils.TestEndpointMapper;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -64,11 +63,10 @@ public void test() throws Exception {
.withBody("it works !!")));
TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port());
- EndpointKey key = new EndpointKey("localhost", wireMockRule.port());
int size = 100;
int concurrentClients = 4;
- try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
+ try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
server.start();
int port = server.getLocalPort();
diff --git a/carapace-server/src/test/java/org/carapaceproxy/DatabaseConfigurationTest.java b/carapace-server/src/test/java/org/carapaceproxy/DatabaseConfigurationTest.java
index 7b3d35033..130fa5c28 100644
--- a/carapace-server/src/test/java/org/carapaceproxy/DatabaseConfigurationTest.java
+++ b/carapace-server/src/test/java/org/carapaceproxy/DatabaseConfigurationTest.java
@@ -19,17 +19,18 @@
under the License.
*/
+
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
import java.io.File;
-import org.carapaceproxy.core.HttpProxyServer;
import java.util.Properties;
import org.carapaceproxy.configstore.HerdDBConfigurationStore;
import org.carapaceproxy.configstore.PropertiesConfigurationStore;
+import org.carapaceproxy.core.HttpProxyServer;
import org.carapaceproxy.server.filters.RegexpMapUserIdFilter;
import org.carapaceproxy.server.filters.XForwardedForRequestFilter;
-import static org.hamcrest.CoreMatchers.instanceOf;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertTrue;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
diff --git a/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java b/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java
index fa98b5ea6..c7628d480 100644
--- a/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java
+++ b/carapace-server/src/test/java/org/carapaceproxy/RawClientTest.java
@@ -71,7 +71,6 @@
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;
-import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -148,7 +147,7 @@ public void clientsKeepAliveSimpleTest() throws Exception {
.withBody("it works !!")));
TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port());
- try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
+ try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
server.start();
int port = server.getLocalPort();
try (RawHttpClient client = new RawHttpClient("localhost", port)) {
@@ -194,9 +193,8 @@ public void downloadSmallPayloadsTest() throws Exception {
.withBody("a")));
TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port());
- EndpointKey key = new EndpointKey("localhost", wireMockRule.port());
- try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
+ try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
server.start();
int port = server.getLocalPort();
@@ -235,7 +233,7 @@ public void testManyInflightRequests() throws Exception {
.withBody("it works !!")));
TestEndpointMapper mapper = new TestEndpointMapper("localhost", wireMockRule.port());
- try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
+ try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
server.start();
int port = server.getLocalPort();
assertTrue(port > 0);
@@ -247,68 +245,68 @@ public void testManyInflightRequests() throws Exception {
clients.add(client);
}
- int i = 0;
for (RawHttpClient client : clients) {
client.close();
- i++;
}
}
}
@Test
public void testKeepAliveTimeout() throws Exception {
- RawHttpServer httpServer = new RawHttpServer(new HttpServlet() {
+ final RawHttpServer httpServer = new RawHttpServer(new HttpServlet() {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("it works !!");
}
});
- httpServer.setIdleTimeout(5);
- int httpServerPort = httpServer.start();
-
- TestEndpointMapper mapper = new TestEndpointMapper("localhost", httpServerPort);
- try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
- server.start();
- int port = server.getLocalPort();
- assertTrue(port > 0);
+ try (httpServer) {
+ httpServer.setIdleTimeout(5);
+ int httpServerPort = httpServer.start();
+
+ TestEndpointMapper mapper = new TestEndpointMapper("localhost", httpServerPort);
+ try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
+ server.start();
+ int port = server.getLocalPort();
+ assertTrue(port > 0);
- try (RawHttpClient client = new RawHttpClient("localhost", port)) {
- for (int j = 0; j < 2; j++) {
- RawHttpClient.HttpResponse res = client.get("/index.html");
- String resp = res.getBodyString();
- System.out.println("RESP: " + resp + "; HEADERS: " + String.join("; ", res.getHeaderLines()));
- Thread.sleep(10_000);
+ try (RawHttpClient client = new RawHttpClient("localhost", port)) {
+ for (int j = 0; j < 2; j++) {
+ RawHttpClient.HttpResponse res = client.get("/index.html");
+ String resp = res.getBodyString();
+ System.out.println("RESP: " + resp + "; HEADERS: " + String.join("; ", res.getHeaderLines()));
+ Thread.sleep(10_000);
+ }
+ } catch (Exception e) {
+ System.out.println("EXCEPTION: " + e);
}
- } catch (Exception e) {
- System.out.println("EXCEPTION: " + e);
}
}
}
@Test
public void testEmptyDataFromServer() throws Exception {
-
- RawHttpServer httpServer = new RawHttpServer(new HttpServlet() {
+ final RawHttpServer httpServer = new RawHttpServer(new HttpServlet() {
public void doGet(HttpServletRequest request, HttpServletResponse response) {
}
});
- int httpServerPort = httpServer.start();
-
- TestEndpointMapper mapper = new TestEndpointMapper("localhost", httpServerPort);
- try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder());) {
- server.start();
- int port = server.getLocalPort();
- assertTrue(port > 0);
+ try (httpServer) {
+ int httpServerPort = httpServer.start();
+ TestEndpointMapper mapper = new TestEndpointMapper("localhost", httpServerPort);
+ try (HttpProxyServer server = HttpProxyServer.buildForTests("localhost", 0, mapper, tmpDir.newFolder())) {
+ server.start();
+ int port = server.getLocalPort();
+ assertTrue(port > 0);
- try (RawHttpClient client = new RawHttpClient("localhost", port)) {
- for (int j = 0; j < 2; j++) {
- RawHttpClient.HttpResponse res = client.get("/index.html");
- String resp = res.getBodyString();
- System.out.println("RESP: " + resp + "; HEADERS: " + String.join("; ", res.getHeaderLines()));
+ try (RawHttpClient client = new RawHttpClient("localhost", port)) {
+ for (int j = 0; j < 2; j++) {
+ RawHttpClient.HttpResponse res = client.get("/index.html");
+ String resp = res.getBodyString();
+ System.out.println("RESP: " + resp + "; HEADERS: " + String.join("; ", res.getHeaderLines()));
+ }
+ } catch (Exception e) {
+ System.out.println("EXCEPTION: " + e);
}
- } catch (Exception e) {
- System.out.println("EXCEPTION: " + e);
}
}
}
@@ -316,12 +314,13 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) {
@Test
public void testServerRequestContinue() throws Exception {
AtomicBoolean responseEnabled = new AtomicBoolean();
- try (DummyServer server = new DummyServer("localhost", 8086, responseEnabled)) {
+ final int dummyServerPort = 8086;
+ try (DummyServer server = new DummyServer("localhost", dummyServerPort, responseEnabled)) {
- TestEndpointMapper mapper = new TestEndpointMapper("localhost", 8086);
+ TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyServerPort);
ExecutorService ex = Executors.newFixedThreadPool(2);
- List futures = new ArrayList<>();
+ List> futures = new ArrayList<>();
CarapaceLogger.setLoggingDebugEnabled(true);
@@ -399,7 +398,7 @@ public void testServerRequestContinue() throws Exception {
}
}));
} finally {
- for (Future future : futures) {
+ for (Future> future : futures) {
try {
future.get();
} catch (InterruptedException | ExecutionException e) {
@@ -437,15 +436,14 @@ public DummyServer(String host, int port, AtomicBoolean responseEnabled) throws
public void initChannel(SocketChannel channel) {
channel.pipeline().addLast(new HttpRequestDecoder());
channel.pipeline().addLast(new HttpResponseEncoder());
- channel.pipeline().addLast(new SimpleChannelInboundHandler