home *** CD-ROM | disk | FTP | other *** search
/ Komputer for Alle 2004 #2 / K-CD-2-2004.ISO / OpenOffice Sv / f_0397 / python-core-2.2.2 / lib / test / test_httplib.py < prev    next >
Encoding:
Python Source  |  2003-07-18  |  1.6 KB  |  59 lines

  1. from test_support import verify,verbose
  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':
  11.             raise httplib.UnimplementedFileMode()
  12.         return StringIO.StringIO(self.text)
  13.  
  14. # Test HTTP status lines
  15.  
  16. body = "HTTP/1.1 200 Ok\n\nText"
  17. sock = FakeSocket(body)
  18. resp = httplib.HTTPResponse(sock, 1)
  19. resp.begin()
  20. print resp.read()
  21. resp.close()
  22.  
  23. body = "HTTP/1.1 400.100 Not Ok\n\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"
  32.  
  33. # Check invalid host_port
  34.  
  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"
  42.  
  43. # test response with multiple message headers with the same field name.
  44. text = ('HTTP/1.1 200 OK\n'
  45.         'Set-Cookie: Customer="WILE_E_COYOTE"; Version="1"; Path="/acme"\n'
  46.         'Set-Cookie: Part_Number="Rocket_Launcher_0001"; Version="1";'
  47.         ' Path="/acme"\n'
  48.         '\n'
  49.         'No body\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"
  59.