1 | n/a | import errno |
---|
2 | n/a | from http import client |
---|
3 | n/a | import io |
---|
4 | n/a | import itertools |
---|
5 | n/a | import os |
---|
6 | n/a | import array |
---|
7 | n/a | import socket |
---|
8 | n/a | |
---|
9 | n/a | import unittest |
---|
10 | n/a | TestCase = unittest.TestCase |
---|
11 | n/a | |
---|
12 | n/a | from test import support |
---|
13 | n/a | |
---|
14 | n/a | here = os.path.dirname(__file__) |
---|
15 | n/a | # Self-signed cert file for 'localhost' |
---|
16 | n/a | CERT_localhost = os.path.join(here, 'keycert.pem') |
---|
17 | n/a | # Self-signed cert file for 'fakehostname' |
---|
18 | n/a | CERT_fakehostname = os.path.join(here, 'keycert2.pem') |
---|
19 | n/a | # Self-signed cert file for self-signed.pythontest.net |
---|
20 | n/a | CERT_selfsigned_pythontestdotnet = os.path.join(here, 'selfsigned_pythontestdotnet.pem') |
---|
21 | n/a | |
---|
22 | n/a | # constants for testing chunked encoding |
---|
23 | n/a | chunked_start = ( |
---|
24 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
25 | n/a | 'Transfer-Encoding: chunked\r\n\r\n' |
---|
26 | n/a | 'a\r\n' |
---|
27 | n/a | 'hello worl\r\n' |
---|
28 | n/a | '3\r\n' |
---|
29 | n/a | 'd! \r\n' |
---|
30 | n/a | '8\r\n' |
---|
31 | n/a | 'and now \r\n' |
---|
32 | n/a | '22\r\n' |
---|
33 | n/a | 'for something completely different\r\n' |
---|
34 | n/a | ) |
---|
35 | n/a | chunked_expected = b'hello world! and now for something completely different' |
---|
36 | n/a | chunk_extension = ";foo=bar" |
---|
37 | n/a | last_chunk = "0\r\n" |
---|
38 | n/a | last_chunk_extended = "0" + chunk_extension + "\r\n" |
---|
39 | n/a | trailers = "X-Dummy: foo\r\nX-Dumm2: bar\r\n" |
---|
40 | n/a | chunked_end = "\r\n" |
---|
41 | n/a | |
---|
42 | n/a | HOST = support.HOST |
---|
43 | n/a | |
---|
44 | n/a | class FakeSocket: |
---|
45 | n/a | def __init__(self, text, fileclass=io.BytesIO, host=None, port=None): |
---|
46 | n/a | if isinstance(text, str): |
---|
47 | n/a | text = text.encode("ascii") |
---|
48 | n/a | self.text = text |
---|
49 | n/a | self.fileclass = fileclass |
---|
50 | n/a | self.data = b'' |
---|
51 | n/a | self.sendall_calls = 0 |
---|
52 | n/a | self.file_closed = False |
---|
53 | n/a | self.host = host |
---|
54 | n/a | self.port = port |
---|
55 | n/a | |
---|
56 | n/a | def sendall(self, data): |
---|
57 | n/a | self.sendall_calls += 1 |
---|
58 | n/a | self.data += data |
---|
59 | n/a | |
---|
60 | n/a | def makefile(self, mode, bufsize=None): |
---|
61 | n/a | if mode != 'r' and mode != 'rb': |
---|
62 | n/a | raise client.UnimplementedFileMode() |
---|
63 | n/a | # keep the file around so we can check how much was read from it |
---|
64 | n/a | self.file = self.fileclass(self.text) |
---|
65 | n/a | self.file.close = self.file_close #nerf close () |
---|
66 | n/a | return self.file |
---|
67 | n/a | |
---|
68 | n/a | def file_close(self): |
---|
69 | n/a | self.file_closed = True |
---|
70 | n/a | |
---|
71 | n/a | def close(self): |
---|
72 | n/a | pass |
---|
73 | n/a | |
---|
74 | n/a | def setsockopt(self, level, optname, value): |
---|
75 | n/a | pass |
---|
76 | n/a | |
---|
77 | n/a | class EPipeSocket(FakeSocket): |
---|
78 | n/a | |
---|
79 | n/a | def __init__(self, text, pipe_trigger): |
---|
80 | n/a | # When sendall() is called with pipe_trigger, raise EPIPE. |
---|
81 | n/a | FakeSocket.__init__(self, text) |
---|
82 | n/a | self.pipe_trigger = pipe_trigger |
---|
83 | n/a | |
---|
84 | n/a | def sendall(self, data): |
---|
85 | n/a | if self.pipe_trigger in data: |
---|
86 | n/a | raise OSError(errno.EPIPE, "gotcha") |
---|
87 | n/a | self.data += data |
---|
88 | n/a | |
---|
89 | n/a | def close(self): |
---|
90 | n/a | pass |
---|
91 | n/a | |
---|
92 | n/a | class NoEOFBytesIO(io.BytesIO): |
---|
93 | n/a | """Like BytesIO, but raises AssertionError on EOF. |
---|
94 | n/a | |
---|
95 | n/a | This is used below to test that http.client doesn't try to read |
---|
96 | n/a | more from the underlying file than it should. |
---|
97 | n/a | """ |
---|
98 | n/a | def read(self, n=-1): |
---|
99 | n/a | data = io.BytesIO.read(self, n) |
---|
100 | n/a | if data == b'': |
---|
101 | n/a | raise AssertionError('caller tried to read past EOF') |
---|
102 | n/a | return data |
---|
103 | n/a | |
---|
104 | n/a | def readline(self, length=None): |
---|
105 | n/a | data = io.BytesIO.readline(self, length) |
---|
106 | n/a | if data == b'': |
---|
107 | n/a | raise AssertionError('caller tried to read past EOF') |
---|
108 | n/a | return data |
---|
109 | n/a | |
---|
110 | n/a | class FakeSocketHTTPConnection(client.HTTPConnection): |
---|
111 | n/a | """HTTPConnection subclass using FakeSocket; counts connect() calls""" |
---|
112 | n/a | |
---|
113 | n/a | def __init__(self, *args): |
---|
114 | n/a | self.connections = 0 |
---|
115 | n/a | super().__init__('example.com') |
---|
116 | n/a | self.fake_socket_args = args |
---|
117 | n/a | self._create_connection = self.create_connection |
---|
118 | n/a | |
---|
119 | n/a | def connect(self): |
---|
120 | n/a | """Count the number of times connect() is invoked""" |
---|
121 | n/a | self.connections += 1 |
---|
122 | n/a | return super().connect() |
---|
123 | n/a | |
---|
124 | n/a | def create_connection(self, *pos, **kw): |
---|
125 | n/a | return FakeSocket(*self.fake_socket_args) |
---|
126 | n/a | |
---|
127 | n/a | class HeaderTests(TestCase): |
---|
128 | n/a | def test_auto_headers(self): |
---|
129 | n/a | # Some headers are added automatically, but should not be added by |
---|
130 | n/a | # .request() if they are explicitly set. |
---|
131 | n/a | |
---|
132 | n/a | class HeaderCountingBuffer(list): |
---|
133 | n/a | def __init__(self): |
---|
134 | n/a | self.count = {} |
---|
135 | n/a | def append(self, item): |
---|
136 | n/a | kv = item.split(b':') |
---|
137 | n/a | if len(kv) > 1: |
---|
138 | n/a | # item is a 'Key: Value' header string |
---|
139 | n/a | lcKey = kv[0].decode('ascii').lower() |
---|
140 | n/a | self.count.setdefault(lcKey, 0) |
---|
141 | n/a | self.count[lcKey] += 1 |
---|
142 | n/a | list.append(self, item) |
---|
143 | n/a | |
---|
144 | n/a | for explicit_header in True, False: |
---|
145 | n/a | for header in 'Content-length', 'Host', 'Accept-encoding': |
---|
146 | n/a | conn = client.HTTPConnection('example.com') |
---|
147 | n/a | conn.sock = FakeSocket('blahblahblah') |
---|
148 | n/a | conn._buffer = HeaderCountingBuffer() |
---|
149 | n/a | |
---|
150 | n/a | body = 'spamspamspam' |
---|
151 | n/a | headers = {} |
---|
152 | n/a | if explicit_header: |
---|
153 | n/a | headers[header] = str(len(body)) |
---|
154 | n/a | conn.request('POST', '/', body, headers) |
---|
155 | n/a | self.assertEqual(conn._buffer.count[header.lower()], 1) |
---|
156 | n/a | |
---|
157 | n/a | def test_content_length_0(self): |
---|
158 | n/a | |
---|
159 | n/a | class ContentLengthChecker(list): |
---|
160 | n/a | def __init__(self): |
---|
161 | n/a | list.__init__(self) |
---|
162 | n/a | self.content_length = None |
---|
163 | n/a | def append(self, item): |
---|
164 | n/a | kv = item.split(b':', 1) |
---|
165 | n/a | if len(kv) > 1 and kv[0].lower() == b'content-length': |
---|
166 | n/a | self.content_length = kv[1].strip() |
---|
167 | n/a | list.append(self, item) |
---|
168 | n/a | |
---|
169 | n/a | # Here, we're testing that methods expecting a body get a |
---|
170 | n/a | # content-length set to zero if the body is empty (either None or '') |
---|
171 | n/a | bodies = (None, '') |
---|
172 | n/a | methods_with_body = ('PUT', 'POST', 'PATCH') |
---|
173 | n/a | for method, body in itertools.product(methods_with_body, bodies): |
---|
174 | n/a | conn = client.HTTPConnection('example.com') |
---|
175 | n/a | conn.sock = FakeSocket(None) |
---|
176 | n/a | conn._buffer = ContentLengthChecker() |
---|
177 | n/a | conn.request(method, '/', body) |
---|
178 | n/a | self.assertEqual( |
---|
179 | n/a | conn._buffer.content_length, b'0', |
---|
180 | n/a | 'Header Content-Length incorrect on {}'.format(method) |
---|
181 | n/a | ) |
---|
182 | n/a | |
---|
183 | n/a | # For these methods, we make sure that content-length is not set when |
---|
184 | n/a | # the body is None because it might cause unexpected behaviour on the |
---|
185 | n/a | # server. |
---|
186 | n/a | methods_without_body = ( |
---|
187 | n/a | 'GET', 'CONNECT', 'DELETE', 'HEAD', 'OPTIONS', 'TRACE', |
---|
188 | n/a | ) |
---|
189 | n/a | for method in methods_without_body: |
---|
190 | n/a | conn = client.HTTPConnection('example.com') |
---|
191 | n/a | conn.sock = FakeSocket(None) |
---|
192 | n/a | conn._buffer = ContentLengthChecker() |
---|
193 | n/a | conn.request(method, '/', None) |
---|
194 | n/a | self.assertEqual( |
---|
195 | n/a | conn._buffer.content_length, None, |
---|
196 | n/a | 'Header Content-Length set for empty body on {}'.format(method) |
---|
197 | n/a | ) |
---|
198 | n/a | |
---|
199 | n/a | # If the body is set to '', that's considered to be "present but |
---|
200 | n/a | # empty" rather than "missing", so content length would be set, even |
---|
201 | n/a | # for methods that don't expect a body. |
---|
202 | n/a | for method in methods_without_body: |
---|
203 | n/a | conn = client.HTTPConnection('example.com') |
---|
204 | n/a | conn.sock = FakeSocket(None) |
---|
205 | n/a | conn._buffer = ContentLengthChecker() |
---|
206 | n/a | conn.request(method, '/', '') |
---|
207 | n/a | self.assertEqual( |
---|
208 | n/a | conn._buffer.content_length, b'0', |
---|
209 | n/a | 'Header Content-Length incorrect on {}'.format(method) |
---|
210 | n/a | ) |
---|
211 | n/a | |
---|
212 | n/a | # If the body is set, make sure Content-Length is set. |
---|
213 | n/a | for method in itertools.chain(methods_without_body, methods_with_body): |
---|
214 | n/a | conn = client.HTTPConnection('example.com') |
---|
215 | n/a | conn.sock = FakeSocket(None) |
---|
216 | n/a | conn._buffer = ContentLengthChecker() |
---|
217 | n/a | conn.request(method, '/', ' ') |
---|
218 | n/a | self.assertEqual( |
---|
219 | n/a | conn._buffer.content_length, b'1', |
---|
220 | n/a | 'Header Content-Length incorrect on {}'.format(method) |
---|
221 | n/a | ) |
---|
222 | n/a | |
---|
223 | n/a | def test_putheader(self): |
---|
224 | n/a | conn = client.HTTPConnection('example.com') |
---|
225 | n/a | conn.sock = FakeSocket(None) |
---|
226 | n/a | conn.putrequest('GET','/') |
---|
227 | n/a | conn.putheader('Content-length', 42) |
---|
228 | n/a | self.assertIn(b'Content-length: 42', conn._buffer) |
---|
229 | n/a | |
---|
230 | n/a | conn.putheader('Foo', ' bar ') |
---|
231 | n/a | self.assertIn(b'Foo: bar ', conn._buffer) |
---|
232 | n/a | conn.putheader('Bar', '\tbaz\t') |
---|
233 | n/a | self.assertIn(b'Bar: \tbaz\t', conn._buffer) |
---|
234 | n/a | conn.putheader('Authorization', 'Bearer mytoken') |
---|
235 | n/a | self.assertIn(b'Authorization: Bearer mytoken', conn._buffer) |
---|
236 | n/a | conn.putheader('IterHeader', 'IterA', 'IterB') |
---|
237 | n/a | self.assertIn(b'IterHeader: IterA\r\n\tIterB', conn._buffer) |
---|
238 | n/a | conn.putheader('LatinHeader', b'\xFF') |
---|
239 | n/a | self.assertIn(b'LatinHeader: \xFF', conn._buffer) |
---|
240 | n/a | conn.putheader('Utf8Header', b'\xc3\x80') |
---|
241 | n/a | self.assertIn(b'Utf8Header: \xc3\x80', conn._buffer) |
---|
242 | n/a | conn.putheader('C1-Control', b'next\x85line') |
---|
243 | n/a | self.assertIn(b'C1-Control: next\x85line', conn._buffer) |
---|
244 | n/a | conn.putheader('Embedded-Fold-Space', 'is\r\n allowed') |
---|
245 | n/a | self.assertIn(b'Embedded-Fold-Space: is\r\n allowed', conn._buffer) |
---|
246 | n/a | conn.putheader('Embedded-Fold-Tab', 'is\r\n\tallowed') |
---|
247 | n/a | self.assertIn(b'Embedded-Fold-Tab: is\r\n\tallowed', conn._buffer) |
---|
248 | n/a | conn.putheader('Key Space', 'value') |
---|
249 | n/a | self.assertIn(b'Key Space: value', conn._buffer) |
---|
250 | n/a | conn.putheader('KeySpace ', 'value') |
---|
251 | n/a | self.assertIn(b'KeySpace : value', conn._buffer) |
---|
252 | n/a | conn.putheader(b'Nonbreak\xa0Space', 'value') |
---|
253 | n/a | self.assertIn(b'Nonbreak\xa0Space: value', conn._buffer) |
---|
254 | n/a | conn.putheader(b'\xa0NonbreakSpace', 'value') |
---|
255 | n/a | self.assertIn(b'\xa0NonbreakSpace: value', conn._buffer) |
---|
256 | n/a | |
---|
257 | n/a | def test_ipv6host_header(self): |
---|
258 | n/a | # Default host header on IPv6 transaction should be wrapped by [] if |
---|
259 | n/a | # it is an IPv6 address |
---|
260 | n/a | expected = b'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \ |
---|
261 | n/a | b'Accept-Encoding: identity\r\n\r\n' |
---|
262 | n/a | conn = client.HTTPConnection('[2001::]:81') |
---|
263 | n/a | sock = FakeSocket('') |
---|
264 | n/a | conn.sock = sock |
---|
265 | n/a | conn.request('GET', '/foo') |
---|
266 | n/a | self.assertTrue(sock.data.startswith(expected)) |
---|
267 | n/a | |
---|
268 | n/a | expected = b'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \ |
---|
269 | n/a | b'Accept-Encoding: identity\r\n\r\n' |
---|
270 | n/a | conn = client.HTTPConnection('[2001:102A::]') |
---|
271 | n/a | sock = FakeSocket('') |
---|
272 | n/a | conn.sock = sock |
---|
273 | n/a | conn.request('GET', '/foo') |
---|
274 | n/a | self.assertTrue(sock.data.startswith(expected)) |
---|
275 | n/a | |
---|
276 | n/a | def test_malformed_headers_coped_with(self): |
---|
277 | n/a | # Issue 19996 |
---|
278 | n/a | body = "HTTP/1.1 200 OK\r\nFirst: val\r\n: nval\r\nSecond: val\r\n\r\n" |
---|
279 | n/a | sock = FakeSocket(body) |
---|
280 | n/a | resp = client.HTTPResponse(sock) |
---|
281 | n/a | resp.begin() |
---|
282 | n/a | |
---|
283 | n/a | self.assertEqual(resp.getheader('First'), 'val') |
---|
284 | n/a | self.assertEqual(resp.getheader('Second'), 'val') |
---|
285 | n/a | |
---|
286 | n/a | def test_parse_all_octets(self): |
---|
287 | n/a | # Ensure no valid header field octet breaks the parser |
---|
288 | n/a | body = ( |
---|
289 | n/a | b'HTTP/1.1 200 OK\r\n' |
---|
290 | n/a | b"!#$%&'*+-.^_`|~: value\r\n" # Special token characters |
---|
291 | n/a | b'VCHAR: ' + bytes(range(0x21, 0x7E + 1)) + b'\r\n' |
---|
292 | n/a | b'obs-text: ' + bytes(range(0x80, 0xFF + 1)) + b'\r\n' |
---|
293 | n/a | b'obs-fold: text\r\n' |
---|
294 | n/a | b' folded with space\r\n' |
---|
295 | n/a | b'\tfolded with tab\r\n' |
---|
296 | n/a | b'Content-Length: 0\r\n' |
---|
297 | n/a | b'\r\n' |
---|
298 | n/a | ) |
---|
299 | n/a | sock = FakeSocket(body) |
---|
300 | n/a | resp = client.HTTPResponse(sock) |
---|
301 | n/a | resp.begin() |
---|
302 | n/a | self.assertEqual(resp.getheader('Content-Length'), '0') |
---|
303 | n/a | self.assertEqual(resp.msg['Content-Length'], '0') |
---|
304 | n/a | self.assertEqual(resp.getheader("!#$%&'*+-.^_`|~"), 'value') |
---|
305 | n/a | self.assertEqual(resp.msg["!#$%&'*+-.^_`|~"], 'value') |
---|
306 | n/a | vchar = ''.join(map(chr, range(0x21, 0x7E + 1))) |
---|
307 | n/a | self.assertEqual(resp.getheader('VCHAR'), vchar) |
---|
308 | n/a | self.assertEqual(resp.msg['VCHAR'], vchar) |
---|
309 | n/a | self.assertIsNotNone(resp.getheader('obs-text')) |
---|
310 | n/a | self.assertIn('obs-text', resp.msg) |
---|
311 | n/a | for folded in (resp.getheader('obs-fold'), resp.msg['obs-fold']): |
---|
312 | n/a | self.assertTrue(folded.startswith('text')) |
---|
313 | n/a | self.assertIn(' folded with space', folded) |
---|
314 | n/a | self.assertTrue(folded.endswith('folded with tab')) |
---|
315 | n/a | |
---|
316 | n/a | def test_invalid_headers(self): |
---|
317 | n/a | conn = client.HTTPConnection('example.com') |
---|
318 | n/a | conn.sock = FakeSocket('') |
---|
319 | n/a | conn.putrequest('GET', '/') |
---|
320 | n/a | |
---|
321 | n/a | # http://tools.ietf.org/html/rfc7230#section-3.2.4, whitespace is no |
---|
322 | n/a | # longer allowed in header names |
---|
323 | n/a | cases = ( |
---|
324 | n/a | (b'Invalid\r\nName', b'ValidValue'), |
---|
325 | n/a | (b'Invalid\rName', b'ValidValue'), |
---|
326 | n/a | (b'Invalid\nName', b'ValidValue'), |
---|
327 | n/a | (b'\r\nInvalidName', b'ValidValue'), |
---|
328 | n/a | (b'\rInvalidName', b'ValidValue'), |
---|
329 | n/a | (b'\nInvalidName', b'ValidValue'), |
---|
330 | n/a | (b' InvalidName', b'ValidValue'), |
---|
331 | n/a | (b'\tInvalidName', b'ValidValue'), |
---|
332 | n/a | (b'Invalid:Name', b'ValidValue'), |
---|
333 | n/a | (b':InvalidName', b'ValidValue'), |
---|
334 | n/a | (b'ValidName', b'Invalid\r\nValue'), |
---|
335 | n/a | (b'ValidName', b'Invalid\rValue'), |
---|
336 | n/a | (b'ValidName', b'Invalid\nValue'), |
---|
337 | n/a | (b'ValidName', b'InvalidValue\r\n'), |
---|
338 | n/a | (b'ValidName', b'InvalidValue\r'), |
---|
339 | n/a | (b'ValidName', b'InvalidValue\n'), |
---|
340 | n/a | ) |
---|
341 | n/a | for name, value in cases: |
---|
342 | n/a | with self.subTest((name, value)): |
---|
343 | n/a | with self.assertRaisesRegex(ValueError, 'Invalid header'): |
---|
344 | n/a | conn.putheader(name, value) |
---|
345 | n/a | |
---|
346 | n/a | |
---|
347 | n/a | class TransferEncodingTest(TestCase): |
---|
348 | n/a | expected_body = b"It's just a flesh wound" |
---|
349 | n/a | |
---|
350 | n/a | def test_endheaders_chunked(self): |
---|
351 | n/a | conn = client.HTTPConnection('example.com') |
---|
352 | n/a | conn.sock = FakeSocket(b'') |
---|
353 | n/a | conn.putrequest('POST', '/') |
---|
354 | n/a | conn.endheaders(self._make_body(), encode_chunked=True) |
---|
355 | n/a | |
---|
356 | n/a | _, _, body = self._parse_request(conn.sock.data) |
---|
357 | n/a | body = self._parse_chunked(body) |
---|
358 | n/a | self.assertEqual(body, self.expected_body) |
---|
359 | n/a | |
---|
360 | n/a | def test_explicit_headers(self): |
---|
361 | n/a | # explicit chunked |
---|
362 | n/a | conn = client.HTTPConnection('example.com') |
---|
363 | n/a | conn.sock = FakeSocket(b'') |
---|
364 | n/a | # this shouldn't actually be automatically chunk-encoded because the |
---|
365 | n/a | # calling code has explicitly stated that it's taking care of it |
---|
366 | n/a | conn.request( |
---|
367 | n/a | 'POST', '/', self._make_body(), {'Transfer-Encoding': 'chunked'}) |
---|
368 | n/a | |
---|
369 | n/a | _, headers, body = self._parse_request(conn.sock.data) |
---|
370 | n/a | self.assertNotIn('content-length', [k.lower() for k in headers.keys()]) |
---|
371 | n/a | self.assertEqual(headers['Transfer-Encoding'], 'chunked') |
---|
372 | n/a | self.assertEqual(body, self.expected_body) |
---|
373 | n/a | |
---|
374 | n/a | # explicit chunked, string body |
---|
375 | n/a | conn = client.HTTPConnection('example.com') |
---|
376 | n/a | conn.sock = FakeSocket(b'') |
---|
377 | n/a | conn.request( |
---|
378 | n/a | 'POST', '/', self.expected_body.decode('latin-1'), |
---|
379 | n/a | {'Transfer-Encoding': 'chunked'}) |
---|
380 | n/a | |
---|
381 | n/a | _, headers, body = self._parse_request(conn.sock.data) |
---|
382 | n/a | self.assertNotIn('content-length', [k.lower() for k in headers.keys()]) |
---|
383 | n/a | self.assertEqual(headers['Transfer-Encoding'], 'chunked') |
---|
384 | n/a | self.assertEqual(body, self.expected_body) |
---|
385 | n/a | |
---|
386 | n/a | # User-specified TE, but request() does the chunk encoding |
---|
387 | n/a | conn = client.HTTPConnection('example.com') |
---|
388 | n/a | conn.sock = FakeSocket(b'') |
---|
389 | n/a | conn.request('POST', '/', |
---|
390 | n/a | headers={'Transfer-Encoding': 'gzip, chunked'}, |
---|
391 | n/a | encode_chunked=True, |
---|
392 | n/a | body=self._make_body()) |
---|
393 | n/a | _, headers, body = self._parse_request(conn.sock.data) |
---|
394 | n/a | self.assertNotIn('content-length', [k.lower() for k in headers]) |
---|
395 | n/a | self.assertEqual(headers['Transfer-Encoding'], 'gzip, chunked') |
---|
396 | n/a | self.assertEqual(self._parse_chunked(body), self.expected_body) |
---|
397 | n/a | |
---|
398 | n/a | def test_request(self): |
---|
399 | n/a | for empty_lines in (False, True,): |
---|
400 | n/a | conn = client.HTTPConnection('example.com') |
---|
401 | n/a | conn.sock = FakeSocket(b'') |
---|
402 | n/a | conn.request( |
---|
403 | n/a | 'POST', '/', self._make_body(empty_lines=empty_lines)) |
---|
404 | n/a | |
---|
405 | n/a | _, headers, body = self._parse_request(conn.sock.data) |
---|
406 | n/a | body = self._parse_chunked(body) |
---|
407 | n/a | self.assertEqual(body, self.expected_body) |
---|
408 | n/a | self.assertEqual(headers['Transfer-Encoding'], 'chunked') |
---|
409 | n/a | |
---|
410 | n/a | # Content-Length and Transfer-Encoding SHOULD not be sent in the |
---|
411 | n/a | # same request |
---|
412 | n/a | self.assertNotIn('content-length', [k.lower() for k in headers]) |
---|
413 | n/a | |
---|
414 | n/a | def test_empty_body(self): |
---|
415 | n/a | # Zero-length iterable should be treated like any other iterable |
---|
416 | n/a | conn = client.HTTPConnection('example.com') |
---|
417 | n/a | conn.sock = FakeSocket(b'') |
---|
418 | n/a | conn.request('POST', '/', ()) |
---|
419 | n/a | _, headers, body = self._parse_request(conn.sock.data) |
---|
420 | n/a | self.assertEqual(headers['Transfer-Encoding'], 'chunked') |
---|
421 | n/a | self.assertNotIn('content-length', [k.lower() for k in headers]) |
---|
422 | n/a | self.assertEqual(body, b"0\r\n\r\n") |
---|
423 | n/a | |
---|
424 | n/a | def _make_body(self, empty_lines=False): |
---|
425 | n/a | lines = self.expected_body.split(b' ') |
---|
426 | n/a | for idx, line in enumerate(lines): |
---|
427 | n/a | # for testing handling empty lines |
---|
428 | n/a | if empty_lines and idx % 2: |
---|
429 | n/a | yield b'' |
---|
430 | n/a | if idx < len(lines) - 1: |
---|
431 | n/a | yield line + b' ' |
---|
432 | n/a | else: |
---|
433 | n/a | yield line |
---|
434 | n/a | |
---|
435 | n/a | def _parse_request(self, data): |
---|
436 | n/a | lines = data.split(b'\r\n') |
---|
437 | n/a | request = lines[0] |
---|
438 | n/a | headers = {} |
---|
439 | n/a | n = 1 |
---|
440 | n/a | while n < len(lines) and len(lines[n]) > 0: |
---|
441 | n/a | key, val = lines[n].split(b':') |
---|
442 | n/a | key = key.decode('latin-1').strip() |
---|
443 | n/a | headers[key] = val.decode('latin-1').strip() |
---|
444 | n/a | n += 1 |
---|
445 | n/a | |
---|
446 | n/a | return request, headers, b'\r\n'.join(lines[n + 1:]) |
---|
447 | n/a | |
---|
448 | n/a | def _parse_chunked(self, data): |
---|
449 | n/a | body = [] |
---|
450 | n/a | trailers = {} |
---|
451 | n/a | n = 0 |
---|
452 | n/a | lines = data.split(b'\r\n') |
---|
453 | n/a | # parse body |
---|
454 | n/a | while True: |
---|
455 | n/a | size, chunk = lines[n:n+2] |
---|
456 | n/a | size = int(size, 16) |
---|
457 | n/a | |
---|
458 | n/a | if size == 0: |
---|
459 | n/a | n += 1 |
---|
460 | n/a | break |
---|
461 | n/a | |
---|
462 | n/a | self.assertEqual(size, len(chunk)) |
---|
463 | n/a | body.append(chunk) |
---|
464 | n/a | |
---|
465 | n/a | n += 2 |
---|
466 | n/a | # we /should/ hit the end chunk, but check against the size of |
---|
467 | n/a | # lines so we're not stuck in an infinite loop should we get |
---|
468 | n/a | # malformed data |
---|
469 | n/a | if n > len(lines): |
---|
470 | n/a | break |
---|
471 | n/a | |
---|
472 | n/a | return b''.join(body) |
---|
473 | n/a | |
---|
474 | n/a | |
---|
475 | n/a | class BasicTest(TestCase): |
---|
476 | n/a | def test_status_lines(self): |
---|
477 | n/a | # Test HTTP status lines |
---|
478 | n/a | |
---|
479 | n/a | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
---|
480 | n/a | sock = FakeSocket(body) |
---|
481 | n/a | resp = client.HTTPResponse(sock) |
---|
482 | n/a | resp.begin() |
---|
483 | n/a | self.assertEqual(resp.read(0), b'') # Issue #20007 |
---|
484 | n/a | self.assertFalse(resp.isclosed()) |
---|
485 | n/a | self.assertFalse(resp.closed) |
---|
486 | n/a | self.assertEqual(resp.read(), b"Text") |
---|
487 | n/a | self.assertTrue(resp.isclosed()) |
---|
488 | n/a | self.assertFalse(resp.closed) |
---|
489 | n/a | resp.close() |
---|
490 | n/a | self.assertTrue(resp.closed) |
---|
491 | n/a | |
---|
492 | n/a | body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText" |
---|
493 | n/a | sock = FakeSocket(body) |
---|
494 | n/a | resp = client.HTTPResponse(sock) |
---|
495 | n/a | self.assertRaises(client.BadStatusLine, resp.begin) |
---|
496 | n/a | |
---|
497 | n/a | def test_bad_status_repr(self): |
---|
498 | n/a | exc = client.BadStatusLine('') |
---|
499 | n/a | self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''') |
---|
500 | n/a | |
---|
501 | n/a | def test_partial_reads(self): |
---|
502 | n/a | # if we have Content-Length, HTTPResponse knows when to close itself, |
---|
503 | n/a | # the same behaviour as when we read the whole thing with read() |
---|
504 | n/a | body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
---|
505 | n/a | sock = FakeSocket(body) |
---|
506 | n/a | resp = client.HTTPResponse(sock) |
---|
507 | n/a | resp.begin() |
---|
508 | n/a | self.assertEqual(resp.read(2), b'Te') |
---|
509 | n/a | self.assertFalse(resp.isclosed()) |
---|
510 | n/a | self.assertEqual(resp.read(2), b'xt') |
---|
511 | n/a | self.assertTrue(resp.isclosed()) |
---|
512 | n/a | self.assertFalse(resp.closed) |
---|
513 | n/a | resp.close() |
---|
514 | n/a | self.assertTrue(resp.closed) |
---|
515 | n/a | |
---|
516 | n/a | def test_mixed_reads(self): |
---|
517 | n/a | # readline() should update the remaining length, so that read() knows |
---|
518 | n/a | # how much data is left and does not raise IncompleteRead |
---|
519 | n/a | body = "HTTP/1.1 200 Ok\r\nContent-Length: 13\r\n\r\nText\r\nAnother" |
---|
520 | n/a | sock = FakeSocket(body) |
---|
521 | n/a | resp = client.HTTPResponse(sock) |
---|
522 | n/a | resp.begin() |
---|
523 | n/a | self.assertEqual(resp.readline(), b'Text\r\n') |
---|
524 | n/a | self.assertFalse(resp.isclosed()) |
---|
525 | n/a | self.assertEqual(resp.read(), b'Another') |
---|
526 | n/a | self.assertTrue(resp.isclosed()) |
---|
527 | n/a | self.assertFalse(resp.closed) |
---|
528 | n/a | resp.close() |
---|
529 | n/a | self.assertTrue(resp.closed) |
---|
530 | n/a | |
---|
531 | n/a | def test_partial_readintos(self): |
---|
532 | n/a | # if we have Content-Length, HTTPResponse knows when to close itself, |
---|
533 | n/a | # the same behaviour as when we read the whole thing with read() |
---|
534 | n/a | body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
---|
535 | n/a | sock = FakeSocket(body) |
---|
536 | n/a | resp = client.HTTPResponse(sock) |
---|
537 | n/a | resp.begin() |
---|
538 | n/a | b = bytearray(2) |
---|
539 | n/a | n = resp.readinto(b) |
---|
540 | n/a | self.assertEqual(n, 2) |
---|
541 | n/a | self.assertEqual(bytes(b), b'Te') |
---|
542 | n/a | self.assertFalse(resp.isclosed()) |
---|
543 | n/a | n = resp.readinto(b) |
---|
544 | n/a | self.assertEqual(n, 2) |
---|
545 | n/a | self.assertEqual(bytes(b), b'xt') |
---|
546 | n/a | self.assertTrue(resp.isclosed()) |
---|
547 | n/a | self.assertFalse(resp.closed) |
---|
548 | n/a | resp.close() |
---|
549 | n/a | self.assertTrue(resp.closed) |
---|
550 | n/a | |
---|
551 | n/a | def test_partial_reads_no_content_length(self): |
---|
552 | n/a | # when no length is present, the socket should be gracefully closed when |
---|
553 | n/a | # all data was read |
---|
554 | n/a | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
---|
555 | n/a | sock = FakeSocket(body) |
---|
556 | n/a | resp = client.HTTPResponse(sock) |
---|
557 | n/a | resp.begin() |
---|
558 | n/a | self.assertEqual(resp.read(2), b'Te') |
---|
559 | n/a | self.assertFalse(resp.isclosed()) |
---|
560 | n/a | self.assertEqual(resp.read(2), b'xt') |
---|
561 | n/a | self.assertEqual(resp.read(1), b'') |
---|
562 | n/a | self.assertTrue(resp.isclosed()) |
---|
563 | n/a | self.assertFalse(resp.closed) |
---|
564 | n/a | resp.close() |
---|
565 | n/a | self.assertTrue(resp.closed) |
---|
566 | n/a | |
---|
567 | n/a | def test_partial_readintos_no_content_length(self): |
---|
568 | n/a | # when no length is present, the socket should be gracefully closed when |
---|
569 | n/a | # all data was read |
---|
570 | n/a | body = "HTTP/1.1 200 Ok\r\n\r\nText" |
---|
571 | n/a | sock = FakeSocket(body) |
---|
572 | n/a | resp = client.HTTPResponse(sock) |
---|
573 | n/a | resp.begin() |
---|
574 | n/a | b = bytearray(2) |
---|
575 | n/a | n = resp.readinto(b) |
---|
576 | n/a | self.assertEqual(n, 2) |
---|
577 | n/a | self.assertEqual(bytes(b), b'Te') |
---|
578 | n/a | self.assertFalse(resp.isclosed()) |
---|
579 | n/a | n = resp.readinto(b) |
---|
580 | n/a | self.assertEqual(n, 2) |
---|
581 | n/a | self.assertEqual(bytes(b), b'xt') |
---|
582 | n/a | n = resp.readinto(b) |
---|
583 | n/a | self.assertEqual(n, 0) |
---|
584 | n/a | self.assertTrue(resp.isclosed()) |
---|
585 | n/a | |
---|
586 | n/a | def test_partial_reads_incomplete_body(self): |
---|
587 | n/a | # if the server shuts down the connection before the whole |
---|
588 | n/a | # content-length is delivered, the socket is gracefully closed |
---|
589 | n/a | body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText" |
---|
590 | n/a | sock = FakeSocket(body) |
---|
591 | n/a | resp = client.HTTPResponse(sock) |
---|
592 | n/a | resp.begin() |
---|
593 | n/a | self.assertEqual(resp.read(2), b'Te') |
---|
594 | n/a | self.assertFalse(resp.isclosed()) |
---|
595 | n/a | self.assertEqual(resp.read(2), b'xt') |
---|
596 | n/a | self.assertEqual(resp.read(1), b'') |
---|
597 | n/a | self.assertTrue(resp.isclosed()) |
---|
598 | n/a | |
---|
599 | n/a | def test_partial_readintos_incomplete_body(self): |
---|
600 | n/a | # if the server shuts down the connection before the whole |
---|
601 | n/a | # content-length is delivered, the socket is gracefully closed |
---|
602 | n/a | body = "HTTP/1.1 200 Ok\r\nContent-Length: 10\r\n\r\nText" |
---|
603 | n/a | sock = FakeSocket(body) |
---|
604 | n/a | resp = client.HTTPResponse(sock) |
---|
605 | n/a | resp.begin() |
---|
606 | n/a | b = bytearray(2) |
---|
607 | n/a | n = resp.readinto(b) |
---|
608 | n/a | self.assertEqual(n, 2) |
---|
609 | n/a | self.assertEqual(bytes(b), b'Te') |
---|
610 | n/a | self.assertFalse(resp.isclosed()) |
---|
611 | n/a | n = resp.readinto(b) |
---|
612 | n/a | self.assertEqual(n, 2) |
---|
613 | n/a | self.assertEqual(bytes(b), b'xt') |
---|
614 | n/a | n = resp.readinto(b) |
---|
615 | n/a | self.assertEqual(n, 0) |
---|
616 | n/a | self.assertTrue(resp.isclosed()) |
---|
617 | n/a | self.assertFalse(resp.closed) |
---|
618 | n/a | resp.close() |
---|
619 | n/a | self.assertTrue(resp.closed) |
---|
620 | n/a | |
---|
621 | n/a | def test_host_port(self): |
---|
622 | n/a | # Check invalid host_port |
---|
623 | n/a | |
---|
624 | n/a | for hp in ("www.python.org:abc", "user:password@www.python.org"): |
---|
625 | n/a | self.assertRaises(client.InvalidURL, client.HTTPConnection, hp) |
---|
626 | n/a | |
---|
627 | n/a | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", |
---|
628 | n/a | "fe80::207:e9ff:fe9b", 8000), |
---|
629 | n/a | ("www.python.org:80", "www.python.org", 80), |
---|
630 | n/a | ("www.python.org:", "www.python.org", 80), |
---|
631 | n/a | ("www.python.org", "www.python.org", 80), |
---|
632 | n/a | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80), |
---|
633 | n/a | ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", 80)): |
---|
634 | n/a | c = client.HTTPConnection(hp) |
---|
635 | n/a | self.assertEqual(h, c.host) |
---|
636 | n/a | self.assertEqual(p, c.port) |
---|
637 | n/a | |
---|
638 | n/a | def test_response_headers(self): |
---|
639 | n/a | # test response with multiple message headers with the same field name. |
---|
640 | n/a | text = ('HTTP/1.1 200 OK\r\n' |
---|
641 | n/a | 'Set-Cookie: Customer="WILE_E_COYOTE"; ' |
---|
642 | n/a | 'Version="1"; Path="/acme"\r\n' |
---|
643 | n/a | 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";' |
---|
644 | n/a | ' Path="/acme"\r\n' |
---|
645 | n/a | '\r\n' |
---|
646 | n/a | 'No body\r\n') |
---|
647 | n/a | hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"' |
---|
648 | n/a | ', ' |
---|
649 | n/a | 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"') |
---|
650 | n/a | s = FakeSocket(text) |
---|
651 | n/a | r = client.HTTPResponse(s) |
---|
652 | n/a | r.begin() |
---|
653 | n/a | cookies = r.getheader("Set-Cookie") |
---|
654 | n/a | self.assertEqual(cookies, hdr) |
---|
655 | n/a | |
---|
656 | n/a | def test_read_head(self): |
---|
657 | n/a | # Test that the library doesn't attempt to read any data |
---|
658 | n/a | # from a HEAD request. (Tickles SF bug #622042.) |
---|
659 | n/a | sock = FakeSocket( |
---|
660 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
661 | n/a | 'Content-Length: 14432\r\n' |
---|
662 | n/a | '\r\n', |
---|
663 | n/a | NoEOFBytesIO) |
---|
664 | n/a | resp = client.HTTPResponse(sock, method="HEAD") |
---|
665 | n/a | resp.begin() |
---|
666 | n/a | if resp.read(): |
---|
667 | n/a | self.fail("Did not expect response from HEAD request") |
---|
668 | n/a | |
---|
669 | n/a | def test_readinto_head(self): |
---|
670 | n/a | # Test that the library doesn't attempt to read any data |
---|
671 | n/a | # from a HEAD request. (Tickles SF bug #622042.) |
---|
672 | n/a | sock = FakeSocket( |
---|
673 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
674 | n/a | 'Content-Length: 14432\r\n' |
---|
675 | n/a | '\r\n', |
---|
676 | n/a | NoEOFBytesIO) |
---|
677 | n/a | resp = client.HTTPResponse(sock, method="HEAD") |
---|
678 | n/a | resp.begin() |
---|
679 | n/a | b = bytearray(5) |
---|
680 | n/a | if resp.readinto(b) != 0: |
---|
681 | n/a | self.fail("Did not expect response from HEAD request") |
---|
682 | n/a | self.assertEqual(bytes(b), b'\x00'*5) |
---|
683 | n/a | |
---|
684 | n/a | def test_too_many_headers(self): |
---|
685 | n/a | headers = '\r\n'.join('Header%d: foo' % i |
---|
686 | n/a | for i in range(client._MAXHEADERS + 1)) + '\r\n' |
---|
687 | n/a | text = ('HTTP/1.1 200 OK\r\n' + headers) |
---|
688 | n/a | s = FakeSocket(text) |
---|
689 | n/a | r = client.HTTPResponse(s) |
---|
690 | n/a | self.assertRaisesRegex(client.HTTPException, |
---|
691 | n/a | r"got more than \d+ headers", r.begin) |
---|
692 | n/a | |
---|
693 | n/a | def test_send_file(self): |
---|
694 | n/a | expected = (b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' |
---|
695 | n/a | b'Accept-Encoding: identity\r\n' |
---|
696 | n/a | b'Transfer-Encoding: chunked\r\n' |
---|
697 | n/a | b'\r\n') |
---|
698 | n/a | |
---|
699 | n/a | with open(__file__, 'rb') as body: |
---|
700 | n/a | conn = client.HTTPConnection('example.com') |
---|
701 | n/a | sock = FakeSocket(body) |
---|
702 | n/a | conn.sock = sock |
---|
703 | n/a | conn.request('GET', '/foo', body) |
---|
704 | n/a | self.assertTrue(sock.data.startswith(expected), '%r != %r' % |
---|
705 | n/a | (sock.data[:len(expected)], expected)) |
---|
706 | n/a | |
---|
707 | n/a | def test_send(self): |
---|
708 | n/a | expected = b'this is a test this is only a test' |
---|
709 | n/a | conn = client.HTTPConnection('example.com') |
---|
710 | n/a | sock = FakeSocket(None) |
---|
711 | n/a | conn.sock = sock |
---|
712 | n/a | conn.send(expected) |
---|
713 | n/a | self.assertEqual(expected, sock.data) |
---|
714 | n/a | sock.data = b'' |
---|
715 | n/a | conn.send(array.array('b', expected)) |
---|
716 | n/a | self.assertEqual(expected, sock.data) |
---|
717 | n/a | sock.data = b'' |
---|
718 | n/a | conn.send(io.BytesIO(expected)) |
---|
719 | n/a | self.assertEqual(expected, sock.data) |
---|
720 | n/a | |
---|
721 | n/a | def test_send_updating_file(self): |
---|
722 | n/a | def data(): |
---|
723 | n/a | yield 'data' |
---|
724 | n/a | yield None |
---|
725 | n/a | yield 'data_two' |
---|
726 | n/a | |
---|
727 | n/a | class UpdatingFile(io.TextIOBase): |
---|
728 | n/a | mode = 'r' |
---|
729 | n/a | d = data() |
---|
730 | n/a | def read(self, blocksize=-1): |
---|
731 | n/a | return next(self.d) |
---|
732 | n/a | |
---|
733 | n/a | expected = b'data' |
---|
734 | n/a | |
---|
735 | n/a | conn = client.HTTPConnection('example.com') |
---|
736 | n/a | sock = FakeSocket("") |
---|
737 | n/a | conn.sock = sock |
---|
738 | n/a | conn.send(UpdatingFile()) |
---|
739 | n/a | self.assertEqual(sock.data, expected) |
---|
740 | n/a | |
---|
741 | n/a | |
---|
742 | n/a | def test_send_iter(self): |
---|
743 | n/a | expected = b'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \ |
---|
744 | n/a | b'Accept-Encoding: identity\r\nContent-Length: 11\r\n' \ |
---|
745 | n/a | b'\r\nonetwothree' |
---|
746 | n/a | |
---|
747 | n/a | def body(): |
---|
748 | n/a | yield b"one" |
---|
749 | n/a | yield b"two" |
---|
750 | n/a | yield b"three" |
---|
751 | n/a | |
---|
752 | n/a | conn = client.HTTPConnection('example.com') |
---|
753 | n/a | sock = FakeSocket("") |
---|
754 | n/a | conn.sock = sock |
---|
755 | n/a | conn.request('GET', '/foo', body(), {'Content-Length': '11'}) |
---|
756 | n/a | self.assertEqual(sock.data, expected) |
---|
757 | n/a | |
---|
758 | n/a | def test_send_type_error(self): |
---|
759 | n/a | # See: Issue #12676 |
---|
760 | n/a | conn = client.HTTPConnection('example.com') |
---|
761 | n/a | conn.sock = FakeSocket('') |
---|
762 | n/a | with self.assertRaises(TypeError): |
---|
763 | n/a | conn.request('POST', 'test', conn) |
---|
764 | n/a | |
---|
765 | n/a | def test_chunked(self): |
---|
766 | n/a | expected = chunked_expected |
---|
767 | n/a | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
---|
768 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
769 | n/a | resp.begin() |
---|
770 | n/a | self.assertEqual(resp.read(), expected) |
---|
771 | n/a | resp.close() |
---|
772 | n/a | |
---|
773 | n/a | # Various read sizes |
---|
774 | n/a | for n in range(1, 12): |
---|
775 | n/a | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
---|
776 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
777 | n/a | resp.begin() |
---|
778 | n/a | self.assertEqual(resp.read(n) + resp.read(n) + resp.read(), expected) |
---|
779 | n/a | resp.close() |
---|
780 | n/a | |
---|
781 | n/a | for x in ('', 'foo\r\n'): |
---|
782 | n/a | sock = FakeSocket(chunked_start + x) |
---|
783 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
784 | n/a | resp.begin() |
---|
785 | n/a | try: |
---|
786 | n/a | resp.read() |
---|
787 | n/a | except client.IncompleteRead as i: |
---|
788 | n/a | self.assertEqual(i.partial, expected) |
---|
789 | n/a | expected_message = 'IncompleteRead(%d bytes read)' % len(expected) |
---|
790 | n/a | self.assertEqual(repr(i), expected_message) |
---|
791 | n/a | self.assertEqual(str(i), expected_message) |
---|
792 | n/a | else: |
---|
793 | n/a | self.fail('IncompleteRead expected') |
---|
794 | n/a | finally: |
---|
795 | n/a | resp.close() |
---|
796 | n/a | |
---|
797 | n/a | def test_readinto_chunked(self): |
---|
798 | n/a | |
---|
799 | n/a | expected = chunked_expected |
---|
800 | n/a | nexpected = len(expected) |
---|
801 | n/a | b = bytearray(128) |
---|
802 | n/a | |
---|
803 | n/a | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
---|
804 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
805 | n/a | resp.begin() |
---|
806 | n/a | n = resp.readinto(b) |
---|
807 | n/a | self.assertEqual(b[:nexpected], expected) |
---|
808 | n/a | self.assertEqual(n, nexpected) |
---|
809 | n/a | resp.close() |
---|
810 | n/a | |
---|
811 | n/a | # Various read sizes |
---|
812 | n/a | for n in range(1, 12): |
---|
813 | n/a | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
---|
814 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
815 | n/a | resp.begin() |
---|
816 | n/a | m = memoryview(b) |
---|
817 | n/a | i = resp.readinto(m[0:n]) |
---|
818 | n/a | i += resp.readinto(m[i:n + i]) |
---|
819 | n/a | i += resp.readinto(m[i:]) |
---|
820 | n/a | self.assertEqual(b[:nexpected], expected) |
---|
821 | n/a | self.assertEqual(i, nexpected) |
---|
822 | n/a | resp.close() |
---|
823 | n/a | |
---|
824 | n/a | for x in ('', 'foo\r\n'): |
---|
825 | n/a | sock = FakeSocket(chunked_start + x) |
---|
826 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
827 | n/a | resp.begin() |
---|
828 | n/a | try: |
---|
829 | n/a | n = resp.readinto(b) |
---|
830 | n/a | except client.IncompleteRead as i: |
---|
831 | n/a | self.assertEqual(i.partial, expected) |
---|
832 | n/a | expected_message = 'IncompleteRead(%d bytes read)' % len(expected) |
---|
833 | n/a | self.assertEqual(repr(i), expected_message) |
---|
834 | n/a | self.assertEqual(str(i), expected_message) |
---|
835 | n/a | else: |
---|
836 | n/a | self.fail('IncompleteRead expected') |
---|
837 | n/a | finally: |
---|
838 | n/a | resp.close() |
---|
839 | n/a | |
---|
840 | n/a | def test_chunked_head(self): |
---|
841 | n/a | chunked_start = ( |
---|
842 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
843 | n/a | 'Transfer-Encoding: chunked\r\n\r\n' |
---|
844 | n/a | 'a\r\n' |
---|
845 | n/a | 'hello world\r\n' |
---|
846 | n/a | '1\r\n' |
---|
847 | n/a | 'd\r\n' |
---|
848 | n/a | ) |
---|
849 | n/a | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
---|
850 | n/a | resp = client.HTTPResponse(sock, method="HEAD") |
---|
851 | n/a | resp.begin() |
---|
852 | n/a | self.assertEqual(resp.read(), b'') |
---|
853 | n/a | self.assertEqual(resp.status, 200) |
---|
854 | n/a | self.assertEqual(resp.reason, 'OK') |
---|
855 | n/a | self.assertTrue(resp.isclosed()) |
---|
856 | n/a | self.assertFalse(resp.closed) |
---|
857 | n/a | resp.close() |
---|
858 | n/a | self.assertTrue(resp.closed) |
---|
859 | n/a | |
---|
860 | n/a | def test_readinto_chunked_head(self): |
---|
861 | n/a | chunked_start = ( |
---|
862 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
863 | n/a | 'Transfer-Encoding: chunked\r\n\r\n' |
---|
864 | n/a | 'a\r\n' |
---|
865 | n/a | 'hello world\r\n' |
---|
866 | n/a | '1\r\n' |
---|
867 | n/a | 'd\r\n' |
---|
868 | n/a | ) |
---|
869 | n/a | sock = FakeSocket(chunked_start + last_chunk + chunked_end) |
---|
870 | n/a | resp = client.HTTPResponse(sock, method="HEAD") |
---|
871 | n/a | resp.begin() |
---|
872 | n/a | b = bytearray(5) |
---|
873 | n/a | n = resp.readinto(b) |
---|
874 | n/a | self.assertEqual(n, 0) |
---|
875 | n/a | self.assertEqual(bytes(b), b'\x00'*5) |
---|
876 | n/a | self.assertEqual(resp.status, 200) |
---|
877 | n/a | self.assertEqual(resp.reason, 'OK') |
---|
878 | n/a | self.assertTrue(resp.isclosed()) |
---|
879 | n/a | self.assertFalse(resp.closed) |
---|
880 | n/a | resp.close() |
---|
881 | n/a | self.assertTrue(resp.closed) |
---|
882 | n/a | |
---|
883 | n/a | def test_negative_content_length(self): |
---|
884 | n/a | sock = FakeSocket( |
---|
885 | n/a | 'HTTP/1.1 200 OK\r\nContent-Length: -1\r\n\r\nHello\r\n') |
---|
886 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
887 | n/a | resp.begin() |
---|
888 | n/a | self.assertEqual(resp.read(), b'Hello\r\n') |
---|
889 | n/a | self.assertTrue(resp.isclosed()) |
---|
890 | n/a | |
---|
891 | n/a | def test_incomplete_read(self): |
---|
892 | n/a | sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n') |
---|
893 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
894 | n/a | resp.begin() |
---|
895 | n/a | try: |
---|
896 | n/a | resp.read() |
---|
897 | n/a | except client.IncompleteRead as i: |
---|
898 | n/a | self.assertEqual(i.partial, b'Hello\r\n') |
---|
899 | n/a | self.assertEqual(repr(i), |
---|
900 | n/a | "IncompleteRead(7 bytes read, 3 more expected)") |
---|
901 | n/a | self.assertEqual(str(i), |
---|
902 | n/a | "IncompleteRead(7 bytes read, 3 more expected)") |
---|
903 | n/a | self.assertTrue(resp.isclosed()) |
---|
904 | n/a | else: |
---|
905 | n/a | self.fail('IncompleteRead expected') |
---|
906 | n/a | |
---|
907 | n/a | def test_epipe(self): |
---|
908 | n/a | sock = EPipeSocket( |
---|
909 | n/a | "HTTP/1.0 401 Authorization Required\r\n" |
---|
910 | n/a | "Content-type: text/html\r\n" |
---|
911 | n/a | "WWW-Authenticate: Basic realm=\"example\"\r\n", |
---|
912 | n/a | b"Content-Length") |
---|
913 | n/a | conn = client.HTTPConnection("example.com") |
---|
914 | n/a | conn.sock = sock |
---|
915 | n/a | self.assertRaises(OSError, |
---|
916 | n/a | lambda: conn.request("PUT", "/url", "body")) |
---|
917 | n/a | resp = conn.getresponse() |
---|
918 | n/a | self.assertEqual(401, resp.status) |
---|
919 | n/a | self.assertEqual("Basic realm=\"example\"", |
---|
920 | n/a | resp.getheader("www-authenticate")) |
---|
921 | n/a | |
---|
922 | n/a | # Test lines overflowing the max line size (_MAXLINE in http.client) |
---|
923 | n/a | |
---|
924 | n/a | def test_overflowing_status_line(self): |
---|
925 | n/a | body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n" |
---|
926 | n/a | resp = client.HTTPResponse(FakeSocket(body)) |
---|
927 | n/a | self.assertRaises((client.LineTooLong, client.BadStatusLine), resp.begin) |
---|
928 | n/a | |
---|
929 | n/a | def test_overflowing_header_line(self): |
---|
930 | n/a | body = ( |
---|
931 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
932 | n/a | 'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n' |
---|
933 | n/a | ) |
---|
934 | n/a | resp = client.HTTPResponse(FakeSocket(body)) |
---|
935 | n/a | self.assertRaises(client.LineTooLong, resp.begin) |
---|
936 | n/a | |
---|
937 | n/a | def test_overflowing_chunked_line(self): |
---|
938 | n/a | body = ( |
---|
939 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
940 | n/a | 'Transfer-Encoding: chunked\r\n\r\n' |
---|
941 | n/a | + '0' * 65536 + 'a\r\n' |
---|
942 | n/a | 'hello world\r\n' |
---|
943 | n/a | '0\r\n' |
---|
944 | n/a | '\r\n' |
---|
945 | n/a | ) |
---|
946 | n/a | resp = client.HTTPResponse(FakeSocket(body)) |
---|
947 | n/a | resp.begin() |
---|
948 | n/a | self.assertRaises(client.LineTooLong, resp.read) |
---|
949 | n/a | |
---|
950 | n/a | def test_early_eof(self): |
---|
951 | n/a | # Test httpresponse with no \r\n termination, |
---|
952 | n/a | body = "HTTP/1.1 200 Ok" |
---|
953 | n/a | sock = FakeSocket(body) |
---|
954 | n/a | resp = client.HTTPResponse(sock) |
---|
955 | n/a | resp.begin() |
---|
956 | n/a | self.assertEqual(resp.read(), b'') |
---|
957 | n/a | self.assertTrue(resp.isclosed()) |
---|
958 | n/a | self.assertFalse(resp.closed) |
---|
959 | n/a | resp.close() |
---|
960 | n/a | self.assertTrue(resp.closed) |
---|
961 | n/a | |
---|
962 | n/a | def test_error_leak(self): |
---|
963 | n/a | # Test that the socket is not leaked if getresponse() fails |
---|
964 | n/a | conn = client.HTTPConnection('example.com') |
---|
965 | n/a | response = None |
---|
966 | n/a | class Response(client.HTTPResponse): |
---|
967 | n/a | def __init__(self, *pos, **kw): |
---|
968 | n/a | nonlocal response |
---|
969 | n/a | response = self # Avoid garbage collector closing the socket |
---|
970 | n/a | client.HTTPResponse.__init__(self, *pos, **kw) |
---|
971 | n/a | conn.response_class = Response |
---|
972 | n/a | conn.sock = FakeSocket('Invalid status line') |
---|
973 | n/a | conn.request('GET', '/') |
---|
974 | n/a | self.assertRaises(client.BadStatusLine, conn.getresponse) |
---|
975 | n/a | self.assertTrue(response.closed) |
---|
976 | n/a | self.assertTrue(conn.sock.file_closed) |
---|
977 | n/a | |
---|
978 | n/a | def test_chunked_extension(self): |
---|
979 | n/a | extra = '3;foo=bar\r\n' + 'abc\r\n' |
---|
980 | n/a | expected = chunked_expected + b'abc' |
---|
981 | n/a | |
---|
982 | n/a | sock = FakeSocket(chunked_start + extra + last_chunk_extended + chunked_end) |
---|
983 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
984 | n/a | resp.begin() |
---|
985 | n/a | self.assertEqual(resp.read(), expected) |
---|
986 | n/a | resp.close() |
---|
987 | n/a | |
---|
988 | n/a | def test_chunked_missing_end(self): |
---|
989 | n/a | """some servers may serve up a short chunked encoding stream""" |
---|
990 | n/a | expected = chunked_expected |
---|
991 | n/a | sock = FakeSocket(chunked_start + last_chunk) #no terminating crlf |
---|
992 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
993 | n/a | resp.begin() |
---|
994 | n/a | self.assertEqual(resp.read(), expected) |
---|
995 | n/a | resp.close() |
---|
996 | n/a | |
---|
997 | n/a | def test_chunked_trailers(self): |
---|
998 | n/a | """See that trailers are read and ignored""" |
---|
999 | n/a | expected = chunked_expected |
---|
1000 | n/a | sock = FakeSocket(chunked_start + last_chunk + trailers + chunked_end) |
---|
1001 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
1002 | n/a | resp.begin() |
---|
1003 | n/a | self.assertEqual(resp.read(), expected) |
---|
1004 | n/a | # we should have reached the end of the file |
---|
1005 | n/a | self.assertEqual(sock.file.read(), b"") #we read to the end |
---|
1006 | n/a | resp.close() |
---|
1007 | n/a | |
---|
1008 | n/a | def test_chunked_sync(self): |
---|
1009 | n/a | """Check that we don't read past the end of the chunked-encoding stream""" |
---|
1010 | n/a | expected = chunked_expected |
---|
1011 | n/a | extradata = "extradata" |
---|
1012 | n/a | sock = FakeSocket(chunked_start + last_chunk + trailers + chunked_end + extradata) |
---|
1013 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
1014 | n/a | resp.begin() |
---|
1015 | n/a | self.assertEqual(resp.read(), expected) |
---|
1016 | n/a | # the file should now have our extradata ready to be read |
---|
1017 | n/a | self.assertEqual(sock.file.read(), extradata.encode("ascii")) #we read to the end |
---|
1018 | n/a | resp.close() |
---|
1019 | n/a | |
---|
1020 | n/a | def test_content_length_sync(self): |
---|
1021 | n/a | """Check that we don't read past the end of the Content-Length stream""" |
---|
1022 | n/a | extradata = b"extradata" |
---|
1023 | n/a | expected = b"Hello123\r\n" |
---|
1024 | n/a | sock = FakeSocket(b'HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n' + expected + extradata) |
---|
1025 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
1026 | n/a | resp.begin() |
---|
1027 | n/a | self.assertEqual(resp.read(), expected) |
---|
1028 | n/a | # the file should now have our extradata ready to be read |
---|
1029 | n/a | self.assertEqual(sock.file.read(), extradata) #we read to the end |
---|
1030 | n/a | resp.close() |
---|
1031 | n/a | |
---|
1032 | n/a | def test_readlines_content_length(self): |
---|
1033 | n/a | extradata = b"extradata" |
---|
1034 | n/a | expected = b"Hello123\r\n" |
---|
1035 | n/a | sock = FakeSocket(b'HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n' + expected + extradata) |
---|
1036 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
1037 | n/a | resp.begin() |
---|
1038 | n/a | self.assertEqual(resp.readlines(2000), [expected]) |
---|
1039 | n/a | # the file should now have our extradata ready to be read |
---|
1040 | n/a | self.assertEqual(sock.file.read(), extradata) #we read to the end |
---|
1041 | n/a | resp.close() |
---|
1042 | n/a | |
---|
1043 | n/a | def test_read1_content_length(self): |
---|
1044 | n/a | extradata = b"extradata" |
---|
1045 | n/a | expected = b"Hello123\r\n" |
---|
1046 | n/a | sock = FakeSocket(b'HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n' + expected + extradata) |
---|
1047 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
1048 | n/a | resp.begin() |
---|
1049 | n/a | self.assertEqual(resp.read1(2000), expected) |
---|
1050 | n/a | # the file should now have our extradata ready to be read |
---|
1051 | n/a | self.assertEqual(sock.file.read(), extradata) #we read to the end |
---|
1052 | n/a | resp.close() |
---|
1053 | n/a | |
---|
1054 | n/a | def test_readline_bound_content_length(self): |
---|
1055 | n/a | extradata = b"extradata" |
---|
1056 | n/a | expected = b"Hello123\r\n" |
---|
1057 | n/a | sock = FakeSocket(b'HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\n' + expected + extradata) |
---|
1058 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
1059 | n/a | resp.begin() |
---|
1060 | n/a | self.assertEqual(resp.readline(10), expected) |
---|
1061 | n/a | self.assertEqual(resp.readline(10), b"") |
---|
1062 | n/a | # the file should now have our extradata ready to be read |
---|
1063 | n/a | self.assertEqual(sock.file.read(), extradata) #we read to the end |
---|
1064 | n/a | resp.close() |
---|
1065 | n/a | |
---|
1066 | n/a | def test_read1_bound_content_length(self): |
---|
1067 | n/a | extradata = b"extradata" |
---|
1068 | n/a | expected = b"Hello123\r\n" |
---|
1069 | n/a | sock = FakeSocket(b'HTTP/1.1 200 OK\r\nContent-Length: 30\r\n\r\n' + expected*3 + extradata) |
---|
1070 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
1071 | n/a | resp.begin() |
---|
1072 | n/a | self.assertEqual(resp.read1(20), expected*2) |
---|
1073 | n/a | self.assertEqual(resp.read(), expected) |
---|
1074 | n/a | # the file should now have our extradata ready to be read |
---|
1075 | n/a | self.assertEqual(sock.file.read(), extradata) #we read to the end |
---|
1076 | n/a | resp.close() |
---|
1077 | n/a | |
---|
1078 | n/a | def test_response_fileno(self): |
---|
1079 | n/a | # Make sure fd returned by fileno is valid. |
---|
1080 | n/a | threading = support.import_module("threading") |
---|
1081 | n/a | |
---|
1082 | n/a | serv = socket.socket( |
---|
1083 | n/a | socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP) |
---|
1084 | n/a | self.addCleanup(serv.close) |
---|
1085 | n/a | serv.bind((HOST, 0)) |
---|
1086 | n/a | serv.listen() |
---|
1087 | n/a | |
---|
1088 | n/a | result = None |
---|
1089 | n/a | def run_server(): |
---|
1090 | n/a | [conn, address] = serv.accept() |
---|
1091 | n/a | with conn, conn.makefile("rb") as reader: |
---|
1092 | n/a | # Read the request header until a blank line |
---|
1093 | n/a | while True: |
---|
1094 | n/a | line = reader.readline() |
---|
1095 | n/a | if not line.rstrip(b"\r\n"): |
---|
1096 | n/a | break |
---|
1097 | n/a | conn.sendall(b"HTTP/1.1 200 Connection established\r\n\r\n") |
---|
1098 | n/a | nonlocal result |
---|
1099 | n/a | result = reader.read() |
---|
1100 | n/a | |
---|
1101 | n/a | thread = threading.Thread(target=run_server) |
---|
1102 | n/a | thread.start() |
---|
1103 | n/a | self.addCleanup(thread.join, float(1)) |
---|
1104 | n/a | conn = client.HTTPConnection(*serv.getsockname()) |
---|
1105 | n/a | conn.request("CONNECT", "dummy:1234") |
---|
1106 | n/a | response = conn.getresponse() |
---|
1107 | n/a | try: |
---|
1108 | n/a | self.assertEqual(response.status, client.OK) |
---|
1109 | n/a | s = socket.socket(fileno=response.fileno()) |
---|
1110 | n/a | try: |
---|
1111 | n/a | s.sendall(b"proxied data\n") |
---|
1112 | n/a | finally: |
---|
1113 | n/a | s.detach() |
---|
1114 | n/a | finally: |
---|
1115 | n/a | response.close() |
---|
1116 | n/a | conn.close() |
---|
1117 | n/a | thread.join() |
---|
1118 | n/a | self.assertEqual(result, b"proxied data\n") |
---|
1119 | n/a | |
---|
1120 | n/a | class ExtendedReadTest(TestCase): |
---|
1121 | n/a | """ |
---|
1122 | n/a | Test peek(), read1(), readline() |
---|
1123 | n/a | """ |
---|
1124 | n/a | lines = ( |
---|
1125 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
1126 | n/a | '\r\n' |
---|
1127 | n/a | 'hello world!\n' |
---|
1128 | n/a | 'and now \n' |
---|
1129 | n/a | 'for something completely different\n' |
---|
1130 | n/a | 'foo' |
---|
1131 | n/a | ) |
---|
1132 | n/a | lines_expected = lines[lines.find('hello'):].encode("ascii") |
---|
1133 | n/a | lines_chunked = ( |
---|
1134 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
1135 | n/a | 'Transfer-Encoding: chunked\r\n\r\n' |
---|
1136 | n/a | 'a\r\n' |
---|
1137 | n/a | 'hello worl\r\n' |
---|
1138 | n/a | '3\r\n' |
---|
1139 | n/a | 'd!\n\r\n' |
---|
1140 | n/a | '9\r\n' |
---|
1141 | n/a | 'and now \n\r\n' |
---|
1142 | n/a | '23\r\n' |
---|
1143 | n/a | 'for something completely different\n\r\n' |
---|
1144 | n/a | '3\r\n' |
---|
1145 | n/a | 'foo\r\n' |
---|
1146 | n/a | '0\r\n' # terminating chunk |
---|
1147 | n/a | '\r\n' # end of trailers |
---|
1148 | n/a | ) |
---|
1149 | n/a | |
---|
1150 | n/a | def setUp(self): |
---|
1151 | n/a | sock = FakeSocket(self.lines) |
---|
1152 | n/a | resp = client.HTTPResponse(sock, method="GET") |
---|
1153 | n/a | resp.begin() |
---|
1154 | n/a | resp.fp = io.BufferedReader(resp.fp) |
---|
1155 | n/a | self.resp = resp |
---|
1156 | n/a | |
---|
1157 | n/a | |
---|
1158 | n/a | |
---|
1159 | n/a | def test_peek(self): |
---|
1160 | n/a | resp = self.resp |
---|
1161 | n/a | # patch up the buffered peek so that it returns not too much stuff |
---|
1162 | n/a | oldpeek = resp.fp.peek |
---|
1163 | n/a | def mypeek(n=-1): |
---|
1164 | n/a | p = oldpeek(n) |
---|
1165 | n/a | if n >= 0: |
---|
1166 | n/a | return p[:n] |
---|
1167 | n/a | return p[:10] |
---|
1168 | n/a | resp.fp.peek = mypeek |
---|
1169 | n/a | |
---|
1170 | n/a | all = [] |
---|
1171 | n/a | while True: |
---|
1172 | n/a | # try a short peek |
---|
1173 | n/a | p = resp.peek(3) |
---|
1174 | n/a | if p: |
---|
1175 | n/a | self.assertGreater(len(p), 0) |
---|
1176 | n/a | # then unbounded peek |
---|
1177 | n/a | p2 = resp.peek() |
---|
1178 | n/a | self.assertGreaterEqual(len(p2), len(p)) |
---|
1179 | n/a | self.assertTrue(p2.startswith(p)) |
---|
1180 | n/a | next = resp.read(len(p2)) |
---|
1181 | n/a | self.assertEqual(next, p2) |
---|
1182 | n/a | else: |
---|
1183 | n/a | next = resp.read() |
---|
1184 | n/a | self.assertFalse(next) |
---|
1185 | n/a | all.append(next) |
---|
1186 | n/a | if not next: |
---|
1187 | n/a | break |
---|
1188 | n/a | self.assertEqual(b"".join(all), self.lines_expected) |
---|
1189 | n/a | |
---|
1190 | n/a | def test_readline(self): |
---|
1191 | n/a | resp = self.resp |
---|
1192 | n/a | self._verify_readline(self.resp.readline, self.lines_expected) |
---|
1193 | n/a | |
---|
1194 | n/a | def _verify_readline(self, readline, expected): |
---|
1195 | n/a | all = [] |
---|
1196 | n/a | while True: |
---|
1197 | n/a | # short readlines |
---|
1198 | n/a | line = readline(5) |
---|
1199 | n/a | if line and line != b"foo": |
---|
1200 | n/a | if len(line) < 5: |
---|
1201 | n/a | self.assertTrue(line.endswith(b"\n")) |
---|
1202 | n/a | all.append(line) |
---|
1203 | n/a | if not line: |
---|
1204 | n/a | break |
---|
1205 | n/a | self.assertEqual(b"".join(all), expected) |
---|
1206 | n/a | |
---|
1207 | n/a | def test_read1(self): |
---|
1208 | n/a | resp = self.resp |
---|
1209 | n/a | def r(): |
---|
1210 | n/a | res = resp.read1(4) |
---|
1211 | n/a | self.assertLessEqual(len(res), 4) |
---|
1212 | n/a | return res |
---|
1213 | n/a | readliner = Readliner(r) |
---|
1214 | n/a | self._verify_readline(readliner.readline, self.lines_expected) |
---|
1215 | n/a | |
---|
1216 | n/a | def test_read1_unbounded(self): |
---|
1217 | n/a | resp = self.resp |
---|
1218 | n/a | all = [] |
---|
1219 | n/a | while True: |
---|
1220 | n/a | data = resp.read1() |
---|
1221 | n/a | if not data: |
---|
1222 | n/a | break |
---|
1223 | n/a | all.append(data) |
---|
1224 | n/a | self.assertEqual(b"".join(all), self.lines_expected) |
---|
1225 | n/a | |
---|
1226 | n/a | def test_read1_bounded(self): |
---|
1227 | n/a | resp = self.resp |
---|
1228 | n/a | all = [] |
---|
1229 | n/a | while True: |
---|
1230 | n/a | data = resp.read1(10) |
---|
1231 | n/a | if not data: |
---|
1232 | n/a | break |
---|
1233 | n/a | self.assertLessEqual(len(data), 10) |
---|
1234 | n/a | all.append(data) |
---|
1235 | n/a | self.assertEqual(b"".join(all), self.lines_expected) |
---|
1236 | n/a | |
---|
1237 | n/a | def test_read1_0(self): |
---|
1238 | n/a | self.assertEqual(self.resp.read1(0), b"") |
---|
1239 | n/a | |
---|
1240 | n/a | def test_peek_0(self): |
---|
1241 | n/a | p = self.resp.peek(0) |
---|
1242 | n/a | self.assertLessEqual(0, len(p)) |
---|
1243 | n/a | |
---|
1244 | n/a | class ExtendedReadTestChunked(ExtendedReadTest): |
---|
1245 | n/a | """ |
---|
1246 | n/a | Test peek(), read1(), readline() in chunked mode |
---|
1247 | n/a | """ |
---|
1248 | n/a | lines = ( |
---|
1249 | n/a | 'HTTP/1.1 200 OK\r\n' |
---|
1250 | n/a | 'Transfer-Encoding: chunked\r\n\r\n' |
---|
1251 | n/a | 'a\r\n' |
---|
1252 | n/a | 'hello worl\r\n' |
---|
1253 | n/a | '3\r\n' |
---|
1254 | n/a | 'd!\n\r\n' |
---|
1255 | n/a | '9\r\n' |
---|
1256 | n/a | 'and now \n\r\n' |
---|
1257 | n/a | '23\r\n' |
---|
1258 | n/a | 'for something completely different\n\r\n' |
---|
1259 | n/a | '3\r\n' |
---|
1260 | n/a | 'foo\r\n' |
---|
1261 | n/a | '0\r\n' # terminating chunk |
---|
1262 | n/a | '\r\n' # end of trailers |
---|
1263 | n/a | ) |
---|
1264 | n/a | |
---|
1265 | n/a | |
---|
1266 | n/a | class Readliner: |
---|
1267 | n/a | """ |
---|
1268 | n/a | a simple readline class that uses an arbitrary read function and buffering |
---|
1269 | n/a | """ |
---|
1270 | n/a | def __init__(self, readfunc): |
---|
1271 | n/a | self.readfunc = readfunc |
---|
1272 | n/a | self.remainder = b"" |
---|
1273 | n/a | |
---|
1274 | n/a | def readline(self, limit): |
---|
1275 | n/a | data = [] |
---|
1276 | n/a | datalen = 0 |
---|
1277 | n/a | read = self.remainder |
---|
1278 | n/a | try: |
---|
1279 | n/a | while True: |
---|
1280 | n/a | idx = read.find(b'\n') |
---|
1281 | n/a | if idx != -1: |
---|
1282 | n/a | break |
---|
1283 | n/a | if datalen + len(read) >= limit: |
---|
1284 | n/a | idx = limit - datalen - 1 |
---|
1285 | n/a | # read more data |
---|
1286 | n/a | data.append(read) |
---|
1287 | n/a | read = self.readfunc() |
---|
1288 | n/a | if not read: |
---|
1289 | n/a | idx = 0 #eof condition |
---|
1290 | n/a | break |
---|
1291 | n/a | idx += 1 |
---|
1292 | n/a | data.append(read[:idx]) |
---|
1293 | n/a | self.remainder = read[idx:] |
---|
1294 | n/a | return b"".join(data) |
---|
1295 | n/a | except: |
---|
1296 | n/a | self.remainder = b"".join(data) |
---|
1297 | n/a | raise |
---|
1298 | n/a | |
---|
1299 | n/a | |
---|
1300 | n/a | class OfflineTest(TestCase): |
---|
1301 | n/a | def test_all(self): |
---|
1302 | n/a | # Documented objects defined in the module should be in __all__ |
---|
1303 | n/a | expected = {"responses"} # White-list documented dict() object |
---|
1304 | n/a | # HTTPMessage, parse_headers(), and the HTTP status code constants are |
---|
1305 | n/a | # intentionally omitted for simplicity |
---|
1306 | n/a | blacklist = {"HTTPMessage", "parse_headers"} |
---|
1307 | n/a | for name in dir(client): |
---|
1308 | n/a | if name.startswith("_") or name in blacklist: |
---|
1309 | n/a | continue |
---|
1310 | n/a | module_object = getattr(client, name) |
---|
1311 | n/a | if getattr(module_object, "__module__", None) == "http.client": |
---|
1312 | n/a | expected.add(name) |
---|
1313 | n/a | self.assertCountEqual(client.__all__, expected) |
---|
1314 | n/a | |
---|
1315 | n/a | def test_responses(self): |
---|
1316 | n/a | self.assertEqual(client.responses[client.NOT_FOUND], "Not Found") |
---|
1317 | n/a | |
---|
1318 | n/a | def test_client_constants(self): |
---|
1319 | n/a | # Make sure we don't break backward compatibility with 3.4 |
---|
1320 | n/a | expected = [ |
---|
1321 | n/a | 'CONTINUE', |
---|
1322 | n/a | 'SWITCHING_PROTOCOLS', |
---|
1323 | n/a | 'PROCESSING', |
---|
1324 | n/a | 'OK', |
---|
1325 | n/a | 'CREATED', |
---|
1326 | n/a | 'ACCEPTED', |
---|
1327 | n/a | 'NON_AUTHORITATIVE_INFORMATION', |
---|
1328 | n/a | 'NO_CONTENT', |
---|
1329 | n/a | 'RESET_CONTENT', |
---|
1330 | n/a | 'PARTIAL_CONTENT', |
---|
1331 | n/a | 'MULTI_STATUS', |
---|
1332 | n/a | 'IM_USED', |
---|
1333 | n/a | 'MULTIPLE_CHOICES', |
---|
1334 | n/a | 'MOVED_PERMANENTLY', |
---|
1335 | n/a | 'FOUND', |
---|
1336 | n/a | 'SEE_OTHER', |
---|
1337 | n/a | 'NOT_MODIFIED', |
---|
1338 | n/a | 'USE_PROXY', |
---|
1339 | n/a | 'TEMPORARY_REDIRECT', |
---|
1340 | n/a | 'BAD_REQUEST', |
---|
1341 | n/a | 'UNAUTHORIZED', |
---|
1342 | n/a | 'PAYMENT_REQUIRED', |
---|
1343 | n/a | 'FORBIDDEN', |
---|
1344 | n/a | 'NOT_FOUND', |
---|
1345 | n/a | 'METHOD_NOT_ALLOWED', |
---|
1346 | n/a | 'NOT_ACCEPTABLE', |
---|
1347 | n/a | 'PROXY_AUTHENTICATION_REQUIRED', |
---|
1348 | n/a | 'REQUEST_TIMEOUT', |
---|
1349 | n/a | 'CONFLICT', |
---|
1350 | n/a | 'GONE', |
---|
1351 | n/a | 'LENGTH_REQUIRED', |
---|
1352 | n/a | 'PRECONDITION_FAILED', |
---|
1353 | n/a | 'REQUEST_ENTITY_TOO_LARGE', |
---|
1354 | n/a | 'REQUEST_URI_TOO_LONG', |
---|
1355 | n/a | 'UNSUPPORTED_MEDIA_TYPE', |
---|
1356 | n/a | 'REQUESTED_RANGE_NOT_SATISFIABLE', |
---|
1357 | n/a | 'EXPECTATION_FAILED', |
---|
1358 | n/a | 'UNPROCESSABLE_ENTITY', |
---|
1359 | n/a | 'LOCKED', |
---|
1360 | n/a | 'FAILED_DEPENDENCY', |
---|
1361 | n/a | 'UPGRADE_REQUIRED', |
---|
1362 | n/a | 'PRECONDITION_REQUIRED', |
---|
1363 | n/a | 'TOO_MANY_REQUESTS', |
---|
1364 | n/a | 'REQUEST_HEADER_FIELDS_TOO_LARGE', |
---|
1365 | n/a | 'INTERNAL_SERVER_ERROR', |
---|
1366 | n/a | 'NOT_IMPLEMENTED', |
---|
1367 | n/a | 'BAD_GATEWAY', |
---|
1368 | n/a | 'SERVICE_UNAVAILABLE', |
---|
1369 | n/a | 'GATEWAY_TIMEOUT', |
---|
1370 | n/a | 'HTTP_VERSION_NOT_SUPPORTED', |
---|
1371 | n/a | 'INSUFFICIENT_STORAGE', |
---|
1372 | n/a | 'NOT_EXTENDED', |
---|
1373 | n/a | 'NETWORK_AUTHENTICATION_REQUIRED', |
---|
1374 | n/a | ] |
---|
1375 | n/a | for const in expected: |
---|
1376 | n/a | with self.subTest(constant=const): |
---|
1377 | n/a | self.assertTrue(hasattr(client, const)) |
---|
1378 | n/a | |
---|
1379 | n/a | |
---|
1380 | n/a | class SourceAddressTest(TestCase): |
---|
1381 | n/a | def setUp(self): |
---|
1382 | n/a | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
1383 | n/a | self.port = support.bind_port(self.serv) |
---|
1384 | n/a | self.source_port = support.find_unused_port() |
---|
1385 | n/a | self.serv.listen() |
---|
1386 | n/a | self.conn = None |
---|
1387 | n/a | |
---|
1388 | n/a | def tearDown(self): |
---|
1389 | n/a | if self.conn: |
---|
1390 | n/a | self.conn.close() |
---|
1391 | n/a | self.conn = None |
---|
1392 | n/a | self.serv.close() |
---|
1393 | n/a | self.serv = None |
---|
1394 | n/a | |
---|
1395 | n/a | def testHTTPConnectionSourceAddress(self): |
---|
1396 | n/a | self.conn = client.HTTPConnection(HOST, self.port, |
---|
1397 | n/a | source_address=('', self.source_port)) |
---|
1398 | n/a | self.conn.connect() |
---|
1399 | n/a | self.assertEqual(self.conn.sock.getsockname()[1], self.source_port) |
---|
1400 | n/a | |
---|
1401 | n/a | @unittest.skipIf(not hasattr(client, 'HTTPSConnection'), |
---|
1402 | n/a | 'http.client.HTTPSConnection not defined') |
---|
1403 | n/a | def testHTTPSConnectionSourceAddress(self): |
---|
1404 | n/a | self.conn = client.HTTPSConnection(HOST, self.port, |
---|
1405 | n/a | source_address=('', self.source_port)) |
---|
1406 | n/a | # We don't test anything here other than the constructor not barfing as |
---|
1407 | n/a | # this code doesn't deal with setting up an active running SSL server |
---|
1408 | n/a | # for an ssl_wrapped connect() to actually return from. |
---|
1409 | n/a | |
---|
1410 | n/a | |
---|
1411 | n/a | class TimeoutTest(TestCase): |
---|
1412 | n/a | PORT = None |
---|
1413 | n/a | |
---|
1414 | n/a | def setUp(self): |
---|
1415 | n/a | self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
---|
1416 | n/a | TimeoutTest.PORT = support.bind_port(self.serv) |
---|
1417 | n/a | self.serv.listen() |
---|
1418 | n/a | |
---|
1419 | n/a | def tearDown(self): |
---|
1420 | n/a | self.serv.close() |
---|
1421 | n/a | self.serv = None |
---|
1422 | n/a | |
---|
1423 | n/a | def testTimeoutAttribute(self): |
---|
1424 | n/a | # This will prove that the timeout gets through HTTPConnection |
---|
1425 | n/a | # and into the socket. |
---|
1426 | n/a | |
---|
1427 | n/a | # default -- use global socket timeout |
---|
1428 | n/a | self.assertIsNone(socket.getdefaulttimeout()) |
---|
1429 | n/a | socket.setdefaulttimeout(30) |
---|
1430 | n/a | try: |
---|
1431 | n/a | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT) |
---|
1432 | n/a | httpConn.connect() |
---|
1433 | n/a | finally: |
---|
1434 | n/a | socket.setdefaulttimeout(None) |
---|
1435 | n/a | self.assertEqual(httpConn.sock.gettimeout(), 30) |
---|
1436 | n/a | httpConn.close() |
---|
1437 | n/a | |
---|
1438 | n/a | # no timeout -- do not use global socket default |
---|
1439 | n/a | self.assertIsNone(socket.getdefaulttimeout()) |
---|
1440 | n/a | socket.setdefaulttimeout(30) |
---|
1441 | n/a | try: |
---|
1442 | n/a | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT, |
---|
1443 | n/a | timeout=None) |
---|
1444 | n/a | httpConn.connect() |
---|
1445 | n/a | finally: |
---|
1446 | n/a | socket.setdefaulttimeout(None) |
---|
1447 | n/a | self.assertEqual(httpConn.sock.gettimeout(), None) |
---|
1448 | n/a | httpConn.close() |
---|
1449 | n/a | |
---|
1450 | n/a | # a value |
---|
1451 | n/a | httpConn = client.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30) |
---|
1452 | n/a | httpConn.connect() |
---|
1453 | n/a | self.assertEqual(httpConn.sock.gettimeout(), 30) |
---|
1454 | n/a | httpConn.close() |
---|
1455 | n/a | |
---|
1456 | n/a | |
---|
1457 | n/a | class PersistenceTest(TestCase): |
---|
1458 | n/a | |
---|
1459 | n/a | def test_reuse_reconnect(self): |
---|
1460 | n/a | # Should reuse or reconnect depending on header from server |
---|
1461 | n/a | tests = ( |
---|
1462 | n/a | ('1.0', '', False), |
---|
1463 | n/a | ('1.0', 'Connection: keep-alive\r\n', True), |
---|
1464 | n/a | ('1.1', '', True), |
---|
1465 | n/a | ('1.1', 'Connection: close\r\n', False), |
---|
1466 | n/a | ('1.0', 'Connection: keep-ALIVE\r\n', True), |
---|
1467 | n/a | ('1.1', 'Connection: cloSE\r\n', False), |
---|
1468 | n/a | ) |
---|
1469 | n/a | for version, header, reuse in tests: |
---|
1470 | n/a | with self.subTest(version=version, header=header): |
---|
1471 | n/a | msg = ( |
---|
1472 | n/a | 'HTTP/{} 200 OK\r\n' |
---|
1473 | n/a | '{}' |
---|
1474 | n/a | 'Content-Length: 12\r\n' |
---|
1475 | n/a | '\r\n' |
---|
1476 | n/a | 'Dummy body\r\n' |
---|
1477 | n/a | ).format(version, header) |
---|
1478 | n/a | conn = FakeSocketHTTPConnection(msg) |
---|
1479 | n/a | self.assertIsNone(conn.sock) |
---|
1480 | n/a | conn.request('GET', '/open-connection') |
---|
1481 | n/a | with conn.getresponse() as response: |
---|
1482 | n/a | self.assertEqual(conn.sock is None, not reuse) |
---|
1483 | n/a | response.read() |
---|
1484 | n/a | self.assertEqual(conn.sock is None, not reuse) |
---|
1485 | n/a | self.assertEqual(conn.connections, 1) |
---|
1486 | n/a | conn.request('GET', '/subsequent-request') |
---|
1487 | n/a | self.assertEqual(conn.connections, 1 if reuse else 2) |
---|
1488 | n/a | |
---|
1489 | n/a | def test_disconnected(self): |
---|
1490 | n/a | |
---|
1491 | n/a | def make_reset_reader(text): |
---|
1492 | n/a | """Return BufferedReader that raises ECONNRESET at EOF""" |
---|
1493 | n/a | stream = io.BytesIO(text) |
---|
1494 | n/a | def readinto(buffer): |
---|
1495 | n/a | size = io.BytesIO.readinto(stream, buffer) |
---|
1496 | n/a | if size == 0: |
---|
1497 | n/a | raise ConnectionResetError() |
---|
1498 | n/a | return size |
---|
1499 | n/a | stream.readinto = readinto |
---|
1500 | n/a | return io.BufferedReader(stream) |
---|
1501 | n/a | |
---|
1502 | n/a | tests = ( |
---|
1503 | n/a | (io.BytesIO, client.RemoteDisconnected), |
---|
1504 | n/a | (make_reset_reader, ConnectionResetError), |
---|
1505 | n/a | ) |
---|
1506 | n/a | for stream_factory, exception in tests: |
---|
1507 | n/a | with self.subTest(exception=exception): |
---|
1508 | n/a | conn = FakeSocketHTTPConnection(b'', stream_factory) |
---|
1509 | n/a | conn.request('GET', '/eof-response') |
---|
1510 | n/a | self.assertRaises(exception, conn.getresponse) |
---|
1511 | n/a | self.assertIsNone(conn.sock) |
---|
1512 | n/a | # HTTPConnection.connect() should be automatically invoked |
---|
1513 | n/a | conn.request('GET', '/reconnect') |
---|
1514 | n/a | self.assertEqual(conn.connections, 2) |
---|
1515 | n/a | |
---|
1516 | n/a | def test_100_close(self): |
---|
1517 | n/a | conn = FakeSocketHTTPConnection( |
---|
1518 | n/a | b'HTTP/1.1 100 Continue\r\n' |
---|
1519 | n/a | b'\r\n' |
---|
1520 | n/a | # Missing final response |
---|
1521 | n/a | ) |
---|
1522 | n/a | conn.request('GET', '/', headers={'Expect': '100-continue'}) |
---|
1523 | n/a | self.assertRaises(client.RemoteDisconnected, conn.getresponse) |
---|
1524 | n/a | self.assertIsNone(conn.sock) |
---|
1525 | n/a | conn.request('GET', '/reconnect') |
---|
1526 | n/a | self.assertEqual(conn.connections, 2) |
---|
1527 | n/a | |
---|
1528 | n/a | |
---|
1529 | n/a | class HTTPSTest(TestCase): |
---|
1530 | n/a | |
---|
1531 | n/a | def setUp(self): |
---|
1532 | n/a | if not hasattr(client, 'HTTPSConnection'): |
---|
1533 | n/a | self.skipTest('ssl support required') |
---|
1534 | n/a | |
---|
1535 | n/a | def make_server(self, certfile): |
---|
1536 | n/a | from test.ssl_servers import make_https_server |
---|
1537 | n/a | return make_https_server(self, certfile=certfile) |
---|
1538 | n/a | |
---|
1539 | n/a | def test_attributes(self): |
---|
1540 | n/a | # simple test to check it's storing the timeout |
---|
1541 | n/a | h = client.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30) |
---|
1542 | n/a | self.assertEqual(h.timeout, 30) |
---|
1543 | n/a | |
---|
1544 | n/a | def test_networked(self): |
---|
1545 | n/a | # Default settings: requires a valid cert from a trusted CA |
---|
1546 | n/a | import ssl |
---|
1547 | n/a | support.requires('network') |
---|
1548 | n/a | with support.transient_internet('self-signed.pythontest.net'): |
---|
1549 | n/a | h = client.HTTPSConnection('self-signed.pythontest.net', 443) |
---|
1550 | n/a | with self.assertRaises(ssl.SSLError) as exc_info: |
---|
1551 | n/a | h.request('GET', '/') |
---|
1552 | n/a | self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED') |
---|
1553 | n/a | |
---|
1554 | n/a | def test_networked_noverification(self): |
---|
1555 | n/a | # Switch off cert verification |
---|
1556 | n/a | import ssl |
---|
1557 | n/a | support.requires('network') |
---|
1558 | n/a | with support.transient_internet('self-signed.pythontest.net'): |
---|
1559 | n/a | context = ssl._create_unverified_context() |
---|
1560 | n/a | h = client.HTTPSConnection('self-signed.pythontest.net', 443, |
---|
1561 | n/a | context=context) |
---|
1562 | n/a | h.request('GET', '/') |
---|
1563 | n/a | resp = h.getresponse() |
---|
1564 | n/a | h.close() |
---|
1565 | n/a | self.assertIn('nginx', resp.getheader('server')) |
---|
1566 | n/a | resp.close() |
---|
1567 | n/a | |
---|
1568 | n/a | @support.system_must_validate_cert |
---|
1569 | n/a | def test_networked_trusted_by_default_cert(self): |
---|
1570 | n/a | # Default settings: requires a valid cert from a trusted CA |
---|
1571 | n/a | support.requires('network') |
---|
1572 | n/a | with support.transient_internet('www.python.org'): |
---|
1573 | n/a | h = client.HTTPSConnection('www.python.org', 443) |
---|
1574 | n/a | h.request('GET', '/') |
---|
1575 | n/a | resp = h.getresponse() |
---|
1576 | n/a | content_type = resp.getheader('content-type') |
---|
1577 | n/a | resp.close() |
---|
1578 | n/a | h.close() |
---|
1579 | n/a | self.assertIn('text/html', content_type) |
---|
1580 | n/a | |
---|
1581 | n/a | def test_networked_good_cert(self): |
---|
1582 | n/a | # We feed the server's cert as a validating cert |
---|
1583 | n/a | import ssl |
---|
1584 | n/a | support.requires('network') |
---|
1585 | n/a | with support.transient_internet('self-signed.pythontest.net'): |
---|
1586 | n/a | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
---|
1587 | n/a | context.verify_mode = ssl.CERT_REQUIRED |
---|
1588 | n/a | context.load_verify_locations(CERT_selfsigned_pythontestdotnet) |
---|
1589 | n/a | h = client.HTTPSConnection('self-signed.pythontest.net', 443, context=context) |
---|
1590 | n/a | h.request('GET', '/') |
---|
1591 | n/a | resp = h.getresponse() |
---|
1592 | n/a | server_string = resp.getheader('server') |
---|
1593 | n/a | resp.close() |
---|
1594 | n/a | h.close() |
---|
1595 | n/a | self.assertIn('nginx', server_string) |
---|
1596 | n/a | |
---|
1597 | n/a | def test_networked_bad_cert(self): |
---|
1598 | n/a | # We feed a "CA" cert that is unrelated to the server's cert |
---|
1599 | n/a | import ssl |
---|
1600 | n/a | support.requires('network') |
---|
1601 | n/a | with support.transient_internet('self-signed.pythontest.net'): |
---|
1602 | n/a | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
---|
1603 | n/a | context.verify_mode = ssl.CERT_REQUIRED |
---|
1604 | n/a | context.load_verify_locations(CERT_localhost) |
---|
1605 | n/a | h = client.HTTPSConnection('self-signed.pythontest.net', 443, context=context) |
---|
1606 | n/a | with self.assertRaises(ssl.SSLError) as exc_info: |
---|
1607 | n/a | h.request('GET', '/') |
---|
1608 | n/a | self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED') |
---|
1609 | n/a | |
---|
1610 | n/a | def test_local_unknown_cert(self): |
---|
1611 | n/a | # The custom cert isn't known to the default trust bundle |
---|
1612 | n/a | import ssl |
---|
1613 | n/a | server = self.make_server(CERT_localhost) |
---|
1614 | n/a | h = client.HTTPSConnection('localhost', server.port) |
---|
1615 | n/a | with self.assertRaises(ssl.SSLError) as exc_info: |
---|
1616 | n/a | h.request('GET', '/') |
---|
1617 | n/a | self.assertEqual(exc_info.exception.reason, 'CERTIFICATE_VERIFY_FAILED') |
---|
1618 | n/a | |
---|
1619 | n/a | def test_local_good_hostname(self): |
---|
1620 | n/a | # The (valid) cert validates the HTTP hostname |
---|
1621 | n/a | import ssl |
---|
1622 | n/a | server = self.make_server(CERT_localhost) |
---|
1623 | n/a | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
---|
1624 | n/a | context.verify_mode = ssl.CERT_REQUIRED |
---|
1625 | n/a | context.load_verify_locations(CERT_localhost) |
---|
1626 | n/a | h = client.HTTPSConnection('localhost', server.port, context=context) |
---|
1627 | n/a | self.addCleanup(h.close) |
---|
1628 | n/a | h.request('GET', '/nonexistent') |
---|
1629 | n/a | resp = h.getresponse() |
---|
1630 | n/a | self.addCleanup(resp.close) |
---|
1631 | n/a | self.assertEqual(resp.status, 404) |
---|
1632 | n/a | |
---|
1633 | n/a | def test_local_bad_hostname(self): |
---|
1634 | n/a | # The (valid) cert doesn't validate the HTTP hostname |
---|
1635 | n/a | import ssl |
---|
1636 | n/a | server = self.make_server(CERT_fakehostname) |
---|
1637 | n/a | context = ssl.SSLContext(ssl.PROTOCOL_TLSv1) |
---|
1638 | n/a | context.verify_mode = ssl.CERT_REQUIRED |
---|
1639 | n/a | context.check_hostname = True |
---|
1640 | n/a | context.load_verify_locations(CERT_fakehostname) |
---|
1641 | n/a | h = client.HTTPSConnection('localhost', server.port, context=context) |
---|
1642 | n/a | with self.assertRaises(ssl.CertificateError): |
---|
1643 | n/a | h.request('GET', '/') |
---|
1644 | n/a | # Same with explicit check_hostname=True |
---|
1645 | n/a | with support.check_warnings(('', DeprecationWarning)): |
---|
1646 | n/a | h = client.HTTPSConnection('localhost', server.port, |
---|
1647 | n/a | context=context, check_hostname=True) |
---|
1648 | n/a | with self.assertRaises(ssl.CertificateError): |
---|
1649 | n/a | h.request('GET', '/') |
---|
1650 | n/a | # With check_hostname=False, the mismatching is ignored |
---|
1651 | n/a | context.check_hostname = False |
---|
1652 | n/a | with support.check_warnings(('', DeprecationWarning)): |
---|
1653 | n/a | h = client.HTTPSConnection('localhost', server.port, |
---|
1654 | n/a | context=context, check_hostname=False) |
---|
1655 | n/a | h.request('GET', '/nonexistent') |
---|
1656 | n/a | resp = h.getresponse() |
---|
1657 | n/a | resp.close() |
---|
1658 | n/a | h.close() |
---|
1659 | n/a | self.assertEqual(resp.status, 404) |
---|
1660 | n/a | # The context's check_hostname setting is used if one isn't passed to |
---|
1661 | n/a | # HTTPSConnection. |
---|
1662 | n/a | context.check_hostname = False |
---|
1663 | n/a | h = client.HTTPSConnection('localhost', server.port, context=context) |
---|
1664 | n/a | h.request('GET', '/nonexistent') |
---|
1665 | n/a | resp = h.getresponse() |
---|
1666 | n/a | self.assertEqual(resp.status, 404) |
---|
1667 | n/a | resp.close() |
---|
1668 | n/a | h.close() |
---|
1669 | n/a | # Passing check_hostname to HTTPSConnection should override the |
---|
1670 | n/a | # context's setting. |
---|
1671 | n/a | with support.check_warnings(('', DeprecationWarning)): |
---|
1672 | n/a | h = client.HTTPSConnection('localhost', server.port, |
---|
1673 | n/a | context=context, check_hostname=True) |
---|
1674 | n/a | with self.assertRaises(ssl.CertificateError): |
---|
1675 | n/a | h.request('GET', '/') |
---|
1676 | n/a | |
---|
1677 | n/a | @unittest.skipIf(not hasattr(client, 'HTTPSConnection'), |
---|
1678 | n/a | 'http.client.HTTPSConnection not available') |
---|
1679 | n/a | def test_host_port(self): |
---|
1680 | n/a | # Check invalid host_port |
---|
1681 | n/a | |
---|
1682 | n/a | for hp in ("www.python.org:abc", "user:password@www.python.org"): |
---|
1683 | n/a | self.assertRaises(client.InvalidURL, client.HTTPSConnection, hp) |
---|
1684 | n/a | |
---|
1685 | n/a | for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", |
---|
1686 | n/a | "fe80::207:e9ff:fe9b", 8000), |
---|
1687 | n/a | ("www.python.org:443", "www.python.org", 443), |
---|
1688 | n/a | ("www.python.org:", "www.python.org", 443), |
---|
1689 | n/a | ("www.python.org", "www.python.org", 443), |
---|
1690 | n/a | ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 443), |
---|
1691 | n/a | ("[fe80::207:e9ff:fe9b]:", "fe80::207:e9ff:fe9b", |
---|
1692 | n/a | 443)): |
---|
1693 | n/a | c = client.HTTPSConnection(hp) |
---|
1694 | n/a | self.assertEqual(h, c.host) |
---|
1695 | n/a | self.assertEqual(p, c.port) |
---|
1696 | n/a | |
---|
1697 | n/a | |
---|
1698 | n/a | class RequestBodyTest(TestCase): |
---|
1699 | n/a | """Test cases where a request includes a message body.""" |
---|
1700 | n/a | |
---|
1701 | n/a | def setUp(self): |
---|
1702 | n/a | self.conn = client.HTTPConnection('example.com') |
---|
1703 | n/a | self.conn.sock = self.sock = FakeSocket("") |
---|
1704 | n/a | self.conn.sock = self.sock |
---|
1705 | n/a | |
---|
1706 | n/a | def get_headers_and_fp(self): |
---|
1707 | n/a | f = io.BytesIO(self.sock.data) |
---|
1708 | n/a | f.readline() # read the request line |
---|
1709 | n/a | message = client.parse_headers(f) |
---|
1710 | n/a | return message, f |
---|
1711 | n/a | |
---|
1712 | n/a | def test_list_body(self): |
---|
1713 | n/a | # Note that no content-length is automatically calculated for |
---|
1714 | n/a | # an iterable. The request will fall back to send chunked |
---|
1715 | n/a | # transfer encoding. |
---|
1716 | n/a | cases = ( |
---|
1717 | n/a | ([b'foo', b'bar'], b'3\r\nfoo\r\n3\r\nbar\r\n0\r\n\r\n'), |
---|
1718 | n/a | ((b'foo', b'bar'), b'3\r\nfoo\r\n3\r\nbar\r\n0\r\n\r\n'), |
---|
1719 | n/a | ) |
---|
1720 | n/a | for body, expected in cases: |
---|
1721 | n/a | with self.subTest(body): |
---|
1722 | n/a | self.conn = client.HTTPConnection('example.com') |
---|
1723 | n/a | self.conn.sock = self.sock = FakeSocket('') |
---|
1724 | n/a | |
---|
1725 | n/a | self.conn.request('PUT', '/url', body) |
---|
1726 | n/a | msg, f = self.get_headers_and_fp() |
---|
1727 | n/a | self.assertNotIn('Content-Type', msg) |
---|
1728 | n/a | self.assertNotIn('Content-Length', msg) |
---|
1729 | n/a | self.assertEqual(msg.get('Transfer-Encoding'), 'chunked') |
---|
1730 | n/a | self.assertEqual(expected, f.read()) |
---|
1731 | n/a | |
---|
1732 | n/a | def test_manual_content_length(self): |
---|
1733 | n/a | # Set an incorrect content-length so that we can verify that |
---|
1734 | n/a | # it will not be over-ridden by the library. |
---|
1735 | n/a | self.conn.request("PUT", "/url", "body", |
---|
1736 | n/a | {"Content-Length": "42"}) |
---|
1737 | n/a | message, f = self.get_headers_and_fp() |
---|
1738 | n/a | self.assertEqual("42", message.get("content-length")) |
---|
1739 | n/a | self.assertEqual(4, len(f.read())) |
---|
1740 | n/a | |
---|
1741 | n/a | def test_ascii_body(self): |
---|
1742 | n/a | self.conn.request("PUT", "/url", "body") |
---|
1743 | n/a | message, f = self.get_headers_and_fp() |
---|
1744 | n/a | self.assertEqual("text/plain", message.get_content_type()) |
---|
1745 | n/a | self.assertIsNone(message.get_charset()) |
---|
1746 | n/a | self.assertEqual("4", message.get("content-length")) |
---|
1747 | n/a | self.assertEqual(b'body', f.read()) |
---|
1748 | n/a | |
---|
1749 | n/a | def test_latin1_body(self): |
---|
1750 | n/a | self.conn.request("PUT", "/url", "body\xc1") |
---|
1751 | n/a | message, f = self.get_headers_and_fp() |
---|
1752 | n/a | self.assertEqual("text/plain", message.get_content_type()) |
---|
1753 | n/a | self.assertIsNone(message.get_charset()) |
---|
1754 | n/a | self.assertEqual("5", message.get("content-length")) |
---|
1755 | n/a | self.assertEqual(b'body\xc1', f.read()) |
---|
1756 | n/a | |
---|
1757 | n/a | def test_bytes_body(self): |
---|
1758 | n/a | self.conn.request("PUT", "/url", b"body\xc1") |
---|
1759 | n/a | message, f = self.get_headers_and_fp() |
---|
1760 | n/a | self.assertEqual("text/plain", message.get_content_type()) |
---|
1761 | n/a | self.assertIsNone(message.get_charset()) |
---|
1762 | n/a | self.assertEqual("5", message.get("content-length")) |
---|
1763 | n/a | self.assertEqual(b'body\xc1', f.read()) |
---|
1764 | n/a | |
---|
1765 | n/a | def test_text_file_body(self): |
---|
1766 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
1767 | n/a | with open(support.TESTFN, "w") as f: |
---|
1768 | n/a | f.write("body") |
---|
1769 | n/a | with open(support.TESTFN) as f: |
---|
1770 | n/a | self.conn.request("PUT", "/url", f) |
---|
1771 | n/a | message, f = self.get_headers_and_fp() |
---|
1772 | n/a | self.assertEqual("text/plain", message.get_content_type()) |
---|
1773 | n/a | self.assertIsNone(message.get_charset()) |
---|
1774 | n/a | # No content-length will be determined for files; the body |
---|
1775 | n/a | # will be sent using chunked transfer encoding instead. |
---|
1776 | n/a | self.assertIsNone(message.get("content-length")) |
---|
1777 | n/a | self.assertEqual("chunked", message.get("transfer-encoding")) |
---|
1778 | n/a | self.assertEqual(b'4\r\nbody\r\n0\r\n\r\n', f.read()) |
---|
1779 | n/a | |
---|
1780 | n/a | def test_binary_file_body(self): |
---|
1781 | n/a | self.addCleanup(support.unlink, support.TESTFN) |
---|
1782 | n/a | with open(support.TESTFN, "wb") as f: |
---|
1783 | n/a | f.write(b"body\xc1") |
---|
1784 | n/a | with open(support.TESTFN, "rb") as f: |
---|
1785 | n/a | self.conn.request("PUT", "/url", f) |
---|
1786 | n/a | message, f = self.get_headers_and_fp() |
---|
1787 | n/a | self.assertEqual("text/plain", message.get_content_type()) |
---|
1788 | n/a | self.assertIsNone(message.get_charset()) |
---|
1789 | n/a | self.assertEqual("chunked", message.get("Transfer-Encoding")) |
---|
1790 | n/a | self.assertNotIn("Content-Length", message) |
---|
1791 | n/a | self.assertEqual(b'5\r\nbody\xc1\r\n0\r\n\r\n', f.read()) |
---|
1792 | n/a | |
---|
1793 | n/a | |
---|
1794 | n/a | class HTTPResponseTest(TestCase): |
---|
1795 | n/a | |
---|
1796 | n/a | def setUp(self): |
---|
1797 | n/a | body = "HTTP/1.1 200 Ok\r\nMy-Header: first-value\r\nMy-Header: \ |
---|
1798 | n/a | second-value\r\n\r\nText" |
---|
1799 | n/a | sock = FakeSocket(body) |
---|
1800 | n/a | self.resp = client.HTTPResponse(sock) |
---|
1801 | n/a | self.resp.begin() |
---|
1802 | n/a | |
---|
1803 | n/a | def test_getting_header(self): |
---|
1804 | n/a | header = self.resp.getheader('My-Header') |
---|
1805 | n/a | self.assertEqual(header, 'first-value, second-value') |
---|
1806 | n/a | |
---|
1807 | n/a | header = self.resp.getheader('My-Header', 'some default') |
---|
1808 | n/a | self.assertEqual(header, 'first-value, second-value') |
---|
1809 | n/a | |
---|
1810 | n/a | def test_getting_nonexistent_header_with_string_default(self): |
---|
1811 | n/a | header = self.resp.getheader('No-Such-Header', 'default-value') |
---|
1812 | n/a | self.assertEqual(header, 'default-value') |
---|
1813 | n/a | |
---|
1814 | n/a | def test_getting_nonexistent_header_with_iterable_default(self): |
---|
1815 | n/a | header = self.resp.getheader('No-Such-Header', ['default', 'values']) |
---|
1816 | n/a | self.assertEqual(header, 'default, values') |
---|
1817 | n/a | |
---|
1818 | n/a | header = self.resp.getheader('No-Such-Header', ('default', 'values')) |
---|
1819 | n/a | self.assertEqual(header, 'default, values') |
---|
1820 | n/a | |
---|
1821 | n/a | def test_getting_nonexistent_header_without_default(self): |
---|
1822 | n/a | header = self.resp.getheader('No-Such-Header') |
---|
1823 | n/a | self.assertEqual(header, None) |
---|
1824 | n/a | |
---|
1825 | n/a | def test_getting_header_defaultint(self): |
---|
1826 | n/a | header = self.resp.getheader('No-Such-Header',default=42) |
---|
1827 | n/a | self.assertEqual(header, 42) |
---|
1828 | n/a | |
---|
1829 | n/a | class TunnelTests(TestCase): |
---|
1830 | n/a | def setUp(self): |
---|
1831 | n/a | response_text = ( |
---|
1832 | n/a | 'HTTP/1.0 200 OK\r\n\r\n' # Reply to CONNECT |
---|
1833 | n/a | 'HTTP/1.1 200 OK\r\n' # Reply to HEAD |
---|
1834 | n/a | 'Content-Length: 42\r\n\r\n' |
---|
1835 | n/a | ) |
---|
1836 | n/a | self.host = 'proxy.com' |
---|
1837 | n/a | self.conn = client.HTTPConnection(self.host) |
---|
1838 | n/a | self.conn._create_connection = self._create_connection(response_text) |
---|
1839 | n/a | |
---|
1840 | n/a | def tearDown(self): |
---|
1841 | n/a | self.conn.close() |
---|
1842 | n/a | |
---|
1843 | n/a | def _create_connection(self, response_text): |
---|
1844 | n/a | def create_connection(address, timeout=None, source_address=None): |
---|
1845 | n/a | return FakeSocket(response_text, host=address[0], port=address[1]) |
---|
1846 | n/a | return create_connection |
---|
1847 | n/a | |
---|
1848 | n/a | def test_set_tunnel_host_port_headers(self): |
---|
1849 | n/a | tunnel_host = 'destination.com' |
---|
1850 | n/a | tunnel_port = 8888 |
---|
1851 | n/a | tunnel_headers = {'User-Agent': 'Mozilla/5.0 (compatible, MSIE 11)'} |
---|
1852 | n/a | self.conn.set_tunnel(tunnel_host, port=tunnel_port, |
---|
1853 | n/a | headers=tunnel_headers) |
---|
1854 | n/a | self.conn.request('HEAD', '/', '') |
---|
1855 | n/a | self.assertEqual(self.conn.sock.host, self.host) |
---|
1856 | n/a | self.assertEqual(self.conn.sock.port, client.HTTP_PORT) |
---|
1857 | n/a | self.assertEqual(self.conn._tunnel_host, tunnel_host) |
---|
1858 | n/a | self.assertEqual(self.conn._tunnel_port, tunnel_port) |
---|
1859 | n/a | self.assertEqual(self.conn._tunnel_headers, tunnel_headers) |
---|
1860 | n/a | |
---|
1861 | n/a | def test_disallow_set_tunnel_after_connect(self): |
---|
1862 | n/a | # Once connected, we shouldn't be able to tunnel anymore |
---|
1863 | n/a | self.conn.connect() |
---|
1864 | n/a | self.assertRaises(RuntimeError, self.conn.set_tunnel, |
---|
1865 | n/a | 'destination.com') |
---|
1866 | n/a | |
---|
1867 | n/a | def test_connect_with_tunnel(self): |
---|
1868 | n/a | self.conn.set_tunnel('destination.com') |
---|
1869 | n/a | self.conn.request('HEAD', '/', '') |
---|
1870 | n/a | self.assertEqual(self.conn.sock.host, self.host) |
---|
1871 | n/a | self.assertEqual(self.conn.sock.port, client.HTTP_PORT) |
---|
1872 | n/a | self.assertIn(b'CONNECT destination.com', self.conn.sock.data) |
---|
1873 | n/a | # issue22095 |
---|
1874 | n/a | self.assertNotIn(b'Host: destination.com:None', self.conn.sock.data) |
---|
1875 | n/a | self.assertIn(b'Host: destination.com', self.conn.sock.data) |
---|
1876 | n/a | |
---|
1877 | n/a | # This test should be removed when CONNECT gets the HTTP/1.1 blessing |
---|
1878 | n/a | self.assertNotIn(b'Host: proxy.com', self.conn.sock.data) |
---|
1879 | n/a | |
---|
1880 | n/a | def test_connect_put_request(self): |
---|
1881 | n/a | self.conn.set_tunnel('destination.com') |
---|
1882 | n/a | self.conn.request('PUT', '/', '') |
---|
1883 | n/a | self.assertEqual(self.conn.sock.host, self.host) |
---|
1884 | n/a | self.assertEqual(self.conn.sock.port, client.HTTP_PORT) |
---|
1885 | n/a | self.assertIn(b'CONNECT destination.com', self.conn.sock.data) |
---|
1886 | n/a | self.assertIn(b'Host: destination.com', self.conn.sock.data) |
---|
1887 | n/a | |
---|
1888 | n/a | def test_tunnel_debuglog(self): |
---|
1889 | n/a | expected_header = 'X-Dummy: 1' |
---|
1890 | n/a | response_text = 'HTTP/1.0 200 OK\r\n{}\r\n\r\n'.format(expected_header) |
---|
1891 | n/a | |
---|
1892 | n/a | self.conn.set_debuglevel(1) |
---|
1893 | n/a | self.conn._create_connection = self._create_connection(response_text) |
---|
1894 | n/a | self.conn.set_tunnel('destination.com') |
---|
1895 | n/a | |
---|
1896 | n/a | with support.captured_stdout() as output: |
---|
1897 | n/a | self.conn.request('PUT', '/', '') |
---|
1898 | n/a | lines = output.getvalue().splitlines() |
---|
1899 | n/a | self.assertIn('header: {}'.format(expected_header), lines) |
---|
1900 | n/a | |
---|
1901 | n/a | |
---|
1902 | n/a | if __name__ == '__main__': |
---|
1903 | n/a | unittest.main(verbosity=2) |
---|