我在提供者端使用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!
}
}
但是,我不知道如何在修改标头和包含令牌之前截取请求。
答案 0 :(得分: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