Skip to content

Commit

Permalink
Merge pull request pallets#1315 from pallets/relative-location
Browse files Browse the repository at this point in the history
Relative Location is relative to current path instead of root
  • Loading branch information
davidism committed May 28, 2018
2 parents 3496bff + 8f8ced0 commit da57cd5
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 12 deletions.
6 changes: 6 additions & 0 deletions CHANGES.rst
Expand Up @@ -52,8 +52,13 @@ Unreleased
- The filenames generated by
:class:`~contrib.profiler.ProfilerMiddleware` can be customized.
(`#1283`_)
- ``Location`` autocorrection in :func:`Response.get_wsgi_headers()
<wrappers.BaseResponse.get_wsgi_headers>` is relative to the current
path rather than the root path. (`#693`_, `#718`_, `#1315`_)

.. _`#609`: https://github.com/pallets/werkzeug/pull/609
.. _`#693`: https://github.com/pallets/werkzeug/pull/693
.. _`#718`: https://github.com/pallets/werkzeug/pull/718
.. _`#724`: https://github.com/pallets/werkzeug/pull/724
.. _`#1023`: https://github.com/pallets/werkzeug/issues/1023
.. _`#1231`: https://github.com/pallets/werkzeug/issues/1231
Expand All @@ -71,6 +76,7 @@ Unreleased
.. _`#1308`: https://github.com/pallets/werkzeug/pull/1308
.. _`#1312`: https://github.com/pallets/werkzeug/pull/1312
.. _`#1314`: https://github.com/pallets/werkzeug/pull/1314
.. _`#1315`: https://github.com/pallets/werkzeug/pull/1315


Version 0.14.1
Expand Down
23 changes: 12 additions & 11 deletions tests/test_wrappers.py
Expand Up @@ -1146,18 +1146,19 @@ class MyResponse(wrappers.Response):
assert 'Content-Length' not in resp.get_wsgi_headers({})


def test_location_header_autocorrect():
env = create_environ()

class MyResponse(wrappers.Response):
autocorrect_location_header = False
resp = MyResponse('Hello World!')
resp.headers['Location'] = '/test'
assert resp.get_wsgi_headers(env)['Location'] == '/test'

@pytest.mark.parametrize(('auto', 'location', 'expect'), (
(False, '/test', '/test'),
(True, '/test', 'http://localhost/test'),
(True, 'test', 'http://localhost/a/b/test'),
(True, './test', 'http://localhost/a/b/test'),
(True, '../test', 'http://localhost/a/test'),
))
def test_location_header_autocorrect(monkeypatch, auto, location, expect):
monkeypatch.setattr(wrappers.Response, 'autocorrect_location_header', auto)
env = create_environ('/a/b/c')
resp = wrappers.Response('Hello World!')
resp.headers['Location'] = '/test'
assert resp.get_wsgi_headers(env)['Location'] == 'http://localhost/test'
resp.headers['Location'] = location
assert resp.get_wsgi_headers(env)['Location'] == expect


def test_204_and_1XX_response_has_no_content_length():
Expand Down
2 changes: 1 addition & 1 deletion werkzeug/wrappers.py
Expand Up @@ -1235,7 +1235,7 @@ def get_wsgi_headers(self, environ):
location = iri_to_uri(location, safe_conversion=True)

if self.autocorrect_location_header:
current_url = get_current_url(environ, root_only=True)
current_url = get_current_url(environ, strip_querystring=True)
if isinstance(current_url, text_type):
current_url = iri_to_uri(current_url)
location = url_join(current_url, location)
Expand Down

0 comments on commit da57cd5

Please sign in to comment.