Skip to content

Commit

Permalink
Enhance ShadowingInterceptor by adding exchange construction method
Browse files Browse the repository at this point in the history
Refactor cloneRequestAndSend to accept an Exchange parameter and use a new buildExchange method for creating requests. Update tests to validate the new functionality in ShadowingInterceptor.
  • Loading branch information
christiangoerdes committed Sep 5, 2024
1 parent 1bd5a25 commit 6b37d6a
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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() +
":" +
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

}
}

0 comments on commit 6b37d6a

Please sign in to comment.