Skip to content

Commit 3f61c91

Browse files
Add support for request bodies that are False in Python
1 parent a39c62f commit 3f61c91

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

e2e/app.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,11 @@
2626
headers={'Content-Type': 'application/json'},
2727
response=json.dumps({'results': [
2828
{'username': 'bob', 'id': 101, 'groups': [234, 123]},
29-
{'username': 'sue', 'id': 102, 'groups': [345, 123]}]}))
29+
{'username': 'sue', 'id': 102, 'groups': [345, 123]}]})),
30+
'no users exist': Response(
31+
status=200,
32+
headers={'Content-Type': 'application/json'},
33+
response=json.dumps([]))
3034
}}
3135

3236

e2e/contracts/test_e2e.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,18 @@ def test_nested(self):
9393
{'username': 'bob', 'id': 123, 'groups': [123]},
9494
{'username': 'bob', 'id': 123, 'groups': [123]}]})
9595

96+
def test_falsey_bodies(self):
97+
(pact
98+
.given('no users exist')
99+
.upon_receiving('a request to insert no users')
100+
.with_request('post', '/users/', body=[])
101+
.will_respond_with(200, body=[]))
102+
103+
with pact:
104+
results = requests.post('http://localhost:1234/users/', json=[])
105+
106+
self.assertEqual(results.json(), [])
107+
96108

97109
class SyntaxErrors(BaseTestCase):
98110
def test_incorrect_number_of_arguments(self):

pact/pact.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def json(self):
296296
if self.headers:
297297
request['headers'] = self.headers
298298

299-
if self.body:
299+
if self.body is not None:
300300
request['body'] = self.body
301301

302302
if self.query:

pact/test/test_pact.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,14 @@ def test_all_options(self):
366366
'headers': {'Accept': 'application/json'},
367367
'query': 'term=test'})
368368

369+
def test_falsey_body(self):
370+
target = Request('GET', '/path', body=[])
371+
result = target.json()
372+
self.assertEqual(result, {
373+
'method': 'GET',
374+
'path': '/path',
375+
'body': []})
376+
369377

370378
class ResponseTestCase(TestCase):
371379
def test_sparse(self):
@@ -382,3 +390,8 @@ def test_all_options(self):
382390
'status': 202,
383391
'body': 'the body',
384392
'headers': {'Content-Type': 'application/json'}})
393+
394+
def test_falsey_body(self):
395+
target = Response(200, body=[])
396+
result = target.json()
397+
self.assertEqual(result, {'status': 200, 'body': []})

0 commit comments

Comments
 (0)