0

I am using PACT JVM in the provider side and I need to add an oAuth2 token to the header before a request is made. I have followed the advice in this FAQ

https://docs.pact.io/faq#how-do-i-test-oauth-or-other-security-headers

and I have created the following class:

public class TransactionPact {

    @TestTarget
    public final Target target = new HttpTarget(8332);

    private static FinanceApiToken financeApiToken;

      @BeforeClass
      public static void getAuthorisationToken() {
        HttpHeaders header = new HttpHeaders();
        header.set("Content-Type", "application/x-www-form-urlencoded");
        header.set("Authorization", "Basic ZmluLWFwaTphcGktc2VjcmV0");
        header.set("Connection", "keep-alive");
        header.set("cache-control", "no-cache");

        HttpEntity<String> request = new HttpEntity<>("username=sap&password=password2&grant_type=password", header);

        RestTemplate restTemplate = new RestTemplate();
        ResponseEntity<FinanceApiToken> response = restTemplate.postForEntity("http://127.0.0.1:8080/oauth/token", request, FinanceApiToken.class);

        if (response.getStatusCode() != HttpStatus.OK) {
            throw new HTTPException(response.getStatusCodeValue());
        }
        financeApiToken = response.getBody();
      }

      @State("the consumer is authorised")
      public void authorise() {


        //MODIFY HEADER HERE!
      }

    }

However, I have no idea how to intercept a request before is made in order to modify the header and include the token.

Evdzhan Mustafa
  • 3,645
  • 1
  • 24
  • 40
user3727540
  • 922
  • 3
  • 11
  • 30

1 Answers1

0

Apparently @TargetRequestFilter does job. Here is the doc https://static.javadoc.io/au.com.dius/pact-jvm-provider-junit_2.11/3.2.3/au/com/dius/pact/provider/junit/TargetRequestFilter.html

user3727540
  • 922
  • 3
  • 11
  • 30