xref: /unit/test/test_http_header.py (revision 942:424c1fdef545)
1import unittest
2import unit
3
4class TestUnitHTTPHeader(unit.TestUnitApplicationPython):
5
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            'Custom-Header': ' ,'
14        })
15
16        self.assertEqual(resp['status'], 200, 'value leading sp status')
17        self.assertEqual(resp['headers']['Custom-Header'], ',',
18            'value leading sp custom header')
19
20    def test_http_header_value_leading_htab(self):
21        self.load('custom_header')
22
23        resp = self.get(headers={
24            'Custom-Header': '\t,'
25        })
26
27        self.assertEqual(resp['status'], 200, 'value leading htab status')
28        self.assertEqual(resp['headers']['Custom-Header'], ',',
29            'value leading htab custom header')
30
31    def test_http_header_value_trailing_sp(self):
32        self.load('custom_header')
33
34        resp = self.get(headers={
35            'Custom-Header': ', '
36        })
37
38        self.assertEqual(resp['status'], 200, 'value trailing sp status')
39        self.assertEqual(resp['headers']['Custom-Header'], ',',
40            'value trailing sp custom header')
41
42    def test_http_header_value_trailing_htab(self):
43        self.load('custom_header')
44
45        resp = self.get(headers={
46            'Custom-Header': ',\t'
47        })
48
49        self.assertEqual(resp['status'], 200, 'value trailing htab status')
50        self.assertEqual(resp['headers']['Custom-Header'], ',',
51            'value trailing htab custom header')
52
53    def test_http_header_value_both_sp(self):
54        self.load('custom_header')
55
56        resp = self.get(headers={
57            'Custom-Header': ' , '
58        })
59
60        self.assertEqual(resp['status'], 200, 'value both sp status')
61        self.assertEqual(resp['headers']['Custom-Header'], ',',
62            'value both sp custom header')
63
64    def test_http_header_value_both_htab(self):
65        self.load('custom_header')
66
67        resp = self.get(headers={
68            'Custom-Header': '\t,\t'
69        })
70
71        self.assertEqual(resp['status'], 200, 'value both htab status')
72        self.assertEqual(resp['headers']['Custom-Header'], ',',
73            'value both htab custom header')
74
75    def test_http_header_value_chars(self):
76        self.load('custom_header')
77
78        resp = self.get(headers={
79            'Custom-Header': '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~'
80        })
81
82        self.assertEqual(resp['status'], 200, 'value chars status')
83        self.assertEqual(resp['headers']['Custom-Header'],
84            '(),/:;<=>?@[\]{}\t !#$%&\'*+-.^_`|~', 'value chars custom header')
85
86    def test_http_header_value_chars_edge(self):
87        self.load('custom_header')
88
89        resp = self.http(b"""GET / HTTP/1.1
90Host: localhost
91Custom-Header: \x20\xFF
92Connection: close
93
94""", raw=True, encoding='latin1')
95
96        self.assertEqual(resp['status'], 200, 'value chars edge status')
97        self.assertEqual(resp['headers']['Custom-Header'], '\xFF',
98            'value chars edge')
99
100    def test_http_header_value_chars_below(self):
101        self.load('custom_header')
102
103        resp = self.http(b"""GET / HTTP/1.1
104Host: localhost
105Custom-Header: \x1F
106Connection: close
107
108""", raw=True)
109
110        self.assertEqual(resp['status'], 400, 'value chars below')
111
112    def test_http_header_field_leading_sp(self):
113        self.load('empty')
114
115        resp = self.get(headers={
116            ' Custom-Header': 'blah'
117        })
118
119        self.assertEqual(resp['status'], 400, 'field leading sp')
120
121    def test_http_header_field_leading_htab(self):
122        self.load('empty')
123
124        resp = self.get(headers={
125            '\tCustom-Header': 'blah'
126        })
127
128        self.assertEqual(resp['status'], 400, 'field leading htab')
129
130    def test_http_header_field_trailing_sp(self):
131        self.load('empty')
132
133        resp = self.get(headers={
134            'Custom-Header ': 'blah'
135        })
136
137        self.assertEqual(resp['status'], 400, 'field trailing sp')
138
139    def test_http_header_field_trailing_htab(self):
140        self.load('empty')
141
142        resp = self.get(headers={
143            'Custom-Header\t': 'blah'
144        })
145
146        self.assertEqual(resp['status'], 400, 'field trailing htab')
147
148    @unittest.expectedFailure
149    def test_http_header_transfer_encoding_chunked(self):
150        self.load('empty')
151
152        resp = self.http(b"""GET / HTTP/1.1
153Host: localhost
154Transfer-Encoding: chunked
155Connection: close
156
157a
1580123456789
1590
160
161""", raw=True)
162
163        self.assertEqual(resp['status'], 200, 'transfer encoding chunked')
164
165    def test_http_header_content_length_big(self):
166        self.load('empty')
167
168        self.assertEqual(self.post(headers={
169            'Content-Length': str(2 ** 64),
170            'Connection': 'close',
171            'Host': 'localhost'
172        }, body='X' * 1000)['status'], 400, 'Content-Length big')
173
174    def test_http_header_content_length_negative(self):
175        self.load('empty')
176
177        self.assertEqual(self.post(headers={
178            'Content-Length': '-100',
179            'Connection': 'close',
180            'Host': 'localhost'
181        }, body='X' * 1000)['status'], 400, 'Content-Length negative')
182
183    def test_http_header_content_length_text(self):
184        self.load('empty')
185
186        self.assertEqual(self.post(headers={
187            'Content-Length': 'blah',
188            'Connection': 'close',
189            'Host': 'localhost'
190        }, body='X' * 1000)['status'], 400, 'Content-Length text')
191
192    def test_http_header_content_length_multiple_values(self):
193        self.load('empty')
194
195        self.assertEqual(self.post(headers={
196            'Content-Length': '41, 42',
197            'Connection': 'close',
198            'Host': 'localhost'
199        }, body='X' * 1000)['status'], 400, 'Content-Length multiple value')
200
201    def test_http_header_content_length_multiple_fields(self):
202        self.load('empty')
203
204        self.assertEqual(self.post(headers={
205            'Content-Length': ['41', '42'],
206            'Connection': 'close',
207            'Host': 'localhost'
208        }, body='X' * 1000)['status'], 400, 'Content-Length multiple fields')
209
210if __name__ == '__main__':
211    TestUnitHTTPHeader.main()
212