Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test client redirect resets path info in 0.15 #1498

sekrause opened this issue Apr 1, 2019 · 2 comments


None yet
2 participants
Copy link

commented Apr 1, 2019

Pull request #1402 changes the redirect handling in werkzeug.test.Client and seems to reuse the environ from the first request to follow the redirect. This leads to problems when the application modifies the environ in-place.

I have a multi-tenant application which resolves the tenant from the first path element in a middleware with werkzeug.wsgi.pop_path_info() before passing the request to a Flask application. This behavior is recommended by the Flask documentation:

However, combining pop_path_info() and redirects will breaks tests with Werkzeug >= 0.15 because when Werkzeug makes the second request following the redirct, it will reuse an environ where the first path element is already gone.

Here is a test application which demonstrates this problem:

from flask import Flask, redirect, url_for
from werkzeug.test import Client
from werkzeug.wrappers import BaseResponse
from werkzeug.wsgi import pop_path_info

app = Flask(__name__)

def first():
    return redirect(url_for('.second'))

def second():
    return b'hello'

client = Client(app, BaseResponse)
r = client.get('/first', follow_redirects=True)
assert == b'hello'

def middleware(environ, start_response):
    print('Before pop_path_info(): %s' % environ['PATH_INFO'])
    print('After pop_path_info(): %s' % environ['PATH_INFO'])
    return app(environ, start_response)

client = Client(middleware, BaseResponse)
r = client.get('/tenant/first', follow_redirects=True)
assert == b'hello'

@davidism davidism changed the title Werkzeug >= 0.15 breaks tests which use redirects and modify the environ Test client redirect resets path info in 0.15 Apr 1, 2019


This comment has been minimized.

Copy link

commented Apr 1, 2019

Related to #1491


This comment has been minimized.

Copy link

commented Apr 1, 2019

I think we need to make a copy of the original environ to pass to the app.

@davidism davidism added this to the 0.15.2 milestone Apr 1, 2019

@davidism davidism added the bug label Apr 1, 2019

@davidism davidism closed this Apr 2, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.