Barry Warsaw | 408b6d3 | 2002-07-30 23:27:12 +0000 | [diff] [blame] | 1 | from test.test_support import verify,verbose |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 2 | import httplib |
| 3 | import StringIO |
| 4 | |
| 5 | class 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 Norwitz | 28bb572 | 2002-04-01 19:00:50 +0000 | [diff] [blame] | 11 | raise httplib.UnimplementedFileMode() |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 12 | return StringIO.StringIO(self.text) |
| 13 | |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 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. |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 17 | |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 18 | import sys |
Jeremy Hylton | 79fa2b6 | 2001-04-13 14:57:44 +0000 | [diff] [blame] | 19 | |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 20 | def test(): |
| 21 | buf = StringIO.StringIO() |
| 22 | _stdout = sys.stdout |
Skip Montanaro | 03ff86d | 2002-03-24 16:54:16 +0000 | [diff] [blame] | 23 | try: |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 24 | sys.stdout = buf |
| 25 | _test() |
| 26 | finally: |
| 27 | sys.stdout = _stdout |
Jeremy Hylton | 6d0a4c7 | 2002-07-07 16:51:37 +0000 | [diff] [blame] | 28 | |
Jeremy Hylton | ba60319 | 2003-01-23 18:02:20 +0000 | [diff] [blame] | 29 | # print individual lines with endings stripped |
| 30 | s = buf.getvalue() |
| 31 | for line in s.split("\n"): |
| 32 | print line.strip() |
| 33 | |
| 34 | def _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 | |
| 81 | test() |