Is it possible to select an entry from a hard coded list based on input value #491

Closed
opened 2025-12-29 15:25:08 +01:00 by adam · 11 comments
Owner

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):

{
    "3000": "company a",
    "3001": "company b"
}

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.

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): ```json { "3000": "company a", "3001": "company b" } ``` 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.
adam added the question label 2025-12-29 15:25:08 +01:00
adam closed this issue 2025-12-29 15:25:08 +01:00
Author
Owner

@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.)

@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.)
Author
Owner

@MichaelIDS commented on GitHub (Feb 20, 2023):

I'd like to be able to have something conceptually achieving this:

if {{request.PathSegments.[2]}} == '3000' then 'company a'
else if {{request.PathSegments.[2]}} == '3001' then 'company b'
else 'no known company for id'

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.

        "Request": {
            "Body": {
                "Matcher": {
                    "Comment": "Checks that no unexpected fields have been included in the JSON object.",
                    "Name": "JmesPathMatcher",
                    "Pattern": "keys(@)[? @!='client' && @!='subclient' && @!='matter' && @!='submatter' && @!='hours' && @!='attorney' && @!='transactionDate' && @!='activityCode' && @!='phaseCode' && @!='rateCode' && @!='serviceCode' && @!='narrative' && @!='bill' && @!='userCode2' && @!='userCode3']|length(@)|starts_with(to_string(@), '0')|!@"
                }
            }
        }
        ```
@MichaelIDS commented on GitHub (Feb 20, 2023): I'd like to be able to have something conceptually achieving this: ``` if {{request.PathSegments.[2]}} == '3000' then 'company a' else if {{request.PathSegments.[2]}} == '3001' then 'company b' else 'no known company for id' ``` 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. ```json "Request": { "Body": { "Matcher": { "Comment": "Checks that no unexpected fields have been included in the JSON object.", "Name": "JmesPathMatcher", "Pattern": "keys(@)[? @!='client' && @!='subclient' && @!='matter' && @!='submatter' && @!='hours' && @!='attorney' && @!='transactionDate' && @!='activityCode' && @!='phaseCode' && @!='rateCode' && @!='serviceCode' && @!='narrative' && @!='bill' && @!='userCode2' && @!='userCode3']|length(@)|starts_with(to_string(@), '0')|!@" } } } ```
Author
Owner

@StefH commented on GitHub (Feb 20, 2023):

Handlebars.Net should have support for IF I think.

Did you try this code?

{{#if request.PathSegments.[2] == '3000'}}
company a
{{else if request.PathSegments.[2] == '3001'}}
company b
{{else}}
not found
{{/if}}
@StefH commented on GitHub (Feb 20, 2023): Handlebars.Net should have support for IF I think. Did you try this code? ``` handlebars {{#if request.PathSegments.[2] == '3000'}} company a {{else if request.PathSegments.[2] == '3001'}} company b {{else}} not found {{/if}} ```
Author
Owner

@MichaelIDS commented on GitHub (Feb 20, 2023):

It accepts it, but it doesn't evaluate the ==. As passing in a value of 999 also triggers the 3000 response.
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}}

@MichaelIDS commented on GitHub (Feb 20, 2023): It accepts it, but it doesn't evaluate the `==`. As passing in a value of `999` also triggers the `3000` response. 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}}```
Author
Owner

@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

"Response": {
  "Data": {
    "3000": "company a",
    "3001": "company b"
  },
  "UseTransformer": true

2]
A new Handlebars.Net helper method like

Dictionary.Map(data, request.PathSegments.[2], 'company not found')

Which will lookup a value in the dictionary, and if not found, return that default string.

This could be the solution for your question.

@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 ``` json "Response": { "Data": { "3000": "company a", "3001": "company b" }, "UseTransformer": true ``` 2] A new Handlebars.Net helper method like ``` handlebars Dictionary.Map(data, request.PathSegments.[2], 'company not found') ``` Which will lookup a value in the dictionary, and if not found, return that default string. This could be the solution for your question.
Author
Owner

@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 :)

@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 :)
Author
Owner

@StefH commented on GitHub (Feb 20, 2023):

https://github.com/WireMock-Net/WireMock.Net/pull/893

@StefH commented on GitHub (Feb 20, 2023): https://github.com/WireMock-Net/WireMock.Net/pull/893
Author
Owner

@StefH commented on GitHub (Feb 20, 2023):

@MichaelIDS
Initial code is implemented:

This can be used like:

{
  "Guid": "127a9e30-3235-4c8c-bf2c-ce714968b867",
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "WildcardMatcher",
          "Pattern": "/data/*"
        }
      ]
    },
    "Methods": [
      "GET"
    ]
  },
  "Data": {
        "1":"one",
        "2":"two"
    },
  "Response": {
    "StatusCode": 200,
    "Body": "{{Dictionary.Lookup data request.PathSegments.[1] 'N/A'}}",
    "Headers": {
      "Content-Type": "application/json; charset=UTF-8",
      "Content-Length": "105",
      "Date": "Wed, 06 Oct 2021 14:41:14 GMT"
    },
    "UseTransformer": true
  }
}

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 20, 2023): @MichaelIDS Initial code is implemented: This can be used like: ``` json { "Guid": "127a9e30-3235-4c8c-bf2c-ce714968b867", "Request": { "Path": { "Matchers": [ { "Name": "WildcardMatcher", "Pattern": "/data/*" } ] }, "Methods": [ "GET" ] }, "Data": { "1":"one", "2":"two" }, "Response": { "StatusCode": 200, "Body": "{{Dictionary.Lookup data request.PathSegments.[1] 'N/A'}}", "Headers": { "Content-Type": "application/json; charset=UTF-8", "Content-Length": "105", "Date": "Wed, 06 Oct 2021 14:41:14 GMT" }, "UseTransformer": true } } ``` 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)
Author
Owner

@StefH commented on GitHub (Feb 25, 2023):

@MichaelIDS Did you have time to verify this?

@StefH commented on GitHub (Feb 25, 2023): @MichaelIDS Did you have time to verify this?
Author
Owner

@MichaelIDS commented on GitHub (Feb 27, 2023):

Hi, not yet, but it is on my list and I will update here once tried.

@MichaelIDS commented on GitHub (Feb 27, 2023): Hi, not yet, but it is on my list and I will update here once tried.
Author
Owner

@StefH commented on GitHub (Mar 9, 2023):

I'll close this issue and if you have another question, just raise a new issue.

@StefH commented on GitHub (Mar 9, 2023): I'll close this issue and if you have another question, just raise a new issue.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/WireMock.Net-wiremock#491