Hi Team, I'm mocking an api cal with condition li...
# help
m
Hi Team, I'm mocking an api cal with condition like , calculate the date range ( start and stop date sent in query) and send the response if they fall into a specific range like 2 months or 3 months and error if 5 or other range months .Any thoughts, please?.
b
Hey @Muthukkumar Ponnusamy , have a look at the ‘Dates and times’ section on https://wiremock.org/docs/request-matching/ and add the conditions you want relative to the current system date? That should work. Happy to write an example if you can give me some more details but probably not before Tuesday. Or have a look here for a working example: https://github.com/basdijkstra/wiremock-workshop/blob/615c75f6700f36eea948f194a7bb75aebe952743/src/test/java/examples/WireMockExamplesTest.java#L94
🧡 1
m
it's standalone. so looking for option to edit the json file. sorry should have mentioned it.
More Details:- I'm using standalone wiremock. Trying to mock an api that provides history between specific date range that is being sent as a query string. The response should be received only if the start date and end date mentioned in query string is two month or four months, if the start date and end date mentioned in query string is not 2 or 4 months then No response(or error).
I'm trying to do this validation at the request part of stub. like using before, after and offset.
b
Can you post an example of the query parameters / query string? Also, two months or four months of what? Should they be two months or four months apart? Two months or four months from the current date? Something else? Could you be a little more specific? Examples help.
m
http://bank.com/transaction?start=01/23/2023&stop=03/23/2023 = I should get an history to transaction since the range is 2 months. http://bank.com/transaction?start=01/23/2023&stop=04/23/2023 = should fail or no response since the range is not 2 months.
As of now I'm trying is to pass start query value to stop query and validate. like below. before:$start expectedoffset: "2" and expectedoffsetunit."month"
b
Clear now! I told you examples help :) This is a perfect case for a custom request filter. You’ll have to build that in Java and then load it using the —extensions flag when you start the standalone mock server. https://wiremock.org/docs/extending-wiremock/ Either a request filter or a custom request matcher might work (I prefer request filters as they act like guard clauses for your mock definitions).
m
Got it. That. you. I will try that out.
So I can use extension for any part of response, right?.
b
What do you mean by ‘any part of the response’? You can load a custom request matcher for the WireMock instance or enable it for a specific responder. Examples for both are in the docs. A request filter is applied to a WireMock instance, it wouldn’t make sense to add that to a specific responder only.
I’m happy to build you something but I won’t be able to do that before May 4th or so. Holiday :)
🙌 1
1
m
No no. This should do. I will figure it out. Thank you for the help.
🙌 1
while working on this I noticed below, only look if you are free. I'm passing a dare in query ?start=29/04/2021(format dd/mm/yyyy). in request I have set below:- before:"2021-05-01" expedtedoffset: "2" offsetunit:"months" my expectation with above condition is like only dates before than 2021/03/01 but 2021/04/29 says stub is matching.
o
May the 4th be with you @Bas Dijkstra yoda