Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

json_body doesn't seem to be working for YAML mocks (standalone) #118

Open
mdevino opened this issue Oct 14, 2024 · 5 comments
Open

json_body doesn't seem to be working for YAML mocks (standalone) #118

mdevino opened this issue Oct 14, 2024 · 5 comments
Labels
bug Something isn't working

Comments

@mdevino
Copy link

mdevino commented Oct 14, 2024

Hi, I've just found the library and am trying to spin up a quick mock server.
I have the following YAML, but it doesn't seem to work as expected:

when:
  method: GET
  path: /health
then:
  status: 200
  json_body: |-
    {
      "status": "healthy"
    }

When I invoke the endpoint, I do get a 200 back, but the response body is empty. Am I missing anything?

@mdevino
Copy link
Author

mdevino commented Oct 15, 2024

I've installed version 0.7.0, but httpmock --version outputs 0.6:

    Replaced package `httpmock v0.7.0` with `httpmock v0.7.0` (executable `httpmock`)
mdevino:mocks$ httpmock --version
httpmock 0.6

@mshytikov
Copy link

mshytikov commented Nov 5, 2024

@mdevino Thank you for opening this issue. I encountered the same problem today while starting to use the library for the first time. Hope the new image will be available soon.
As tmp workaround until the new docker image is available you can just replace json_body: with just body: I just tested it and it seems to work.

@alexliesenfeld
Copy link
Owner

alexliesenfeld commented Nov 14, 2024

You can safely use body instead of json_body since json_body is merely a helper method. It takes a serde object, serializes it, and sets the result as the response body.

You are right though @mdevino, it seems at the moment json_body is missing in the corresponding data structure that stores decoded YAML content. I can fix that.

The only difference I see that json_body would do on top of what body already does is parsing/validating JSON. Otherwise the two would be identical. If anyone sees additional features, please let me know.

We should also add a check that returns an error in case YAML files contain unknown attributes. This way users would be informed about unknown / invalid YAML fields.

@alexliesenfeld alexliesenfeld added the bug Something isn't working label Nov 14, 2024
@mdevino
Copy link
Author

mdevino commented Nov 14, 2024

Hi @alexliesenfeld. Thanks for the reply. Would body trim leading/trailing spaces (new lines, tabs, etc.)?

@alexliesenfeld
Copy link
Owner

alexliesenfeld commented Nov 14, 2024

No, it will respond with the original content. Example:

This definition:

when:
  method: GET
then:
  status: 200
  body: '  hello  '

Will respond with:

curl localhost:5050
  hello  

observe the two spaces before and after "hello".

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants