blob: 29f4503d47cb9cd90760b5c8de3052613f30f1b4 [file] [log] [blame]
Barry Warsaw408b6d32002-07-30 23:27:12 +00001from test.test_support import verify,verbose
Jeremy Hylton79fa2b62001-04-13 14:57:44 +00002import httplib
3import StringIO
4
5class FakeSocket:
6 def __init__(self, text):
7 self.text = text
8
9 def makefile(self, mode, bufsize=None):
10 if mode != 'r' and mode != 'rb':
Neal Norwitz28bb5722002-04-01 19:00:50 +000011 raise httplib.UnimplementedFileMode()
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000012 return StringIO.StringIO(self.text)
13
Jeremy Hyltonba603192003-01-23 18:02:20 +000014# 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.
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000017
Jeremy Hyltonba603192003-01-23 18:02:20 +000018import sys
Jeremy Hylton79fa2b62001-04-13 14:57:44 +000019
Jeremy Hyltonba603192003-01-23 18:02:20 +000020def test():
21 buf = StringIO.StringIO()
22 _stdout = sys.stdout
Skip Montanaro03ff86d2002-03-24 16:54:16 +000023 try:
Jeremy Hyltonba603192003-01-23 18:02:20 +000024 sys.stdout = buf
25 _test()
26 finally:
27 sys.stdout = _stdout
Jeremy Hylton6d0a4c72002-07-07 16:51:37 +000028
Jeremy Hyltonba603192003-01-23 18:02:20 +000029 # print individual lines with endings stripped
30 s = buf.getvalue()
31 for line in s.split("\n"):
32 print line.strip()
33
34def _test():
35 # Test HTTP status lines
36
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()
43
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"
53
54 # Check invalid host_port
55
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"
63
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"
80
Jeremy Hyltonc1b2cb92003-05-05 16:13:58 +000081 # test that the library doesn't attempt to read any data
82 # from a head request
83 conn = httplib.HTTPConnection("www.python.org")
84 conn.connect()
85 conn.request("HEAD", "/", headers={"Connection" : "keep-alive"})
86 resp = conn.getresponse()
87 if resp.status != 200:
88 raise AssertionError, "Expected status 200, got %d" % resp.status
89 if resp.read() != "":
90 raise AssertionError, "Did not expect response from HEAD request"
91 resp.close()
92 conn.close()
93
Jeremy Hyltonba603192003-01-23 18:02:20 +000094test()