annotate Lib/test/test_httplib.py @ 54294:dba767bc3155 legacy-trunk

remove mention of old ctypes version
author Benjamin Peterson <benjamin@python.org>
date Sat, 23 May 2009 20:59:09 +0000
parents 2d69d0d3a9ca
children 2e2c142bbeaa 6768b2350c57
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
17483
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
1 import httplib
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
2 import StringIO
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
3 import socket
29778
ce988394c754 Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28978
diff changeset
4
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
5 from unittest import TestCase
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
6
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
7 from test import test_support
17483
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
8
46362
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 45494
diff changeset
9 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
10
17483
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
11 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
12 def __init__(self, text, fileclass=StringIO.StringIO):
17483
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
13 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
14 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
15 self.data = ''
17483
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
16
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
17 def sendall(self, data):
40333
90d355829bbb Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents: 40265
diff changeset
18 self.data += data
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
19
17483
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
20 def makefile(self, mode, bufsize=None):
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
21 if mode != 'r' and mode != 'rb':
22738
826bc1522d22 Use attributes appropriately
Neal Norwitz <nnorwitz@gmail.com>
parents: 22495
diff changeset
22 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
23 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
24
ce988394c754 Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28978
diff changeset
25 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
26 """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
27
ce988394c754 Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28978
diff changeset
28 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
29 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
30 """
ce988394c754 Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28978
diff changeset
31 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
32 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
33 if data == '':
ce988394c754 Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28978
diff changeset
34 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
35 return data
ce988394c754 Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28978
diff changeset
36
ce988394c754 Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28978
diff changeset
37 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
38 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
39 if data == '':
ce988394c754 Fix SF bug 764095: Don't use network in test_httplib.
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 28978
diff changeset
40 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
41 return data
17483
71752a41b57f Add test for SF bug #405427
Jeremy Hylton <jeremy@alum.mit.edu>
parents:
diff changeset
42
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
43
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
44 class HeaderTests(TestCase):
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
45 def test_auto_headers(self):
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
46 # 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
47 # .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
48
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
49 import httplib
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
50
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
51 class HeaderCountingBuffer(list):
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
52 def __init__(self):
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
53 self.count = {}
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
54 def append(self, item):
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
55 kv = item.split(':')
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
56 if len(kv) > 1:
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
57 # 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
58 lcKey = kv[0].lower()
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
59 self.count.setdefault(lcKey, 0)
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
60 self.count[lcKey] += 1
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
61 list.append(self, item)
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 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
64 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
65 conn = httplib.HTTPConnection('example.com')
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
66 conn.sock = FakeSocket('blahblahblah')
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
67 conn._buffer = HeaderCountingBuffer()
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
68
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
69 body = 'spamspamspam'
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
70 headers = {}
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
71 if explicit_header:
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
72 headers[header] = str(len(body))
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
73 conn.request('POST', '/', body, headers)
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
74 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
75
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
76 class BasicTest(TestCase):
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
77 def test_status_lines(self):
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
78 # 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
79
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
80 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
81 sock = FakeSocket(body)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
82 resp = httplib.HTTPResponse(sock)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
83 resp.begin()
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
84 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
85 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
86
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
87 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
88 sock = FakeSocket(body)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
89 resp = httplib.HTTPResponse(sock)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
90 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
91
43584
da44334ebea2 Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents: 43098
diff changeset
92 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
93 # 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
94 # 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
95 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
96 sock = FakeSocket(body)
da44334ebea2 Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents: 43098
diff changeset
97 resp = httplib.HTTPResponse(sock)
da44334ebea2 Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents: 43098
diff changeset
98 resp.begin()
da44334ebea2 Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents: 43098
diff changeset
99 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
100 self.assertFalse(resp.isclosed())
da44334ebea2 Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents: 43098
diff changeset
101 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
102 self.assertTrue(resp.isclosed())
da44334ebea2 Issue #1580738. When HTTPConnection reads the whole stream with read(),
Facundo Batista <facundobatista@gmail.com>
parents: 43098
diff changeset
103
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
104 def test_host_port(self):
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
105 # Check invalid host_port
22495
91f9a24887cb add test of InvalidURL
Skip Montanaro <skip@pobox.com>
parents: 20089
diff changeset
106
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
107 for hp in ("www.python.org:abc", "www.python.org:"):
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
108 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
109
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
110 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
111 8000),
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
112 ("www.python.org:80", "www.python.org", 80),
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
113 ("www.python.org", "www.python.org", 80),
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
114 ("[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
115 http = httplib.HTTP(hp)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
116 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
117 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
118 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
119 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
120 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
121
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
122 def test_response_headers(self):
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
123 # 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
124 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
125 '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
126 ' Version="1"; Path="/acme"\r\n'
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
127 '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
128 ' Path="/acme"\r\n'
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
129 '\r\n'
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
130 'No body\r\n')
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
131 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
132 ', '
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
133 '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
134 s = FakeSocket(text)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
135 r = httplib.HTTPResponse(s)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
136 r.begin()
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
137 cookies = r.getheader("Set-Cookie")
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
138 if cookies != hdr:
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
139 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
140
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
141 def test_read_head(self):
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
142 # 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
143 # from a HEAD request. (Tickles SF bug #622042.)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
144 sock = FakeSocket(
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
145 'HTTP/1.1 200 OK\r\n'
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
146 'Content-Length: 14432\r\n'
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
147 '\r\n',
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
148 NoEOFStringIO)
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
149 resp = httplib.HTTPResponse(sock, method="HEAD")
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
150 resp.begin()
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
151 if resp.read() != "":
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
152 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
153
40333
90d355829bbb Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents: 40265
diff changeset
154 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
155 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
156 '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
157
90d355829bbb Patch #1065257: Support passing open files as body in
Martin v. Löwis <martin@v.loewis.de>
parents: 40265
diff changeset
158 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
159 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
160 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
161 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
162 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
163 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
164
45493
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
165 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
166 chunked_start = (
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
167 '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
168 '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
169 '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
170 '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
171 '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
172 '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
173 )
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
174 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
175 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
176 resp.begin()
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
177 self.assertEquals(resp.read(), 'hello world')
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
178 resp.close()
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
179
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
180 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
181 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
182 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
183 resp.begin()
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
184 try:
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
185 resp.read()
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
186 except httplib.IncompleteRead, i:
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
187 self.assertEquals(i.partial, 'hello world')
51952
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
188 self.assertEqual(repr(i),'IncompleteRead(11 bytes read)')
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
189 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
190 else:
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
191 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
192 finally:
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
193 resp.close()
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
194
45494
7efa4d1bced3 #1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents: 45493
diff changeset
195 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
196 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
197 '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
198 resp = httplib.HTTPResponse(sock, method="GET")
7efa4d1bced3 #1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents: 45493
diff changeset
199 resp.begin()
7efa4d1bced3 #1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents: 45493
diff changeset
200 self.assertEquals(resp.read(), 'Hello\r\n')
7efa4d1bced3 #1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents: 45493
diff changeset
201 resp.close()
7efa4d1bced3 #1627: httplib now ignores negative Content-Length headers.
Georg Brandl <georg@python.org>
parents: 45493
diff changeset
202
51952
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
203 def test_incomplete_read(self):
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
204 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
205 resp = httplib.HTTPResponse(sock, method="GET")
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
206 resp.begin()
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
207 try:
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
208 resp.read()
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
209 except httplib.IncompleteRead as i:
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
210 self.assertEquals(i.partial, 'Hello\r\n')
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
211 self.assertEqual(repr(i),
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
212 "IncompleteRead(7 bytes read, 3 more expected)")
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
213 self.assertEqual(str(i),
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
214 "IncompleteRead(7 bytes read, 3 more expected)")
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
215 else:
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
216 self.fail('IncompleteRead expected')
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
217 finally:
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
218 resp.close()
2d69d0d3a9ca Merged revisions 70107 via svnmerge from
Benjamin Peterson <benjamin@python.org>
parents: 50004
diff changeset
219
45493
21a5e9a5483a #900744: If an invalid chunked-encoding header is sent by a server,
Georg Brandl <georg@python.org>
parents: 45468
diff changeset
220
36560
9a8396f460af Move test case for HTTP response dict to httplib.
Georg Brandl <georg@python.org>
parents: 33497
diff changeset
221 class OfflineTest(TestCase):
9a8396f460af Move test case for HTTP response dict to httplib.
Georg Brandl <georg@python.org>
parents: 33497
diff changeset
222 def test_responses(self):
9a8396f460af Move test case for HTTP response dict to httplib.
Georg Brandl <georg@python.org>
parents: 33497
diff changeset
223 self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found")
9a8396f460af Move test case for HTTP response dict to httplib.
Georg Brandl <georg@python.org>
parents: 33497
diff changeset
224
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
225 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
226 PORT = None
41532
4b9fa8092e82 Whitespace normalization. Ugh, we really need to do this more often.
Neal Norwitz <nnorwitz@gmail.com>
parents: 41301
diff changeset
227
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
228 def setUp(self):
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
229 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
230 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
231 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
232
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
233 def tearDown(self):
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
234 self.serv.close()
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
235 self.serv = None
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
236
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
237 def testTimeoutAttribute(self):
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
238 '''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
239 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
240 '''
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
241 # default -- use global socket timeout
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
242 self.assert_(socket.getdefaulttimeout() is None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
243 socket.setdefaulttimeout(30)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
244 try:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
245 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
246 httpConn.connect()
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
247 finally:
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
248 socket.setdefaulttimeout(None)
41292
18995bd8bc8b Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents: 41288
diff changeset
249 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
250 httpConn.close()
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
251
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
252 # no timeout -- do not use global socket default
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
253 self.assert_(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
254 socket.setdefaulttimeout(30)
18995bd8bc8b Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents: 41288
diff changeset
255 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
256 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
257 timeout=None)
41292
18995bd8bc8b Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents: 41288
diff changeset
258 httpConn.connect()
18995bd8bc8b Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents: 41288
diff changeset
259 finally:
47372
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
260 socket.setdefaulttimeout(None)
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
261 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
262 httpConn.close()
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
263
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
264 # a value
e2c5ae30cffe Fixed the semantic of timeout for socket.create_connection and
Facundo Batista <facundobatista@gmail.com>
parents: 46365
diff changeset
265 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
266 httpConn.connect()
41292
18995bd8bc8b Surrounded with try/finally to socket's default timeout setting
Facundo Batista <facundobatista@gmail.com>
parents: 41288
diff changeset
267 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
268 httpConn.close()
41288
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
269
a73aa0b821ca Added a 'create_connect()' function to socket.py, which creates a
Facundo Batista <facundobatista@gmail.com>
parents: 40333
diff changeset
270
41802
81b178f0b1ef Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents: 41532
diff changeset
271 class HTTPSTimeoutTest(TestCase):
81b178f0b1ef Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents: 41532
diff changeset
272 # 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
273
81b178f0b1ef Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents: 41532
diff changeset
274 def test_attributes(self):
81b178f0b1ef Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents: 41532
diff changeset
275 # 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
276 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
277 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
278 self.assertEqual(h.timeout, 30)
41802
81b178f0b1ef Added timeout support to HTTPSConnection, through the
Facundo Batista <facundobatista@gmail.com>
parents: 41532
diff changeset
279
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
280 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
281 test_support.run_unittest(HeaderTests, OfflineTest, BasicTest, TimeoutTest,
d917da2b1946 - Issue #2550: The approach used by client/server code for obtaining ports
Trent Nelson <trent.nelson@snakebite.org>
parents: 45494
diff changeset
282 HTTPSTimeoutTest)
33001
4fd59651fe3f SF bug 874842 and patch 997626: httplib bugs
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 29778
diff changeset
283
40265
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
284 if __name__ == '__main__':
c3707da80aef Completely convert test_httplib to unittest.
Georg Brandl <georg@python.org>
parents: 36560
diff changeset
285 test_main()