jenkins-bot has submitted this change and it was merged. (
https://gerrit.wikimedia.org/r/372166 )
Change subject: threadedhttp: add default for content-type
......................................................................
threadedhttp: add default for content-type
Add default in case content-type is missing from response.
Bug: T166617
Change-Id: I1e6722486502557a35f6804c15b0c89a0aaeb21c
---
M pywikibot/comms/threadedhttp.py
M tests/http_tests.py
2 files changed, 13 insertions(+), 1 deletion(-)
Approvals:
jenkins-bot: Verified
Xqt: Looks good to me, approved
diff --git a/pywikibot/comms/threadedhttp.py b/pywikibot/comms/threadedhttp.py
index 03386cf..4e1f9e1 100644
--- a/pywikibot/comms/threadedhttp.py
+++ b/pywikibot/comms/threadedhttp.py
@@ -118,7 +118,7 @@
def header_encoding(self):
"""Return charset given by the response header."""
if not hasattr(self, '_header_encoding'):
- content_type = self.response_headers['content-type']
+ content_type = self.response_headers.get('content-type', '')
pos = content_type.find('charset=')
if pos >= 0:
pos += len('charset=')
diff --git a/tests/http_tests.py b/tests/http_tests.py
index b590ad7..9404ffa 100644
--- a/tests/http_tests.py
+++ b/tests/http_tests.py
@@ -439,6 +439,18 @@
req._data = resp
return req
+ def test_no_content_type(self):
+ """Test decoding without content-type (and then no
charset)."""
+ req = threadedhttp.HttpRequest('')
+ resp = requests.Response()
+ resp.headers = {}
+ resp._content = CharsetTestCase.LATIN1_BYTES[:]
+ req._data = resp
+ self.assertIsNone(req.charset)
+ self.assertEqual('latin1', req.encoding)
+ self.assertEqual(req.raw, CharsetTestCase.LATIN1_BYTES)
+ self.assertEqual(req.content, CharsetTestCase.STR)
+
def test_no_charset(self):
"""Test decoding without explicit charset."""
req = threadedhttp.HttpRequest('')
--
To view, visit
https://gerrit.wikimedia.org/r/372166
To unsubscribe, visit
https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I1e6722486502557a35f6804c15b0c89a0aaeb21c
Gerrit-PatchSet: 2
Gerrit-Project: pywikibot/core
Gerrit-Branch: master
Gerrit-Owner: Mpaa <mpaa.wiki(a)gmail.com>
Gerrit-Reviewer: John Vandenberg <jayvdb(a)gmail.com>
Gerrit-Reviewer: Xqt <info(a)gno.de>
Gerrit-Reviewer: jenkins-bot <>