Advertisement
Guest User

Untitled

a guest
Feb 20th, 2018
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. """Support for using zope.testbrowser from Zope2.
  2.  
  3. Mostly just copy and paste from zope.testbrowser.testing.
  4.  
  5. BBB: Five 1.4 includes this module (``Five/testbrowser.py``).
  6. """
  7.  
  8. import urllib2
  9.  
  10. import mechanize
  11. import ClientCookie
  12.  
  13. from zope.testbrowser import testing
  14. from zope.testbrowser import browser
  15. import zope.publisher.http
  16.  
  17.  
  18. class PublisherConnection(testing.PublisherConnection):
  19.  
  20. def __init__(self, host):
  21. from Testing.ZopeTestCase.zopedoctest.functional import http
  22. self.caller = http
  23. self.host = host
  24.  
  25. def getresponse(self):
  26. """Return a ``urllib2`` compatible response.
  27.  
  28. The goal of ths method is to convert the Zope Publisher's reseponse to
  29. a ``urllib2`` compatible response, which is also understood by
  30. mechanize.
  31. """
  32. real_response = self.response._response
  33. status = real_response.getStatus()
  34. reason = zope.publisher.http.status_reasons[real_response.status]
  35.  
  36. headers = real_response.headers.items()
  37. headers.sort()
  38. headers.insert(0, ('Status', "%s %s" % (status, reason)))
  39. headers = '\r\n'.join('%s: %s' % h for h in headers)
  40. headers += '\r\n' + '\r\n'.join(real_response._cookie_list())
  41. content = real_response.body
  42. return testing.PublisherResponse(content, headers, status, reason)
  43.  
  44.  
  45. class PublisherHTTPHandler(urllib2.HTTPHandler):
  46. """Special HTTP handler to use the Zope Publisher."""
  47.  
  48. http_request = urllib2.AbstractHTTPHandler.do_request_
  49.  
  50. def http_open(self, req):
  51. """Open an HTTP connection having a ``urllib2`` request."""
  52. # Here we connect to the publisher.
  53. return self.do_open(PublisherConnection, req)
  54.  
  55.  
  56. class PublisherMechanizeBrowser(mechanize.Browser):
  57. """Special ``mechanize`` browser using the Zope Publisher HTTP handler."""
  58.  
  59. handler_classes = {
  60. # scheme handlers
  61. "http": PublisherHTTPHandler,
  62.  
  63. "_http_error": ClientCookie.HTTPErrorProcessor,
  64. "_http_request_upgrade": ClientCookie.HTTPRequestUpgradeProcessor,
  65. "_http_default_error": urllib2.HTTPDefaultErrorHandler,
  66.  
  67. # feature handlers
  68. "_authen": urllib2.HTTPBasicAuthHandler,
  69. "_redirect": ClientCookie.HTTPRedirectHandler,
  70. "_cookies": ClientCookie.HTTPCookieProcessor,
  71. "_refresh": ClientCookie.HTTPRefreshProcessor,
  72. "_referer": mechanize.Browser.handler_classes['_referer'],
  73. "_equiv": ClientCookie.HTTPEquivProcessor,
  74. "_seek": ClientCookie.SeekableProcessor,
  75. }
  76.  
  77. default_schemes = ["http"]
  78. default_others = ["_http_error", "_http_request_upgrade",
  79. "_http_default_error"]
  80. default_features = ["_authen", "_redirect", "_cookies", "_seek"]
  81.  
  82.  
  83. class Browser(browser.Browser):
  84. """A Zope ``testbrowser` Browser that uses the Zope Publisher."""
  85.  
  86. def __init__(self, url=None):
  87. mech_browser = PublisherMechanizeBrowser()
  88. # override the http handler class
  89. mech_browser.handler_classes["http"] = PublisherHTTPHandler
  90. super(Browser, self).__init__(url=url, mech_browser=mech_browser)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement