-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.py
103 lines (80 loc) · 3.07 KB
/
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
try:
import unittest2 as unittest
except ImportError:
import unittest
from flask import Flask
try:
from flask_headers import headers # support local usage without installed package
except:
from flask.ext.headers import headers # this is how you would normally import
class DefaultsTestCase(unittest.TestCase):
def setUp(self):
self.app = Flask(__name__)
@self.app.route('/')
@headers(foo='bar')
def wildcard():
return 'Welcome!'
def test_foo_in_headers(self):
''' Ensure foo:bar is in the headers!
'''
with self.app.test_client() as c:
result = c.get('/')
self.assertEqual(result.headers.get('foo'), 'bar')
class DictionaryTestCase(unittest.TestCase):
_headers_dict = {"foo":"bar", "baz":"qux"}
def setUp(self):
self.app = Flask(__name__)
@self.app.route('/')
@headers(self._headers_dict)
def wildcard():
return 'Welcome!'
def test_dict_in_headers(self):
''' Ensure foo:bar is in the headers!
'''
with self.app.test_client() as c:
result = c.get('/')
for k,v in self._headers_dict.items():
self.assertEqual(result.headers.get(str(k)), str(v))
class HybridTestCase(unittest.TestCase):
_headers_dict = {"foo":"bar", "baz":"qux"}
_headers_dict_id = id(_headers_dict)
_headers_dict_copy = dict(_headers_dict)
def setUp(self):
self.app = Flask(__name__)
@self.app.route('/')
@headers(self._headers_dict, extraParameter='MagicNumberSleven')
def wildcard():
return 'Welcome!'
@self.app.route('/bar')
def other():
return 'baz'
def test_no_mutation(self):
'''
Ensure that even a hybrid approach does not mutate the underlying
dictionary passed into the headers.
Note: this is probably a silly test case, but I think it is important
to document sideeffects, so I did.
'''
self.assertEqual(self._headers_dict_copy, self._headers_dict)
self.assertEqual(id(self._headers_dict), self._headers_dict_id)
def test_dict_in_headers(self):
'''
Ensure that the full set of headers are set.
'''
with self.app.test_client() as c:
result = c.get('/')
for k,v in self._headers_dict.items():
self.assertEqual(result.headers.get(str(k)), str(v))
self.assertEqual(result.headers.get('extraParameter'), 'MagicNumberSleven')
def test_no_headers_different_route(self):
'''
Ensure the headers are only injected into the proper route!
Note: this is simply a sanity check.
'''
with self.app.test_client() as c:
result = c.get('/bar')
for k,v in self._headers_dict.items():
self.assertNotEqual(result.headers.get(str(k)), str(v))
self.assertNotEqual(result.headers.get('extraParameter'), 'MagicNumberSleven')
if __name__ == "__main__":
unittest.main()