PACT - Изменить заголовок, чтобы включить токен oAuth2 - PullRequest
0 голосов
/ 30 мая 2019

Я использую PACT JVM на стороне провайдера, и мне нужно добавить токен oAuth2 в заголовок, прежде чем будет сделан запрос.Я следовал совету в этом FAQ

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

и создал следующий класс:

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!
      }

    }

Однако я понятия не имею, какперехватить запрос перед тем, как изменить заголовок и включить токен.

1 Ответ

0 голосов
/ 30 мая 2019

Видимо @TargetRequestFilter делает работу. Вот документ https://static.javadoc.io/au.com.dius/pact-jvm-provider-junit_2.11/3.2.3/au/com/dius/pact/provider/junit/TargetRequestFilter.html

...