Skip to content

Commit

Permalink
Attempt but fail to write a test for LP #646872
Browse files Browse the repository at this point in the history
  • Loading branch information
Brian Sutherland committed Mar 16, 2011
1 parent 0e6797d commit a36dc60
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/zope/testbrowser/ftests/wsgitestapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def __call__(self, environ, start_response):
req = Request(environ)
handler = {'/set_status.html': set_status,
'/echo.html': echo,
'/redirect.html': redirect,
'/echo_one.html': echo_one,
'/set_header.html': set_header,
'/set_cookie.html': set_cookie,
Expand Down Expand Up @@ -127,6 +128,13 @@ def echo(req):
items.append('Body: %r' % body)
return Response('\n'.join(items))

def redirect(req):
loc = req.params['to']
resp = Response("You are being redirected to %s" % loc)
resp.location = loc
resp.status = int(req.params.get('type', 302))
return resp

def echo_one(req):
resp = repr(req.environ.get(req.params['var']))
return Response(resp)
Expand Down
27 changes: 27 additions & 0 deletions src/zope/testbrowser/tests/test_wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
##############################################################################

import unittest
from urllib import urlencode
from wsgiref.simple_server import demo_app

import zope.testbrowser.wsgi
Expand All @@ -29,6 +30,32 @@ def make_wsgi_app(self):

class TestBrowser(unittest.TestCase):

def test_redirect(self):
app = WSGITestApplication()
browser = zope.testbrowser.wsgi.Browser(wsgi_app=app)
# redirecting locally works
browser.open('http://localhost/redirect.html?%s'
% urlencode(dict(to='/set_status.html')))
self.assertEquals(browser.url, 'http://localhost/set_status.html')
browser.open('http://localhost/redirect.html?%s'
% urlencode(dict(to='/set_status.html', type='301')))
self.assertEquals(browser.url, 'http://localhost/set_status.html')
browser.open('http://localhost/redirect.html?%s'
% urlencode(dict(to='http://localhost/set_status.html')))
self.assertEquals(browser.url, 'http://localhost/set_status.html')
browser.open('http://localhost/redirect.html?%s'
% urlencode(dict(to='http://localhost/set_status.html', type='301')))
self.assertEquals(browser.url, 'http://localhost/set_status.html')
# non-local redirects raise HostNotAllowed error
self.assertRaises(zope.testbrowser.wsgi.HostNotAllowed,
browser.open,
'http://localhost/redirect.html?%s'
% urlencode(dict(to='http://www.google.com/')))
self.assertRaises(zope.testbrowser.wsgi.HostNotAllowed,
browser.open,
'http://localhost/redirect.html?%s'
% urlencode(dict(to='http://www.google.com/', type='301')))

def test_allowed_domains(self):
browser = zope.testbrowser.wsgi.Browser(wsgi_app=demo_app)
# external domains are not allowed
Expand Down

0 comments on commit a36dc60

Please sign in to comment.