`json_body` doesn't seem to be working for YAML mocks (standalone)
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?
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
@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.
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.
Hi @alexliesenfeld. Thanks for the reply. Would body trim leading/trailing spaces (new lines, tabs, etc.)?
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".
Just submitted https://github.com/httpmock/httpmock/pull/190 that fixes support for json_body in YAML mocks