-
Notifications
You must be signed in to change notification settings - Fork 0
/
hello.py
executable file
·164 lines (118 loc) · 4.06 KB
/
hello.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
flaskies.hello
==============
``'hello world'`` examples
--------------------------
.. ::
Copyright: (c) 2017 by Victor Hui.
Licence: BSD-3-Clause (see LICENSE for more details)
* test running:
- ``GET`` method of :meth:`flask.request`
- :meth:`flask.escape`
- :meth:`flask.Markup`
- :meth:`Flask.testclient`
- :meth:`flask.render_template`
* create a :py:obj:`hello` :class:`Blueprint`
.. code-block:: python
hello = Blueprint('hello',import_name=__name__)
* define three :py:obj:`view_func`'s:
.. literalinclude:: ../hello.py
:pyobject: say
.. literalinclude:: ../hello.py
:pyobject: say_escaped
.. literalinclude:: ../hello.py
:pyobject: viewtestcases
-----
"""
from __future__ import unicode_literals
import sys, re
from flask import (
Flask, Blueprint, render_template, url_for,
escape, Markup)
hello = Blueprint('hello',import_name=__name__)
@hello.route('/hello')
@hello.route('/hello/<friends>')
def say(friends='world'):
return 'hello {}!'.format(friends)
@hello.route('/Hello')
@hello.route('/Hello/<friends>')
def say_escaped(friends='World'):
return 'Hello {}!'.format(escape(friends))
re_findall_testcases = re.compile(
""">>> got.* = testclient.get\('(.*)'\)"""
).findall
def href(url,descr=None):
"""return ``Markup('<a href="{0}">{1}</a>')`` for (`url, descr`);
>>> assert href("/") == Markup('<a href="/">/</a>')
"""
if descr is None:
descr = url
return Markup('<a href="{0}">{1}</a>'.format(url,escape(descr)))
def gettestcases():
"""return testcases used here in the doctest;
>>> testapp = Flask(__name__)
>>> testapp.register_blueprint(hello)
>>> testclient = testapp.test_client()
>>> with testapp.test_request_context():
... testcases = gettestcases()
>>> testcases == [
... '/hello',
... '/hello/there',
... '/hello/<friends>',
... '/Hello/<friends>'
... ]
True
* basic tests:
>>> got = testclient.get('/hello')
>>> assert got.status == '200 OK'
>>> assert got.get_data(as_text=True) == 'hello world!'
>>> got = testclient.get('/hello/there')
>>> assert got.status == '200 OK'
>>> assert got.get_data(as_text=True) == 'hello there!'
* ``/hello``/`<friends>` does not take trailing slash,
>>> testclient.get('/hello/')
<Response streamed [404 NOT FOUND]>
* :meth:`flask.escape` and not, rules are *case sensitive*!
>>> got = testclient.get('/hello/<friends>')
>>> assert got.status == '200 OK'
>>> assert got.get_data(as_text=True) == 'hello <friends>!'
>>> got = testclient.get('/Hello/<friends>')
>>> assert got.status == '200 OK'
>>> assert got.get_data(as_text=True) == 'Hello <friends>!'
* :meth:`flask.escape` and :meth:`flask.Markup`:
>>> (escape('<em>escaped</em>') ==
... Markup(u'<em>escaped</em>') ==
... escape(Markup(u'<em>escaped</em>')))
True
>>> (escape(Markup('<em>escaped</em>')) ==
... Markup(Markup('<em>escaped</em>')) ==
... Markup('<em>escaped</em>'))
True
"""
url_prefix = url_for('hello.viewtestcases')[:-len('/testcases')]
testcases= re_findall_testcases(gettestcases.__doc__)
return [ url_prefix + testcase for testcase in testcases ]
@hello.route('/testcases')
def viewtestcases():
hrefs = [ (href(url),) for url in gettestcases() ]
return render_template(
'tableview.htm',caption='hello',
tables=[dict(records=hrefs,headings=('testcases',)),])
def create_hello_app():
"""app factory"""
app = Flask(__name__)
app.config['TEMPLATES_AUTO_RELOAD'] = True
app.register_blueprint(hello)
app.add_url_rule("/",endpoint='hello.viewtestcases')
return app
if __name__ == '__main__':
import sys
import doctest
app = create_hello_app()
if sys.argv[0] != "":
app.run(debug=True,use_reloader=True)
else:
print(doctest.testmod(optionflags=doctest.REPORT_ONLY_FIRST_FAILURE))
exec(doctest.script_from_examples(gettestcases.__doc__))