Content handlers are responsible for preparing request data and evaluating response data based on the content-type of the request and response. A content handler operates as follows:
By default, gabbi provides content handlers for JSON. In that content handler the data test key is converted from structured YAML into a JSON string. Response bodies are converted from a JSON string into a data structure in response_data that is used when evaluating response_json_paths entries in a test or doing JSONPath-based $RESPONSE[] substitutions.
Further content handlers can be added as extensions. Test authors may need these extensions for their own suites, or enterprising developers may wish to create and distribute extensions for others to use.
Note
One extension that is likely to be useful is a content handler that turns data into url-encoded form data suitable for POST and turns an HTML response into a DOM object.
Content handlers are an evolution of the response handler concept in earlier versions gabbi. To preserve backwards compatibility with existing response handlers, old style response handlers are still allowed, but new handlers should implement the content handler interface (described below).
Registering additional custom handlers is done by passing a subclass of ContentHandler to build_tests():
driver.build_tests(test_dir, loader, host=None,
intercept=simple_wsgi.SimpleWsgi,
content_handlers=[MyContentHandler])
If pytest is being used:
driver.py_test_generator(test_dir, intercept=simple_wsgi.SimpleWsgi,
content_handlers=[MyContenHandler])
Warning
When there are multiple handlers listed that accept the same content-type, the one that is earliest in the list will be used.
With gabbi-run, custom handlers can be loaded via the --response-handler option – see load_response_handlers() for details.
Note
The use of the --response-handler argument is done to preserve backwards compatibility and avoid excessive arguments. Both types of handler may be passed to the argument.
Creating a content handler requires subclassing ContentHandler and implementing several methods. These methods are described below, but inspecting JSONHandler will be instructive in highlighting required arguments and techniques.
To provide a response_<something> response-body evaluator a subclass must define:
To translate request or response bodies to or from structured data a subclass must define an accepts method. This should return True if this class is willing to translate the provided content-type. During request processing it is given the value of the content-type header that will be sent in the request. During response processing it is given the value of the content-type header of the response. This makes it possible to handle different request and response bodies in the same handler, if desired. For example a handler might accept application/x-www-form-urlencoded and text/html.
If accepts is defined two additional static methods should be defined:
Finally if a replacer class method is defined, then when a $RESPONSE substitution is encountered, replacer will be passed the response_data of the prior test and the argument within the $RESPONSE.
Please see the JSONHandler source for additional detail.