Merge pull request #31 from cdent/verbose-testing

Add verbose reporting of request and response
This commit is contained in:
Chris Dent
2015-04-29 18:54:57 +01:00
5 changed files with 50 additions and 2 deletions

View File

@@ -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

View File

@@ -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()

View File

@@ -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,

View File

@@ -10,6 +10,7 @@ defaults:
tests:
- name: get simple page
url: /
verbose: True
- name: inheritance of defaults
response_headers:

View File

@@ -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