英文:
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("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();
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<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
@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() {
}
}
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("start_time", new Date().toString());
Here the full piece.
@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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论