:wave: Hello, team! I'm seeking help for the firs...
# wiremock-java
r
👋 Hello, team! I'm seeking help for the first time using WireMock within the Quarkus platform version 3.13.0. I have added the dependencies below.
Copy code
<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'}}
.
Copy code
{
  "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.
Copy code
@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:
Copy code
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:
Copy code
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?
l
I haven't seen the
wiremock.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:
Copy code
{
  "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"
    ]
  }
}
r
Hi, thanks for the quick response! The suggested property is a Copilot hallucination 🙂 . After adding "transformers": ["response-template"], it worked for me!
🙌 1
also replaced the constant value 1732032000 in expires_on using the expression of Handlebars : {{math (jsonPath request.body '$.currentEpochTime') '+' 1800}}. Thanks!
l
Great stuff. Glad that worked