Comment 1 for bug 98482

Revision history for this message
Benji York (benji) wrote :

> = Request - Entry #1 by jamesh on Feb 7, 2007 6:35 am
>
> I would like to be able to use zope.testbrowser to test a redirect to a
> remote site.

I've wanted to do this myself, but there are a couple of downsides. First, I believe this will complicate the code somewhat for what is a pretty rare need.

The other is in the class of "you shouldn't want to do that anyway". Requiring network connectivity for a test should be avoided when possible, and you don't want to test that the other site is available, only that your app redirects properly.

Here are some options (in the order I would prefer if this were my test):

* create a custom testbrowser subclass that disables the redirection plugin that urllib2 (and therefore mechanize and testbrowser) uses and then inspect the headers to ensure the redirect happens as you expect

* for this part of your test use the "old style" functional tests to make sure you get a 301 redirect to the site you want to go to.

* use the non-Zope publisher version of testbrowser for this test (this means your test setup will have to start a "real" Zope instance to listen on a port)

Here's a rough implementation of the first option (only lightly tested):

 from zope.testbrowser.testing import PublisherMechanizeBrowser
 from zope.testbrowser.browser import Browser

 my_features = PublisherMechanizeBrowser.default_features[:]
 my_features.remove('_redirect')

 class NonRedirectingMechanizeBrowser(PublisherMechanizeBrowser):
     default_features = my_features

 browser = Browser(mech_browser=NonRedirectingMechanizeBrowser())
 browser.open('http://launchpad.dev/ubuntu')
 assert browser.headers['location'] == 'redirected URL'