falcon-jsonify
falcon-jsonify copied to clipboard
process_request Middlewhere failing during falcon simulated tests
When attempting to use falcons inbuilt request simulation, Falcon throws an assertion error due to the middleware not passing the required parameters to req.stream.read(): pytest output:
def test_post(client, mock_mongo):
test_obj = {}
> resp = client.simulate_post(URL, body=json.dumps(test_obj))
tests/acceptance/test_stuff:107:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
venv/lib/python3.7/site-packages/falcon/testing/client.py:711: in simulate_post
return self.simulate_request('POST', path, **kwargs)
venv/lib/python3.7/site-packages/falcon/testing/client.py:764: in simulate_request
return simulate_request(self.app, *args, **kwargs)
venv/lib/python3.7/site-packages/falcon/testing/client.py:348: in simulate_request
iterable = validator(env, srmock)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/validate.py:181: in lint_app
iterator = application(environ, start_response_wrapper)
venv/lib/python3.7/site-packages/falcon/api.py:231: in __call__
process_request(req, resp)
venv/lib/python3.7/site-packages/falcon_jsonify/__init__.py:97: in process_request
body = req.stream.read()
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/validate.py:197: in read
assert_(len(args) == 1)
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
cond = False, args = ()
def assert_(cond, *args):
if not cond:
> raise AssertionError(*args)
E AssertionError
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/wsgiref/validate.py:128: AssertionError
This can be fixed by using the workaround suggested here: https://falcon.readthedocs.io/en/stable/api/request_and_response.html#falcon.Request.stream
# If Content-Length happens to be 0, or the header is
# missing altogether, this will not block.
data = req.stream.read(req.content_length or 0)
Without this, any simulated post calls or calls with an expected body seem to fail.
@AndreiRegiani Can you take a look at this? I also just ran into this issue. Sending a post with a data body fails without this fix.
i.e the following post would fail - since the middleware will fail to read the body
echo '{"number": "5"}' | http -v POST localhost:8001/create