Skip to content

Commit

Permalink
Fix: using Response.apparent_encoding when Response.encoding is None
Browse files Browse the repository at this point in the history
  • Loading branch information
cyprienc committed Sep 17, 2020
1 parent 1f9a3a2 commit 0c6441b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
4 changes: 2 additions & 2 deletions sseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def _connect(self):
requester = self.session or requests
self.resp = requester.get(self.url, stream=True, **self.requests_kwargs)
self.resp_iterator = self.iter_content()
self.decoder = codecs.getincrementaldecoder(
self.resp.encoding)(errors='replace')
encoding = self.resp.encoding or self.resp.apparent_encoding
self.decoder = codecs.getincrementaldecoder(encoding)(errors='replace')

# TODO: Ensure we're handling redirects. Might also stick the 'origin'
# attribute on Events like the Javascript spec requires.
Expand Down
10 changes: 6 additions & 4 deletions test_sseclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,10 @@ def test_eols():


class FakeResponse(object):
def __init__(self, status_code, content, headers=None):
def __init__(self, status_code, content, headers=None, encoding="utf-8"):
self.status_code = status_code
self.encoding = "utf-8"
self.encoding = encoding
self.apparent_encoding = "utf-8"
if not isinstance(content, six.text_type):
content = content.decode("utf-8")
self.stream = content
Expand All @@ -95,9 +96,10 @@ def join_events(*events):


# Tests of parsing a multi event stream
def test_last_id_remembered(monkeypatch):
@pytest.mark.parametrize("encoding", ["utf-8", None])
def test_last_id_remembered(monkeypatch, encoding):
content = 'data: message 1\nid: abcdef\n\ndata: message 2\n\n'
fake_get = mock.Mock(return_value=FakeResponse(200, content))
fake_get = mock.Mock(return_value=FakeResponse(200, content, encoding=encoding))
monkeypatch.setattr(requests, 'get', fake_get)

c = sseclient.SSEClient('http://blah.com')
Expand Down

0 comments on commit 0c6441b

Please # to comment.