From d3cff80beb560a0be908b2513bdc906aad43b58c Mon Sep 17 00:00:00 2001 From: Markus Unterwaditzer Date: Tue, 13 May 2014 19:01:40 +0200 Subject: [PATCH] Fix #51 --- tests/storage/test_http.py | 7 +++---- vdirsyncer/storage/http.py | 3 ++- vdirsyncer/sync.py | 2 +- vdirsyncer/utils.py | 12 +++++++++++- 4 files changed, 17 insertions(+), 7 deletions(-) diff --git a/tests/storage/test_http.py b/tests/storage/test_http.py index 76d54ca..da926dc 100644 --- a/tests/storage/test_http.py +++ b/tests/storage/test_http.py @@ -58,8 +58,6 @@ def test_split_collection_timezones(): given = [tuple(x) for x in split_collection(full)] expected = [(u'BEGIN:VCALENDAR',) + timezone + item + (u'END:VCALENDAR',) for item in items] - print(given) - print(expected) assert given == expected @@ -72,7 +70,7 @@ def test_list(monkeypatch): u'DESCRIPTION:Beschreibung des Termines\n' u'END:VEVENT'), (u'BEGIN:VEVENT\n' - u'SUMMARY:Eine zweite Kurzinfo\n' + u'SUMMARY:Eine zweite Küèrzinfo\n' u'DESCRIPTION:Beschreibung des anderen Termines\n' u' With an extra line for description\n' u'BEGIN:VALARM\n' @@ -96,7 +94,8 @@ def test_list(monkeypatch): r.status_code = 200 assert responses r._content = responses.pop().encode('utf-8') - r.encoding = 'utf-8' + r.headers['Content-Type'] = 'text/icalendar' + r.encoding = 'ISO-8859-1' return r monkeypatch.setattr('requests.request', get) diff --git a/vdirsyncer/storage/http.py b/vdirsyncer/storage/http.py index a5cc2e4..6943a6c 100644 --- a/vdirsyncer/storage/http.py +++ b/vdirsyncer/storage/http.py @@ -132,7 +132,8 @@ class HttpStorage(Storage): self._settings = { 'verify': prepare_verify(verify), - 'auth': prepare_auth(auth, username, password) + 'auth': prepare_auth(auth, username, password), + 'latin1_fallback': False } self.username, self.password = username, password self.useragent = useragent diff --git a/vdirsyncer/sync.py b/vdirsyncer/sync.py index 1bdb538..7298daa 100644 --- a/vdirsyncer/sync.py +++ b/vdirsyncer/sync.py @@ -18,7 +18,7 @@ import itertools from . import exceptions, log -from .utils import iteritems, itervalues +from .utils import iteritems sync_logger = log.get(__name__) diff --git a/vdirsyncer/utils.py b/vdirsyncer/utils.py index de3de4f..505ea0b 100644 --- a/vdirsyncer/utils.py +++ b/vdirsyncer/utils.py @@ -187,7 +187,7 @@ def get_password(username, resource): def request(method, url, data=None, headers=None, auth=None, verify=None, - session=None): + session=None, latin1_fallback=True): '''wrapper method for requests, to ease logging and mocking''' if session is None: @@ -200,7 +200,17 @@ def request(method, url, data=None, headers=None, auth=None, verify=None, logger.debug(data) logger.debug('Sending request...') r = func(method, url, data=data, headers=headers, auth=auth, verify=verify) + + # See https://github.com/kennethreitz/requests/issues/2042 + content_type = r.headers.get('Content-Type', '') + if not latin1_fallback and \ + 'charset' not in content_type and \ + content_type.startswith('text/'): + logger.debug('Removing latin1 fallback') + r.encoding = None + logger.debug(r.status_code) logger.debug(r.headers) logger.debug(r.content) + return r