diff --git a/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java b/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java index 5e6fadde9..e76130534 100644 --- a/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java +++ b/carapace-server/src/main/java/org/carapaceproxy/core/HttpProxyServer.java @@ -287,6 +287,11 @@ public int getLocalPort() { return listeners.getLocalPort(); } + @VisibleForTesting + public void setMapper(final EndpointMapper mapper) { + this.mapper = mapper; + } + public void startAdminInterface() throws Exception { if (!adminServerEnabled) { return; diff --git a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java index 21121e8b8..b9680c18f 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/StuckRequestsTest.java @@ -44,9 +44,9 @@ import org.carapaceproxy.server.config.NetworkListenerConfiguration; import org.carapaceproxy.server.config.RouteConfiguration; import org.carapaceproxy.server.config.SafeBackendSelector; -import org.carapaceproxy.server.mapper.EndpointMapper; import org.carapaceproxy.server.mapper.StandardEndpointMapper; import org.carapaceproxy.server.mapper.requestmatcher.RegexpRequestMatcher; +import org.carapaceproxy.utils.EndpointMapperHolder; import org.carapaceproxy.utils.RawHttpClient; import org.junit.Rule; import org.junit.Test; @@ -85,14 +85,14 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt final int theport = wireMockRule.port(); EndpointKey key = new EndpointKey("localhost", theport); - final EndpointMapper.Factory mapperFactory = parent -> { + final EndpointMapperHolder mapperFactory = new EndpointMapperHolder(parent -> { StandardEndpointMapper mapper = new StandardEndpointMapper(parent, SafeBackendSelector::new); mapper.addBackend(new BackendConfiguration("backend-a", "localhost", theport, "/", -1)); mapper.addDirector(new DirectorConfiguration("director-1").addBackend("backend-a")); mapper.addAction(new ActionConfiguration("proxy-1", ActionConfiguration.TYPE_PROXY, "director-1", null, -1)); mapper.addRoute(new RouteConfiguration("route-1", "proxy-1", true, new RegexpRequestMatcher(PROPERTY_URI, ".*index.html.*"))); return mapper; - }; + }); try (HttpProxyServer server = new HttpProxyServer(mapperFactory, tmpDir.newFolder());) { Properties properties = new Properties(); properties.put("connectionsmanager.stuckrequesttimeout", "100"); // ms @@ -100,6 +100,7 @@ public void testBackendUnreachableOnStuckRequest(boolean backendsUnreachableOnSt // configure resets all listeners configurations server.configureAtBoot(new PropertiesConfigurationStore(properties)); server.addListener(new NetworkListenerConfiguration("localhost", 0)); + server.setMapper(mapperFactory.getEndpointMapper()); assertEquals(100, server.getCurrentConfiguration().getStuckRequestTimeout()); assertEquals(backendsUnreachableOnStuckRequests, server.getCurrentConfiguration().isBackendsUnreachableOnStuckRequests()); server.start(); diff --git a/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java b/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java index 9ed576dd3..574ea016f 100644 --- a/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java +++ b/carapace-server/src/test/java/org/carapaceproxy/backends/UnreachableBackendTest.java @@ -69,7 +69,6 @@ public UnreachableBackendTest(boolean useCache) { @Test public void testWithUnreachableBackend() throws Exception { - stubFor(get(urlEqualTo("/index.html")) .willReturn(aResponse() .withStatus(200) @@ -114,11 +113,12 @@ public void testEmptyResponse() throws Exception { TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache); EndpointKey key = new EndpointKey("localhost", dummyport); - try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) { + try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder());) { Properties properties = new Properties(); // configure resets all listeners configurations server.configureAtBoot(new PropertiesConfigurationStore(properties)); server.addListener(new NetworkListenerConfiguration("localhost", 0)); + server.setMapper(mapper); server.start(); int port = server.getLocalPort(); assertTrue(port > 0); @@ -182,10 +182,11 @@ public void testNonHttpResponseThenClose() throws Exception { TestEndpointMapper mapper = new TestEndpointMapper("localhost", dummyport, useCache); EndpointKey key = new EndpointKey("localhost", dummyport); - try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder())) { + try (HttpProxyServer server = new HttpProxyServer(mapper, tmpDir.newFolder());) { Properties properties = new Properties(); server.configureAtBoot(new PropertiesConfigurationStore(properties)); server.addListener(new NetworkListenerConfiguration("localhost", 0)); + server.setMapper(mapper); server.start(); int port = server.getLocalPort(); diff --git a/carapace-server/src/test/java/org/carapaceproxy/utils/EndpointMapperHolder.java b/carapace-server/src/test/java/org/carapaceproxy/utils/EndpointMapperHolder.java new file mode 100644 index 000000000..2ceaffe16 --- /dev/null +++ b/carapace-server/src/test/java/org/carapaceproxy/utils/EndpointMapperHolder.java @@ -0,0 +1,26 @@ +package org.carapaceproxy.utils; + +import org.carapaceproxy.core.HttpProxyServer; +import org.carapaceproxy.server.config.ConfigurationNotValidException; +import org.carapaceproxy.server.mapper.EndpointMapper; + +public class EndpointMapperHolder implements EndpointMapper.Factory { + private final EndpointMapper.Factory factory; + private EndpointMapper endpointMapper; + public EndpointMapperHolder(final EndpointMapper.Factory factory) { + this.factory = factory; + } + + public EndpointMapper getEndpointMapper() { + return endpointMapper; + } + + @Override + public EndpointMapper build(final HttpProxyServer httpProxyServer) throws ConfigurationNotValidException { + if (endpointMapper == null) { + endpointMapper = factory.build(httpProxyServer); + } + + return endpointMapper; + } +}