如何在 JVM PACT 合同中更新 POST 参数体上的动态日期?

huangapple go评论58阅读模式
英文:

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

问题

以下是您提供的内容的中文翻译部分:

我在合同文件中有一个需要从参数中获取日期的POST请求,用于进行PACT测试。

return builder
    .uponReceiving("创建Zoom会议的请求")
    .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("创建Zoom会议的请求")
            .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() {

    //模拟URL
    RestAssured.baseURI = "http://localhost:8080";

    Response response = RestAssured
            .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("创建Zoom会议的请求")
public void sampleState() {
}

}

非常感谢。

英文:

I have a POST request that takes date as a param from within my contract file for a PACT test.

return builder
.uponReceiving(&quot;A request to create a Zoom meeting&quot;)
.path(createMeeting)
.method(&quot;POST&quot;)
.headers(headers)
.body(&quot;{\&quot;title\&quot;:\&quot;My title\&quot;,\&quot;start_time\&quot;:\&quot;2020-08-28T14:30:00Z+01:00\&quot;,\&quot;duration\&quot;:30,\&quot;provider\&quot;:\&quot;ZOOM\&quot;}&quot;)
.willRespondWith()
.body(body)
.toPact();

But I'd like this to be dynamic, having perhaps today's or tomorrow's date, otherwise it would have an expired date. Could you please advise on how to do do this and if possible to keep it from the consumer side.

These are both Consumer and Provider samples for my request.

Consumer

@ExtendWith(PactConsumerTestExt.class)
public class PACTConsumerEdUiVcTest {
Map&lt;String, String&gt; headers = new HashMap&lt;&gt;();
String createMeeting = &quot;/manage/create-meeting&quot;;
@Pact(provider = VC, consumer = ED_UI)
public RequestResponsePact createPact(PactDslWithProvider builder) {
headers.put(&quot;Content-Type&quot;, &quot;application/json&quot;);
DslPart body = new PactDslJsonBody()
.date(&quot;start_time&quot;, &quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.000&#39;Z&#39;&quot;, new Date());
return builder
.uponReceiving(&quot;A request to create a Zoom meeting&quot;)
.path(createMeeting)
.method(&quot;POST&quot;)
.headers(headers)
.body(&quot;{\&quot;title\&quot;:\&quot;My title\&quot;,\&quot;start_time\&quot;:\&quot;2020-08-28T14:30:00Z+01:00\&quot;,\&quot;duration\&quot;:30,\&quot;provider\&quot;:\&quot;ZOOM\&quot;}&quot;)
.willRespondWith()
.body(body)
.toPact();
}
@Test
@PactTestFor(providerName = VC, port = &quot;8080&quot;)
public void runTest() {
//Mock url
RestAssured.baseURI = &quot;http://localhost:8080&quot;;
Response response = RestAssured //todo: dynamic start time that won&#39;t expire. 27/08/2020
.given()
.headers(headers)
.when()
.body(&quot;{\&quot;title\&quot;:\&quot;My title\&quot;,\&quot;start_time\&quot;:\&quot;2020-08-28T14:30:00Z+01:00\&quot;,\&quot;duration\&quot;:30,\&quot;provider\&quot;:\&quot;ZOOM\&quot;}&quot;)
.post(createMeeting);
assert (response.getStatusCode() == 200);
}
}

Provider

@Provider(VC)
@PactFolder(&quot;target/pacts&quot;)
public class PactProviderEdUiVcTest {
@TestTemplate
@ExtendWith(PactVerificationInvocationContextProvider.class)
void pactTestTemplate(PactVerificationContext context, HttpRequest request) {
request.addHeader(&quot;Authorization&quot;, AUTHORIZATION_TOKEN);
context.verifyInteraction();
}
@BeforeEach
void before(PactVerificationContext context) {
context.setTarget(new HttpsTestTarget(BASE_PACT_VC_URL, 443, &quot;/&quot;));
getAuthorizationToken(UserType.TEACHER);
}
@State(&quot;A request to create a Zoom meeting&quot;)
public void sampleState() {
}
}

Many thanks.

答案1

得分: 0

能够使其工作的方法是将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
            .given()
            .headers(headers)
            .when()
            .body(map)
            .post(createMeeting);

    assert (response.getStatusCode() == 200);
}
英文:

Able to make it working changing the RestAssured implementation to accept a map and use the date as its value.

map.put(&quot;start_time&quot;, new Date().toString());

Here the full piece.

@Test
@PactTestFor(providerName = VC, port = &quot;8080&quot;)
public void runTest() {
//Mock url
RestAssured.baseURI = &quot;http://localhost:8080&quot;;
Map&lt;String, Object&gt; map = new HashMap&lt;&gt;();
map.put(&quot;title&quot;, &quot;MyTitle&quot;);
map.put(&quot;start_time&quot;, new Date().toString());
map.put(&quot;duration&quot;, 30);
map.put(&quot;provider&quot;, &quot;ZOOM&quot;);
Response response = RestAssured //todo: dynamic start time that won&#39;t expire. 27/08/2020
.given()
.headers(headers)
.when()
.body(map)
.post(createMeeting);
assert (response.getStatusCode() == 200);
}

huangapple
  • 本文由 发表于 2020年8月27日 20:41:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/63616229.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定