-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_webtest_aiohttp.py
More file actions
69 lines (54 loc) · 1.83 KB
/
test_webtest_aiohttp.py
File metadata and controls
69 lines (54 loc) · 1.83 KB
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
# -*- coding: utf-8 -*-
import asyncio
import json
import pytest
from aiohttp import web
from aiohttp.web import json_response
from webtest_aiohttp import TestApp as WebTestApp
@pytest.fixture()
def app():
@asyncio.coroutine
def handler(request):
return json_response({'message': 'Hello world'})
@asyncio.coroutine
def echo_post(request):
form_body = yield from request.post()
return json_response(dict(form_body))
@asyncio.coroutine
def echo_json(request):
json_body = yield from request.json()
return json_response(json_body)
@asyncio.coroutine
def echo_headers(request):
return json_response(dict(request.headers))
@asyncio.coroutine
def echo_params(request):
return json_response(dict(request.query))
app_ = web.Application()
app_.router.add_route('GET', '/', handler)
app_.router.add_route('POST', '/echo_post', echo_post)
app_.router.add_route('POST', '/echo_json', echo_json)
app_.router.add_route('GET', '/echo_headers', echo_headers)
app_.router.add_route('GET', '/echo_params', echo_params)
return app_
@pytest.fixture()
def wt(app, loop):
return WebTestApp(app, loop=loop)
def test_get(wt):
res = wt.get('/')
assert res.status_code == 200
expected = {'message': 'Hello world'}
assert res.json == expected
assert res.text == json.dumps(expected)
def test_post_form(wt):
res = wt.post('/echo_post', {'name': 'Steve'})
assert res.json == {'name': 'Steve'}
def test_post_json(wt):
res = wt.post_json('/echo_json', {'name': 'Steve'})
assert res.json == {'name': 'Steve'}
def test_headers(wt):
res = wt.get('/echo_headers', headers={'X-Foo': 'Bar'})
assert res.json['X-Foo'] == 'Bar'
def test_params(wt):
res = wt.get('/echo_params?foo=bar')
assert res.json == {'foo': 'bar'}