smocker-dev / smocker

Smocker is a simple and efficient HTTP mock server and proxy
https://smocker.dev
MIT License
1.14k stars 61 forks source link

fix response body when using go_template_json #277

Closed Powerrr closed 1 year ago

Powerrr commented 1 year ago

Check that body is not nil before serializing it to json. This fixes an issue where a mock like this

- request:
    method: GET
    path: /foo
  dynamic_response:
    engine: go_template_json
    script: |
      {
        "status": 302,
        "headers": {"Location": "https://example.org/"}
      }

returns literal null as a response body instead of an empty body.

PS: to be honest, I think this entire if block is redundant because it makes the behavior inconsistent between dynamic-response mocks and static-response mocks: in static-response mocks we only allow strings as body and don't convert arbitrary objects into strings.

gwleclerc commented 1 year ago

This if has been added because without it, the user should escape all the quotation marks when he wants to return a json object in a go_template script which makes the mocks more difficult to read and write.