using stubby multiple responses for the same patch request endpoint

761 views Asked by At

This is my patch request mapping in data.yaml

request:
    url: ^/api/test
    method: PATCH
    headers:
      Content-Type: application/json 

  response:
    headers:
      Content-Type: application/json
    status: 200
    file: response/test-1.json

api path api/test is a PATCH request which accepts a single request parameter in it's body {testVar: "1111"}

what I need to implement is when request param is {testVar: "1111"} -> call response/test-1.json when request param is {testVar: "2222"} -> call response/test-2.json

how can this be implemented?

I tried the query params, request params etc. but with no luck

1

There are 1 answers

1
Alexander Zagniotov On BEST ANSWER

Have a read at:

  1. https://stubby4j.com/docs/http_endpoint_configuration_howto.html#dynamic-token-replacement-in-stubbed-response. More specifically the following section Where to specify the template.
  2. Also, have a look at the following YAML which is a part of my functional test suite: https://github.com/azagniotov/stubby4j/blob/master/src/functional-test/resources/yaml/include-regex-dynamic-tokens-templated-stubs.yaml#L13

The idea here is:

In your POST/PATCH request payload, you can specify one of the params to be a regex, e.g.: {"testVar": "(.*)"}. The regex's tokens upon matching (i.e.: the value of (.*)), can be used as a replacement token for your response configuration. In other words, you should be able to load the respective JSON file as needed.

But, to make this a little easier for you, try the following YAML config:

- request:
    method: PATCH
    url: ^/api/test
    headers:
      content-type: application/json
    post: >
      {"testVar": "(.*)"}

  response:
    headers:
      content-type: application/json
    status: 200
    file: response/test-<% post.1 %>.json

Let me know if the above works for you. For reference, I have tested the above config in the following PR: https://github.com/azagniotov/stubby4j/pull/280