Verified Commit 0b6ab0f5 authored by Mary's avatar Mary 🎉

misc.bodyparser: make some tests

parent a4b1f5c2
Pipeline #49380798 passed with stage
in 2 minutes and 5 seconds
......@@ -17,7 +17,7 @@ def _owo(string: str) -> str:
return string.replace('0', '0w0').replace('r', 'w')
@bp.middleware('request')
async def bodyparser(request):
def bodyparser(request):
"""Make body available at request.body"""
if request.body:
# This _probably_ won't become an issue but no sense in tempting fate
......@@ -33,7 +33,7 @@ async def bodyparser(request):
for key in form.keys():
new_form[key] = str(urllib.parse.unquote(form[key][0]))
request.body = new_form
elif request.headers.get('content-type', '').startswith('application/json') or (request.headers.get('content-type', None) != None and request.body.startswith(curly)):
elif request.headers.get('content-type', '').startswith('application/json') or (request.headers.get('content-type', None) == None and request.body.startswith(curly)):
# Shaky, but it should be alright since we
# need to support people who for whatever reason can't set it or something
request.body = request.json
......
# elixire: Image Host software
# Copyright 2018-2019, elixi.re Team and the elixire contributors
# SPDX-License-Identifier: AGPL-3.0-only
from sanic.request import Request
from multidict import CIMultiDict
import pytest
from ..api.bp.misc import bodyparser
from ..api.errors import BadInput
from ..api import schema
equivalent_dict = {
"foo": True,
"bar": "baz",
"foobar": "barfoo"
}
equivalent_urlencoded = b"foo=True&bar=baz&foobar=barfoo"
equivalent_json = b'{"foo":true,"bar":"baz","foobar":"barfoo"}'
# We really don't need to hook much up
def create_urlencoded_request():
request = Request(bytes("/", "utf8"), CIMultiDict([
("content-type", "application/x-www-form-urlencoded")
]), None, "POST", None)
request.body = equivalent_urlencoded
return request
def create_garbage_request(ambiguous=False):
headers = CIMultiDict()
if not ambiguous:
headers["content-type"] = "application/octet-stream"
request = Request(bytes("/", "utf8"), headers, None, "POST", None)
request.body = equivalent_urlencoded
return request
def create_json_request(ambiguous=False):
headers = CIMultiDict()
if not ambiguous:
headers["content-type"] = "application/json"
request = Request(bytes("/", "utf8"), headers, None, "POST", None)
request.body = equivalent_json
return request
# Test the testing environment essentially
def test_validation():
validate_body(equivalent_dict)
with pytest.raises(BadInput) as e_info:
validate_body({"foo":False, "bar":"baz", "foobar":"barfoo"})
def test_formdata_parsing():
request = create_urlencoded_request()
bodyparser(request)
validate_body(request.body)
def test_json_parsing():
request = create_json_request()
bodyparser(request)
validate_body(request.body)
def test_json_ambiguous_parsing():
request = create_json_request(True)
bodyparser(request)
validate_body(request.body)
def test_invalid_ambiguous_parsing():
request = create_garbage_request(True)
bodyparser(request)
assert isinstance(request.body, dict) == False
def test_invalid_parsing():
request = create_garbage_request(False)
bodyparser(request)
assert isinstance(request.body, dict) == False
def validate_body(doc):
schema.validate(doc, {
"foo": {"type": "boolean", "required": True, "coerce": bool, "allowed": [True]},
"bar": {"type": "string", "required": True, "allowed": ["baz"]},
"foobar": {"type": "string", "required": True}
})
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment