Search code examples
pythonunit-testingpyramid

How to follow Pyramid redirect on tests?


I have a view which returns like:

headers = remember(request, str(user.id))
return HTTPFound(location=request.route_url('home'), headers=headers)

and I'm writing a test but how do I follow the redirect from the code above? I still get the HTTPFound object and also its response.request which is supposed to be the request which initiated the response is giving me None.

Here's what my test code looks so far:

request = testing.DummyRequest(
    post=MultiDict(email='me@gmail.com', password='random'))
response = login(request)

here, response is the HTTPFound but how do I follow the redirect to home?


Solution

  • I realize that this isn't using DummyRequest

    I would recommend functional testing because WebTest makes it easier to do, and managing.

    On the response that returns a redirect you can call follow to follow the full request.

    http://webtest.pythonpaste.org/en/latest/index.html

    redirect_response = self.testapp.post(
        '/signup', params=post_params, status=302)
    full_response = redirect_response.follow()