gabbi Package¶
case
Module¶
A single HTTP request represented as a subclass of unittest.TestCase
The test case encapsulates the request headers and body and expected response headers and body. When the test is run an HTTP request is made using urllib3. Assertions are made against the reponse.
-
class
gabbi.case.
HTTPTestCase
(methodName='runTest')¶ Bases:
unittest.case.TestCase
Encapsulate a single HTTP request as a TestCase.
If the test is a member of a sequence of requests, ensure that prior tests are run.
To keep the test harness happy we need to make sure the setUp and tearDown are only run once.
-
assert_in_or_print_output
(expected, iterable)¶ Assert the iterable contains expected or print some output.
If the output is long, it is limited by either GABBI_MAX_CHARS_OUTPUT in the environment or the MAX_CHARS_OUTPUT constant.
-
base_test
= {'status': '200', 'xfail': False, 'redirects': False, 'verbose': False, 'query_parameters': {}, 'url': '', 'skip': '', 'name': '', 'ssl': False, 'request_headers': {}, 'poll': {}, 'data': '', 'method': 'GET', 'desc': ''}¶
-
static
extract_json_path_value
(data, path)¶ Extract the value at JSON Path path from the data.
The input data is a Python datastructure, not a JSON string.
-
replace_template
(message)¶ Replace magic strings in message.
-
response_handlers
= []¶
-
run
(result=None)¶ Store the current result handler on this test.
-
setUp
()¶
-
tearDown
()¶
-
test_request
()¶ Run this request if it has not yet run.
If there is a prior test in the sequence, run it first.
-
-
gabbi.case.
potentialFailure
(func)¶ Decorate a test method that is expected to fail if ‘xfail’ is true.
driver
Module¶
fixture
Module¶
Manage fixtures for gabbi at the test suite level.
-
class
gabbi.fixture.
GabbiFixture
¶ Bases:
object
A context manager that operates as a fixture.
Subclasses must implement
start_fixture
andstop_fixture
, each of which contain the logic for stopping and starting whatever the fixture is. What a fixture is is left as an exercise for the implementor.These context managers will be nested so any actual work needs to happen in
start_fixture
andstop_fixture
and not in__init__
. Otherwise exception handling will not work properly.-
start_fixture
()¶ Implement the actual workings of starting the fixture here.
-
stop_fixture
()¶ Implement the actual workings of stopping the fixture here.
-
-
exception
gabbi.fixture.
GabbiFixtureError
¶ Bases:
exceptions.Exception
Generic exception for GabbiFixture.
-
class
gabbi.fixture.
SkipAllFixture
¶ Bases:
gabbi.fixture.GabbiFixture
A fixture that skips all the tests in the current suite.
-
start_fixture
()¶
-
-
gabbi.fixture.
nest
(*args, **kwds)¶ Nest a series of fixtures.
This is duplicated from
nested
in the stdlib, which has been deprecated because of issues with how exceptions are difficult to handle during__init__
. Gabbi needs to nest an unknown number of fixtures dynamically, so thewith
syntax that replacesnested
will not work.
handlers
Module¶
Handlers for processing the body of a response in various ways.
-
class
gabbi.handlers.
ForbiddenHeadersResponseHandler
(test_class)¶ Bases:
gabbi.handlers.ResponseHandler
Test that listed headers are not in the response.
-
action
(test, forbidden, value=None)¶
-
test_key_suffix
= 'forbidden_headers'¶
-
test_key_value
= []¶
-
-
class
gabbi.handlers.
HeadersResponseHandler
(test_class)¶ Bases:
gabbi.handlers.ResponseHandler
Compare expected headers with actual headers.
If a header value is wrapped in
/
it is treated as a raw regular expression.Headers values are always treated as strings.
-
action
(test, header, value=None)¶
-
test_key_suffix
= 'headers'¶
-
test_key_value
= {}¶
-
-
class
gabbi.handlers.
JSONResponseHandler
(test_class)¶ Bases:
gabbi.handlers.ResponseHandler
Test for matching json paths in the json_data.
-
action
(test, path, value=None)¶ Test json_paths against json data.
-
test_key_suffix
= 'json_paths'¶
-
test_key_value
= {}¶
-
-
class
gabbi.handlers.
ResponseHandler
(test_class)¶ Bases:
object
Add functionality for making assertions about an HTTP response.
A subclass may implement two methods:
action
andpreprocess
.preprocess
takes one argument, theTestCase
. It is called exactly once for each test before looping across the assertions. It is used, rarely, to copy thetest.output
into a useful form (such as a parsed DOM).action
takes two or three arguments. Iftest_key_value
is a listaction
is called with the test case and a single list item. Iftest_key_value
is a dict thenaction
is called with the test case and a key and value pair.-
action
(test, item, value=None)¶ Test an individual entry for this response handler.
If the entry is a key value pair the key is in item and the value in value. Otherwise the entry is considered a single item from a list.
-
preprocess
(test)¶ Do any pre-single-test preprocessing.
-
test_key_suffix
= ''¶
-
test_key_value
= []¶
-
-
class
gabbi.handlers.
StringResponseHandler
(test_class)¶ Bases:
gabbi.handlers.ResponseHandler
Test for matching strings in the the response body.
-
action
(test, expected, value=None)¶
-
test_key_suffix
= 'strings'¶
-
test_key_value
= []¶
-
suite
Module¶
runner
Module¶
utils
Module¶
Utility functions grab bag.
-
gabbi.utils.
create_url
(base_url, host, port=None, prefix='', ssl=False)¶ Given pieces of a path-based url, return a fully qualified url.
-
gabbi.utils.
decode_response_content
(header_dict, content)¶ Decode content to a proper string.
-
gabbi.utils.
extract_content_type
(header_dict)¶ Extract content-type from headers.
-
gabbi.utils.
get_colorizer
(stream)¶ Return a function to colorize a string.
Only if stream is a tty .
-
gabbi.utils.
host_info_from_target
(target, prefix=None)¶ Turn url or host:port and target into test destination.
-
gabbi.utils.
load_yaml
(handle=None, yaml_file=None)¶ Read and parse any YAML file or filehandle.
Let exceptions flow where they may.
If no file or handle is provided, read from STDIN.
-
gabbi.utils.
not_binary
(content_type)¶ Decide if something is content we’d like to treat as a string.
httpclient
Module¶
Subclass of Http class for verbosity.
-
class
gabbi.httpclient.
Http
(num_pools=10, headers=None, **connection_pool_kw)¶ Bases:
urllib3.poolmanager.PoolManager
A subclass of the urllib3.PoolManager to munge the data.
This transforms the response to look more like what httplib2 provided when it was used as the httpclient.
-
request
(absolute_uri, method, body, headers, redirect)¶
-
-
class
gabbi.httpclient.
VerboseHttp
(**kwargs)¶ Bases:
gabbi.httpclient.Http
A subclass of Http that verbosely reports on activity.
If the output is a tty or
GABBI_FORCE_COLOR
is set in the environment, then output will be colorized according toCOLORMAP
.Output can include request and response headers, request and response body content (if of a printable content-type), or both.
The color of the output has reasonable defaults. These may be overridden by setting the following environment variables
- GABBI_CAPTION_COLOR
- GABBI_HEADER_COLOR
- GABBI_REQUEST_COLOR
- GABBI_STATUS_COLOR
to any of: BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITE
-
COLORMAP
= {'status': 'CYAN', 'caption': 'BLUE', 'request': 'CYAN', 'header': 'YELLOW'}¶
-
HEADER_BLACKLIST
= ['status', 'reason']¶
-
REQUEST_PREFIX
= '>'¶
-
RESPONSE_PREFIX
= '<'¶
-
request
(absolute_uri, method, body, headers, redirect)¶ Display request parameters before requesting.
-
gabbi.httpclient.
get_http
(verbose=False, caption='')¶ Return an Http class for making requests.