Stubby: stub PUT requests for same URLS with distinct body

1k Views Asked by At

I'd like to match distinct bodies for the same PUT URL, but stubby4j always matches the first case, no matter what's the content of the body.

Example:

- request:
    url: /individuals/.*/address$
    method: PUT
    body: >
      {
        "type": "MOBILE",
        (other input fields)  
      }
  response:
    status: 400
    body: >
      {
        "type": "BAD_REQUEST",
        "message": "Validation error on request."
      }
- request:
    url: /individuals/.*/address$
    method: PUT
    body: >
      {
        "type": "HOME",
        (other input fields)
      }
  response:
    status: 200

In this case, no matter what's the value of the parameter "type" in my request, it always matches with the first stub.

2

There are 2 best solutions below

0
On BEST ANSWER

Apologies for the late response.

In the recent & current versions of stubby4j (i.e.: 7.x.x) it is possible to match distinct bodies for the same PUT/GET/POST/PATCH/etc URLs.

For example, the following is a valid YAML config that would work (I simplified a tad the YAML config provided by the OP for the sake of example):

-  request:
      url: /individuals/.*/address$
      method: PUT
      post: >
        {"type": "MOBILE"}

   response:
      status: 400
      body: >
        {"type": "BAD_REQUEST"}


-  request:
      url: /individuals/.*/address$
      method: PUT
      post: >
        {"type": "HOME"}

   response:
      body: OK
      status: 200

To note:

  • The request does not get the body key, use post key instead for stubbing your payload for POST/PUT/PATCH (or file if your payload is too big)
  • The body key should only be used in response, not request
  • The json key is invalid & not supported by stubby4j

Please refer to the stubby4j user manual for more information about the YAML config for request & response: https://stubby4j.com/docs/http_endpoint_configuration_howto.html

0
On

Try to change the request body to json data with headers.

- request:
    url: /individuals/.*/address$
    method: PUT
    headers:
       content-type: application/json
    json: '{"type": "MOBILE"}'
  response: ...

- request:
    url: /individuals/.*/address$
    method: PUT
    headers:
       content-type: application/json
    json: '{"type": "HOME"}'
  response: ...