Hello, im new to Pact and i want to know if there ...
# pact-jvm
m
Hello, im new to Pact and i want to know if there is an easy way to overcome protected endpoints as the Provider. Endpoint expects an Bearer Token.
y
Hi, yes you can, This is covered in our workshop, would recommend you run though it https://github.com/pact-foundation/pact-workshop-jvm-spring
m
Hey i already saw this example, sadly it doesnt work for me. Maybe i have the wrong dependency versions i dont know. Just to clarify i need to modify the request before sending it. my Code looks like this
Copy code
@SpringBootTest(classes = { X.class }, webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
@Provider("ProviderX")
@Consumer("ConsumerX")
@PactFolder("src/test/resources/pacts")
@ExtendWith(SpringExtension.class)
public class XProviderTest {


    @BeforeEach
    void before(PactVerificationContext context) throws MalformedURLException {
        String myProviderUrl = "<Http://localhost:8080>";
        context.setTarget(HttpTestTarget.fromUrl(new URL(myProviderUrl)));

    }


    @TestTemplate    @ExtendWith(PactVerificationInvocationContextProvider.class)
    void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request) {
        replaceAuthHeader(request);
        context.verifyInteraction();
    }

    private void replaceAuthHeader(HttpRequest request) {
        if (request.containsHeader("authorization")) {
            String header = "Bearer " + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").format(new Date());
            request.removeHeaders("authorization");
            request.addHeader("authorization", header);
        }
    }
i need to change the replaceAuthHeader method later for our rules for a valid Bearer Token. but i get an error while running the test. org.junit.jupiter.api.extension.ParameterResolutionException: No ParameterResolver registered for parameter [org.apache.http.HttpRequest arg1] in method [void pactVerificationTestTemplate(au.com.dius.pact.provider.junit5.PactVerificationContext,org.apache.http.HttpRequest)].
y
Hmm that looks the same as our example, I note your constructing your url with port, which is fine, your auth header isn't capitalised? Is that correct. What versions of the pact provider lib are you using? (And what version Java?) thx
m
yes it isnt capitalised. Im using java version 11 and provider version 4.3.6
y
Cheers, are you able to talk the time to create a reproduction, possibly from that example repo? That's probably where I would start as we have a working example, upgrade it and see it it breaks (fyi not checked what version it is running atm) Otherwise some more familiar with the Java offerings will help out 👍
m
Ok, thanks!
👍 1
y
Just in case you haven't seen these docs, if you have its useful reference ( for me especially, if I need to update things later :) ) https://docs.pact.io/implementation_guides/jvm/provider/junit5spring https://docs.pact.io/implementation_guides/jvm/provider/junit5#modifying-the-requests-before-they-are-sent