-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_funcroute.py
96 lines (72 loc) · 2.67 KB
/
test_funcroute.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import webtest
import mock
from json import loads, dumps
from funcroute import *
class Expando(object):
"""
A completely promiscuous object.
"""
def __init__(self, **attrs):
self.update(attrs)
def update(self, attrs):
self.__dict__.update(attrs)
return self
def pytest_funcarg__responder(request):
return Matcher(Expando())
def pytest_funcarg__default(request):
default = mock.Mock()
default.return_value = ('200 OK', None, ('default response',))
return default
def pytest_funcarg__wtfix(request):
responder = pytest_funcarg__responder(request)
return (responder.handler, responder, webtest.TestApp(responder))
def pytest_funcarg__defaultfix(request):
(handler, responder, wt), default = (pytest_funcarg__wtfix(request),
pytest_funcarg__default(request))
handler.default = default
return (handler, responder, wt, default)
def test_init_w_str():
assert Matcher('labmod')
def test_init_w_obj():
assert Matcher(mock.Mock())
def test_root_to_default(defaultfix):
handler, responder, wt, default = defaultfix
res = wt.get('/')
assert res.status_int == 200
assert res.normal_body == 'default response'
def test_root_to_root(wtfix, default):
handler, responder, wt = wtfix
handler.root = default
res = wt.get('/')
assert res.status_int == 200
assert res.normal_body == 'default response'
def test_any_to_default(defaultfix):
handler, responder, wt, default = defaultfix
res = wt.get('/whatever')
assert res.status_int == 200
assert res.normal_body == 'default response'
def test_missing(wtfix):
handler, responder, wt = wtfix
res = wt.get('/unavailable', status=404)
def test_text_defaults():
assert text('foo') == ('200 OK', {'Content-Type': 'text/plain'}, ('foo',))
def test_text_defaults():
assert html('foo') == ('200 OK', {'Content-Type': 'text/html'}, ('foo',))
def test_html_other_status():
assert (html('foo', '404 NOT FOUND') ==
('404 NOT FOUND', {'Content-Type': 'text/html'}, ('foo',)) )
def test_json_defaults():
assert (json('foo') ==
('200 OK', {'Content-Type': 'application/json'}, ('"foo"',)))
def test_json_other_status():
assert (json('foo', '404') ==
('404', {'Content-Type': 'application/json'}, ('"foo"',)))
def test_post(wtfix):
handler, responder, wt = wtfix
handler.api = (lambda req, *segs:
json(loads(req.body)) )
res = wt.post('/api',
dumps('foo'),
{'Content-Type': 'application/json'})
assert (res ==
'200 OK', {'Content-Type': 'application/json'}, '"foo"')