Skip to content

Commit

Permalink
test: restore mapper injection
Browse files Browse the repository at this point in the history
  • Loading branch information
NiccoMlt committed Nov 14, 2024
1 parent 40fd6c2 commit c8925be
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -85,21 +85,22 @@ 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
properties.put("connectionsmanager.backendsunreachableonstuckrequests", backendsUnreachableOnStuckRequests + "");
// 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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ public UnreachableBackendTest(boolean useCache) {

@Test
public void testWithUnreachableBackend() throws Exception {

stubFor(get(urlEqualTo("/index.html"))
.willReturn(aResponse()
.withStatus(200)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}

0 comments on commit c8925be

Please sign in to comment.