[twill] Access to the WSGI environment with wsgi_intercept

Ken Kuhlman ken at redlagoon.net
Thu Jan 3 13:54:35 PST 2008


I've been trying to do a better job of testing my (turbogears) web apps
lately, and am trying to decide if I should be using twill or paste.fixture.
I like a lot of things about twill, but one of the key strengths of paste
(IMHO) is that it's "in process" testing feature includes the ability to
dump the wsgi environment (unlike wsgi_intercept, which otherwise is a very
nice hack).   Access to the environ is very handy [1] when you're doing MVC
work & trying to seperate what the controller's doing from what the rendered
result looks like.
Anyway, code speaks louder than words, so I've included an example of what
I'd like to see, and a patch to twill to make it work.   I'd be interested
to know if you all think I'm on the right track or if I'm all wet.  I'm a
twill & wsgi n00b, so my apologies if I've missed anything obvious.

Thanks!
-Ken

[1] This is the best link I could find -- sorry, I've read so many of Titus'
and Ian Bicking's posts lately that my head is spinning.. :
http://groups.google.com/group/turbogears-trunk/browse_thread/thread/9ba8aec93a82c81b



Example wsgi app:

#Based off of Grig Gheorghiu's April 2006 blog entry on something Titus
showed him:
#http://agiletesting.blogspot.com/2006/04/in-
process-web-app-testing-with-twill.html
import twill
from StringIO import StringIO
def simple_app(environ, start_response):
    status = '200 OK'
    response_headers = [('Content-type','text/plain')]
    environ['simple_app'] = "WSGI's taking over!!"
    start_response(status, response_headers)
    return ['Hello world!\n']

twill.add_wsgi_intercept('localhost', 8001, lambda: simple_app)
twill.set_output(StringIO())
print twill.commands.go("http://localhost:8001/")
print twill.commands.show()
print twill.commands.showwsgienv('simple_app')

Produces the result:

http://localhost:8001/
Hello world!
WSGI's taking over!!


Hacks to twill's code to make this work:

diff -ru twill-latest/twill/commands.py twill-hacked/twill/commands.py
--- twill-latest/twill/commands.py      2008-01-02 03:05:47.000000000 -0600
+++ twill-hacked/twill/commands.py      2008-01-02 21:01:34.000000000 -0600
@@ -32,6 +32,7 @@
            'showforms',
            'showlinks',
            'showhistory',
+           'showwsgienv',
            'submit',
            'formvalue',
            'fv',
@@ -277,6 +278,16 @@
     print>>OUT, html
     return html

+def showwsgienv(key):
+    """
+    >> showwsgienv
+
+    Show the WSGI Environment.
+    """
+    from twill.wsgi_intercept import environ
+    if key:
+        environ = environ[key]
+    print>>OUT, environ
+    return environ
+
 def echo(*strs):
     """
     >> echo <list> <of> <strings>
diff -ru twill-latest/twill/wsgi_intercept.py
twill-hacked/twill/wsgi_intercept.py
--- twill-latest/twill/wsgi_intercept.py        2008-01-02 03:05:
47.000000000 -0600
+++ twill-hacked/twill/wsgi_intercept.py        2008-01-02 20:26:
06.000000000 -0600
@@ -13,6 +13,9 @@
 import traceback

 debuglevel = 0
+
+environ = dict()
+
 # 1 basic
 # 2 verbose

@@ -68,6 +71,7 @@
     # parse the input up to the first blank line (or its end).
     #

+    global environ
     environ = {}

     method_line = inp.readline()
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.idyll.org/pipermail/twill/attachments/20080103/eed673fa/attachment.htm 


More information about the twill mailing list