def test_json(req): """Test GET / POST json""" # numbers are complete list data = req.json if req.is_chunked_request: raw = b'' # chunk must be read with extra method, uwsgi has own chunk = uwsgi.chunked_read() if uwsgi else req.read_chunk() while chunk: raw += chunk chunk = uwsgi.chunked_read() if uwsgi else req.read_chunk() data = parse_json_request(raw, req.charset) return JSONResponse(status_code=418, message="I'm teapot :-)", numbers=list(range(5)), request=data)
def test_utf_struct(self): rval = parse_json_request(b'{"lang":"\xc4\x8de\xc5\xa1tina"}') assert rval == {"lang": "čeština"}
def test_utf8(self): rval = parse_json_request(b'"\xc4\x8de\xc5\xa1tina"') assert rval == "čeština"
def test_unicode_struct(self): rval = parse_json_request(b'{"lang":"\\u010de\\u0161tina"}') assert rval == {"lang": "čeština"}
def test_error(self): with raises(HTTPException) as err: parse_json_request(BytesIO(b"abraka")) assert err.value.args[0] == 400 assert 'error' in err.value.args[1]
def test_unicode(self): rval = parse_json_request(b'"\\u010de\\u0161tina"') assert rval == "čeština"
def test_bool(self): assert isinstance(parse_json_request(b"true"), bool)
def test_null(self): assert parse_json_request(b"null") is None
def test_float(self): assert isinstance(parse_json_request(b"3.14"), float)
def test_int(self): assert isinstance(parse_json_request(b"23"), int)
def test_text(self): assert isinstance(parse_json_request(b'"text"'), str)
def test_list(self): assert isinstance(parse_json_request(b"[]"), JsonList)
def test_str(self): assert isinstance(parse_json_request(b"{}"), JsonDict)