Created
March 8, 2009 11:38
-
-
Save mgax/75735 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Zope2 functional testing using Twill | |
import sys | |
from StringIO import StringIO | |
import twill | |
from ZPublisher.Response import Response | |
from ZPublisher.Test import publish_module | |
from Testing import ZopeTestCase | |
class TwillTestCase(ZopeTestCase.PortalTestCase): | |
""" | |
Functional test - use Twill (http://twill.idyll.org/) for client-side tests | |
""" | |
wsgi_debug = False | |
def divert_twill_output(self): | |
""" the Twill browser is messy and uses globals; this will divert its output """ | |
self._twill_original_output = twill.browser.OUT | |
self.browser_output = StringIO() | |
twill.browser.OUT = self.browser_output | |
def restore_twill_output(self): | |
twill.browser.OUT = self._twill_original_output | |
def setUp(self): | |
twill.add_wsgi_intercept('localhost', 80, lambda: self.wsgi_request) | |
self.browser = twill.browser.TwillBrowser() | |
self.divert_twill_output() | |
super(TwillTestCase, self).setUp() | |
def tearDown(self): | |
super(TwillTestCase, self).tearDown() | |
self.restore_twill_output() | |
twill.remove_wsgi_intercept('localhost', 80) | |
def wsgi_request(self, environ, start_response): | |
if self.wsgi_debug: | |
print 'wsgi_request start' | |
print environ | |
print '...' | |
outstream = StringIO() | |
response = Response(stdout=outstream, stderr=sys.stderr) | |
extra = { | |
'SESSION': self.app.REQUEST.SESSION, | |
'AUTHENTICATED_USER': self.app.REQUEST.AUTHENTICATED_USER, | |
} | |
publish_module('Zope2', environ=environ, response=response, extra=extra, stdin=environ['wsgi.input']) | |
output = outstream.getvalue() | |
headers, body = output.split('\n\n', 1) | |
headers = [header.split(': ', 1) for header in headers.split('\n')] | |
status = headers.pop(0)[1] | |
if self.wsgi_debug: | |
print 'wsgi_request done, status="%s"' % status | |
print ' ' + '\n'.join([': '.join(header) for header in headers]) | |
headers = [ (header[0], ', '.join(header[1:])) for header in headers ] | |
if 'content-type' not in (header[0].lower() for header in headers): | |
headers.append( ('Content-Type', 'text/html; charset=utf-8') ) | |
start_response(status, headers) | |
return [body] | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment