Files
deb-python-falcon/tests/test_wsgiref_inputwrapper_with_size.py
Matthew Miller 22b46ade84 fix(Request): Prevent req.stream.read() from raising an error during tests
Add `wsgiref.validate.InputWrapper` to the list of classes that are checked for wrapping in 
Request's initializer. This prevents an error from being raised when testing an app that
calls `req.stream.read()` without specifying a size argument.
2016-04-14 17:47:08 -05:00

37 lines
1.4 KiB
Python

import json
import falcon
from falcon import testing
class TypeResource(testing.SimpleTestResource):
"""A simple resource to return the posted request body."""
@falcon.before(testing.capture_responder_args)
def on_post(self, req, resp, **kwargs):
resp.status = falcon.HTTP_200
# NOTE(masterkale): No size needs to be specified here because we're
# emulating a stream read in production. The request should be wrapped
# well enough to automatically specify a size when calling `read()`
# during either production or when running tests
resp.body = json.dumps({'data': req.stream.read().decode('utf-8')})
class TestWsgiRefInputWrapper(testing.TestCase):
def setUp(self):
super(TestWsgiRefInputWrapper, self).setUp()
# Set up a route to our TypeResoure
self.type_route = '/type'
self.api.add_route(self.type_route, TypeResource())
def test_resources_can_read_request_stream_during_tests(self):
"""Make sure we can perform a simple request during testing.
Originally, testing would fail after performing a request because no
size was specified when calling `wsgiref.validate.InputWrapper.read()`
via `req.stream.read()`"""
result = self.simulate_post(path=self.type_route, body='hello')
self.assertEqual(result.status, falcon.HTTP_200)
self.assertEqual(result.json, {'data': 'hello'})