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

Fix issue #44 and add a regression test. #46

Merged
merged 1 commit into from
Feb 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pex/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def content(self, link):

with contextlib.closing(self.open(link)) as fp:
encoding = message_from_string(str(fp.headers)).get_content_charset(self.DEFAULT_ENCODING)
return fp.read().decode(encoding, errors='replace')
return fp.read().decode(encoding, 'replace')


Context.register(UrllibContext)
Expand Down Expand Up @@ -222,7 +222,7 @@ def content(self, link):
raise self.Error('Context.content only works with remote URLs.')

with contextlib.closing(self.open(link)) as request:
return request.read().decode(request.encoding or self.DEFAULT_ENCODING, errors='replace')
return request.read().decode(request.encoding or self.DEFAULT_ENCODING, 'replace')


if requests:
Expand Down
20 changes: 19 additions & 1 deletion tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import pytest
from twitter.common.contextutil import temporary_file

from pex.http import Context, RequestsContext, StreamFilelike
from pex.http import Context, RequestsContext, StreamFilelike, UrllibContext
from pex.link import Link

try:
Expand Down Expand Up @@ -203,3 +203,21 @@ def test_requests_context_retries_read_timeout_retries_exhausted():

with pytest.raises(Context.Error):
context.read(Link.wrap(url))


def test_urllib_context_utf8_encoding():
BYTES = b'this is a decoded utf8 string'

with temporary_file() as tf:
tf.write(BYTES)
tf.flush()
local_link = Link.wrap(tf.name)

# Trick UrllibContext into thinking this is a remote link
class MockUrllibContext(UrllibContext):
def open(self, link):
return super(MockUrllibContext, self).open(local_link)

context = MockUrllibContext()
assert context.content(Link.wrap('http://www.google.com')) == BYTES.decode(
UrllibContext.DEFAULT_ENCODING)