Mercurial > cpython-lru_cache-weakref
annotate Lib/test/test_httplib.py @ 79675:c0a2294de787 2.7
Issue #16199: Backport 2to3 typo fix from 3.2.
| author | Chris Jerdonek <chris.jerdonek@gmail.com> |
|---|---|
| date | Thu, 11 Oct 2012 16:15:46 -0700 |
| parents | 57f1d13c2cd4 |
| children | 2186f7b99c28 |
| rev | line source |
|---|---|
|
65080
e1f6ce836fc5
Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
64862
diff
changeset
|
1 import httplib |
|
56286
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
2 import array |
|
17483
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
3 import httplib |
|
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
4 import StringIO |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
5 import socket |
|
63194
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
6 import errno |
|
29778
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
7 |
|
58113
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
8 import unittest |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
9 TestCase = unittest.TestCase |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
10 |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
11 from test import test_support |
|
17483
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
12 |
|
46362
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
45494
diff
changeset
|
13 HOST = test_support.HOST |
|
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
45494
diff
changeset
|
14 |
|
17483
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
15 class FakeSocket: |
|
29778
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
16 def __init__(self, text, fileclass=StringIO.StringIO): |
|
17483
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
17 self.text = text |
|
29778
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
18 self.fileclass = fileclass |
|
40333
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
19 self.data = '' |
|
17483
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
20 |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
21 def sendall(self, data): |
|
56286
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
22 self.data += ''.join(data) |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
23 |
|
17483
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
24 def makefile(self, mode, bufsize=None): |
|
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
25 if mode != 'r' and mode != 'rb': |
|
22738
826bc1522d22
Use attributes appropriately
Neal Norwitz <nnorwitz@gmail.com>
parents:
22495
diff
changeset
|
26 raise httplib.UnimplementedFileMode() |
|
29778
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
27 return self.fileclass(self.text) |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
28 |
|
63194
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
29 class EPipeSocket(FakeSocket): |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
30 |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
31 def __init__(self, text, pipe_trigger): |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
32 # When sendall() is called with pipe_trigger, raise EPIPE. |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
33 FakeSocket.__init__(self, text) |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
34 self.pipe_trigger = pipe_trigger |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
35 |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
36 def sendall(self, data): |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
37 if self.pipe_trigger in data: |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
38 raise socket.error(errno.EPIPE, "gotcha") |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
39 self.data += data |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
40 |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
41 def close(self): |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
42 pass |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
43 |
|
29778
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
44 class NoEOFStringIO(StringIO.StringIO): |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
45 """Like StringIO, but raises AssertionError on EOF. |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
46 |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
47 This is used below to test that httplib doesn't try to read |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
48 more from the underlying file than it should. |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
49 """ |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
50 def read(self, n=-1): |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
51 data = StringIO.StringIO.read(self, n) |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
52 if data == '': |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
53 raise AssertionError('caller tried to read past EOF') |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
54 return data |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
55 |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
56 def readline(self, length=None): |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
57 data = StringIO.StringIO.readline(self, length) |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
58 if data == '': |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
59 raise AssertionError('caller tried to read past EOF') |
|
ce988394c754
Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
28978
diff
changeset
|
60 return data |
|
17483
71752a41b57f
Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff
changeset
|
61 |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
62 |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
63 class HeaderTests(TestCase): |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
64 def test_auto_headers(self): |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
65 # Some headers are added automatically, but should not be added by |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
66 # .request() if they are explicitly set. |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
67 |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
68 class HeaderCountingBuffer(list): |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
69 def __init__(self): |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
70 self.count = {} |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
71 def append(self, item): |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
72 kv = item.split(':') |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
73 if len(kv) > 1: |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
74 # item is a 'Key: Value' header string |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
75 lcKey = kv[0].lower() |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
76 self.count.setdefault(lcKey, 0) |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
77 self.count[lcKey] += 1 |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
78 list.append(self, item) |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
79 |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
80 for explicit_header in True, False: |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
81 for header in 'Content-length', 'Host', 'Accept-encoding': |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
82 conn = httplib.HTTPConnection('example.com') |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
83 conn.sock = FakeSocket('blahblahblah') |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
84 conn._buffer = HeaderCountingBuffer() |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
85 |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
86 body = 'spamspamspam' |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
87 headers = {} |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
88 if explicit_header: |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
89 headers[header] = str(len(body)) |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
90 conn.request('POST', '/', body, headers) |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
91 self.assertEqual(conn._buffer.count[header.lower()], 1) |
|
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
92 |
|
77047
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
93 def test_content_length_0(self): |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
94 |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
95 class ContentLengthChecker(list): |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
96 def __init__(self): |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
97 list.__init__(self) |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
98 self.content_length = None |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
99 def append(self, item): |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
100 kv = item.split(':', 1) |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
101 if len(kv) > 1 and kv[0].lower() == 'content-length': |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
102 self.content_length = kv[1].strip() |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
103 list.append(self, item) |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
104 |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
105 # POST with empty body |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
106 conn = httplib.HTTPConnection('example.com') |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
107 conn.sock = FakeSocket(None) |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
108 conn._buffer = ContentLengthChecker() |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
109 conn.request('POST', '/', '') |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
110 self.assertEqual(conn._buffer.content_length, '0', |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
111 'Header Content-Length not set') |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
112 |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
113 # PUT request with empty body |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
114 conn = httplib.HTTPConnection('example.com') |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
115 conn.sock = FakeSocket(None) |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
116 conn._buffer = ContentLengthChecker() |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
117 conn.request('PUT', '/', '') |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
118 self.assertEqual(conn._buffer.content_length, '0', |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
119 'Header Content-Length not set') |
|
57f1d13c2cd4
Fix Issue14721: Send Content-length: 0 for empty body () in the http.request
Senthil Kumaran <senthil@uthcode.com>
parents:
76603
diff
changeset
|
120 |
|
65080
e1f6ce836fc5
Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
64862
diff
changeset
|
121 def test_putheader(self): |
|
e1f6ce836fc5
Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
64862
diff
changeset
|
122 conn = httplib.HTTPConnection('example.com') |
|
e1f6ce836fc5
Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
64862
diff
changeset
|
123 conn.sock = FakeSocket(None) |
|
e1f6ce836fc5
Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
64862
diff
changeset
|
124 conn.putrequest('GET','/') |
|
e1f6ce836fc5
Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
64862
diff
changeset
|
125 conn.putheader('Content-length',42) |
|
e1f6ce836fc5
Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
64862
diff
changeset
|
126 self.assertTrue('Content-length: 42' in conn._buffer) |
|
e1f6ce836fc5
Merged revisions 85205 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
64862
diff
changeset
|
127 |
|
66197
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
128 def test_ipv6host_header(self): |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
129 # Default host header on IPv6 transaction should wrapped by [] if |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
130 # its actual IPv6 address |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
131 expected = 'GET /foo HTTP/1.1\r\nHost: [2001::]:81\r\n' \ |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
132 'Accept-Encoding: identity\r\n\r\n' |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
133 conn = httplib.HTTPConnection('[2001::]:81') |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
134 sock = FakeSocket('') |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
135 conn.sock = sock |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
136 conn.request('GET', '/foo') |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
137 self.assertTrue(sock.data.startswith(expected)) |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
138 |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
139 expected = 'GET /foo HTTP/1.1\r\nHost: [2001:102A::]\r\n' \ |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
140 'Accept-Encoding: identity\r\n\r\n' |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
141 conn = httplib.HTTPConnection('[2001:102A::]') |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
142 sock = FakeSocket('') |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
143 conn.sock = sock |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
144 conn.request('GET', '/foo') |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
145 self.assertTrue(sock.data.startswith(expected)) |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
146 |
|
b7d02ae45d15
Merged revisions 86450 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
65080
diff
changeset
|
147 |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
148 class BasicTest(TestCase): |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
149 def test_status_lines(self): |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
150 # Test HTTP status lines |
|
27348
5acf6967d6dd
Fix for SF bug 661340: test_httplib fails on the mac.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24442
diff
changeset
|
151 |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
152 body = "HTTP/1.1 200 Ok\r\n\r\nText" |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
153 sock = FakeSocket(body) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
154 resp = httplib.HTTPResponse(sock) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
155 resp.begin() |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
156 self.assertEqual(resp.read(), 'Text') |
|
43584
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
157 self.assertTrue(resp.isclosed()) |
|
27348
5acf6967d6dd
Fix for SF bug 661340: test_httplib fails on the mac.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24442
diff
changeset
|
158 |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
159 body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText" |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
160 sock = FakeSocket(body) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
161 resp = httplib.HTTPResponse(sock) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
162 self.assertRaises(httplib.BadStatusLine, resp.begin) |
|
27348
5acf6967d6dd
Fix for SF bug 661340: test_httplib fails on the mac.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24442
diff
changeset
|
163 |
|
59107
45212a247ed4
Issue #7427: improve the representation of httplib.BadStatusLine exceptions.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
58113
diff
changeset
|
164 def test_bad_status_repr(self): |
|
45212a247ed4
Issue #7427: improve the representation of httplib.BadStatusLine exceptions.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
58113
diff
changeset
|
165 exc = httplib.BadStatusLine('') |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
166 self.assertEqual(repr(exc), '''BadStatusLine("\'\'",)''') |
|
59107
45212a247ed4
Issue #7427: improve the representation of httplib.BadStatusLine exceptions.
Dirkjan Ochtman <dirkjan@ochtman.nl>
parents:
58113
diff
changeset
|
167 |
|
43584
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
168 def test_partial_reads(self): |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
169 # if we have a lenght, the system knows when to close itself |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
170 # same behaviour than when we read the whole thing with read() |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
171 body = "HTTP/1.1 200 Ok\r\nContent-Length: 4\r\n\r\nText" |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
172 sock = FakeSocket(body) |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
173 resp = httplib.HTTPResponse(sock) |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
174 resp.begin() |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
175 self.assertEqual(resp.read(2), 'Te') |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
176 self.assertFalse(resp.isclosed()) |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
177 self.assertEqual(resp.read(2), 'xt') |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
178 self.assertTrue(resp.isclosed()) |
|
da44334ebea2
Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents:
43098
diff
changeset
|
179 |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
180 def test_host_port(self): |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
181 # Check invalid host_port |
| 22495 | 182 |
|
72974
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
183 # Note that httplib does not accept user:password@ in the host-port. |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
184 for hp in ("www.python.org:abc", "user:password@www.python.org"): |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
185 self.assertRaises(httplib.InvalidURL, httplib.HTTP, hp) |
|
27348
5acf6967d6dd
Fix for SF bug 661340: test_httplib fails on the mac.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24442
diff
changeset
|
186 |
|
50004
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
187 for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b", |
|
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
188 8000), |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
189 ("www.python.org:80", "www.python.org", 80), |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
190 ("www.python.org", "www.python.org", 80), |
|
72974
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
191 ("www.python.org:", "www.python.org", 80), |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
192 ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)): |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
193 http = httplib.HTTP(hp) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
194 c = http._conn |
|
50004
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
195 if h != c.host: |
|
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
196 self.fail("Host incorrectly parsed: %s != %s" % (h, c.host)) |
|
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
197 if p != c.port: |
|
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
198 self.fail("Port incorrectly parsed: %s != %s" % (p, c.host)) |
|
27348
5acf6967d6dd
Fix for SF bug 661340: test_httplib fails on the mac.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
24442
diff
changeset
|
199 |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
200 def test_response_headers(self): |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
201 # test response with multiple message headers with the same field name. |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
202 text = ('HTTP/1.1 200 OK\r\n' |
|
50004
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
203 'Set-Cookie: Customer="WILE_E_COYOTE";' |
|
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
204 ' Version="1"; Path="/acme"\r\n' |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
205 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";' |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
206 ' Path="/acme"\r\n' |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
207 '\r\n' |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
208 'No body\r\n') |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
209 hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"' |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
210 ', ' |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
211 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"') |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
212 s = FakeSocket(text) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
213 r = httplib.HTTPResponse(s) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
214 r.begin() |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
215 cookies = r.getheader("Set-Cookie") |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
216 if cookies != hdr: |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
217 self.fail("multiple headers not combined properly") |
|
33488
71ed4b35fdc4
Search from the end of the host/port combination to find the colon which
Skip Montanaro <skip@pobox.com>
parents:
33001
diff
changeset
|
218 |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
219 def test_read_head(self): |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
220 # Test that the library doesn't attempt to read any data |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
221 # from a HEAD request. (Tickles SF bug #622042.) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
222 sock = FakeSocket( |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
223 'HTTP/1.1 200 OK\r\n' |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
224 'Content-Length: 14432\r\n' |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
225 '\r\n', |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
226 NoEOFStringIO) |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
227 resp = httplib.HTTPResponse(sock, method="HEAD") |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
228 resp.begin() |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
229 if resp.read() != "": |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
230 self.fail("Did not expect response from HEAD request") |
|
28978
83fe0f5c9359
SF bug 622042: Don't expect response body from HEAD request.
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
27348
diff
changeset
|
231 |
|
40333
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
232 def test_send_file(self): |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
233 expected = 'GET /foo HTTP/1.1\r\nHost: example.com\r\n' \ |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
234 'Accept-Encoding: identity\r\nContent-Length:' |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
235 |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
236 body = open(__file__, 'rb') |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
237 conn = httplib.HTTPConnection('example.com') |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
238 sock = FakeSocket(body) |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
239 conn.sock = sock |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
240 conn.request('GET', '/foo', body) |
|
90d355829bbb
Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents:
40265
diff
changeset
|
241 self.assertTrue(sock.data.startswith(expected)) |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
242 |
|
56286
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
243 def test_send(self): |
|
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
244 expected = 'this is a test this is only a test' |
|
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
245 conn = httplib.HTTPConnection('example.com') |
|
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
246 sock = FakeSocket(None) |
|
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
247 conn.sock = sock |
|
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
248 conn.send(expected) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
249 self.assertEqual(expected, sock.data) |
|
56286
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
250 sock.data = '' |
|
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
251 conn.send(array.array('c', expected)) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
252 self.assertEqual(expected, sock.data) |
|
56286
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
253 sock.data = '' |
|
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
254 conn.send(StringIO.StringIO(expected)) |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
255 self.assertEqual(expected, sock.data) |
|
56286
2e2c142bbeaa
Merged revisions 75134 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
51952
diff
changeset
|
256 |
|
45493
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
257 def test_chunked(self): |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
258 chunked_start = ( |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
259 'HTTP/1.1 200 OK\r\n' |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
260 'Transfer-Encoding: chunked\r\n\r\n' |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
261 'a\r\n' |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
262 'hello worl\r\n' |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
263 '1\r\n' |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
264 'd\r\n' |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
265 ) |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
266 sock = FakeSocket(chunked_start + '0\r\n') |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
267 resp = httplib.HTTPResponse(sock, method="GET") |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
268 resp.begin() |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
269 self.assertEqual(resp.read(), 'hello world') |
|
45493
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
270 resp.close() |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
271 |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
272 for x in ('', 'foo\r\n'): |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
273 sock = FakeSocket(chunked_start + x) |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
274 resp = httplib.HTTPResponse(sock, method="GET") |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
275 resp.begin() |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
276 try: |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
277 resp.read() |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
278 except httplib.IncompleteRead, i: |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
279 self.assertEqual(i.partial, 'hello world') |
|
51952
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
280 self.assertEqual(repr(i),'IncompleteRead(11 bytes read)') |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
281 self.assertEqual(str(i),'IncompleteRead(11 bytes read)') |
|
45493
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
282 else: |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
283 self.fail('IncompleteRead expected') |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
284 finally: |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
285 resp.close() |
|
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
286 |
|
61057
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
287 def test_chunked_head(self): |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
288 chunked_start = ( |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
289 'HTTP/1.1 200 OK\r\n' |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
290 'Transfer-Encoding: chunked\r\n\r\n' |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
291 'a\r\n' |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
292 'hello world\r\n' |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
293 '1\r\n' |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
294 'd\r\n' |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
295 ) |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
296 sock = FakeSocket(chunked_start + '0\r\n') |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
297 resp = httplib.HTTPResponse(sock, method="HEAD") |
|
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
298 resp.begin() |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
299 self.assertEqual(resp.read(), '') |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
300 self.assertEqual(resp.status, 200) |
|
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
301 self.assertEqual(resp.reason, 'OK') |
|
62070
8245683e6ec6
Merged revisions 81691 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
61057
diff
changeset
|
302 self.assertTrue(resp.isclosed()) |
|
61057
1e1cc672ebdd
Merged revisions 80583 via svnmerge from
Senthil Kumaran <orsenthil@gmail.com>
parents:
56286
diff
changeset
|
303 |
|
45494
7efa4d1bced3
#1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents:
45493
diff
changeset
|
304 def test_negative_content_length(self): |
|
50004
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
305 sock = FakeSocket('HTTP/1.1 200 OK\r\n' |
|
3c7eef187bea
Merged revisions 67398,67423-67424,67432,67440-67441,67444-67445,67454,67457,67463 via svnmerge from
Georg Brandl <georg@python.org>
parents:
47372
diff
changeset
|
306 'Content-Length: -1\r\n\r\nHello\r\n') |
|
45494
7efa4d1bced3
#1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents:
45493
diff
changeset
|
307 resp = httplib.HTTPResponse(sock, method="GET") |
|
7efa4d1bced3
#1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents:
45493
diff
changeset
|
308 resp.begin() |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
309 self.assertEqual(resp.read(), 'Hello\r\n') |
|
45494
7efa4d1bced3
#1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents:
45493
diff
changeset
|
310 resp.close() |
|
7efa4d1bced3
#1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents:
45493
diff
changeset
|
311 |
|
51952
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
312 def test_incomplete_read(self): |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
313 sock = FakeSocket('HTTP/1.1 200 OK\r\nContent-Length: 10\r\n\r\nHello\r\n') |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
314 resp = httplib.HTTPResponse(sock, method="GET") |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
315 resp.begin() |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
316 try: |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
317 resp.read() |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
318 except httplib.IncompleteRead as i: |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
319 self.assertEqual(i.partial, 'Hello\r\n') |
|
51952
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
320 self.assertEqual(repr(i), |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
321 "IncompleteRead(7 bytes read, 3 more expected)") |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
322 self.assertEqual(str(i), |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
323 "IncompleteRead(7 bytes read, 3 more expected)") |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
324 else: |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
325 self.fail('IncompleteRead expected') |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
326 finally: |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
327 resp.close() |
|
2d69d0d3a9ca
Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents:
50004
diff
changeset
|
328 |
|
63194
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
329 def test_epipe(self): |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
330 sock = EPipeSocket( |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
331 "HTTP/1.0 401 Authorization Required\r\n" |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
332 "Content-type: text/html\r\n" |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
333 "WWW-Authenticate: Basic realm=\"example\"\r\n", |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
334 b"Content-Length") |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
335 conn = httplib.HTTPConnection("example.com") |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
336 conn.sock = sock |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
337 self.assertRaises(socket.error, |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
338 lambda: conn.request("PUT", "/url", "body")) |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
339 resp = conn.getresponse() |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
340 self.assertEqual(401, resp.status) |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
341 self.assertEqual("Basic realm=\"example\"", |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
342 resp.getheader("www-authenticate")) |
|
8072dfd97e1d
Forward port r70643 (#5542) + part of r83120 (just remove the comment)
Victor Stinner <victor.stinner@haypocalc.com>
parents:
62068
diff
changeset
|
343 |
|
64862
2c73e6ce6a75
Fix Issue1327971: HTTPResponse should expose a proper fileno attribute
Senthil Kumaran <orsenthil@gmail.com>
parents:
63194
diff
changeset
|
344 def test_filenoattr(self): |
|
2c73e6ce6a75
Fix Issue1327971: HTTPResponse should expose a proper fileno attribute
Senthil Kumaran <orsenthil@gmail.com>
parents:
63194
diff
changeset
|
345 # Just test the fileno attribute in the HTTPResponse Object. |
|
2c73e6ce6a75
Fix Issue1327971: HTTPResponse should expose a proper fileno attribute
Senthil Kumaran <orsenthil@gmail.com>
parents:
63194
diff
changeset
|
346 body = "HTTP/1.1 200 Ok\r\n\r\nText" |
|
2c73e6ce6a75
Fix Issue1327971: HTTPResponse should expose a proper fileno attribute
Senthil Kumaran <orsenthil@gmail.com>
parents:
63194
diff
changeset
|
347 sock = FakeSocket(body) |
|
2c73e6ce6a75
Fix Issue1327971: HTTPResponse should expose a proper fileno attribute
Senthil Kumaran <orsenthil@gmail.com>
parents:
63194
diff
changeset
|
348 resp = httplib.HTTPResponse(sock) |
|
2c73e6ce6a75
Fix Issue1327971: HTTPResponse should expose a proper fileno attribute
Senthil Kumaran <orsenthil@gmail.com>
parents:
63194
diff
changeset
|
349 self.assertTrue(hasattr(resp,'fileno'), |
|
2c73e6ce6a75
Fix Issue1327971: HTTPResponse should expose a proper fileno attribute
Senthil Kumaran <orsenthil@gmail.com>
parents:
63194
diff
changeset
|
350 'HTTPResponse should expose a fileno attribute') |
|
45493
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
351 |
|
67030
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
352 # Test lines overflowing the max line size (_MAXLINE in http.client) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
353 |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
354 def test_overflowing_status_line(self): |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
355 self.skipTest("disabled for HTTP 0.9 support") |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
356 body = "HTTP/1.1 200 Ok" + "k" * 65536 + "\r\n" |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
357 resp = httplib.HTTPResponse(FakeSocket(body)) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
358 self.assertRaises((httplib.LineTooLong, httplib.BadStatusLine), resp.begin) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
359 |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
360 def test_overflowing_header_line(self): |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
361 body = ( |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
362 'HTTP/1.1 200 OK\r\n' |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
363 'X-Foo: bar' + 'r' * 65536 + '\r\n\r\n' |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
364 ) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
365 resp = httplib.HTTPResponse(FakeSocket(body)) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
366 self.assertRaises(httplib.LineTooLong, resp.begin) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
367 |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
368 def test_overflowing_chunked_line(self): |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
369 body = ( |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
370 'HTTP/1.1 200 OK\r\n' |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
371 'Transfer-Encoding: chunked\r\n\r\n' |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
372 + '0' * 65536 + 'a\r\n' |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
373 'hello world\r\n' |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
374 '0\r\n' |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
375 ) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
376 resp = httplib.HTTPResponse(FakeSocket(body)) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
377 resp.begin() |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
378 self.assertRaises(httplib.LineTooLong, resp.read) |
|
965314f12f71
Merged revisions 87373,87381 via svnmerge from
Antoine Pitrou <solipsis@pitrou.net>
parents:
66348
diff
changeset
|
379 |
|
76603
685c1db976c4
httplib test for early eof response. related to Issue13684
Senthil Kumaran <senthil@uthcode.com>
parents:
73135
diff
changeset
|
380 def test_early_eof(self): |
|
685c1db976c4
httplib test for early eof response. related to Issue13684
Senthil Kumaran <senthil@uthcode.com>
parents:
73135
diff
changeset
|
381 # Test httpresponse with no \r\n termination, |
|
685c1db976c4
httplib test for early eof response. related to Issue13684
Senthil Kumaran <senthil@uthcode.com>
parents:
73135
diff
changeset
|
382 body = "HTTP/1.1 200 Ok" |
|
685c1db976c4
httplib test for early eof response. related to Issue13684
Senthil Kumaran <senthil@uthcode.com>
parents:
73135
diff
changeset
|
383 sock = FakeSocket(body) |
|
685c1db976c4
httplib test for early eof response. related to Issue13684
Senthil Kumaran <senthil@uthcode.com>
parents:
73135
diff
changeset
|
384 resp = httplib.HTTPResponse(sock) |
|
685c1db976c4
httplib test for early eof response. related to Issue13684
Senthil Kumaran <senthil@uthcode.com>
parents:
73135
diff
changeset
|
385 resp.begin() |
|
685c1db976c4
httplib test for early eof response. related to Issue13684
Senthil Kumaran <senthil@uthcode.com>
parents:
73135
diff
changeset
|
386 self.assertEqual(resp.read(), '') |
|
685c1db976c4
httplib test for early eof response. related to Issue13684
Senthil Kumaran <senthil@uthcode.com>
parents:
73135
diff
changeset
|
387 self.assertTrue(resp.isclosed()) |
|
45493
21a5e9a5483a
#900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents:
45468
diff
changeset
|
388 |
|
36560
9a8396f460af
Move test case for HTTP response dict to httplib.
Georg Brandl <georg@python.org>
parents:
33497
diff
changeset
|
389 class OfflineTest(TestCase): |
|
9a8396f460af
Move test case for HTTP response dict to httplib.
Georg Brandl <georg@python.org>
parents:
33497
diff
changeset
|
390 def test_responses(self): |
|
66348
614c84670c63
Merged revisions 86596 via svnmerge from
Ezio Melotti <ezio.melotti@gmail.com>
parents:
66197
diff
changeset
|
391 self.assertEqual(httplib.responses[httplib.NOT_FOUND], "Not Found") |
|
36560
9a8396f460af
Move test case for HTTP response dict to httplib.
Georg Brandl <georg@python.org>
parents:
33497
diff
changeset
|
392 |
|
58113
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
393 |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
394 class SourceAddressTest(TestCase): |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
395 def setUp(self): |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
396 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
397 self.port = test_support.bind_port(self.serv) |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
398 self.source_port = test_support.find_unused_port() |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
399 self.serv.listen(5) |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
400 self.conn = None |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
401 |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
402 def tearDown(self): |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
403 if self.conn: |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
404 self.conn.close() |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
405 self.conn = None |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
406 self.serv.close() |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
407 self.serv = None |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
408 |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
409 def testHTTPConnectionSourceAddress(self): |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
410 self.conn = httplib.HTTPConnection(HOST, self.port, |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
411 source_address=('', self.source_port)) |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
412 self.conn.connect() |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
413 self.assertEqual(self.conn.sock.getsockname()[1], self.source_port) |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
414 |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
415 @unittest.skipIf(not hasattr(httplib, 'HTTPSConnection'), |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
416 'httplib.HTTPSConnection not defined') |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
417 def testHTTPSConnectionSourceAddress(self): |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
418 self.conn = httplib.HTTPSConnection(HOST, self.port, |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
419 source_address=('', self.source_port)) |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
420 # We don't test anything here other the constructor not barfing as |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
421 # this code doesn't deal with setting up an active running SSL server |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
422 # for an ssl_wrapped connect() to actually return from. |
|
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
423 |
|
36560
9a8396f460af
Move test case for HTTP response dict to httplib.
Georg Brandl <georg@python.org>
parents:
33497
diff
changeset
|
424 |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
425 class TimeoutTest(TestCase): |
|
46362
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
45494
diff
changeset
|
426 PORT = None |
|
41532
4b9fa8092e82
Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents:
41301
diff
changeset
|
427 |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
428 def setUp(self): |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
429 self.serv = socket.socket(socket.AF_INET, socket.SOCK_STREAM) |
|
46365
7fd274a58d5c
Fix typo with regards to self.PORT shadowing class variables with the same name.
Trent Nelson <trent.nelson@snakebite.org>
parents:
46362
diff
changeset
|
430 TimeoutTest.PORT = test_support.bind_port(self.serv) |
|
41301
3c0913b65e8d
Closing the HTTP connection after each test, and listening more.
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
431 self.serv.listen(5) |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
432 |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
433 def tearDown(self): |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
434 self.serv.close() |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
435 self.serv = None |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
436 |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
437 def testTimeoutAttribute(self): |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
438 '''This will prove that the timeout gets through |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
439 HTTPConnection and into the socket. |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
440 ''' |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
441 # default -- use global socket timeout |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51952
diff
changeset
|
442 self.assertTrue(socket.getdefaulttimeout() is None) |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
443 socket.setdefaulttimeout(30) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
444 try: |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
445 httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
446 httpConn.connect() |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
447 finally: |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
448 socket.setdefaulttimeout(None) |
|
41292
18995bd8bc8b
Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents:
41288
diff
changeset
|
449 self.assertEqual(httpConn.sock.gettimeout(), 30) |
|
41301
3c0913b65e8d
Closing the HTTP connection after each test, and listening more.
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
450 httpConn.close() |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
451 |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
452 # no timeout -- do not use global socket default |
|
55002
6768b2350c57
convert usage of fail* to assert*
Benjamin Peterson <benjamin@python.org>
parents:
51952
diff
changeset
|
453 self.assertTrue(socket.getdefaulttimeout() is None) |
|
41292
18995bd8bc8b
Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents:
41288
diff
changeset
|
454 socket.setdefaulttimeout(30) |
|
18995bd8bc8b
Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents:
41288
diff
changeset
|
455 try: |
|
46365
7fd274a58d5c
Fix typo with regards to self.PORT shadowing class variables with the same name.
Trent Nelson <trent.nelson@snakebite.org>
parents:
46362
diff
changeset
|
456 httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, |
|
7fd274a58d5c
Fix typo with regards to self.PORT shadowing class variables with the same name.
Trent Nelson <trent.nelson@snakebite.org>
parents:
46362
diff
changeset
|
457 timeout=None) |
|
41292
18995bd8bc8b
Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents:
41288
diff
changeset
|
458 httpConn.connect() |
|
18995bd8bc8b
Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents:
41288
diff
changeset
|
459 finally: |
|
47372
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
460 socket.setdefaulttimeout(None) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
461 self.assertEqual(httpConn.sock.gettimeout(), None) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
462 httpConn.close() |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
463 |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
464 # a value |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
465 httpConn = httplib.HTTPConnection(HOST, TimeoutTest.PORT, timeout=30) |
|
e2c5ae30cffe
Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents:
46365
diff
changeset
|
466 httpConn.connect() |
|
41292
18995bd8bc8b
Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents:
41288
diff
changeset
|
467 self.assertEqual(httpConn.sock.gettimeout(), 30) |
|
41301
3c0913b65e8d
Closing the HTTP connection after each test, and listening more.
Facundo Batista <facundobatista@gmail.com>
parents:
41292
diff
changeset
|
468 httpConn.close() |
|
41288
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
469 |
|
a73aa0b821ca
Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents:
40333
diff
changeset
|
470 |
|
41802
81b178f0b1ef
Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents:
41532
diff
changeset
|
471 class HTTPSTimeoutTest(TestCase): |
|
81b178f0b1ef
Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents:
41532
diff
changeset
|
472 # XXX Here should be tests for HTTPS, there isn't any right now! |
|
81b178f0b1ef
Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents:
41532
diff
changeset
|
473 |
|
81b178f0b1ef
Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents:
41532
diff
changeset
|
474 def test_attributes(self): |
|
81b178f0b1ef
Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents:
41532
diff
changeset
|
475 # simple test to check it's storing it |
|
43098
54def18eb31f
One more fix to consider missing SSL support.
Thomas Wouters <thomas@python.org>
parents:
41802
diff
changeset
|
476 if hasattr(httplib, 'HTTPSConnection'): |
|
46362
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
45494
diff
changeset
|
477 h = httplib.HTTPSConnection(HOST, TimeoutTest.PORT, timeout=30) |
|
43098
54def18eb31f
One more fix to consider missing SSL support.
Thomas Wouters <thomas@python.org>
parents:
41802
diff
changeset
|
478 self.assertEqual(h.timeout, 30) |
|
41802
81b178f0b1ef
Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents:
41532
diff
changeset
|
479 |
|
73135
e0499b2b28aa
Issue #10860: Skip the new test if HTTPS is not available
Petri Lehtinen <petri@digip.org>
parents:
72974
diff
changeset
|
480 @unittest.skipIf(not hasattr(httplib, 'HTTPS'), 'httplib.HTTPS not available') |
|
72974
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
481 def test_host_port(self): |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
482 # Check invalid host_port |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
483 |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
484 # Note that httplib does not accept user:password@ in the host-port. |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
485 for hp in ("www.python.org:abc", "user:password@www.python.org"): |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
486 self.assertRaises(httplib.InvalidURL, httplib.HTTP, hp) |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
487 |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
488 for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b", |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
489 8000), |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
490 ("pypi.python.org:443", "pypi.python.org", 443), |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
491 ("pypi.python.org", "pypi.python.org", 443), |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
492 ("pypi.python.org:", "pypi.python.org", 443), |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
493 ("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 443)): |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
494 http = httplib.HTTPS(hp) |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
495 c = http._conn |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
496 if h != c.host: |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
497 self.fail("Host incorrectly parsed: %s != %s" % (h, c.host)) |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
498 if p != c.port: |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
499 self.fail("Port incorrectly parsed: %s != %s" % (p, c.host)) |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
500 |
|
a3e48273dce3
Fixes #10860: Handle empty port after port delimiter in httplib
Łukasz Langa <lukasz@langa.pl>
parents:
68256
diff
changeset
|
501 |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
502 def test_main(verbose=None): |
|
46362
d917da2b1946
- Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents:
45494
diff
changeset
|
503 test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest, |
|
58113
a19ad299095f
issue3972: HTTPConnection and HTTPSConnection now support a
Gregory P. Smith <greg@mad-scientist.com>
parents:
56285
diff
changeset
|
504 HTTPSTimeoutTest, SourceAddressTest) |
|
33001
4fd59651fe3f
SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
29778
diff
changeset
|
505 |
|
40265
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
506 if __name__ == '__main__': |
|
c3707da80aef
Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents:
36560
diff
changeset
|
507 test_main() |
