Move setting of ioready 'wait' earlier in call chain, to
[python/dscho.git] / Lib / test / test_httplib.py
blob8764455ccfd07f1583fded1c5c6ccd4080d3798c
1 from test.test_support import verify,verbose
2 import httplib
3 import StringIO
5 class FakeSocket:
6 def __init__(self, text):
7 self.text = text
9 def makefile(self, mode, bufsize=None):
10 if mode != 'r' and mode != 'rb':
11 raise httplib.UnimplementedFileMode()
12 return StringIO.StringIO(self.text)
14 # Collect output to a buffer so that we don't have to cope with line-ending
15 # issues across platforms. Specifically, the headers will have \r\n pairs
16 # and some platforms will strip them from the output file.
18 import sys
20 def test():
21 buf = StringIO.StringIO()
22 _stdout = sys.stdout
23 try:
24 sys.stdout = buf
25 _test()
26 finally:
27 sys.stdout = _stdout
29 # print individual lines with endings stripped
30 s = buf.getvalue()
31 for line in s.split("\n"):
32 print line.strip()
34 def _test():
35 # Test HTTP status lines
37 body = "HTTP/1.1 200 Ok\r\n\r\nText"
38 sock = FakeSocket(body)
39 resp = httplib.HTTPResponse(sock, 1)
40 resp.begin()
41 print resp.read()
42 resp.close()
44 body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
45 sock = FakeSocket(body)
46 resp = httplib.HTTPResponse(sock, 1)
47 try:
48 resp.begin()
49 except httplib.BadStatusLine:
50 print "BadStatusLine raised as expected"
51 else:
52 print "Expect BadStatusLine"
54 # Check invalid host_port
56 for hp in ("www.python.org:abc", "www.python.org:"):
57 try:
58 h = httplib.HTTP(hp)
59 except httplib.InvalidURL:
60 print "InvalidURL raised as expected"
61 else:
62 print "Expect InvalidURL"
64 # test response with multiple message headers with the same field name.
65 text = ('HTTP/1.1 200 OK\r\n'
66 'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\r\n'
67 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
68 ' Path="/acme"\r\n'
69 '\r\n'
70 'No body\r\n')
71 hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
72 ', '
73 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
74 s = FakeSocket(text)
75 r = httplib.HTTPResponse(s, 1)
76 r.begin()
77 cookies = r.getheader("Set-Cookie")
78 if cookies != hdr:
79 raise AssertionError, "multiple headers not combined properly"
81 test()