If simplejson is installed, Trestle uses a special output checker for output that claims to be json data. When Trestle sees a response with the content type "application/json", the json checker is automatically activated and used in place of the normal output checker.
In the examples below, the full responses are:
GET /list
a list
[1, 2, 3, "a cow", "a monkey", [2, 5, 6], "the end"]
GET /dict
a dict
{"a": 1, "b": "hello", "another": [5, 1, 9], "sub": {"key": "value"}}
GET /string
a string
"The quick brown dog jumped over the lazy fox. Again."
GET /number
a number
1293039483
When using the json output checker, a variety of wildcard-like features are supported.
The special construct <any> may be used as a list term or dict key or value. In a list, it matches one or more terms.
GET /list
a list
[1, 2, 3, "a cow", "a monkey", [2, 5, 6], "the end"]
[<any>]
[1, 2, 3, <any>]
[<any>, "the end"]
[<any>, "a cow", <any>]
In a dict, when used as a key and value, it matches one or more keys.
GET /dict
a dict
{"a": 1, "b": "hello", "another": [5, 1, 9], "sub": {"key": "value"}}
{"a": 1, <any>: <any>}
A timestamp wildcard matches strings of 6 or more contiguous digits.
GET /number
a timestamp-like number
<timestamp>
This document was built from the following .rst document:
Trestle JSON support
--------------------
.. fixtures:: about_json
If simplejson_ is installed, Trestle uses a special output checker for output
that claims to be json data. When Trestle sees a response with the content
type "application/json", the json checker is automatically activated and used
in place of the normal output checker.
In the examples below, the full responses are:
.. request:: a list
GET /list
..
.. response::
[1, 2, 3, "a cow", "a monkey", [2, 5, 6], "the end"]
..
.. request:: a dict
GET /dict
..
.. response::
{"a": 1, "b": "hello", "another": [5, 1, 9], "sub": {"key": "value"}}
..
.. request:: a string
GET /string
..
.. response::
"The quick brown dog jumped over the lazy fox. Again."
..
.. request:: a number
GET /number
..
.. response::
1293039483
..
When using the json output checker, a variety of wildcard-like
features are supported.
<any>
~~~~~
The special construct <any> may be used as a list term or dict key or
value. In a list, it matches one or more terms.
.. request:: a list
GET /list
..
.. response::
[1, 2, 3, "a cow", "a monkey", [2, 5, 6], "the end"]
..
.. response::
[<any>]
..
.. response::
[1, 2, 3, <any>]
..
.. response::
[<any>, "the end"]
..
.. response::
[<any>, "a cow", <any>]
..
In a dict, when used as a key **and value**, it matches one or more keys.
.. request:: a dict
GET /dict
..
.. response::
{"a": 1, "b": "hello", "another": [5, 1, 9], "sub": {"key": "value"}}
..
.. response::
{"a": 1, <any>: <any>}
..
<timestamp>
~~~~~~~~~~~
A timestamp wildcard matches strings of 6 or more contiguous digits.
.. request:: a timestamp-like number
GET /number
..
.. response::
<timestamp>
..
Source
^^^^^^
This document was built from the following .rst document:
.. include:: json.rst
:literal:
Using this fixture module:
.. include:: about_json.py
:literal:
.. _`simplejson` : http://pypi.python.org/pypi/simplejson
Using this fixture module:
from paste.fixture import TestApp
from simplejson import dumps
from cgi import parse_qsl
from trestle.json import odict
LIST = [1, 2, 3, "a cow", "a monkey", [2, 5, 6], "the end"]
DICT = odict(
[("a", 1), ("b", "hello"), ("another", [5, 1, 9]),
("sub", {"key": "value"})])
STRING = "The quick brown dog jumped over the lazy fox. Again."
NUMBER = 1293039483
def app(environ, start_response):
start_response('200 Ok', [('Content-type', 'application/json')])
thing = environ['PATH_INFO'][1:].upper()
if environ['REQUEST_METHOD'] == 'GET':
return [dumps(globals()[thing])]
elif environ['REQUEST_METHOD'] == 'POST':
post_data = str(environ['wsgi.input'].read())
params = parse_qsl(post_data)
return ['You said: ',
' and '.join('%s=%s' % p for p in params)]
def setup_all(cls):
pass
def teardown_all(cls):
pass
def setup_each(inst):
pass
def teardown_each(inst):
pass
client = TestApp(app)