update tests for relative redirects

This commit is contained in:
David Lord 2022-03-25 11:48:26 -07:00
parent ce7b884b73
commit 81c7ff7f8d
No known key found for this signature in database
GPG Key ID: 7A1C87E3F5BC42A8
6 changed files with 20 additions and 9 deletions

View File

@ -52,6 +52,9 @@ Unreleased
:issue:`4095, 4295, 4297` :issue:`4095, 4295, 4297`
- Fix typing for ``__exit__`` methods for better compatibility with - Fix typing for ``__exit__`` methods for better compatibility with
``ExitStack``. :issue:`4474` ``ExitStack``. :issue:`4474`
- From Werkzeug, for redirect responses the ``Location`` header URL
will remain relative, and exclude the scheme and domain, by default.
:pr:`4496`
Version 2.0.3 Version 2.0.3

View File

@ -266,7 +266,7 @@ messages.
response = client.post( response = client.post(
'/auth/register', data={'username': 'a', 'password': 'a'} '/auth/register', data={'username': 'a', 'password': 'a'}
) )
assert 'http://localhost/auth/login' == response.headers['Location'] assert response.headers["Location"] == "/auth/login"
with app.app_context(): with app.app_context():
assert get_db().execute( assert get_db().execute(
@ -319,7 +319,7 @@ The tests for the ``login`` view are very similar to those for
def test_login(client, auth): def test_login(client, auth):
assert client.get('/auth/login').status_code == 200 assert client.get('/auth/login').status_code == 200
response = auth.login() response = auth.login()
assert response.headers['Location'] == 'http://localhost/' assert response.headers["Location"] == "/"
with client: with client:
client.get('/') client.get('/')
@ -404,7 +404,7 @@ is returned. If a ``post`` with the given ``id`` doesn't exist,
)) ))
def test_login_required(client, path): def test_login_required(client, path):
response = client.post(path) response = client.post(path)
assert response.headers['Location'] == 'http://localhost/auth/login' assert response.headers["Location"] == "/auth/login"
def test_author_required(app, client, auth): def test_author_required(app, client, auth):
@ -479,7 +479,7 @@ no longer exist in the database.
def test_delete(client, auth, app): def test_delete(client, auth, app):
auth.login() auth.login()
response = client.post('/1/delete') response = client.post('/1/delete')
assert response.headers['Location'] == 'http://localhost/' assert response.headers["Location"] == "/"
with app.app_context(): with app.app_context():
db = get_db() db = get_db()

View File

@ -11,7 +11,7 @@ def test_register(client, app):
# test that successful registration redirects to the login page # test that successful registration redirects to the login page
response = client.post("/auth/register", data={"username": "a", "password": "a"}) response = client.post("/auth/register", data={"username": "a", "password": "a"})
assert "http://localhost/auth/login" == response.headers["Location"] assert response.headers["Location"] == "/auth/login"
# test that the user was inserted into the database # test that the user was inserted into the database
with app.app_context(): with app.app_context():
@ -42,7 +42,7 @@ def test_login(client, auth):
# test that successful login redirects to the index page # test that successful login redirects to the index page
response = auth.login() response = auth.login()
assert response.headers["Location"] == "http://localhost/" assert response.headers["Location"] == "/"
# login request set the user_id in the session # login request set the user_id in the session
# check that the user is loaded from the session # check that the user is loaded from the session

View File

@ -19,7 +19,7 @@ def test_index(client, auth):
@pytest.mark.parametrize("path", ("/create", "/1/update", "/1/delete")) @pytest.mark.parametrize("path", ("/create", "/1/update", "/1/delete"))
def test_login_required(client, path): def test_login_required(client, path):
response = client.post(path) response = client.post(path)
assert response.headers["Location"] == "http://localhost/auth/login" assert response.headers["Location"] == "/auth/login"
def test_author_required(app, client, auth): def test_author_required(app, client, auth):
@ -75,7 +75,7 @@ def test_create_update_validate(client, auth, path):
def test_delete(client, auth, app): def test_delete(client, auth, app):
auth.login() auth.login()
response = client.post("/1/delete") response = client.post("/1/delete")
assert response.headers["Location"] == "http://localhost/" assert response.headers["Location"] == "/"
with app.app_context(): with app.app_context():
db = get_db() db = get_db()

View File

@ -155,6 +155,8 @@ class Response(ResponseBase):
json_module = json json_module = json
autocorrect_location_header = False
@property @property
def max_cookie_size(self) -> int: # type: ignore def max_cookie_size(self) -> int: # type: ignore
"""Read-only view of the :data:`MAX_COOKIE_SIZE` config key. """Read-only view of the :data:`MAX_COOKIE_SIZE` config key.

View File

@ -19,6 +19,12 @@ def test_aborting(app):
with app.test_client() as c: with app.test_client() as c:
rv = c.get("/") rv = c.get("/")
assert rv.headers["Location"] == "http://localhost/test" location_parts = rv.headers["Location"].rpartition("/")
if location_parts[0]:
# For older Werkzeug that used absolute redirects.
assert location_parts[0] == "http://localhost"
assert location_parts[2] == "test"
rv = c.get("/test") rv = c.get("/test")
assert rv.data == b"42" assert rv.data == b"42"