openfile(): Go back to opening the files in text mode. This undoes
[python/dscho.git] / Lib / test / test_httplib.py
blob4d8dbc8997645c4fe3c62d32a8f088ce81cff228
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 # Test HTTP status lines
16 body = "HTTP/1.1 200 Ok\r\n\r\nText"
17 sock = FakeSocket(body)
18 resp = httplib.HTTPResponse(sock, 1)
19 resp.begin()
20 print resp.read()
21 resp.close()
23 body = "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
24 sock = FakeSocket(body)
25 resp = httplib.HTTPResponse(sock, 1)
26 try:
27 resp.begin()
28 except httplib.BadStatusLine:
29 print "BadStatusLine raised as expected"
30 else:
31 print "Expect BadStatusLine"
33 # Check invalid host_port
35 for hp in ("www.python.org:abc", "www.python.org:"):
36 try:
37 h = httplib.HTTP(hp)
38 except httplib.InvalidURL:
39 print "InvalidURL raised as expected"
40 else:
41 print "Expect InvalidURL"
43 # test response with multiple message headers with the same field name.
44 text = ('HTTP/1.1 200 OK\r\n'
45 'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\r\n'
46 'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
47 ' Path="/acme"\r\n'
48 '\r\n'
49 'No body\r\n')
50 hdr = ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
51 ', '
52 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
53 s = FakeSocket(text)
54 r = httplib.HTTPResponse(s, 1)
55 r.begin()
56 cookies = r.getheader("Set-Cookie")
57 if cookies != hdr:
58 raise AssertionError, "multiple headers not combined properly"