Skip to content

Commit

Permalink
workaround for #3712 (#3722)
Browse files Browse the repository at this point in the history
* workaround for #3712

* appease the linter

* remove hasattr call, all responses have a location, it may just be None
  • Loading branch information
ewdurbin authored Apr 16, 2018
1 parent 12603b0 commit 0878192
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
42 changes: 42 additions & 0 deletions tests/unit/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,41 @@ def _test_factory(fifo, start):
assert len(set(id(obj) for obj in objects)) == len(threads)


class TestUnicodeRedirectTween:
def test_basic_redirect(self):
response = pretend.stub(location="/a/path/to/nowhere")
handler = pretend.call_recorder(lambda request: response)
registry = pretend.stub()
tween = warehouse.http.unicode_redirect_tween_factory(
handler, registry)
request = pretend.stub(
path="/A/pAtH/tO/nOwHeRe/",
)
assert tween(request) == response

def test_unicode_basic_redirect(self):
response = pretend.stub(location="/pypi/\u2603/json/")
handler = pretend.call_recorder(lambda request: response)
registry = pretend.stub()
tween = warehouse.http.unicode_redirect_tween_factory(
handler, registry)
request = pretend.stub(
path="/pypi/snowman/json/",
)
assert tween(request).location == "/pypi/%E2%98%83/json/"

def test_not_redirect(self):
response = pretend.stub(location=None)
handler = pretend.call_recorder(lambda request: response)
registry = pretend.stub()
tween = warehouse.http.unicode_redirect_tween_factory(
handler, registry)
request = pretend.stub(
path="/wu/tang/",
)
assert tween(request) == response


def test_includeme():
config = pretend.stub(
registry=pretend.stub(
Expand All @@ -79,10 +114,17 @@ def test_includeme():
add_request_method=pretend.call_recorder(
lambda *args, **kwargs: None
),
add_tween=pretend.call_recorder(
lambda *args, **kwargs: None
),
)
warehouse.http.includeme(config)

assert len(config.add_request_method.calls) == 1
assert len(config.add_tween.calls) == 1
call = config.add_request_method.calls[0]
assert isinstance(call.args[0], warehouse.http.ThreadLocalSessionFactory)
assert call.kwargs == {"name": "http", "reify": True}
assert config.add_tween.calls == [
pretend.call("warehouse.http.unicode_redirect_tween_factory")
]
19 changes: 19 additions & 0 deletions warehouse/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import threading
import requests

from urllib.parse import quote_plus


class ThreadLocalSessionFactory:
def __init__(self, config=None):
Expand All @@ -37,8 +39,25 @@ def __call__(self, request):
return session


def unicode_redirect_tween_factory(handler, request):

def unicode_redirect_tween(request):
response = handler(request)
if response.location:
try:
response.location.encode('ascii')
except UnicodeEncodeError:
response.location = '/'.join(
[quote_plus(x) for x in response.location.split('/')])

return response

return unicode_redirect_tween


def includeme(config):
config.add_request_method(
ThreadLocalSessionFactory(config.registry.settings.get("http")),
name="http", reify=True
)
config.add_tween("warehouse.http.unicode_redirect_tween_factory")

1 comment on commit 0878192

@alhockly
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

woaaah the site looks awesome now!!

Please sign in to comment.