Hi, I have a frontend application that communicat...
# pact-jvm
m
Hi, I have a frontend application that communicates with a Spring Boot backend microservice through a Kong API gateway. I want to write contract tests using Pact, but I am not sure how to handle the fact that the frontend talks to the backend through the gateway. In particular, the paths used by the frontend (e.g., "/api/v3/users") are different from those used by the backend (e.g., "/users") due to the gateway configuration. I read in the Pact documentation (https://docs.pact.io/recipes/apigateway#use-case-1-basic) that I could use request filters in the provider test to swap out the correct path, but I am not sure how to do this. Could someone provide an example of how to implement this solution, or suggest another way to approach this issue? This is some code snippet of how my provider looks like right now:
Copy code
@BeforeEach
    void before(PactVerificationContext context, @LocalServerPort int port) {
        context.setTarget(new HttpTestTarget("localhost", port));
    }

    @TestTemplate
    @ExtendWith(PactVerificationSpringProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context) {
        context.verifyInteraction();
    }
While I could implement a solution using this approach, I don't think it's the most optimal way to solve the problem.
Copy code
@BeforeEach
    void before(PactVerificationContext context, @LocalServerPort int port) {
        context.setTarget(new HttpTestTarget("localhost", port));
        Request request = ((RequestResponseInteraction) context.getInteraction()).getRequest();
        request.setPath(request.getPath().replace("/api/v3", ""));
    }
u
Better to get JUnit to inject the real request into the test method. You can just add an HTTP request object as a parameter to pactVerificationTestTemplate. See https://github.com/pact-foundation/pact-jvm/tree/master/provider/junit5#modifying-the-requests-before-they-are-sent
1