blob: c57793ded2f14b276c5f32826ac9d2d45770bcbe [file] [log] [blame]
Jeremy Hylton79fa2b62001-04-13 14:57:44 +00001import httplib
2import StringIO
Jeremy Hylton121d34a2003-07-08 12:36:58 +00003import sys
4
5from test.test_support import verify,verbose
Jeremy Hylton79fa2b62001-04-13 14:57:44 +00006
7class FakeSocket:
Jeremy Hylton121d34a2003-07-08 12:36:58 +00008 def __init__(self, text, fileclass=StringIO.StringIO):
Jeremy Hylton79fa2b62001-04-13 14:57:44 +00009 self.text = text
Jeremy Hylton121d34a2003-07-08 12:36:58 +000010 self.fileclass = fileclass
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000011
12 def makefile(self, mode, bufsize=None):
13 if mode != 'r' and mode != 'rb':
Neal Norwitz28bb5722002-04-01 19:00:50 +000014 raise httplib.UnimplementedFileMode()
Jeremy Hylton121d34a2003-07-08 12:36:58 +000015 return self.fileclass(self.text)
16
17class NoEOFStringIO(StringIO.StringIO):
18 """Like StringIO, but raises AssertionError on EOF.
19
20 This is used below to test that httplib doesn't try to read
21 more from the underlying file than it should.
22 """
23 def read(self, n=-1):
24 data = StringIO.StringIO.read(self, n)
25 if data == '':
26 raise AssertionError('caller tried to read past EOF')
27 return data
28
29 def readline(self, length=None):
30 data = StringIO.StringIO.readline(self, length)
31 if data == '':
32 raise AssertionError('caller tried to read past EOF')
33 return data
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000034
Jeremy Hyltonba603192003-01-23 18:02:20 +000035# Collect output to a buffer so that we don't have to cope with line-ending
36# issues across platforms. Specifically, the headers will have \r\n pairs
37# and some platforms will strip them from the output file.
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000038
Jeremy Hyltonba603192003-01-23 18:02:20 +000039def test():
40 buf = StringIO.StringIO()
41 _stdout = sys.stdout
Skip Montanaro03ff86d2002-03-24 16:54:16 +000042 try:
Jeremy Hyltonba603192003-01-23 18:02:20 +000043 sys.stdout = buf
44 _test()
45 finally:
46 sys.stdout = _stdout
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +000047
Jeremy Hyltonba603192003-01-23 18:02:20 +000048 # print individual lines with endings stripped
49 s = buf.getvalue()
50 for line in s.split("\n"):
51 print line.strip()
52
53def _test():
54 # Test HTTP status lines
55
56 body = "HTTP/1.1 200 Ok\r\n\r\nText"
57 sock = FakeSocket(body)
58 resp = httplib.HTTPResponse(sock, 1)
59 resp.begin()
60 print resp.read()
61 resp.close()
62
63 body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
64 sock = FakeSocket(body)
65 resp = httplib.HTTPResponse(sock, 1)
66 try:
67 resp.begin()
68 except httplib.BadStatusLine:
69 print "BadStatusLine raised as expected"
70 else:
71 print "Expect BadStatusLine"
72
73 # Check invalid host_port
74
75 for hp in ("www.python.org:abc", "www.python.org:"):
76 try:
77 h = httplib.HTTP(hp)
78 except httplib.InvalidURL:
79 print "InvalidURL raised as expected"
80 else:
81 print "Expect InvalidURL"
82
83 # test response with multiple message headers with the same field name.
84 text = ('HTTP/1.1 200 OK\r\n'
85 'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\r\n'
86 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
87 ' Path="/acme"\r\n'
88 '\r\n'
89 'No body\r\n')
90 hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
91 ', '
92 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
93 s = FakeSocket(text)
94 r = httplib.HTTPResponse(s, 1)
95 r.begin()
96 cookies = r.getheader("Set-Cookie")
97 if cookies != hdr:
98 raise AssertionError, "multiple headers not combined properly"
99
Jeremy Hylton121d34a2003-07-08 12:36:58 +0000100 # Test that the library doesn't attempt to read any data
101 # from a HEAD request. (Tickles SF bug #622042.)
102 sock = FakeSocket(
103 'HTTP/1.1 200 OK\r\n'
104 'Content-Length: 14432\r\n'
105 '\r\n',
106 NoEOFStringIO)
107 resp = httplib.HTTPResponse(sock, 1, method="HEAD")
108 resp.begin()
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000109 if resp.read() != "":
110 raise AssertionError, "Did not expect response from HEAD request"
111 resp.close()
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +0000112
Jeremy Hyltonba603192003-01-23 18:02:20 +0000113test()