From 3f61c91204c4d17e7ae4448ba8a55c31427d12e3 Mon Sep 17 00:00:00 2001 From: Matthew Balvanz Date: Tue, 30 May 2017 21:57:46 -0500 Subject: [PATCH] Add support for request bodies that are False in Python --- e2e/app.py | 6 +++++- e2e/contracts/test_e2e.py | 12 ++++++++++++ pact/pact.py | 2 +- pact/test/test_pact.py | 13 +++++++++++++ 4 files changed, 31 insertions(+), 2 deletions(-) diff --git a/e2e/app.py b/e2e/app.py index a91a1c0a72..1c6b779b5b 100644 --- a/e2e/app.py +++ b/e2e/app.py @@ -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([])) }} diff --git a/e2e/contracts/test_e2e.py b/e2e/contracts/test_e2e.py index 72468fbfc4..1f59271404 100644 --- a/e2e/contracts/test_e2e.py +++ b/e2e/contracts/test_e2e.py @@ -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): diff --git a/pact/pact.py b/pact/pact.py index 9f5bf054a9..f931538ff8 100644 --- a/pact/pact.py +++ b/pact/pact.py @@ -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: diff --git a/pact/test/test_pact.py b/pact/test/test_pact.py index 04de687112..1a41b897d7 100644 --- a/pact/test/test_pact.py +++ b/pact/test/test_pact.py @@ -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): @@ -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': []})