我有一个 POST 请求,它将日期作为我的合同文件中的参数进行 PACT 测试。
return builder
.uponReceiving("A request to create a Zoom meeting")
.path(createMeeting)
.method("POST")
.headers(headers)
.body("{\"title\":\"My title\",\"start_time\":\"2020-08-28T14:30:00Z+01:00\",\"duration\":30,\"provider\":\"ZOOM\"}")
.willRespondWith()
.body(body)
.toPact();
但我希望这是动态的,可能有今天或明天的日期,否则它会有一个过期的日期。你能否就如何做到这一点提出建议,如果可能的话,不要让消费者看到它。
这些都是我要求的消费者和提供者样本。
消费者
@ExtendWith(PactConsumerTestExt.class)
public class PACTConsumerEdUiVcTest {
Map<String, String> headers = new HashMap<>();
String createMeeting = "/manage/create-meeting";
@Pact(provider = VC, consumer = ED_UI)
public RequestResponsePact createPact(PactDslWithProvider builder) {
headers.put("Content-Type", "application/json");
DslPart body = new PactDslJsonBody()
.date("start_time", "yyyy-MM-dd'T'HH:mm:ss.000'Z'", new Date());
return builder
.uponReceiving("A request to create a Zoom meeting")
.path(createMeeting)
.method("POST")
.headers(headers)
.body("{\"title\":\"My title\",\"start_time\":\"2020-08-28T14:30:00Z+01:00\",\"duration\":30,\"provider\":\"ZOOM\"}")
.willRespondWith()
.body(body)
.toPact();
}
@Test
@PactTestFor(providerName = VC, port = "8080")
public void runTest() {
//Mock url
RestAssured.baseURI = "http://localhost:8080";
Response response = RestAssured //todo: dynamic start time that won't expire. 27/08/2020
.given()
.headers(headers)
.when()
.body("{\"title\":\"My title\",\"start_time\":\"2020-08-28T14:30:00Z+01:00\",\"duration\":30,\"provider\":\"ZOOM\"}")
.post(createMeeting);
assert (response.getStatusCode() == 200);
}
}
提供者
@Provider(VC)
@PactFolder("target/pacts")
public class PactProviderEdUiVcTest {
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactTestTemplate(PactVerificationContext context, HttpRequest request) {
request.addHeader("Authorization", AUTHORIZATION_TOKEN);
context.verifyInteraction();
}
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpsTestTarget(BASE_PACT_VC_URL, 443, "/"));
getAuthorizationToken(UserType.TEACHER);
}
@State("A request to create a Zoom meeting")
public void sampleState() {
}
}
非常感谢。