Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 1 | import httplib |
| 2 | import StringIO |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 3 | import sys |
| 4 | |
| 5 | from test.test_support import verify,verbose |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 6 | |
| 7 | class FakeSocket: |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 8 | def __init__(self, text, fileclass=StringIO.StringIO): |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 9 | self.text = text |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 10 | self.fileclass = fileclass |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 11 | |
| 12 | def makefile(self, mode, bufsize=None): |
| 13 | if mode != 'r' and mode != 'rb': |
Neal Norwitz | 28bb572 | 2002-04-01 19:00:50 +0000 | [diff] [blame] | 14 | raise httplib.UnimplementedFileMode() |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 15 | return self.fileclass(self.text) |
| 16 | |
| 17 | class NoEOFStringIO(StringIO.StringIO): |
| 18 | """Like StringIO, but raises AssertionError on EOF. |
| 19 | |
| 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 |
| 28 | |
| 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 |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 34 | |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 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. |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 38 | |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 39 | def test(): |
| 40 | buf = StringIO.StringIO() |
| 41 | _stdout = sys.stdout |
Skip Montanaro | 03ff86d | 2002-03-24 16:54:16 +0000 | [diff] [blame] | 42 | try: |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 43 | sys.stdout = buf |
| 44 | _test() |
| 45 | finally: |
| 46 | sys.stdout = _stdout |
Jeremy Hylton | 6d0a4c7 | 2002-07-07 16:51:37 +0000 | [diff] [blame] | 47 | |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 48 | # print individual lines with endings stripped |
| 49 | s = buf.getvalue() |
| 50 | for line in s.split("\n"): |
| 51 | print line.strip() |
| 52 | |
| 53 | def _test(): |
| 54 | # Test HTTP status lines |
| 55 | |
| 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() |
| 62 | |
| 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" |
| 72 | |
| 73 | # Check invalid host_port |
| 74 | |
| 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" |
| 82 | |
| 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" |
| 99 | |
Jeremy Hylton | 121d34a | 2003-07-08 12:36:58 +0000 | [diff] [blame] | 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() |
Jeremy Hylton | c1b2cb9 | 2003-05-05 16:13:58 +0000 | [diff] [blame] | 109 | if resp.read() != "": |
| 110 | raise AssertionError, "Did not expect response from HEAD request" |
| 111 | resp.close() |
Jeremy Hylton | c1b2cb9 | 2003-05-05 16:13:58 +0000 | [diff] [blame] | 112 | |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 113 | test() |