Raphael
11/19/2024, 3:08 PM<dependency>
<groupId>io.quarkiverse.wiremock</groupId>
<artifactId>quarkus-wiremock</artifactId>
<version>1.4.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>io.quarkiverse.wiremock</groupId>
<artifactId>quarkus-wiremock-test</artifactId>
<version>1.4.0</version>
<scope>test</scope>
</dependency>
I want to check the token JSON with a dynamic parameter for epoch time using WireMock. I created a template called token-issue-mapping.json
and utilized the Handlebars templating feature. In this template, I want to extract the current epoch time from the request body using the following expression: {{jsonPath request.body '$.currentEpochTime'}}
.
{
"request": {
"method": "POST",
"url": "/LN.WebServices/api/Token/Issue",
"headers": {
"Content-Type": {
"equalTo": "application/json"
}
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"token_type\":\"Bearer\",\"expires_in\":1800,\"expires_on\":1732032000,\"not_before\":{{jsonPath request.body '$.currentEpochTime'}},\"resource\":\"JwtTokenIssuer\",\"access_token\":\"eyJ0eXAiOiJKV1QifQ.eyJhdWQiOiJ1cm46YnJpZGdlcnhnIiwiaXNzIjoiSnd0V\"}"
}
}
In my method test, I am passing the body JSON with the current epoch time.
@Test
void testTokenIssueEndpoint() {
Assertions.assertNotNull(wiremock);
long currentEpochTime = Instant.now().getEpochSecond();
given()
.contentType("application/json")
.body("{\"currentEpochTime\": " + currentEpochTime + "}")
.when()
.post(url + "/LN.WebServices/api/Token/Issue")
.then()
.statusCode(200)
.body("token_type", is("Bearer"))
.body("expires_in", is(1800))
.body("expires_on", is(1732032000))//(int) (currentEpochTime + 1800)
.body("not_before", is((int) currentEpochTime))
.body("resource", is("JwtTokenIssuer"))
.body("access_token", is("eyJ0eXAiOiJKV1QifQ.eyJhdWQiOiJ1cm46YnJpZGdlcnhnIiwiaXNzIjoiSnd0V"));
}
I also enabled in application.properties the Handlebars templating feature:
wiremock.templating.enabled=true
When I run the test, it's failed to evaluate the {{jsonPath request.body '$.currentEpochTime'}}:
see below snippet log console of the exception:
Request method: POST
Request URI: <http://localhost:54850/LN.WebServices/api/Token/Issue>
Proxy: <none>
Request params: <none>
Query params: <none>
Form params: <none>
Path params: <none>
Headers: Accept=*/*
Content-Type=application/json
Cookies: <none>
Multiparts: <none>
Body:
{
"currentEpochTime": 1732027918
}
HTTP/1.1 200 OK
Content-Type: application/json
Matched-Stub-Id: d2dc9a9a-e8e9-49fb-b54c-7cef7e9414b8
Content-Encoding: gzip
Transfer-Encoding: chunked
{"token_type":"Bearer","expires_in":1800,"expires_on":1732032000,"not_before":{{jsonPath request.body '$.currentEpochTime'}},"resource":"JwtTokenIssuer","access_token":"eyJ0eXAiOiJKV1QifQ.eyJhdWQiOiJ1cm46YnJpZGdlcnhnIiwiaXNzIjoiSnd0V"}
groovy.json.JsonException: Expected a string key on line: 1, column: 80.
But got '{' instead.
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
What might be wrong in my case?Lee Turner
11/19/2024, 4:35 PMwiremock.templating.enabled=true
property before. Are you sure this is the correct property to set to enable templating?
Could you try adding it to the template and see if that works:
{
"request": {
"method": "POST",
"url": "/LN.WebServices/api/Token/Issue",
"headers": {
"Content-Type": {
"equalTo": "application/json"
}
}
},
"response": {
"status": 200,
"headers": {
"Content-Type": "application/json"
},
"body": "{\"token_type\":\"Bearer\",\"expires_in\":1800,\"expires_on\":1732032000,\"not_before\":{{jsonPath request.body '$.currentEpochTime'}},\"resource\":\"JwtTokenIssuer\",\"access_token\":\"eyJ0eXAiOiJKV1QifQ.eyJhdWQiOiJ1cm46YnJpZGdlcnhnIiwiaXNzIjoiSnd0V\"}",
"transformers": [
"response-template"
]
}
}
Raphael
11/20/2024, 11:38 AMRaphael
11/20/2024, 11:42 AMLee Turner
11/20/2024, 1:07 PM