forked from moraes/tipfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrouting_test.py
169 lines (133 loc) · 5.94 KB
/
routing_test.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
from __future__ import with_statement
from tipfy import Tipfy, RequestHandler, Response
from tipfy.routing import HandlerPrefix, NamePrefix, Router, Rule
from tipfy.utils import url_for
import test_utils
class TestRouter(test_utils.BaseTestCase):
def test_add(self):
app = Tipfy()
router = Router(app)
self.assertEqual(len(list(router.map.iter_rules())), 0)
router.add(Rule('/', name='home', handler='HomeHandler'))
self.assertEqual(len(list(router.map.iter_rules())), 1)
router.add([
Rule('/about', name='about', handler='AboutHandler'),
Rule('/contact', name='contact', handler='ContactHandler'),
])
self.assertEqual(len(list(router.map.iter_rules())), 3)
class TestRouting(test_utils.BaseTestCase):
#==========================================================================
# HandlerPrefix
#==========================================================================
def test_handler_prefix(self):
rules = [
HandlerPrefix('resources.handlers.', [
Rule('/', name='home', handler='HomeHandler'),
Rule('/defaults', name='defaults', handler='HandlerWithRuleDefaults', defaults={'foo': 'bar'}),
])
]
app = Tipfy(rules)
client = app.get_test_client()
response = client.get('/')
self.assertEqual(response.data, 'Hello, World!')
response = client.get('/defaults')
self.assertEqual(response.data, 'bar')
#==========================================================================
# NamePrefix
#==========================================================================
def test_name_prefix(self):
class DummyHandler(RequestHandler):
def get(self, **kwargs):
return ''
rules = [
NamePrefix('company-', [
Rule('/', name='home', handler=DummyHandler),
Rule('/about', name='about', handler=DummyHandler),
Rule('/contact', name='contact', handler=DummyHandler),
]),
]
app = Tipfy(rules)
with app.get_test_handler('/') as handler:
self.assertEqual(handler.url_for('company-home'), '/')
self.assertEqual(handler.url_for('company-about'), '/about')
self.assertEqual(handler.url_for('company-contact'), '/contact')
with app.get_test_handler('/') as handler:
self.assertEqual(handler.request.rule.name, 'company-home')
with app.get_test_handler('/about') as handler:
self.assertEqual(handler.request.rule.name, 'company-about')
with app.get_test_handler('/contact') as handler:
self.assertEqual(handler.request.rule.name, 'company-contact')
#==========================================================================
# RegexConverter
#==========================================================================
def test_regex_converter(self):
class TestHandler(RequestHandler):
def get(self, **kwargs):
return Response(kwargs.get('path'))
app = Tipfy([
Rule('/<regex(".*"):path>', name='home', handler=TestHandler),
])
client = app.get_test_client()
response = client.get('/foo')
self.assertEqual(response.data, 'foo')
response = client.get('/foo/bar')
self.assertEqual(response.data, 'foo/bar')
response = client.get('/foo/bar/baz')
self.assertEqual(response.data, 'foo/bar/baz')
def test_url_for(self):
class DummyHandler(RequestHandler):
def get(self, **kwargs):
return ''
rules = [
NamePrefix('company-', [
Rule('/', name='home', handler=DummyHandler),
Rule('/about', name='about', handler=DummyHandler),
Rule('/contact', name='contact', handler=DummyHandler),
]),
]
app = Tipfy(rules)
with app.get_test_handler('/') as handler:
self.assertEqual(url_for('company-home'), '/')
self.assertEqual(url_for('company-about'), '/about')
self.assertEqual(url_for('company-contact'), '/contact')
class TestAlternativeRouting(test_utils.BaseTestCase):
def test_handler(self):
rules = [
HandlerPrefix('resources.alternative_routing.', [
Rule('/', name='home', handler='HomeHandler'),
Rule('/foo', name='home/foo', handler='HomeHandler:foo'),
Rule('/bar', name='home/bar', handler='HomeHandler:bar'),
Rule('/other/foo', name='other/foo', handler='OtherHandler:foo'),
Rule('/other/bar', name='other/bar', handler='OtherHandler:bar'),
])
]
app = Tipfy(rules)
client = app.get_test_client()
response = client.get('/')
self.assertEqual(response.data, 'home-get')
response = client.get('/foo')
self.assertEqual(response.data, 'home-foo')
response = client.get('/bar')
self.assertEqual(response.data, 'home-bar')
response = client.get('/other/foo')
self.assertEqual(response.data, 'other-foo')
response = client.get('/other/bar')
self.assertEqual(response.data, 'other-bar')
def test_function_handler(self):
rules = [
HandlerPrefix('resources.alternative_routing.', [
Rule('/', name='home', handler='home'),
Rule('/foo', name='home/foo', handler='foo'),
Rule('/bar', name='home/bar', handler='bar'),
])
]
app = Tipfy(rules)
client = app.get_test_client()
response = client.get('/')
self.assertEqual(response.data, 'home')
response = client.get('/foo')
self.assertEqual(response.data, 'foo')
response = client.get('/bar')
self.assertEqual(response.data, 'bar')
if __name__ == '__main__':
test_utils.main()