Skip to content

Commit

Permalink
Add support for request bodies that are False in Python
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewbalvanz-wf committed May 31, 2017
1 parent a39c62f commit 3f61c91
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 2 deletions.
6 changes: 5 additions & 1 deletion e2e/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,11 @@
headers={'Content-Type': 'application/json'},
response=json.dumps({'results': [
{'username': 'bob', 'id': 101, 'groups': [234, 123]},
{'username': 'sue', 'id': 102, 'groups': [345, 123]}]}))
{'username': 'sue', 'id': 102, 'groups': [345, 123]}]})),
'no users exist': Response(
status=200,
headers={'Content-Type': 'application/json'},
response=json.dumps([]))
}}


Expand Down
12 changes: 12 additions & 0 deletions e2e/contracts/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,18 @@ def test_nested(self):
{'username': 'bob', 'id': 123, 'groups': [123]},
{'username': 'bob', 'id': 123, 'groups': [123]}]})

def test_falsey_bodies(self):
(pact
.given('no users exist')
.upon_receiving('a request to insert no users')
.with_request('post', '/users/', body=[])
.will_respond_with(200, body=[]))

with pact:
results = requests.post('http://localhost:1234/users/', json=[])

self.assertEqual(results.json(), [])


class SyntaxErrors(BaseTestCase):
def test_incorrect_number_of_arguments(self):
Expand Down
2 changes: 1 addition & 1 deletion pact/pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ def json(self):
if self.headers:
request['headers'] = self.headers

if self.body:
if self.body is not None:
request['body'] = self.body

if self.query:
Expand Down
13 changes: 13 additions & 0 deletions pact/test/test_pact.py
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,14 @@ def test_all_options(self):
'headers': {'Accept': 'application/json'},
'query': 'term=test'})

def test_falsey_body(self):
target = Request('GET', '/path', body=[])
result = target.json()
self.assertEqual(result, {
'method': 'GET',
'path': '/path',
'body': []})


class ResponseTestCase(TestCase):
def test_sparse(self):
Expand All @@ -382,3 +390,8 @@ def test_all_options(self):
'status': 202,
'body': 'the body',
'headers': {'Content-Type': 'application/json'}})

def test_falsey_body(self):
target = Response(200, body=[])
result = target.json()
self.assertEqual(result, {'status': 200, 'body': []})

0 comments on commit 3f61c91

Please sign in to comment.