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

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

Closed
mefellows opened this issue Dec 30, 2018 · 7 comments
Closed

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

mefellows opened this issue Dec 30, 2018 · 7 comments

Comments

@mefellows
Copy link
Member

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 pact-foundation/pact-js#257 for background.

@bethesque
Copy link
Member

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
Copy link
Member Author

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
Copy link

NiklasEi commented Jun 2, 2021

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

@bethesque
Copy link
Member

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
Copy link
Member Author

Thanks Beth.

@NiklasEi what language are you using that needs it?

@NiklasEi
Copy link

NiklasEi commented Jun 3, 2021

Thanks for the update!

@NiklasEi what language are you using that needs it?

Javascript

@mefellows
Copy link
Member Author

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

themichaelyan pushed a commit to animoto/pact-mock_service that referenced this issue Feb 4, 2022
* feat: allow mock service host to be configured

pact-foundation/pact-ruby#186

* chore(release): version 3.0.0

* fix: add missing host argument to server spawn

fixes: pact-foundation#102

* chore(release): version 3.0.1

* Improve logging robustness

Do not pretty print a JSON object when serialization or serialization fail, for details see pact-foundation#103.

* Update interaction_replay.rb

* Initial version of the unit test

* Move the test to appropriate context

* Update interaction_replay_spec.rb

* Update interaction_replay_spec.rb

* Update interaction_replay_spec.rb

* Update interaction_replay_spec.rb

* Update interaction_replay.rb

* Update interaction_replay_spec.rb

* Update interaction_replay_spec.rb

* feat: pact-stub-service log level cli opt

* chore(release): version 3.1.0

* chore(release): version 3.1.1

* feat(skip writing to pact): Use writable_interactions when writing to pact file

* Add test for #as_json

* Add feature spec

* Bump pact-support dependency

* Fix tests

* chore(release): version 3.2.0

* CI: Add 2.7 to travis ci

* fix: remove apparently unused require for thwait

* chore(release): version 3.2.1

* feat: log a warning when too many interactions are set on the mock service at once

* chore(release): version 3.3.0

* fix: put metadata on the correct decorator

* chore(release): version 3.3.1

* chore: remove jruby until somebody shows they're actually using it

* chore(release): version 3.4.0

* feat: add token, username and password options to stub service (pact-foundation#118)

Co-authored-by: Beth Skurrie <bethesque@users.noreply.github.com>

* chore(release): version 3.5.0

* docs: document that you can set the broker token via an env var

[ci-skip]

* chore: separate test and deploy stages

* chore: update .travis.yml

[ci-skip]

* chore: update .travis.yml

* test: add expectations to make sure metadata: nil isn't stored in the pact (pact-foundation#119)

* feat: add 'Access-Control-Allow-Headers' = true to cors response headers (pact-foundation#121)

* chore(release): version 3.6.0

* fix:  fix Ruby 2.7 kwargs warning (pact-foundation#122)

* chore(release): version 3.6.1

* fix: update thor dependency (pact-foundation#124)

Co-authored-by: Lindsey Hattamer <lindsey.hattamer@oddball.io>

Co-authored-by: Lindsey Hattamer <lindsey.hattamer@oddball.io>

* chore: add github workflow for gem release

* chore: disable tests as they're not running properly on github workflow

* chore(release): version 3.6.2

* feat: do not require files until command is executing

* chore: update release workflow [ci-skip]

* feat: use Pact::Query.parse_string to parse query string

* chore(deps): update rake

* docs: update travis badge

* chore(release): version 3.7.0

* feat: include interaction diffs in verification response

cc: @TimothyJones

* chore: add tests to github actions

* chore: allow workflow dispatch to release

* chore(release): version 3.8.0

* chore: tests are hanging. try a subset

* chore: try rspec

* chore: disabling tests, they hang

* chore: create issue template

* feat: pass host into WEBrick options to allow configuration (pact-foundation#128)

Co-authored-by: Matthew Hall <matthew.hall@partnerize.com>

* chore(release): version 3.9.0

* chore: handle http 2

* fix: check for nil body rather than falsey body when determining how to render mocked response
Fixes: pact-foundation#99

* chore(release): version 3.9.1

Co-authored-by: Beth Skurrie <beth@bethesque.com>
Co-authored-by: Alexander Bolshakov <lextiz@gmail.com>
Co-authored-by: YOU54F <yousafn@gmail.com>
Co-authored-by: Beth Skurrie <bethesque@users.noreply.github.com>
Co-authored-by: Simon Nizov <simon.nizov@gmail.com>
Co-authored-by: Chavez <matthew@el-chavez.me>
Co-authored-by: Chavez <mtchavez@users.noreply.github.com>
Co-authored-by: Matt Fellows <matt.fellows@onegeek.com.au>
Co-authored-by: vandemark <jnvbeach@gmail.com>
Co-authored-by: Bartek Bułat <barthez@users.noreply.github.com>
Co-authored-by: Michael R. Fleet <f1337@users.noreply.github.com>
Co-authored-by: Lindsey Hattamer <lindsey.hattamer@oddball.io>
Co-authored-by: mhall58 <matthewhall58@gmail.com>
Co-authored-by: Matthew Hall <matthew.hall@partnerize.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants