Files
deb-python-falcon/tests/test_validators.py
John Vrbanac 3495344ff4 feat: Request and Response media-type handling (#1050)
This is a first pass at a non-breaking change that'll allow for a customizable media handling system. This approach combines many of the suggestions brought up by the community in #145.

One the thing that is left out of this PR is handling full content negotiation (i.e. connecting the request's accept header to the response's content-type). Unfortunately, this is a harder problem to solve in a backwards compatible fashion that doesn't affect performance. However, especially as we move towards v2, I think that would be a great opportunity to revisit full negotiation. In the meantime, there are several easy workarounds for people needing this functionality.

Closes #145
2017-06-14 23:38:47 -06:00

52 lines
1019 B
Python

import sys
import pytest
import falcon
from falcon.media.validators import jsonschema
skip_py26 = pytest.mark.skipif(
sys.version_info[:2] == (2, 6),
reason='Minimum Python version for this feature is 2.7.x'
)
basic_schema = {
'type': 'object',
'properies': {
'message': {
'type': 'string',
},
},
'required': ['message'],
}
class SampleResource(object):
@jsonschema.validate(basic_schema)
def on_get(self, req, resp):
assert req.media is not None
class RequestStub(object):
media = {'message': 'something'}
@skip_py26
def test_jsonschema_validation_success():
req = RequestStub()
res = SampleResource()
assert res.on_get(req, None) is None
@skip_py26
def test_jsonschema_validation_failure():
req = RequestStub()
req.media = {}
res = SampleResource()
with pytest.raises(falcon.HTTPBadRequest) as err:
res.on_get(req, None)
assert err.value.description == '\'message\' is a required property'