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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
|
import os
import unittest
from unit.applications.lang.python import TestApplicationPython
class TestStatic(TestApplicationPython):
prerequisites = {'modules': ['python']}
def setUp(self):
super().setUp()
os.makedirs(self.testdir + '/assets/dir')
with open(self.testdir + '/assets/index.html', 'w') as index:
index.write('0123456789')
os.makedirs(self.testdir + '/assets/403')
os.chmod(self.testdir + '/assets/403', 0o000)
self._load_conf(
{
"listeners": {
"*:7080": {"pass": "routes"},
"*:7081": {"pass": "applications/empty"},
},
"routes": [{"action": {"share": self.testdir + "/assets"}}],
"applications": {
"empty": {
"type": "python",
"processes": {"spare": 0},
"path": self.current_dir + "/python/empty",
"working_directory": self.current_dir
+ "/python/empty",
"module": "wsgi",
}
},
}
)
def tearDown(self):
os.chmod(self.testdir + '/assets/403', 0o777)
super().tearDown()
def test_fallback(self):
self.assertIn(
'success',
self.conf({"share": "/blah"}, 'routes/0/action'),
'configure bad path no fallback',
)
self.assertEqual(self.get()['status'], 404, 'bad path no fallback')
self.assertIn(
'success',
self.conf(
{"share": "/blah", "fallback": {"pass": "applications/empty"}},
'routes/0/action',
),
'configure bad path fallback',
)
resp = self.get()
self.assertEqual(resp['status'], 200, 'bad path fallback status')
self.assertEqual(resp['body'], '', 'bad path fallback')
def test_fallback_valid_path(self):
self.assertIn(
'success',
self.conf(
{
"share": self.testdir + "/assets",
"fallback": {"pass": "applications/empty"},
},
'routes/0/action',
),
'configure fallback',
)
resp = self.get()
self.assertEqual(resp['status'], 200, 'fallback status')
self.assertEqual(resp['body'], '0123456789', 'fallback')
resp = self.get(url='/403/')
self.assertEqual(resp['status'], 200, 'fallback status 403')
self.assertEqual(resp['body'], '', 'fallback 403')
resp = self.post()
self.assertEqual(resp['status'], 200, 'fallback status 405')
self.assertEqual(resp['body'], '', 'fallback 405')
self.assertEqual(
self.get(url='/dir')['status'], 301, 'fallback status 301'
)
def test_fallback_nested(self):
self.assertIn(
'success',
self.conf(
{
"share": "/blah",
"fallback": {
"share": "/blah/blah",
"fallback": {"pass": "applications/empty"},
},
},
'routes/0/action',
),
'configure fallback nested',
)
resp = self.get()
self.assertEqual(resp['status'], 200, 'fallback nested status')
self.assertEqual(resp['body'], '', 'fallback nested')
def test_fallback_share(self):
self.assertIn(
'success',
self.conf(
{
"share": "/blah",
"fallback": {"share": self.testdir + "/assets"},
},
'routes/0/action',
),
'configure fallback share',
)
resp = self.get()
self.assertEqual(resp['status'], 200, 'fallback share status')
self.assertEqual(resp['body'], '0123456789', 'fallback share')
resp = self.head()
self.assertEqual(resp['status'], 200, 'fallback share status HEAD')
self.assertEqual(resp['body'], '', 'fallback share HEAD')
self.assertEqual(
self.get(url='/dir')['status'], 301, 'fallback share status 301'
)
def test_fallback_proxy(self):
self.assertIn(
'success',
self.conf(
{
"share": "/blah",
"fallback": {"proxy": "http://127.0.0.1:7081"},
},
'routes/0/action',
),
'configure fallback proxy',
)
resp = self.get()
self.assertEqual(resp['status'], 200, 'fallback proxy status')
self.assertEqual(resp['body'], '', 'fallback proxy')
@unittest.skip('not yet')
def test_fallback_proxy_cycle(self):
self.assertIn(
'success',
self.conf(
{
"share": "/blah",
"fallback": {"proxy": "http://127.0.0.1:7080"},
},
'routes/0/action',
),
'configure fallback cycle',
)
self.assertNotEqual(self.get()['status'], 200, 'fallback cycle')
self.assertIn(
'success', self.conf_delete('listeners/*:7081'), 'delete listener'
)
self.assertNotEqual(self.get()['status'], 200, 'fallback cycle 2')
def test_fallback_invalid(self):
self.assertIn(
'error',
self.conf({"share": "/blah", "fallback": {}}, 'routes/0/action'),
'configure fallback empty',
)
self.assertIn(
'error',
self.conf({"share": "/blah", "fallback": ""}, 'routes/0/action'),
'configure fallback not object',
)
self.assertIn(
'error',
self.conf(
{
"proxy": "http://127.0.0.1:7081",
"fallback": {"share": "/blah"},
},
'routes/0/action',
),
'configure fallback proxy invalid',
)
self.assertIn(
'error',
self.conf(
{
"pass": "applications/empty",
"fallback": {"share": "/blah"},
},
'routes/0/action',
),
'configure fallback pass invalid',
)
self.assertIn(
'error',
self.conf({"fallback": {"share": "/blah"}}, 'routes/0/action'),
'configure fallback only',
)
if __name__ == '__main__':
TestStatic.main()
|