pact-jvm-provider - JUnit5 Spring - 无法添加授权 headers
pact-jvm-provider - JUnit5 Spring - Unable to add authorization headers
我有一个 Spring 启动应用程序,我正在使用 Pact-JVM 为其编写合同测试。它是一个提供者。
我在 build.gradle 中添加了以下协议 dependencies/configurations:
我正在使用 Java 11,Gradle 6.3
plugins {
id 'au.com.dius.pact' version '4.1.6'
}
dependencies {
testImplementation 'ch.qos.logback:logback-core:1.2.3'
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
testImplementation 'au.com.dius.pact.provider:junit5:4.1.6'
}
这是我的验证测试:
@Provider("product")
@PactBroker(host = "localhost", scheme = "http", port = "9292")
class PactVerificationTest {
@BeforeEach
void setupTestTarget(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", 9002, "/"));
}
@TargetRequestFilter
public void addAuthorizationHeadersStripContextPathRequestFilter(HttpRequest request) throws URISyntaxException {
request.addHeader("USER_ID", "1231324123414");
request.addHeader("AB_USER", "1231324123414");
request.addHeader("AB_USERGROUP", "installtrial");
String rawUri = request.getRequestLine().getUri();
((HttpRequestBase) request).setURI(new URI(rawUri.replaceFirst("/product/", "/")));
}
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
@State("Given products available")
public void testProductDetails() {
}
}
我希望在 TargetRequestFilter 注释方法中添加的授权 headers 成为请求的一部分,但请求中缺少那些 headers。你能帮我解决我做错了什么吗?
此外,我正在寻找使用最新版本 pact-jvm 的示例。我在 pact-jvm.
提供的示例中找不到
Pact-JVM Slack 团队来拯救!
在 Pact-JVM + JUnit5 的情况下,添加 headers/request 操作可以在 @TestTemplate 注释方法本身中完成 - https://docs.pact.io/implementation_guides/jvm/provider/junit5/#modifying-the-requests-before-they-are-sent
现在我的方法是这样的:
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request) {
addAuthenticationHeaders(request);
context.verifyInteraction();
}
如果您想动态地更改 授权headers,我们就是这样做的。在这里,我们以教师身份开始测试,并在需要时调用该方法以用学生令牌覆盖访问令牌。
@Provider(PACT_PROVIDER)
@Consumer(PACT_CONSUMER)
@PactBroker(host = BROKER_PACT_URL)
public class PactProviderGcTest {
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactTestTemplate(PactVerificationContext context, HttpRequest request) {
request.addHeader("Authorization", "Bearer " + accessToken);
context.verifyInteraction();
}
@BeforeAll
public static void setUp() throws JSONException {
accessToken = getTeacherAccessToken();
}
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpsTestTarget(BASE_PACT_URL, 443, "/"));
}
@State("A request to retrieve google courses as a teacher")
void sampleState() {}
@State("A request to retrieve a google course as a teacher")
void sampleState1() {}
@State("A request to turn in a submission as a student")
void sampleState2() throws JSONException {
accessToken = getAccessTokenStudent();
}
}
// 获取令牌方法
public static String getTeacherAccessToken() {
accessToken = retrieveTokenFor("Teacher");
return accessToken;
}
public static String getAccessTokenStudent() throws JSONException {
accessToken = retrieveTokenFor("Student");
return accessToken;
}
我有一个 Spring 启动应用程序,我正在使用 Pact-JVM 为其编写合同测试。它是一个提供者。
我在 build.gradle 中添加了以下协议 dependencies/configurations: 我正在使用 Java 11,Gradle 6.3
plugins {
id 'au.com.dius.pact' version '4.1.6'
}
dependencies {
testImplementation 'ch.qos.logback:logback-core:1.2.3'
testImplementation 'ch.qos.logback:logback-classic:1.2.3'
testImplementation 'au.com.dius.pact.provider:junit5:4.1.6'
}
这是我的验证测试:
@Provider("product")
@PactBroker(host = "localhost", scheme = "http", port = "9292")
class PactVerificationTest {
@BeforeEach
void setupTestTarget(PactVerificationContext context) {
context.setTarget(new HttpTestTarget("localhost", 9002, "/"));
}
@TargetRequestFilter
public void addAuthorizationHeadersStripContextPathRequestFilter(HttpRequest request) throws URISyntaxException {
request.addHeader("USER_ID", "1231324123414");
request.addHeader("AB_USER", "1231324123414");
request.addHeader("AB_USERGROUP", "installtrial");
String rawUri = request.getRequestLine().getUri();
((HttpRequestBase) request).setURI(new URI(rawUri.replaceFirst("/product/", "/")));
}
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context) {
context.verifyInteraction();
}
@State("Given products available")
public void testProductDetails() {
}
}
我希望在 TargetRequestFilter 注释方法中添加的授权 headers 成为请求的一部分,但请求中缺少那些 headers。你能帮我解决我做错了什么吗?
此外,我正在寻找使用最新版本 pact-jvm 的示例。我在 pact-jvm.
提供的示例中找不到Pact-JVM Slack 团队来拯救!
在 Pact-JVM + JUnit5 的情况下,添加 headers/request 操作可以在 @TestTemplate 注释方法本身中完成 - https://docs.pact.io/implementation_guides/jvm/provider/junit5/#modifying-the-requests-before-they-are-sent
现在我的方法是这样的:
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactVerificationTestTemplate(PactVerificationContext context, HttpRequest request) {
addAuthenticationHeaders(request);
context.verifyInteraction();
}
如果您想动态地更改 授权headers,我们就是这样做的。在这里,我们以教师身份开始测试,并在需要时调用该方法以用学生令牌覆盖访问令牌。
@Provider(PACT_PROVIDER)
@Consumer(PACT_CONSUMER)
@PactBroker(host = BROKER_PACT_URL)
public class PactProviderGcTest {
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactTestTemplate(PactVerificationContext context, HttpRequest request) {
request.addHeader("Authorization", "Bearer " + accessToken);
context.verifyInteraction();
}
@BeforeAll
public static void setUp() throws JSONException {
accessToken = getTeacherAccessToken();
}
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpsTestTarget(BASE_PACT_URL, 443, "/"));
}
@State("A request to retrieve google courses as a teacher")
void sampleState() {}
@State("A request to retrieve a google course as a teacher")
void sampleState1() {}
@State("A request to turn in a submission as a student")
void sampleState2() throws JSONException {
accessToken = getAccessTokenStudent();
}
}
// 获取令牌方法
public static String getTeacherAccessToken() {
accessToken = retrieveTokenFor("Teacher");
return accessToken;
}
public static String getAccessTokenStudent() throws JSONException {
accessToken = retrieveTokenFor("Student");
return accessToken;
}