File: test_wsgiref_inputwrapper_with_size.py

package info (click to toggle)
python-falcon 4.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,172 kB
  • sloc: python: 33,608; javascript: 92; sh: 50; makefile: 50
file content (33 lines) | stat: -rw-r--r-- 1,186 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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.
        resp.text = json.dumps({'data': req.bounded_stream.read().decode('utf-8')})


class TestWsgiRefInputWrapper:
    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()`"""
        app = falcon.App()
        type_route = '/type'
        app.add_route(type_route, TypeResource())
        client = testing.TestClient(app)

        result = client.simulate_post(path=type_route, body='hello')

        assert result.status == falcon.HTTP_200
        assert result.json == {'data': 'hello'}