5 from test
.test_support
import verify
,verbose
8 def __init__(self
, text
, fileclass
=StringIO
.StringIO
):
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.
24 data
= StringIO
.StringIO
.read(self
, n
)
26 raise AssertionError('caller tried to read past EOF')
29 def readline(self
, length
=None):
30 data
= StringIO
.StringIO
.readline(self
, length
)
32 raise AssertionError('caller tried to read past EOF')
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.
40 buf
= StringIO
.StringIO()
48 # print individual lines with endings stripped
50 for line
in s
.split("\n"):
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)
63 body
= "HTTP/1.1 400.100 Not Ok\r\n\r\nText"
64 sock
= FakeSocket(body
)
65 resp
= httplib
.HTTPResponse(sock
, 1)
68 except httplib
.BadStatusLine
:
69 print "BadStatusLine raised as expected"
71 print "Expect BadStatusLine"
73 # Check invalid host_port
75 for hp
in ("www.python.org:abc", "www.python.org:"):
78 except httplib
.InvalidURL
:
79 print "InvalidURL raised as expected"
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";'
90 hdr
= ('Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"'
92 'Part_Number="Rocket_Launcher_0001"; Version="1"; Path="/acme"')
94 r
= httplib
.HTTPResponse(s
, 1)
96 cookies
= r
.getheader("Set-Cookie")
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.)
103 'HTTP/1.1 200 OK\r\n'
104 'Content-Length: 14432\r\n'
107 resp
= httplib
.HTTPResponse(sock
, 1, method
="HEAD")
109 if resp
.read() != "":
110 raise AssertionError, "Did not expect response from HEAD request"