mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-01-11 22:30:41 +01:00
Is it possible to select an entry from a hard coded list based on input value #491
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @MichaelIDS on GitHub (Feb 20, 2023).
Originally assigned to: @StefH on GitHub.
I've been looking over the various Response Templating libraries and I can't find one which I can use to take a request value and do a lookup in a hard coded list and then use this in the response.
I haven't been able to test Linq as I can't find a way to get it to work, but again I suspect my desire to provide this list to lookup in would be an issue.
Does anyone have any ideas on possible approaches?
Request has a clientId path parameter that I can get via:
{{request.PathSegments.[2]}}I want to include a static lookup list of this concept (exact structure can be anything that works):
As in the request we send the clientId and the sender knows the expected company name already. So I want to be able to look up the company name and return it so our program can validate that the response is for the expected company. This check is to detect high level data drift between the requesting and responding systems, and so I am trying to make a way to allow testing our checking and response handling code.
A request on our side is that its all done via the JSON stub definition method, so that we avoid requiring a c# developer to maintain this.
@StefH commented on GitHub (Feb 20, 2023):
You want to use JSON Mappings in combination with response templating?
(Else if you can define the mapping in C# code, then you can use any C# code.)
@MichaelIDS commented on GitHub (Feb 20, 2023):
I'd like to be able to have something conceptually achieving this:
Whether that's as IF statements or using some sort of loop over a hard-coded data structure doesn't really matter.
I was hoping there was a trick to do this with current integrated helpers.
As on the Request side I was able to eventually find a way to flag when "unexpected fields included in posted JSON object" via some weird checks using JmesPathMatcher.
@StefH commented on GitHub (Feb 20, 2023):
Handlebars.Net should have support for IF I think.
Did you try this code?
@MichaelIDS commented on GitHub (Feb 20, 2023):
It accepts it, but it doesn't evaluate the
==. As passing in a value of999also triggers the3000response.From a quick check earlier it seems that handlebars IF just checks if something exists in the data structure, it doesn't support real evaluation without custom handlers being made.
I had to do the simpler code in testing to avoid errors as well.
{{#if request.PathSegments.[2]=='3000'}}company a{{else}}not found{{/if}}@StefH commented on GitHub (Feb 20, 2023):
To solve this issue, 2 things need to be in place:
1]
Some logic in WireMock.Net to define a data-object (dictionary) in the response
2]
A new Handlebars.Net helper method like
Which will lookup a value in the dictionary, and if not found, return that default string.
This could be the solution for your question.
@MichaelIDS commented on GitHub (Feb 20, 2023):
thanks for the answer. At least I know I didn't miss anything obvious.
I suspect in the future we will move to including c# customisations based on the business's other long-term hopes around automated testing. So I don't think there's any rush for JSON support for this from my side, I can put this on the c# only feature list :)
@StefH commented on GitHub (Feb 20, 2023):
https://github.com/WireMock-Net/WireMock.Net/pull/893
@StefH commented on GitHub (Feb 20, 2023):
@MichaelIDS
Initial code is implemented:
This can be used like:
With test request to
http://locahost/data/1.Use preview version "1.5.16-ci-17039".
(https://github.com/WireMock-Net/WireMock.Net/wiki/MyGet-preview-versions)
@StefH commented on GitHub (Feb 25, 2023):
@MichaelIDS Did you have time to verify this?
@MichaelIDS commented on GitHub (Feb 27, 2023):
Hi, not yet, but it is on my list and I will update here once tried.
@StefH commented on GitHub (Mar 9, 2023):
I'll close this issue and if you have another question, just raise a new issue.