Is it possible to use wiremock to dynamically retu...
# help
j
Is it possible to use wiremock to dynamically return a response based on unix datetimes sent as query parameters? These times would always be relative to now (i.e. an hour ago, a day ago, a week ago). For instance, suppose I have some api endpoint
/api/v1/endpoint?from=1715887464&to=1716492264
where
to
is always now but
from
is some time before now . Currently using the wiremock/wiremock:2.35.1-1 docker image
a
Hello @Joshua Osko, I am not clear what you are trying to do. You want the response based on your query parameter?
l
You can create dynamic responses in WireMock. All the docs are in the response templating part of the website - https://wiremock.org/docs/response-templating/ In the response templating docs you will see that you can reference the details of the incoming request:
{{request.query.from}}
{{<http://request.query.to|request.query.to>}}
You can also parse a date in a unix format:
Copy code
// Format can also be unix (epoch seconds) or epoch (epoch milliseconds)
{{date (parseDate request.headers.MyDate format='unix')}}
Obviously a lot of this will depend on what you want to do in your response but hopefully that will get you started. Also, it is worth noting that the format of how query parameters are accessed from the request changed so you might need to take a look at that for the version you are running. You are on a crazy old version of WireMock. Is there a reason you can't use the latest (3.6.0)
a
If what you are trying to do is return a formatted datetime based on that unit timestamp, you can do so with templates. https://wiremock.org/docs/response-templating/
j
I want to return different responses based on the timestamp specified by a
to_timestamp
query param and a
from_timestamp
query param. The rest of the request is the same, but two of the params are unix timestamps and they will change each time this endpoint is called. Essentially we want to have three different responses: one for returning data over either the last day, last week, or last hour; however the service we are mocking here uses timestamps so we can't just statically use some tag like "last-week" and hence there will always be different values for these timestamps each time this endpoint is called. We are not trying to format the datetime or change the query params in any way. In other words if we make the request 100 times there would be 100 different `to_timestamp`s and 100 different `from_timestamp`s, but the delta between the to_timestamp and from_timestamp would either be an hour, a day, or a week. We want to map those 100 different to_timstamps and 100 different from_timestamp combos to one of 3 potential responses (this is a demo data system and we are presenting data over these three different time ranges) I'm unclear on how the response templates would work for this. I want to change the entire response based on the difference between the two query params Will upgrade our wiremock - had no idea it was that out of date. hah
l
I am not sure we have the date/time based helpers or matchers to make this possible at the moment. Some handlebars implementations have additional helpers like
daysBetween
,
weeksBetween
and the more generic
timeBetween
which might have helped here. The implementation of handlebars we are using doesn't have those helpers which is a shame. I don't think it would be too hard to put a WireMock extension together that adds these helpers if that was something you fancied looking at?