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 * ``ssl``: Make this request use SSL? Defaults to ``False``. This only
comes into play if the ``url`` does not provide a scheme (see comes into play if the ``url`` does not provide a scheme (see
:doc:`host` for more info). :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 * ``redirects``: If ``True`` automatically follow redirects. Defaults
to ``False``. to ``False``.
* ``request_headers``: A dictionary of key-value pairs representing * ``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. made using httplib2. Assertions are made against the reponse.
""" """
from __future__ import print_function
import copy import copy
import functools import functools
import json import json
@@ -49,6 +51,7 @@ REPLACERS = [
BASE_TEST = { BASE_TEST = {
'name': '', 'name': '',
'desc': '', 'desc': '',
'verbose': False,
'ssl': False, 'ssl': False,
'redirects': False, 'redirects': False,
'method': 'GET', 'method': 'GET',
@@ -299,6 +302,13 @@ class HTTPTestCase(testcase.TestCase):
if test['redirects']: if test['redirects']:
self.http.follow_redirects = True 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._run_request(full_url, method, headers, body)
self._assert_response() self._assert_response()

View File

@@ -34,13 +34,13 @@ import os
from unittest import suite from unittest import suite
import uuid import uuid
import httplib2
import six import six
import yaml import yaml
from gabbi import case from gabbi import case
from gabbi import handlers from gabbi import handlers
from gabbi import suite as gabbi_suite from gabbi import suite as gabbi_suite
from gabbi import utils
RESPONSE_HANDLERS = [ RESPONSE_HANDLERS = [
handlers.StringResponseHandler, 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 # Use metaclasses to build a class of the necessary type
# and name with relevant arguments. # and name with relevant arguments.
http_class = utils.get_http(verbose=test['verbose'])
klass = TestBuilder(test_name, (case.HTTPTestCase,), klass = TestBuilder(test_name, (case.HTTPTestCase,),
{'test_data': test, {'test_data': test,
'test_directory': test_directory, 'test_directory': test_directory,
'fixtures': fixture_classes, 'fixtures': fixture_classes,
'http': httplib2.Http(), 'http': http_class,
'host': host, 'host': host,
'intercept': intercept, 'intercept': intercept,
'port': port, 'port': port,

View File

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

View File

@@ -15,6 +15,33 @@
# under the License. # under the License.
"""Utility functions grab bag.""" """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): def decode_content(response, content):
"""Decode content to a proper string.""" """Decode content to a proper string."""
@@ -33,6 +60,13 @@ def decode_content(response, content):
return 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): def not_binary(content_type):
"""Decide if something is content we'd like to treat as a string.""" """Decide if something is content we'd like to treat as a string."""
return (content_type.startswith('text/') or return (content_type.startswith('text/') or