diff --git a/core/src/main/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptor.java b/core/src/main/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptor.java index e0196c56c..29e1c41f6 100644 --- a/core/src/main/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptor.java +++ b/core/src/main/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptor.java @@ -11,9 +11,12 @@ import com.predic8.membrane.core.interceptor.Outcome; import com.predic8.membrane.core.rules.AbstractServiceProxy.Target; import com.predic8.membrane.core.transport.http.HttpClient; +import com.predic8.membrane.core.util.URIFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import java.io.IOException; +import java.net.URISyntaxException; import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutorService; @@ -41,7 +44,7 @@ public void bodyChunk(byte[] buffer, int offset, int length) {} @Override public void bodyComplete(AbstractBody body) { - cloneRequestAndSend(body); + cloneRequestAndSend(body, exc); } }); return CONTINUE; @@ -52,15 +55,12 @@ public String getShortDescription() { return "Sends requests to shadow hosts (processed in the background)."; } - public void cloneRequestAndSend(AbstractBody body) { + public void cloneRequestAndSend(AbstractBody body, Exchange exchange) { ExecutorService executor = newCachedThreadPool(); for (Target target : targets) { Exchange exc; try { - exc = new Request.Builder() - .body(body.getContent()) - .get(getDestFromTarget(target, router.getParentProxy(this).getKey().getPath())) - .buildExchange(); + exc = buildExchange(body, exchange, target); } catch (Exception e) { log.error("Error creating request for target {}", target, e); continue; @@ -78,13 +78,28 @@ public void cloneRequestAndSend(AbstractBody body) { } } + static Exchange buildExchange(AbstractBody body, Exchange exchange, Target target) throws URISyntaxException, IOException { + return new Request.Builder() + .body(body.getContent()) + .header(exchange.getRequest().getHeader()) + .method(exchange.getRequest().getMethod()) + .url( + new URIFactory(), + getDestFromTarget( + target, + exchange.getOriginalRequestUri() + ) + ) + .buildExchange(); + } + static String getDestFromTarget(Target t, String path) { - return (t.getUrl() != null) ? t.getUrl() : extracted(t, path); + return (t.getUrl() != null) ? t.getUrl() : buildTargetUrl(t, path); } @SuppressWarnings("HttpUrlsUsage") - private static String extracted(Target t, String path) { + private static String buildTargetUrl(Target t, String path) { return ((t.getSslParser() != null) ? "https://" : "http://") + t.getHost() + ":" + diff --git a/core/src/test/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptorTest.java b/core/src/test/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptorTest.java index 6ed225b89..44fe62a0a 100644 --- a/core/src/test/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptorTest.java +++ b/core/src/test/java/com/predic8/membrane/core/interceptor/shadowing/ShadowingInterceptorTest.java @@ -1,37 +1,40 @@ package com.predic8.membrane.core.interceptor.shadowing; +import com.predic8.membrane.core.exchange.Exchange; +import com.predic8.membrane.core.http.Body; +import com.predic8.membrane.core.http.MimeType; +import com.predic8.membrane.core.http.Request; import com.predic8.membrane.core.rules.AbstractServiceProxy.Target; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; +import java.io.IOException; +import java.net.URISyntaxException; +import java.net.http.HttpHeaders; + +import static com.predic8.membrane.core.http.Header.CONTENT_TYPE; +import static com.predic8.membrane.core.http.MimeType.APPLICATION_JSON; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.mockito.Mockito.when; +import static org.junit.jupiter.api.Assertions.assertNotNull; class ShadowingInterceptorTest { - @Mock - private Target mockTarget; - - @BeforeEach - void setUp() { - MockitoAnnotations.openMocks(this); - } - @Test - void testGetDestFromTarget_WithUrl() { - when(mockTarget.getUrl()).thenReturn("http://example.com"); - String result = ShadowingInterceptor.getDestFromTarget(mockTarget, "/path"); - assertEquals("http://example.com", result); + void buildExchangeTest() throws URISyntaxException, IOException { + Exchange exc = ShadowingInterceptor.buildExchange( + new Body("foo".getBytes()), + new Request.Builder() + .post("https://www.google.com") + .header(CONTENT_TYPE, APPLICATION_JSON) + .buildExchange(), + new Target() {{ + setUrl("https://www.predic8.com:9000/foo"); + }} + ); + + assertNotNull(exc); + assertEquals("POST", exc.getRequest().getMethod()); + assertEquals("/foo", exc.getRequest().getUri()); + assertEquals("https://www.predic8.com:9000/foo", exc.getDestinations().get(0)); + assertEquals(APPLICATION_JSON, exc.getRequest().getHeader().getContentType()); } - - @Test - void testGetDestFromTarget_WithoutUrl() { - when(mockTarget.getHost()).thenReturn("localhost"); - when(mockTarget.getPort()).thenReturn(8080); - String result = ShadowingInterceptor.getDestFromTarget(mockTarget, "/path"); - assertEquals("http://localhost:8080/path", result); - } - -} +} \ No newline at end of file