forked from marshmallow-code/webargs
/
test_aiohttpparser.py
74 lines (58 loc) · 2.8 KB
/
test_aiohttpparser.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
import asyncio
import webtest
import webtest_aiohttp
import pytest
from io import BytesIO
from webargs.core import MARSHMALLOW_VERSION_INFO
from webargs.testing import CommonTestCase
from tests.apps.aiohttp_app import create_app
class TestAIOHTTPParser(CommonTestCase):
def create_app(self):
return create_app()
def create_testapp(self, app):
loop = asyncio.get_event_loop()
self.loop = loop
return webtest_aiohttp.TestApp(app, loop=loop)
def after_create_app(self):
self.loop.close()
@pytest.mark.skip(reason="files location not supported for aiohttpparser")
def test_parse_files(self, testapp):
pass
def test_parse_match_info(self, testapp):
assert testapp.get("/echo_match_info/42").json == {"mymatch": 42}
def test_use_args_on_method_handler(self, testapp):
assert testapp.get("/echo_method").json == {"name": "World"}
assert testapp.get("/echo_method?name=Steve").json == {"name": "Steve"}
assert testapp.get("/echo_method_view").json == {"name": "World"}
assert testapp.get("/echo_method_view?name=Steve").json == {"name": "Steve"}
def test_invalid_status_code_passed_to_validation_error(self, testapp):
res = testapp.get("/error_invalid?text=foo", expect_errors=True)
assert res.status_code == 500
# regression test for https://github.com/sloria/webargs/issues/165
def test_multiple_args(self, testapp):
res = testapp.post_json(
"/echo_multiple_args", {"first": "1", "last": "2", "_ignore": 0}
)
assert res.json == {"first": "1", "last": "2"}
# regression test for https://github.com/sloria/webargs/issues/145
def test_nested_many_with_data_key(self, testapp):
res = testapp.post_json("/echo_nested_many_data_key", {"x_field": [{"id": 42}]})
# https://github.com/marshmallow-code/marshmallow/pull/714
if MARSHMALLOW_VERSION_INFO[0] < 3:
assert res.json == {"x_field": [{"id": 42}]}
res = testapp.post_json("/echo_nested_many_data_key", {"X-Field": [{"id": 24}]})
assert res.json == {"x_field": [{"id": 24}]}
res = testapp.post_json("/echo_nested_many_data_key", {})
assert res.json == {}
def test_schema_as_kwargs_view(self, testapp):
assert testapp.get("/echo_use_schema_as_kwargs").json == {"name": "World"}
assert testapp.get("/echo_use_schema_as_kwargs?name=Chandler").json == {
"name": "Chandler"
}
# https://github.com/sloria/webargs/pull/297
def test_empty_json_body(self, testapp):
environ = {"CONTENT_TYPE": "application/json", "wsgi.input": BytesIO(b"")}
req = webtest.TestRequest.blank("/echo", environ)
resp = testapp.do_request(req)
assert resp.json == {"name": "World"}