test_http_header.py (973:1abd9fc2caf3) test_http_header.py (1017:887a4bbabf1e)
1import unittest
2import unit
3
1import unittest
2import unit
3
4class TestUnitHTTPHeader(unit.TestUnitApplicationPython):
5
4
5class TestUnitHTTPHeader(unit.TestUnitApplicationPython):
6 def setUpClass():
7 unit.TestUnit().check_modules('python')
8
9 def test_http_header_value_leading_sp(self):
10 self.load('custom_header')
11
6 def setUpClass():
7 unit.TestUnit().check_modules('python')
8
9 def test_http_header_value_leading_sp(self):
10 self.load('custom_header')
11
12 resp = self.get(headers={
13 'Host': 'localhost',
14 'Custom-Header': ' ,',
15 'Connection': 'close'
16 })
12 resp = self.get(
13 headers={
14 'Host': 'localhost',
15 'Custom-Header': ' ,',
16 'Connection': 'close',
17 }
18 )
17
18 self.assertEqual(resp['status'], 200, 'value leading sp status')
19
20 self.assertEqual(resp['status'], 200, 'value leading sp status')
19 self.assertEqual(resp['headers']['Custom-Header'], ',',
20 'value leading sp custom header')
21 self.assertEqual(
22 resp['headers']['Custom-Header'],
23 ',',
24 'value leading sp custom header',
25 )
21
22 def test_http_header_value_leading_htab(self):
23 self.load('custom_header')
24
26
27 def test_http_header_value_leading_htab(self):
28 self.load('custom_header')
29
25 resp = self.get(headers={
26 'Host': 'localhost',
27 'Custom-Header': '\t,',
28 'Connection': 'close'
29 })
30 resp = self.get(
31 headers={
32 'Host': 'localhost',
33 'Custom-Header': '\t,',
34 'Connection': 'close',
35 }
36 )
30
31 self.assertEqual(resp['status'], 200, 'value leading htab status')
37
38 self.assertEqual(resp['status'], 200, 'value leading htab status')
32 self.assertEqual(resp['headers']['Custom-Header'], ',',
33 'value leading htab custom header')
39 self.assertEqual(
40 resp['headers']['Custom-Header'],
41 ',',
42 'value leading htab custom header',
43 )
34
35 def test_http_header_value_trailing_sp(self):
36 self.load('custom_header')
37
44
45 def test_http_header_value_trailing_sp(self):
46 self.load('custom_header')
47
38 resp = self.get(headers={
39 'Host': 'localhost',
40 'Custom-Header': ', ',
41 'Connection': 'close'
42 })
48 resp = self.get(
49 headers={
50 'Host': 'localhost',
51 'Custom-Header': ', ',
52 'Connection': 'close',
53 }
54 )
43
44 self.assertEqual(resp['status'], 200, 'value trailing sp status')
55
56 self.assertEqual(resp['status'], 200, 'value trailing sp status')
45 self.assertEqual(resp['headers']['Custom-Header'], ',',
46 'value trailing sp custom header')
57 self.assertEqual(
58 resp['headers']['Custom-Header'],
59 ',',
60 'value trailing sp custom header',
61 )
47
48 def test_http_header_value_trailing_htab(self):
49 self.load('custom_header')
50
62
63 def test_http_header_value_trailing_htab(self):
64 self.load('custom_header')
65
51 resp = self.get(headers={
52 'Host': 'localhost',
53 'Custom-Header': ',\t',
54 'Connection': 'close'
55 })
66 resp = self.get(
67 headers={
68 'Host': 'localhost',
69 'Custom-Header': ',\t',
70 'Connection': 'close',
71 }
72 )
56
57 self.assertEqual(resp['status'], 200, 'value trailing htab status')
73
74 self.assertEqual(resp['status'], 200, 'value trailing htab status')
58 self.assertEqual(resp['headers']['Custom-Header'], ',',
59 'value trailing htab custom header')
75 self.assertEqual(
76 resp['headers']['Custom-Header'],
77 ',',
78 'value trailing htab custom header',
79 )
60
61 def test_http_header_value_both_sp(self):
62 self.load('custom_header')
63
80
81 def test_http_header_value_both_sp(self):
82 self.load('custom_header')
83
64 resp = self.get(headers={
65 'Host': 'localhost',
66 'Custom-Header': ' , ',
67 'Connection': 'close'
68 })
84 resp = self.get(
85 headers={
86 'Host': 'localhost',
87 'Custom-Header': ' , ',
88 'Connection': 'close',
89 }
90 )
69
70 self.assertEqual(resp['status'], 200, 'value both sp status')
91
92 self.assertEqual(resp['status'], 200, 'value both sp status')
71 self.assertEqual(resp['headers']['Custom-Header'], ',',
72 'value both sp custom header')
93 self.assertEqual(
94 resp['headers']['Custom-Header'],
95 ',',
96 'value both sp custom header',
97 )
73
74 def test_http_header_value_both_htab(self):
75 self.load('custom_header')
76
98
99 def test_http_header_value_both_htab(self):
100 self.load('custom_header')
101
77 resp = self.get(headers={
78 'Host': 'localhost',
79 'Custom-Header': '\t,\t',
80 'Connection': 'close'
81 })
102 resp = self.get(
103 headers={
104 'Host': 'localhost',
105 'Custom-Header': '\t,\t',
106 'Connection': 'close',
107 }
108 )
82
83 self.assertEqual(resp['status'], 200, 'value both htab status')
109
110 self.assertEqual(resp['status'], 200, 'value both htab status')
84 self.assertEqual(resp['headers']['Custom-Header'], ',',
85 'value both htab custom header')
111 self.assertEqual(
112 resp['headers']['Custom-Header'],
113 ',',
114 'value both htab custom header',
115 )
86
87 def test_http_header_value_chars(self):
88 self.load('custom_header')
89
116
117 def test_http_header_value_chars(self):
118 self.load('custom_header')
119
90 resp = self.get(headers={
91 'Host': 'localhost',
92 'Custom-Header': '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~',
93 'Connection': 'close'
94 })
120 resp = self.get(
121 headers={
122 'Host': 'localhost',
123 'Custom-Header': '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~',
124 'Connection': 'close',
125 }
126 )
95
96 self.assertEqual(resp['status'], 200, 'value chars status')
127
128 self.assertEqual(resp['status'], 200, 'value chars status')
97 self.assertEqual(resp['headers']['Custom-Header'],
98 '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~', 'value chars custom header')
129 self.assertEqual(
130 resp['headers']['Custom-Header'],
131 '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~',
132 'value chars custom header',
133 )
99
100 def test_http_header_value_chars_edge(self):
101 self.load('custom_header')
102
134
135 def test_http_header_value_chars_edge(self):
136 self.load('custom_header')
137
103 resp = self.http(b"""GET / HTTP/1.1
138 resp = self.http(
139 b"""GET / HTTP/1.1
104Host: localhost
105Custom-Header: \x20\xFF
106Connection: close
107
140Host: localhost
141Custom-Header: \x20\xFF
142Connection: close
143
108""", raw=True, encoding='latin1')
144""",
145 raw=True,
146 encoding='latin1',
147 )
109
110 self.assertEqual(resp['status'], 200, 'value chars edge status')
148
149 self.assertEqual(resp['status'], 200, 'value chars edge status')
111 self.assertEqual(resp['headers']['Custom-Header'], '\xFF',
112 'value chars edge')
150 self.assertEqual(
151 resp['headers']['Custom-Header'], '\xFF', 'value chars edge'
152 )
113
114 def test_http_header_value_chars_below(self):
115 self.load('custom_header')
116
153
154 def test_http_header_value_chars_below(self):
155 self.load('custom_header')
156
117 resp = self.http(b"""GET / HTTP/1.1
157 resp = self.http(
158 b"""GET / HTTP/1.1
118Host: localhost
119Custom-Header: \x1F
120Connection: close
121
159Host: localhost
160Custom-Header: \x1F
161Connection: close
162
122""", raw=True)
163""",
164 raw=True,
165 )
123
124 self.assertEqual(resp['status'], 400, 'value chars below')
125
126 def test_http_header_field_leading_sp(self):
127 self.load('empty')
128
166
167 self.assertEqual(resp['status'], 400, 'value chars below')
168
169 def test_http_header_field_leading_sp(self):
170 self.load('empty')
171
129 resp = self.get(headers={
130 'Host': 'localhost',
131 ' Custom-Header': 'blah',
132 'Connection': 'close'
133 })
172 resp = self.get(
173 headers={
174 'Host': 'localhost',
175 ' Custom-Header': 'blah',
176 'Connection': 'close',
177 }
178 )
134
135 self.assertEqual(resp['status'], 400, 'field leading sp')
136
137 def test_http_header_field_leading_htab(self):
138 self.load('empty')
139
179
180 self.assertEqual(resp['status'], 400, 'field leading sp')
181
182 def test_http_header_field_leading_htab(self):
183 self.load('empty')
184
140 resp = self.get(headers={
141 'Host': 'localhost',
142 '\tCustom-Header': 'blah',
143 'Connection': 'close'
144 })
185 resp = self.get(
186 headers={
187 'Host': 'localhost',
188 '\tCustom-Header': 'blah',
189 'Connection': 'close',
190 }
191 )
145
146 self.assertEqual(resp['status'], 400, 'field leading htab')
147
148 def test_http_header_field_trailing_sp(self):
149 self.load('empty')
150
192
193 self.assertEqual(resp['status'], 400, 'field leading htab')
194
195 def test_http_header_field_trailing_sp(self):
196 self.load('empty')
197
151 resp = self.get(headers={
152 'Host': 'localhost',
153 'Custom-Header ': 'blah',
154 'Connection': 'close'
155 })
198 resp = self.get(
199 headers={
200 'Host': 'localhost',
201 'Custom-Header ': 'blah',
202 'Connection': 'close',
203 }
204 )
156
157 self.assertEqual(resp['status'], 400, 'field trailing sp')
158
159 def test_http_header_field_trailing_htab(self):
160 self.load('empty')
161
205
206 self.assertEqual(resp['status'], 400, 'field trailing sp')
207
208 def test_http_header_field_trailing_htab(self):
209 self.load('empty')
210
162 resp = self.get(headers={
163 'Host': 'localhost',
164 'Custom-Header\t': 'blah',
165 'Connection': 'close'
166 })
211 resp = self.get(
212 headers={
213 'Host': 'localhost',
214 'Custom-Header\t': 'blah',
215 'Connection': 'close',
216 }
217 )
167
168 self.assertEqual(resp['status'], 400, 'field trailing htab')
169
170 def test_http_header_content_length_big(self):
171 self.load('empty')
172
218
219 self.assertEqual(resp['status'], 400, 'field trailing htab')
220
221 def test_http_header_content_length_big(self):
222 self.load('empty')
223
173 self.assertEqual(self.post(headers={
174 'Host': 'localhost',
175 'Content-Length': str(2 ** 64),
176 'Connection': 'close'
177 }, body='X' * 1000)['status'], 400, 'Content-Length big')
224 self.assertEqual(
225 self.post(
226 headers={
227 'Host': 'localhost',
228 'Content-Length': str(2 ** 64),
229 'Connection': 'close',
230 },
231 body='X' * 1000,
232 )['status'],
233 400,
234 'Content-Length big',
235 )
178
179 def test_http_header_content_length_negative(self):
180 self.load('empty')
181
236
237 def test_http_header_content_length_negative(self):
238 self.load('empty')
239
182 self.assertEqual(self.post(headers={
183 'Host': 'localhost',
184 'Content-Length': '-100',
185 'Connection': 'close'
186 }, body='X' * 1000)['status'], 400, 'Content-Length negative')
240 self.assertEqual(
241 self.post(
242 headers={
243 'Host': 'localhost',
244 'Content-Length': '-100',
245 'Connection': 'close',
246 },
247 body='X' * 1000,
248 )['status'],
249 400,
250 'Content-Length negative',
251 )
187
188 def test_http_header_content_length_text(self):
189 self.load('empty')
190
252
253 def test_http_header_content_length_text(self):
254 self.load('empty')
255
191 self.assertEqual(self.post(headers={
192 'Host': 'localhost',
193 'Content-Length': 'blah',
194 'Connection': 'close'
195 }, body='X' * 1000)['status'], 400, 'Content-Length text')
256 self.assertEqual(
257 self.post(
258 headers={
259 'Host': 'localhost',
260 'Content-Length': 'blah',
261 'Connection': 'close',
262 },
263 body='X' * 1000,
264 )['status'],
265 400,
266 'Content-Length text',
267 )
196
197 def test_http_header_content_length_multiple_values(self):
198 self.load('empty')
199
268
269 def test_http_header_content_length_multiple_values(self):
270 self.load('empty')
271
200 self.assertEqual(self.post(headers={
201 'Host': 'localhost',
202 'Content-Length': '41, 42',
203 'Connection': 'close'
204 }, body='X' * 1000)['status'], 400, 'Content-Length multiple value')
272 self.assertEqual(
273 self.post(
274 headers={
275 'Host': 'localhost',
276 'Content-Length': '41, 42',
277 'Connection': 'close',
278 },
279 body='X' * 1000,
280 )['status'],
281 400,
282 'Content-Length multiple value',
283 )
205
206 def test_http_header_content_length_multiple_fields(self):
207 self.load('empty')
208
284
285 def test_http_header_content_length_multiple_fields(self):
286 self.load('empty')
287
209 self.assertEqual(self.post(headers={
210 'Host': 'localhost',
211 'Content-Length': ['41', '42'],
212 'Connection': 'close'
213 }, body='X' * 1000)['status'], 400, 'Content-Length multiple fields')
288 self.assertEqual(
289 self.post(
290 headers={
291 'Host': 'localhost',
292 'Content-Length': ['41', '42'],
293 'Connection': 'close',
294 },
295 body='X' * 1000,
296 )['status'],
297 400,
298 'Content-Length multiple fields',
299 )
214
215 def test_http_header_host_absent(self):
216 self.load('host')
217
218 resp = self.get(headers={'Connection': 'close'})
219
220 self.assertEqual(resp['status'], 200, 'Host absent status')
300
301 def test_http_header_host_absent(self):
302 self.load('host')
303
304 resp = self.get(headers={'Connection': 'close'})
305
306 self.assertEqual(resp['status'], 200, 'Host absent status')
221 self.assertNotEqual(resp['headers']['X-Server-Name'], '',
222 'Host absent SERVER_NAME')
307 self.assertNotEqual(
308 resp['headers']['X-Server-Name'], '', 'Host absent SERVER_NAME'
309 )
223
224 def test_http_header_host_empty(self):
225 self.load('host')
226
310
311 def test_http_header_host_empty(self):
312 self.load('host')
313
227 resp = self.get(headers={
228 'Host': '',
229 'Connection': 'close'
230 })
314 resp = self.get(headers={'Host': '', 'Connection': 'close'})
231
232 self.assertEqual(resp['status'], 200, 'Host empty status')
315
316 self.assertEqual(resp['status'], 200, 'Host empty status')
233 self.assertNotEqual(resp['headers']['X-Server-Name'], '',
234 'Host empty SERVER_NAME')
317 self.assertNotEqual(
318 resp['headers']['X-Server-Name'], '', 'Host empty SERVER_NAME'
319 )
235
236 def test_http_header_host_big(self):
237 self.load('empty')
238
320
321 def test_http_header_host_big(self):
322 self.load('empty')
323
239 self.assertEqual(self.get(headers={
240 'Host': 'X' * 10000,
241 'Connection': 'close'
242 })['status'], 431, 'Host big')
324 self.assertEqual(
325 self.get(headers={'Host': 'X' * 10000, 'Connection': 'close'})[
326 'status'
327 ],
328 431,
329 'Host big',
330 )
243
244 def test_http_header_host_port(self):
245 self.load('host')
246
331
332 def test_http_header_host_port(self):
333 self.load('host')
334
247 resp = self.get(headers={
248 'Host': 'exmaple.com:7080',
249 'Connection': 'close'
250 })
335 resp = self.get(
336 headers={'Host': 'exmaple.com:7080', 'Connection': 'close'}
337 )
251
252 self.assertEqual(resp['status'], 200, 'Host port status')
338
339 self.assertEqual(resp['status'], 200, 'Host port status')
253 self.assertEqual(resp['headers']['X-Server-Name'], 'exmaple.com',
254 'Host port SERVER_NAME')
255 self.assertEqual(resp['headers']['X-Http-Host'], 'exmaple.com:7080',
256 'Host port HTTP_HOST')
340 self.assertEqual(
341 resp['headers']['X-Server-Name'],
342 'exmaple.com',
343 'Host port SERVER_NAME',
344 )
345 self.assertEqual(
346 resp['headers']['X-Http-Host'],
347 'exmaple.com:7080',
348 'Host port HTTP_HOST',
349 )
257
258 def test_http_header_host_port_empty(self):
259 self.load('host')
260
350
351 def test_http_header_host_port_empty(self):
352 self.load('host')
353
261 resp = self.get(headers={
262 'Host': 'exmaple.com:',
263 'Connection': 'close'
264 })
354 resp = self.get(
355 headers={'Host': 'exmaple.com:', 'Connection': 'close'}
356 )
265
266 self.assertEqual(resp['status'], 200, 'Host port empty status')
357
358 self.assertEqual(resp['status'], 200, 'Host port empty status')
267 self.assertEqual(resp['headers']['X-Server-Name'], 'exmaple.com',
268 'Host port empty SERVER_NAME')
269 self.assertEqual(resp['headers']['X-Http-Host'], 'exmaple.com:',
270 'Host port empty HTTP_HOST')
359 self.assertEqual(
360 resp['headers']['X-Server-Name'],
361 'exmaple.com',
362 'Host port empty SERVER_NAME',
363 )
364 self.assertEqual(
365 resp['headers']['X-Http-Host'],
366 'exmaple.com:',
367 'Host port empty HTTP_HOST',
368 )
271
272 def test_http_header_host_literal(self):
273 self.load('host')
274
369
370 def test_http_header_host_literal(self):
371 self.load('host')
372
275 resp = self.get(headers={
276 'Host': '127.0.0.1',
277 'Connection': 'close'
278 })
373 resp = self.get(headers={'Host': '127.0.0.1', 'Connection': 'close'})
279
280 self.assertEqual(resp['status'], 200, 'Host literal status')
374
375 self.assertEqual(resp['status'], 200, 'Host literal status')
281 self.assertEqual(resp['headers']['X-Server-Name'], '127.0.0.1',
282 'Host literal SERVER_NAME')
376 self.assertEqual(
377 resp['headers']['X-Server-Name'],
378 '127.0.0.1',
379 'Host literal SERVER_NAME',
380 )
283
284 def test_http_header_host_literal_ipv6(self):
285 self.load('host')
286
381
382 def test_http_header_host_literal_ipv6(self):
383 self.load('host')
384
287 resp = self.get(headers={
288 'Host': '[::1]:7080',
289 'Connection': 'close'
290 })
385 resp = self.get(headers={'Host': '[::1]:7080', 'Connection': 'close'})
291
292 self.assertEqual(resp['status'], 200, 'Host literal ipv6 status')
386
387 self.assertEqual(resp['status'], 200, 'Host literal ipv6 status')
293 self.assertEqual(resp['headers']['X-Server-Name'], '[::1]',
294 'Host literal ipv6 SERVER_NAME')
295 self.assertEqual(resp['headers']['X-Http-Host'], '[::1]:7080',
296 'Host literal ipv6 HTTP_HOST')
388 self.assertEqual(
389 resp['headers']['X-Server-Name'],
390 '[::1]',
391 'Host literal ipv6 SERVER_NAME',
392 )
393 self.assertEqual(
394 resp['headers']['X-Http-Host'],
395 '[::1]:7080',
396 'Host literal ipv6 HTTP_HOST',
397 )
297
298 def test_http_header_host_trailing_period(self):
299 self.load('host')
300
398
399 def test_http_header_host_trailing_period(self):
400 self.load('host')
401
301 resp = self.get(headers={
302 'Host': '127.0.0.1.',
303 'Connection': 'close'
304 })
402 resp = self.get(headers={'Host': '127.0.0.1.', 'Connection': 'close'})
305
306 self.assertEqual(resp['status'], 200, 'Host trailing period status')
403
404 self.assertEqual(resp['status'], 200, 'Host trailing period status')
307 self.assertEqual(resp['headers']['X-Server-Name'], '127.0.0.1',
308 'Host trailing period SERVER_NAME')
309 self.assertEqual(resp['headers']['X-Http-Host'], '127.0.0.1.',
310 'Host trailing period HTTP_HOST')
405 self.assertEqual(
406 resp['headers']['X-Server-Name'],
407 '127.0.0.1',
408 'Host trailing period SERVER_NAME',
409 )
410 self.assertEqual(
411 resp['headers']['X-Http-Host'],
412 '127.0.0.1.',
413 'Host trailing period HTTP_HOST',
414 )
311
312 def test_http_header_host_trailing_period_2(self):
313 self.load('host')
314
415
416 def test_http_header_host_trailing_period_2(self):
417 self.load('host')
418
315 resp = self.get(headers={
316 'Host': 'EXAMPLE.COM.',
317 'Connection': 'close'
318 })
419 resp = self.get(
420 headers={'Host': 'EXAMPLE.COM.', 'Connection': 'close'}
421 )
319
320 self.assertEqual(resp['status'], 200, 'Host trailing period 2 status')
422
423 self.assertEqual(resp['status'], 200, 'Host trailing period 2 status')
321 self.assertEqual(resp['headers']['X-Server-Name'], 'example.com',
322 'Host trailing period 2 SERVER_NAME')
323 self.assertEqual(resp['headers']['X-Http-Host'], 'EXAMPLE.COM.',
324 'Host trailing period 2 HTTP_HOST')
424 self.assertEqual(
425 resp['headers']['X-Server-Name'],
426 'example.com',
427 'Host trailing period 2 SERVER_NAME',
428 )
429 self.assertEqual(
430 resp['headers']['X-Http-Host'],
431 'EXAMPLE.COM.',
432 'Host trailing period 2 HTTP_HOST',
433 )
325
326 def test_http_header_host_case_insensitive(self):
327 self.load('host')
328
434
435 def test_http_header_host_case_insensitive(self):
436 self.load('host')
437
329 resp = self.get(headers={
330 'Host': 'EXAMPLE.COM',
331 'Connection': 'close'
332 })
438 resp = self.get(headers={'Host': 'EXAMPLE.COM', 'Connection': 'close'})
333
334 self.assertEqual(resp['status'], 200, 'Host case insensitive')
439
440 self.assertEqual(resp['status'], 200, 'Host case insensitive')
335 self.assertEqual(resp['headers']['X-Server-Name'], 'example.com',
336 'Host case insensitive SERVER_NAME')
441 self.assertEqual(
442 resp['headers']['X-Server-Name'],
443 'example.com',
444 'Host case insensitive SERVER_NAME',
445 )
337
338 def test_http_header_host_double_dot(self):
339 self.load('empty')
340
446
447 def test_http_header_host_double_dot(self):
448 self.load('empty')
449
341 self.assertEqual(self.get(headers={
342 'Host': '127.0.0..1',
343 'Connection': 'close'
344 })['status'], 400, 'Host double dot')
450 self.assertEqual(
451 self.get(headers={'Host': '127.0.0..1', 'Connection': 'close'})[
452 'status'
453 ],
454 400,
455 'Host double dot',
456 )
345
346 def test_http_header_host_slash(self):
347 self.load('empty')
348
457
458 def test_http_header_host_slash(self):
459 self.load('empty')
460
349 self.assertEqual(self.get(headers={
350 'Host': '/localhost',
351 'Connection': 'close'
352 })['status'], 400, 'Host slash')
461 self.assertEqual(
462 self.get(headers={'Host': '/localhost', 'Connection': 'close'})[
463 'status'
464 ],
465 400,
466 'Host slash',
467 )
353
354 def test_http_header_host_multiple_fields(self):
355 self.load('empty')
356
468
469 def test_http_header_host_multiple_fields(self):
470 self.load('empty')
471
357 self.assertEqual(self.get(headers={
358 'Host': ['localhost', 'example.com'],
359 'Connection': 'close'
360 })['status'], 400, 'Host multiple fields')
472 self.assertEqual(
473 self.get(
474 headers={
475 'Host': ['localhost', 'example.com'],
476 'Connection': 'close',
477 }
478 )['status'],
479 400,
480 'Host multiple fields',
481 )
361
482
483
362if __name__ == '__main__':
363 TestUnitHTTPHeader.main()
484if __name__ == '__main__':
485 TestUnitHTTPHeader.main()