test_return.py (1477:b93d1acf81bd) test_return.py (1596:b7e2d4d92624)
1import re
2
3from unit.applications.proto import TestApplicationProto
4
5
6class TestReturn(TestApplicationProto):
7 prerequisites = {}
8
1import re
2
3from unit.applications.proto import TestApplicationProto
4
5
6class TestReturn(TestApplicationProto):
7 prerequisites = {}
8
9 def setUp(self):
10 super().setUp()
9 def setup_method(self):
10 super().setup_method()
11
12 self._load_conf(
13 {
14 "listeners": {"*:7080": {"pass": "routes"}},
15 "routes": [{"action": {"return": 200}}],
16 "applications": {},
17 }
18 )

--- 11 unchanged lines hidden (view full) ---

30Connection: close
31
32"""
33
34 return self.http(to_send, raw_resp=True, raw=True)
35
36 def test_return(self):
37 resp = self.get()
11
12 self._load_conf(
13 {
14 "listeners": {"*:7080": {"pass": "routes"}},
15 "routes": [{"action": {"return": 200}}],
16 "applications": {},
17 }
18 )

--- 11 unchanged lines hidden (view full) ---

30Connection: close
31
32"""
33
34 return self.http(to_send, raw_resp=True, raw=True)
35
36 def test_return(self):
37 resp = self.get()
38 self.assertEqual(resp['status'], 200)
39 self.assertIn('Server', resp['headers'])
40 self.assertIn('Date', resp['headers'])
41 self.assertEqual(resp['headers']['Content-Length'], '0')
42 self.assertEqual(resp['headers']['Connection'], 'close')
43 self.assertEqual(resp['body'], '', 'body')
38 assert resp['status'] == 200
39 assert 'Server' in resp['headers']
40 assert 'Date' in resp['headers']
41 assert resp['headers']['Content-Length'] == '0'
42 assert resp['headers']['Connection'] == 'close'
43 assert resp['body'] == '', 'body'
44
45 resp = self.post(body='blah')
44
45 resp = self.post(body='blah')
46 self.assertEqual(resp['status'], 200)
47 self.assertEqual(resp['body'], '', 'body')
46 assert resp['status'] == 200
47 assert resp['body'] == '', 'body'
48
49 resp = self.get_resps_sc()
48
49 resp = self.get_resps_sc()
50 self.assertEqual(len(re.findall('200 OK', resp)), 10)
51 self.assertEqual(len(re.findall('Connection:', resp)), 1)
52 self.assertEqual(len(re.findall('Connection: close', resp)), 1)
50 assert len(re.findall('200 OK', resp)) == 10
51 assert len(re.findall('Connection:', resp)) == 1
52 assert len(re.findall('Connection: close', resp)) == 1
53
54 resp = self.get(http_10=True)
53
54 resp = self.get(http_10=True)
55 self.assertEqual(resp['status'], 200)
56 self.assertIn('Server', resp['headers'])
57 self.assertIn('Date', resp['headers'])
58 self.assertEqual(resp['headers']['Content-Length'], '0')
59 self.assertNotIn('Connection', resp['headers'])
60 self.assertEqual(resp['body'], '', 'body')
55 assert resp['status'] == 200
56 assert 'Server' in resp['headers']
57 assert 'Date' in resp['headers']
58 assert resp['headers']['Content-Length'] == '0'
59 assert 'Connection' not in resp['headers']
60 assert resp['body'] == '', 'body'
61
62 def test_return_update(self):
61
62 def test_return_update(self):
63 self.assertIn('success', self.conf('0', 'routes/0/action/return'))
63 assert 'success' in self.conf('0', 'routes/0/action/return')
64
65 resp = self.get()
64
65 resp = self.get()
66 self.assertEqual(resp['status'], 0)
67 self.assertEqual(resp['body'], '')
66 assert resp['status'] == 0
67 assert resp['body'] == ''
68
68
69 self.assertIn('success', self.conf('404', 'routes/0/action/return'))
69 assert 'success' in self.conf('404', 'routes/0/action/return')
70
71 resp = self.get()
70
71 resp = self.get()
72 self.assertEqual(resp['status'], 404)
73 self.assertNotEqual(resp['body'], '')
72 assert resp['status'] == 404
73 assert resp['body'] != ''
74
74
75 self.assertIn('success', self.conf('598', 'routes/0/action/return'))
75 assert 'success' in self.conf('598', 'routes/0/action/return')
76
77 resp = self.get()
76
77 resp = self.get()
78 self.assertEqual(resp['status'], 598)
79 self.assertNotEqual(resp['body'], '')
78 assert resp['status'] == 598
79 assert resp['body'] != ''
80
80
81 self.assertIn('success', self.conf('999', 'routes/0/action/return'))
81 assert 'success' in self.conf('999', 'routes/0/action/return')
82
83 resp = self.get()
82
83 resp = self.get()
84 self.assertEqual(resp['status'], 999)
85 self.assertEqual(resp['body'], '')
84 assert resp['status'] == 999
85 assert resp['body'] == ''
86
87 def test_return_location(self):
88 reserved = ":/?#[]@!$&'()*+,;="
86
87 def test_return_location(self):
88 reserved = ":/?#[]@!$&'()*+,;="
89 unreserved = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
90 "0123456789-._~")
89 unreserved = (
90 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
91 "0123456789-._~"
92 )
91 unsafe = " \"%<>\\^`{|}"
92 unsafe_enc = "%20%22%25%3C%3E%5C%5E%60%7B%7C%7D"
93
94 def check_location(location, expect=None):
95 if expect is None:
96 expect = location
97
93 unsafe = " \"%<>\\^`{|}"
94 unsafe_enc = "%20%22%25%3C%3E%5C%5E%60%7B%7C%7D"
95
96 def check_location(location, expect=None):
97 if expect is None:
98 expect = location
99
98 self.assertIn(
99 'success',
100 self.conf(
101 {"return": 301, "location": location}, 'routes/0/action'
102 ),
103 'configure location'
104 )
100 assert 'success' in self.conf(
101 {"return": 301, "location": location}, 'routes/0/action'
102 ), 'configure location'
105
103
106 self.assertEqual(self.get()['headers']['Location'], expect)
104 assert self.get()['headers']['Location'] == expect
107
108 # FAIL: can't specify empty header value.
109 # check_location("")
110
111 check_location(reserved)
112
113 # After first "?" all other "?" encoded.
114 check_location("/?" + reserved, "/?:/%3F#[]@!$&'()*+,;=")

