Hi team, Is there a way to read data from a file u...
# help
a
Hi team, Is there a way to read data from a file using templated responses. Example
Copy code
{
  "request": {
    "method": "GET",
    "urlPathTemplate": "/api/user/{username}"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "<template>",
    "transformers": [
      "response-template"
    ]
  }
}
In the above stub if the body template can read users from a file users.json iterate through the list of users and match path param agains the user.username and return it as response? We can achieve this using custom extensions but would like to know if there is a straightforward way of doing it. TIA
t
You can template the
bodyFileName
field and therefore e.g. have a different body file per value of your
{username}
parameter.
So instead of
body
you’d have
"bodyFileName": "user-{{request.path.user}}.json"
a
Hi Tom, thanks for response. I would also want to serve GetUsers API which can search based on say firstName. Having each user in separate file may not serve this use case.
Copy code
// <http://localhost:8080/api/users?firstName=John>
{
      "request": {
        "method": "GET",
        "url": "/api/users"
      },
      "response": {
        "status": 200,
        "headers": {
          "Content-Type": "application/json"
        },
        "body": "<template>",
        "transformers": ["response-template"]
      }
    }
t
An alternative is to use response templating and to use the query parameter value as a value in the response, maybe along with some random values for other things that aren’t in the request. It really depends on what your tests need in terms of specific data.
a
I'm trying to mock simple CRUD operation on user entity along with search API with pagination. But the write operation changes should be reflected in read API response. The order of write operations could be random, so scenarios might not be useful.
t
Scenarios aren’t order dependent per se. You can configure transitions to trigger on different match criteria so that it doesn’t matter what order the requests arrive, although if you want more control you can use the state extension.
Also worth mentioning WireMock Cloud got an advanced state feature recently: https://docs.wiremock.io/dynamic-state/overview/
🆗 1
a
Lets say I have to add user1 and user2, How can search API return [user1] or [user2] or [user1, user2] or [user2, user1] using scenarios
t
That sounds like responding to different filter criteria in the query string rather than state?
a
lets say filters were not given then it should return all the users added
t
OK, so you could have a stub with no query parameter matchers returning all users, then a stub for each of the specific values of the query parameter you want to match distinctly.
a
Stub with no query parameter matchers should return users based on users created through Post API. So if only one of user1/user2 are created then response should contain just 1 user. If both were create then [user1,user2]. if none were created then empty list
t
If you absolutely need to retrieve users you’ve previously posted rather than canned, you’ll either need to use the state extension or WireMock Cloud’s advanced state feature.
a
got it. Will explore the state features
👍 1