From 75394fbf6138f10841b26c9635e17e28dcb4cb91 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Mon, 27 Apr 2015 14:07:17 +0100 Subject: [PATCH 1/2] Add verbose reporting of request and response In normal testing setups it is not possible to see the requests and responses being made. If a test is set to 'verbose: True' then the test request info and the httplib2 request and response data will be produced to STDOUT. --- gabbi/case.py | 10 +++++++++ gabbi/driver.py | 5 +++-- gabbi/gabbits_intercept/self.yaml | 1 + gabbi/utils.py | 34 +++++++++++++++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) diff --git a/gabbi/case.py b/gabbi/case.py index d623e79..2f10444 100644 --- a/gabbi/case.py +++ b/gabbi/case.py @@ -20,6 +20,8 @@ response headers and body. When the test is run an HTTP request is made using httplib2. Assertions are made against the reponse. """ +from __future__ import print_function + import copy import functools import json @@ -49,6 +51,7 @@ REPLACERS = [ BASE_TEST = { 'name': '', 'desc': '', + 'verbose': False, 'ssl': False, 'redirects': False, 'method': 'GET', @@ -299,6 +302,13 @@ class HTTPTestCase(testcase.TestCase): if test['redirects']: self.http.follow_redirects = True + # Print some information about this request is asked. + if test['verbose']: + print('\n###########################') + print('%s %s' % (method, full_url)) + for key in headers: + print('%s: %s' % (key, headers[key])) + self._run_request(full_url, method, headers, body) self._assert_response() diff --git a/gabbi/driver.py b/gabbi/driver.py index 7f06230..029abbb 100644 --- a/gabbi/driver.py +++ b/gabbi/driver.py @@ -34,13 +34,13 @@ import os from unittest import suite import uuid -import httplib2 import six import yaml from gabbi import case from gabbi import handlers from gabbi import suite as gabbi_suite +from gabbi import utils RESPONSE_HANDLERS = [ handlers.StringResponseHandler, @@ -159,11 +159,12 @@ def test_suite_from_yaml(loader, test_base_name, test_yaml, test_directory, # Use metaclasses to build a class of the necessary type # and name with relevant arguments. + http_class = utils.get_http(verbose=test['verbose']) klass = TestBuilder(test_name, (case.HTTPTestCase,), {'test_data': test, 'test_directory': test_directory, 'fixtures': fixture_classes, - 'http': httplib2.Http(), + 'http': http_class, 'host': host, 'intercept': intercept, 'port': port, diff --git a/gabbi/gabbits_intercept/self.yaml b/gabbi/gabbits_intercept/self.yaml index d82d2e4..14ea19a 100644 --- a/gabbi/gabbits_intercept/self.yaml +++ b/gabbi/gabbits_intercept/self.yaml @@ -10,6 +10,7 @@ defaults: tests: - name: get simple page url: / + verbose: True - name: inheritance of defaults response_headers: diff --git a/gabbi/utils.py b/gabbi/utils.py index e211dd0..b10f4c1 100644 --- a/gabbi/utils.py +++ b/gabbi/utils.py @@ -15,6 +15,33 @@ # under the License. """Utility functions grab bag.""" +from __future__ import print_function + +import httplib2 + + +class VerboseHttp(httplib2.Http): + """A subclass of Http that verbosely reports on activity.""" + + def _request(self, conn, host, absolute_uri, request_uri, method, body, + headers, redirections, cachekey): + """Display request parameters before requesting.""" + + print('\n%s %s\nHost: %s' % (method, request_uri, host)) + for key in headers: + print('%s: %s' % (key, headers[key])) + + (response, content) = httplib2.Http._request( + self, conn, host, absolute_uri, request_uri, method, body, + headers, redirections, cachekey + ) + + print() + for key in response.dict: + print('%s: %s' % (key, response.dict[key])) + + return (response, content) + def decode_content(response, content): """Decode content to a proper string.""" @@ -33,6 +60,13 @@ def decode_content(response, content): return content +def get_http(verbose=False): + """Return an Http class for making requests.""" + if verbose: + return VerboseHttp() + return httplib2.Http() + + def not_binary(content_type): """Decide if something is content we'd like to treat as a string.""" return (content_type.startswith('text/') or From 5050b4de4092b8d9471868488c9875054b09b7d7 Mon Sep 17 00:00:00 2001 From: Chris Dent Date: Tue, 28 Apr 2015 11:42:38 +0100 Subject: [PATCH 2/2] Add docs for verbose rule. --- docs/source/format.rst | 2 ++ 1 file changed, 2 insertions(+) diff --git a/docs/source/format.rst b/docs/source/format.rst index 54d409f..1d1868f 100644 --- a/docs/source/format.rst +++ b/docs/source/format.rst @@ -46,6 +46,8 @@ these allow substitutions (explained below). * ``ssl``: Make this request use SSL? Defaults to ``False``. This only comes into play if the ``url`` does not provide a scheme (see :doc:`host` for more info). +* ``verbose``: If ``True`` print a representation of the current + request and response to ``stdout``. Defaults to ``False``. * ``redirects``: If ``True`` automatically follow redirects. Defaults to ``False``. * ``request_headers``: A dictionary of key-value pairs representing