pact-foundation / pact-mock_service

Provides a mock service for use with Pact
https://pact.io
MIT License
73 stars 69 forks source link

Unable to set response body to "falsey" values #99

Closed mefellows closed 3 years ago

mefellows commented 5 years ago

Setting the response body directly to a null or false value results in an empty response from the mock server.

How to reproduce:

bundle exec bin/pact-mock-service service \
  --port 1234 \
  --consumer Foo \
  --provider Bar \
  --pact-specification-version 2 \
  --pact-dir ./tmp/pacts \
  --log ./tmp/bar_mock_service.log \
  --log-level DEBUG &
pid=$!

# Wait a few seconds, then register the interaction:

curl -X POST -H "X-Pact-Mock-Service: true"  localhost:1234/interactions -d '{
  "description": "foo",
  "request": {
    "path": "/foo",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": null
  }
}'

# Test the response
curl localhost:1234/foo

I believe the render_body method is to blame, specifically this line which checks for a falsey value of the body, meaning if the body is nil or false it returns an empty string.

I couldn't glean from the tests the exact behaviour as to why this statement existed in the first place, but my suspicion is that we need to be able to deal with the 'body not present' case to avoid returning null. However, this is presenting the explicit null / false case.

FWIW removing that line didn't cause in test failures, so perhaps we need to get some coverage on it.

See https://github.com/pact-foundation/pact-js/issues/257 for background.

bethesque commented 5 years ago

This is tricky. What are you expecting the HTTP request to look like? null or false? How would you differentiate it from an empty body?

mefellows commented 5 years ago

This is tricky.

Indeed!

In this case, the body is a literal false. I think it could be differentiated because it would have the header Content-Length: 5. At least, that is the only way i can think of separating them.

NiklasEi commented 3 years ago

Any updates here? This issue makes it impossible to write pact tests for endpoints returning only a boolean.

bethesque commented 3 years ago

I've changed it to return '' if body.nil? and released the mock service. I have not done any testing on it. You'll need to get the pact client of whichever language you're using updated to the latest pact-ruby-standalone.

mefellows commented 3 years ago

Thanks Beth.

@NiklasEi what language are you using that needs it?

NiklasEi commented 3 years ago

Thanks for the update!

@NiklasEi what language are you using that needs it?

Javascript

mefellows commented 3 years ago

Yep, will get a new release out this week. Thanks