如何更新 JVM PACT 合同中 post 参数的动态日期?

How to update dynamic date on body for post params within JVM PACT contract?

我有一个 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() {
}

}

非常感谢。

能够改变 RestAssured 实现以接受 map 并使用日期作为其值。

map.put("start_time", new Date().toString());

这里是全文。

@Test
@PactTestFor(providerName = VC, port = "8080")
public void runTest() {

    //Mock url
    RestAssured.baseURI = "http://localhost:8080";

    Map<String, Object> map = new HashMap<>();
    map.put("title", "MyTitle");
    map.put("start_time", new Date().toString());
    map.put("duration", 30);
    map.put("provider", "ZOOM");

    Response response = RestAssured //todo: dynamic start time that won't expire. 27/08/2020
            .given()
            .headers(headers)
            .when()
            .body(map)
            .post(createMeeting);

    assert (response.getStatusCode() == 200);
}