mirror of
https://github.com/python/cpython.git
synced 2025-11-01 18:51:43 +00:00
Completely convert test_httplib to unittest.
This commit is contained in:
parent
e184433654
commit
71a2089712
2 changed files with 57 additions and 97 deletions
|
|
@ -70,95 +70,68 @@ class HeaderTests(TestCase):
|
|||
conn.request('POST', '/', body, headers)
|
||||
self.assertEqual(conn._buffer.count[header.lower()], 1)
|
||||
|
||||
# Collect output to a buffer so that we don't have to cope with line-ending
|
||||
# issues across platforms. Specifically, the headers will have \r\n pairs
|
||||
# and some platforms will strip them from the output file.
|
||||
class BasicTest(TestCase):
|
||||
def test_status_lines(self):
|
||||
# Test HTTP status lines
|
||||
|
||||
def test():
|
||||
buf = StringIO.StringIO()
|
||||
_stdout = sys.stdout
|
||||
try:
|
||||
sys.stdout = buf
|
||||
_test()
|
||||
finally:
|
||||
sys.stdout = _stdout
|
||||
|
||||
# print individual lines with endings stripped
|
||||
s = buf.getvalue()
|
||||
for line in s.split("\n"):
|
||||
print line.strip()
|
||||
|
||||
def _test():
|
||||
# Test HTTP status lines
|
||||
|
||||
body = "HTTP/1.1 200 Ok\r\n\r\nText"
|
||||
sock = FakeSocket(body)
|
||||
resp = httplib.HTTPResponse(sock, 1)
|
||||
resp.begin()
|
||||
print resp.read()
|
||||
resp.close()
|
||||
|
||||
body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
|
||||
sock = FakeSocket(body)
|
||||
resp = httplib.HTTPResponse(sock, 1)
|
||||
try:
|
||||
body = "HTTP/1.1 200 Ok\r\n\r\nText"
|
||||
sock = FakeSocket(body)
|
||||
resp = httplib.HTTPResponse(sock)
|
||||
resp.begin()
|
||||
except httplib.BadStatusLine:
|
||||
print "BadStatusLine raised as expected"
|
||||
else:
|
||||
print "Expect BadStatusLine"
|
||||
self.assertEqual(resp.read(), 'Text')
|
||||
resp.close()
|
||||
|
||||
# Check invalid host_port
|
||||
body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
|
||||
sock = FakeSocket(body)
|
||||
resp = httplib.HTTPResponse(sock)
|
||||
self.assertRaises(httplib.BadStatusLine, resp.begin)
|
||||
|
||||
for hp in ("www.python.org:abc", "www.python.org:"):
|
||||
try:
|
||||
h = httplib.HTTP(hp)
|
||||
except httplib.InvalidURL:
|
||||
print "InvalidURL raised as expected"
|
||||
else:
|
||||
print "Expect InvalidURL"
|
||||
def test_host_port(self):
|
||||
# Check invalid host_port
|
||||
|
||||
for hp,h,p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b", 8000),
|
||||
("www.python.org:80", "www.python.org", 80),
|
||||
("www.python.org", "www.python.org", 80),
|
||||
("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)):
|
||||
try:
|
||||
for hp in ("www.python.org:abc", "www.python.org:"):
|
||||
self.assertRaises(httplib.InvalidURL, httplib.HTTP, hp)
|
||||
|
||||
for hp, h, p in (("[fe80::207:e9ff:fe9b]:8000", "fe80::207:e9ff:fe9b", 8000),
|
||||
("www.python.org:80", "www.python.org", 80),
|
||||
("www.python.org", "www.python.org", 80),
|
||||
("[fe80::207:e9ff:fe9b]", "fe80::207:e9ff:fe9b", 80)):
|
||||
http = httplib.HTTP(hp)
|
||||
except httplib.InvalidURL:
|
||||
print "InvalidURL raised erroneously"
|
||||
c = http._conn
|
||||
if h != c.host: raise AssertionError, ("Host incorrectly parsed", h, c.host)
|
||||
if p != c.port: raise AssertionError, ("Port incorrectly parsed", p, c.host)
|
||||
c = http._conn
|
||||
if h != c.host: self.fail("Host incorrectly parsed: %s != %s" % (h, c.host))
|
||||
if p != c.port: self.fail("Port incorrectly parsed: %s != %s" % (p, c.host))
|
||||
|
||||
# test response with multiple message headers with the same field name.
|
||||
text = ('HTTP/1.1 200 OK\r\n'
|
||||
'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\r\n'
|
||||
'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
|
||||
' Path="/acme"\r\n'
|
||||
'\r\n'
|
||||
'No body\r\n')
|
||||
hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
|
||||
', '
|
||||
'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
|
||||
s = FakeSocket(text)
|
||||
r = httplib.HTTPResponse(s, 1)
|
||||
r.begin()
|
||||
cookies = r.getheader("Set-Cookie")
|
||||
if cookies != hdr:
|
||||
raise AssertionError, "multiple headers not combined properly"
|
||||
def test_response_headers(self):
|
||||
# test response with multiple message headers with the same field name.
|
||||
text = ('HTTP/1.1 200 OK\r\n'
|
||||
'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\r\n'
|
||||
'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
|
||||
' Path="/acme"\r\n'
|
||||
'\r\n'
|
||||
'No body\r\n')
|
||||
hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
|
||||
', '
|
||||
'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
|
||||
s = FakeSocket(text)
|
||||
r = httplib.HTTPResponse(s)
|
||||
r.begin()
|
||||
cookies = r.getheader("Set-Cookie")
|
||||
if cookies != hdr:
|
||||
self.fail("multiple headers not combined properly")
|
||||
|
||||
# Test that the library doesn't attempt to read any data
|
||||
# from a HEAD request. (Tickles SF bug #622042.)
|
||||
sock = FakeSocket(
|
||||
'HTTP/1.1 200 OK\r\n'
|
||||
'Content-Length: 14432\r\n'
|
||||
'\r\n',
|
||||
NoEOFStringIO)
|
||||
resp = httplib.HTTPResponse(sock, 1, method="HEAD")
|
||||
resp.begin()
|
||||
if resp.read() != "":
|
||||
raise AssertionError, "Did not expect response from HEAD request"
|
||||
resp.close()
|
||||
def test_read_head(self):
|
||||
# Test that the library doesn't attempt to read any data
|
||||
# from a HEAD request. (Tickles SF bug #622042.)
|
||||
sock = FakeSocket(
|
||||
'HTTP/1.1 200 OK\r\n'
|
||||
'Content-Length: 14432\r\n'
|
||||
'\r\n',
|
||||
NoEOFStringIO)
|
||||
resp = httplib.HTTPResponse(sock, method="HEAD")
|
||||
resp.begin()
|
||||
if resp.read() != "":
|
||||
self.fail("Did not expect response from HEAD request")
|
||||
resp.close()
|
||||
|
||||
|
||||
class OfflineTest(TestCase):
|
||||
|
|
@ -166,7 +139,7 @@ class OfflineTest(TestCase):
|
|||
self.assertEquals(httplib.responses[httplib.NOT_FOUND], "Not Found")
|
||||
|
||||
def test_main(verbose=None):
|
||||
tests = [HeaderTests,OfflineTest]
|
||||
test_support.run_unittest(*tests)
|
||||
test_support.run_unittest(HeaderTests, OfflineTest, BasicTest)
|
||||
|
||||
test()
|
||||
if __name__ == '__main__':
|
||||
test_main()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue