0

我在提供者端使用 PACT JVM,我需要在发出请求之前将 oAuth2 令牌添加到标头。我已遵循此常见问题解答中的建议

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

    }

但是,我不知道如何在发出请求之前拦截请求以修改标头并包含令牌。

4

1 回答 1

0

显然@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

于 2019-05-30T10:23:35.703 回答