Skip to content

Commit

Permalink
Merge pull request #186 from cdent/fix-182
Browse files Browse the repository at this point in the history
Pretty print JSON bodies in verbose tests
  • Loading branch information
cdent committed Nov 25, 2016
2 parents f27b9ab + 6f5d936 commit 1ef37f4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 13 deletions.
18 changes: 14 additions & 4 deletions gabbi/httpclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import urllib3

from gabbi.handlers import jsonhandler
from gabbi import utils


Expand Down Expand Up @@ -138,11 +139,20 @@ def _print_headers(self, headers, prefix=''):

def _print_body(self, headers, content):
"""Output body if not binary."""
if self._show_body and utils.not_binary(
utils.extract_content_type(headers)[0]):
content_type = utils.extract_content_type(headers)[0]
if self._show_body and utils.not_binary(content_type):
content = utils.decode_response_content(headers, content)
# TODO(cdent): Using the JSONHandler here instead of
# just the json module to make it clear that eventually
# we could pretty print any printable output by using a
# handler's loads() and dumps(). Not doing that now
# because it would be pointless (no other interesting
# handlers) and this approach may be entirely wrong.
if jsonhandler.JSONHandler.accepts(content_type):
data = jsonhandler.JSONHandler.loads(content)
content = jsonhandler.JSONHandler.dumps(data, pretty=True)
self._verbose_output('')
self._verbose_output(
utils.decode_response_content(headers, content))
self._verbose_output(content)

def _print_header(self, name, value, prefix='', stream=None):
"""Output one single header."""
Expand Down
18 changes: 18 additions & 0 deletions gabbi/tests/gabbits_runner/test_verbose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
tests:

- name: POST data with verbose true
verbose: true
POST: /
request_headers:
content-type: application/json
data:
- our text

- name: structured data
verbose: true
POST: /
request_headers:
content-type: application/json
data:
cow: moo
dog: bark
21 changes: 21 additions & 0 deletions gabbi/tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,27 @@ def test_exit_code(self):
except SystemExit as err:
self.assertSuccess(err)

def test_verbose_output_formatting(self):
"""Confirm that a verbose test handles output properly."""
sys.argv = ['gabbi-run', 'http://%s:%s/foo' % (self.host, self.port)]

sys.argv.append('--')
sys.argv.append('gabbi/tests/gabbits_runner/test_verbose.yaml')
with self.server():
try:
runner.run()
except SystemExit as err:
self.assertSuccess(err)

sys.stdout.seek(0)
output = sys.stdout.read()
self.assertIn('"our text"', output)
self.assertIn('"cow": "moo"', output)
self.assertIn('"dog": "bark"', output)
# confirm pretty printing
self.assertIn('{\n', output)
self.assertIn('}\n', output)

def assertSuccess(self, exitError):
errors = exitError.args[0]
if errors:
Expand Down
9 changes: 0 additions & 9 deletions gabbi/tests/test_verbose.yaml

This file was deleted.

0 comments on commit 1ef37f4

Please sign in to comment.