Skip to content

Commit

Permalink
Enhance ShadowingInterceptor to include header in request cloning
Browse files Browse the repository at this point in the history
- Add header parameter to cloneRequestAndSend and buildExchange methods
- Capture request headers for shadowing functionality
- Update tests to verify header handling during shadow requests
  • Loading branch information
christiangoerdes committed Sep 6, 2024
1 parent 00446a5 commit 0f747f3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public class ShadowingInterceptor extends AbstractInterceptor {

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
Header header = new Header(exc.getRequest().getHeader());
exc.getRequest().getBody().getObservers().add(new MessageObserver() {
@Override
public void bodyRequested(AbstractBody body) {}
Expand All @@ -41,7 +42,7 @@ public void bodyChunk(byte[] buffer, int offset, int length) {}

@Override
public void bodyComplete(AbstractBody body) {
cloneRequestAndSend(body, exc);
cloneRequestAndSend(body, exc, header);
}
});
return CONTINUE;
Expand All @@ -52,12 +53,12 @@ public String getShortDescription() {
return "Sends requests to shadow hosts (processed in the background).";
}

public void cloneRequestAndSend(AbstractBody body, Exchange exchange) {
public void cloneRequestAndSend(AbstractBody body, Exchange exchange, Header header) {
ExecutorService executor = newCachedThreadPool();
for (Target target : targets) {
Exchange exc;
try {
exc = buildExchange(body, exchange, target);
exc = buildExchange(body, exchange, target, header);
} catch (Exception e) {
log.error("Error creating request for target {}", target, e);
continue;
Expand All @@ -75,10 +76,10 @@ public void cloneRequestAndSend(AbstractBody body, Exchange exchange) {
}
}

static Exchange buildExchange(AbstractBody body, Exchange exchange, Target target) throws URISyntaxException, IOException {
static Exchange buildExchange(AbstractBody body, Exchange exchange, Target target, Header header) throws URISyntaxException, IOException {
return new Request.Builder()
.body(body.getContent())
.header(new Header(exchange.getRequest().getHeader()))
.header(header)
.method(exchange.getRequest().getMethod())
.url(
new URIFactory(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.predic8.membrane.core.http.Header;
import com.predic8.membrane.core.http.Request;
import com.predic8.membrane.core.interceptor.misc.ReturnInterceptor;
import com.predic8.membrane.core.interceptor.misc.SetHeaderInterceptor;
import com.predic8.membrane.core.rules.AbstractServiceProxy.Target;
import com.predic8.membrane.core.rules.Rule;
import com.predic8.membrane.core.rules.ServiceProxy;
Expand All @@ -16,6 +17,7 @@
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mockito;

import java.util.List;
Expand All @@ -24,8 +26,7 @@
import static io.restassured.RestAssured.given;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.*;

class ShadowingInterceptorTest {

Expand Down Expand Up @@ -55,7 +56,8 @@ void setUp() throws Exception {
.buildExchange(),
new Target() {{
setUrl("https://www.predic8.com:9000/foo");
}}
}},
header
);
}

Expand All @@ -72,7 +74,14 @@ static void startup() throws Exception {
setHost("localhost");
setPort(3000);
}}));
interceptorRule.setInterceptors(List.of(shadowingInterceptor, new ReturnInterceptor()));
interceptorRule.setInterceptors(List.of(
shadowingInterceptor,
new SetHeaderInterceptor() {{
setName("foo");
setValue("bar");
}},
new ReturnInterceptor()
));

interceptorRouter.getRuleManager().addProxyAndOpenPortIfNew(interceptorRule);
interceptorRouter.init();
Expand Down Expand Up @@ -109,10 +118,24 @@ void testIfShadowTargetIsCalled() throws Exception {
verify(returnInterceptorMock, times(1)).handleRequest(any(Exchange.class));
}

/**
* Verifies that the shadow target is called and the ReturnInterceptor's
* handleRequest() is invoked with an Exchange object not containing the "foo" header.
*/
@Test
void testIfShadowTargetHasFooHeader() throws Exception {
given().when().get("http://localhost:2000").then().statusCode(200);

ArgumentCaptor<Exchange> exchangeCaptor = ArgumentCaptor.forClass(Exchange.class);
verify(returnInterceptorMock, atLeastOnce()).handleRequest(exchangeCaptor.capture());

assertNull(exchangeCaptor.getValue().getRequest().getHeader().getFirstValue("foo"));
}


@Test
void buildExchangeTest() {
assertNotNull(exc);
assertNotSame(header, exc.getRequest().getHeader());
assertEquals("POST", exc.getRequest().getMethod());
assertEquals("/foo", exc.getRequest().getUri());
assertEquals("https://www.predic8.com:9000/foo", exc.getDestinations().get(0));
Expand Down

0 comments on commit 0f747f3

Please sign in to comment.