--- 25 unchanged lines hidden (view full) ---

140 # then whole string will be encoded.
141 check_location("%20")
142 check_location("/%20?%20#%20")
143 check_location(" %20", "%20%2520")
144 check_location("%20 ", "%2520%20")
145 check_location("/%20?%20#%20 ", "/%2520?%2520#%2520%20")
146
147 def test_return_location_edit(self):
105
106 # FAIL: can't specify empty header value.
107 # check_location("")
108
109 check_location(reserved)
110
111 # After first "?" all other "?" encoded.
112 check_location("/?" + reserved, "/?:/%3F#[]@!$&'()*+,;=")

--- 25 unchanged lines hidden (view full) ---

138 # then whole string will be encoded.
139 check_location("%20")
140 check_location("/%20?%20#%20")
141 check_location(" %20", "%20%2520")
142 check_location("%20 ", "%2520%20")
143 check_location("/%20?%20#%20 ", "/%2520?%2520#%2520%20")
144
145 def test_return_location_edit(self):
148 self.assertIn(
149 'success',
150 self.conf(
151 {"return": 302, "location": "blah"}, 'routes/0/action'
152 ),
153 'configure init location'
154 )
155 self.assertEqual(self.get()['headers']['Location'], 'blah')
146 assert 'success' in self.conf(
147 {"return": 302, "location": "blah"}, 'routes/0/action'
148 ), 'configure init location'
149 assert self.get()['headers']['Location'] == 'blah'
156
150
157 self.assertIn(
158 'success',
159 self.conf_delete('routes/0/action/location'),
160 'location delete'
161 )
162 self.assertNotIn('Location', self.get()['headers'])
151 assert 'success' in self.conf_delete(
152 'routes/0/action/location'
153 ), 'location delete'
154 assert 'Location' not in self.get()['headers']
163
155
164 self.assertIn(
165 'success',
166 self.conf('"blah"', 'routes/0/action/location'),
167 'location restore'
168 )
169 self.assertEqual(self.get()['headers']['Location'], 'blah')
156 assert 'success' in self.conf(
157 '"blah"', 'routes/0/action/location'
158 ), 'location restore'
159 assert self.get()['headers']['Location'] == 'blah'
170
160
171 self.assertIn(
172 'error',
173 self.conf_post('"blah"', 'routes/0/action/location'),
174 'location method not allowed'
175 )
176 self.assertEqual(self.get()['headers']['Location'], 'blah')
161 assert 'error' in self.conf_post(
162 '"blah"', 'routes/0/action/location'
163 ), 'location method not allowed'
164 assert self.get()['headers']['Location'] == 'blah'
177
178 def test_return_invalid(self):
179 def check_error(conf):
165
166 def test_return_invalid(self):
167 def check_error(conf):
180 self.assertIn('error', self.conf(conf, 'routes/0/action'))
168 assert 'error' in self.conf(conf, 'routes/0/action')
181
182 check_error({"return": "200"})
183 check_error({"return": []})
184 check_error({"return": 80.1})
185 check_error({"return": 1000})
186 check_error({"return": -1})
187 check_error({"return": 200, "share": "/blah"})
188
169
170 check_error({"return": "200"})
171 check_error({"return": []})
172 check_error({"return": 80.1})
173 check_error({"return": 1000})
174 check_error({"return": -1})
175 check_error({"return": 200, "share": "/blah"})
176
189 self.assertIn(
190 'error', self.conf('001', 'routes/0/action/return'), 'leading zero'
191 )
177 assert 'error' in self.conf(
178 '001', 'routes/0/action/return'
179 ), 'leading zero'
192
193 check_error({"return": 301, "location": 0})
194 check_error({"return": 301, "location": []})
180
181 check_error({"return": 301, "location": 0})
182 check_error({"return": 301, "location": []})
195
196
197if __name__ == '__main__':
198 TestReturn.main()