WireMock-Net / WireMock.Net

WireMock.Net is a flexible product for stubbing and mocking web HTTP responses using advanced request matching and response templating. Based on the functionality from http://WireMock.org, but extended with more functionality.
Apache License 2.0
1.42k stars 209 forks source link

Dynamic response files using Handlebars templating #270

Closed denstorti closed 5 years ago

denstorti commented 5 years ago

Hi,

I have a question about the current capabilities of templating.

I'd like to retrieve responses from files using WireMock.Net, and for that I am using static mappings with "BodyAsFile" attribute.

Example:

{
    "Request": {
        "Path": {
            "Matchers": [
                {
                    "Name": "WildcardMatcher",
                    "Pattern": "/bodyasfilexmltest",
                    "IgnoreCase": false
                }
            ]
        },
        "Methods": [
            "get"
        ]
    },
  "Response": {
    "StatusCode": 200,
    "Headers": { "Content-Type": "application/xml" },
    "BodyAsFile": "c:\\temp-wiremock\\__admin\\mappings\\MyXmlResponse.xml",
    "UseTransformer": false
  }
}

But I'd like to vary the response directory based on the request attributes, pretty much as Handlebars templates do.

Example (MyUniqueNumber would be the name of each folder):

"BodyAsFile": "c:\\temp-wiremock\\__admin\\mappings\\_{{request.query.MyUniqueNumber}}_\\MyXmlResponse.xml",

In this example the variable is in the Path, but it could be appended to the file name as well.

I did test it and it seems the library tries to load the file from disk when loaded and not per request, so it cannot find the invalid path "c:\temp-wiremock\_admin\mappings\_{{request.query.MyUniqueNumber}}\MyXmlResponse.xml".

Is this scenario currently supported (maybe in a different way)?

StefH commented 5 years ago

This scenario is not yet supported, I'll take a look and modify the code.

StefH commented 5 years ago

@denstorti Can you try MyGet version WireMock.Net.1.0.15-ci-11270 ?

Note that you need to set "UseTransformer": true in the mapping.

denstorti commented 5 years ago

@StefH, I could test it using the MyUniqueNumber as query string and also as JSON Path

Example using JSON Path

Input data (save as data.json):

{
  "MyUniqueNumber": "4444"
}

Mapping:

{
  "Request": {
    "Path": {
      "Matchers": [
        {
          "Name": "WildcardMatcher",
          "Pattern": "/postbodyasfile",
          "IgnoreCase": false
        }
      ]
    },
    "Methods": [
      "post"
    ]
  },
  "Response": {
    "StatusCode": 200,
    "Headers": { "Content-Type": "application/json" },
    "BodyAsFile": "c:/MockResponses/{{JsonPath.SelectToken request.body \"$.MyUniqueNumber\"}}/serv1.json",
    "UseTransformer": true
  }
}

curl test: curl -d @data.json http://localhost:9091/postbodyasfile

Thank you!

StefH commented 5 years ago

OK.

I will release a new NuGet today.

Note that instead of JsonPath, you can also use JmesPath as a matcher. http://jmespath.org/ https://github.com/WireMock-Net/WireMock.Net/wiki/Request-Matching#jmes-path-jmespathmatcher