Whitespace normalization.
[python/dscho.git] / Lib / test / test_httplib.py
blobc57793ded2f14b276c5f32826ac9d2d45770bcbe
1 import httplib
2 import StringIO
3 import sys
5 from test.test_support import verify,verbose
7 class FakeSocket:
8 def __init__(self, text, fileclass=StringIO.StringIO):
9 self.text = text
10 self.fileclass = fileclass
12 def makefile(self, mode, bufsize=None):
13 if mode != 'r' and mode != 'rb':
14 raise httplib.UnimplementedFileMode()
15 return self.fileclass(self.text)
17 class NoEOFStringIO(StringIO.StringIO):
18 """Like StringIO, but raises AssertionError on EOF.
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
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
35 # 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.
39 def test():
40 buf = StringIO.StringIO()
41 _stdout = sys.stdout
42 try:
43 sys.stdout = buf
44 _test()
45 finally:
46 sys.stdout = _stdout
48 # print individual lines with endings stripped
49 s = buf.getvalue()
50 for line in s.split("\n"):
51 print line.strip()
53 def _test():
54 # Test HTTP status lines
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()
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"
73 # Check invalid host_port
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"
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"
100 # 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()
109 if resp.read() != "":
110 raise AssertionError, "Did not expect response from HEAD request"
111 resp.close()
113 test()