Source

django-spotnet / spotnet / tests / parsing.py

Full commit
try:
    from django.utils import unittest
except ImportError:
    try:
        import unittest2 as unittest
    except ImportError:
        import unittest

from mock import Mock
from spotnet import settings
from spotnet.post import RawPost


class ParsingTest(unittest.TestCase):
    def construct(self, headers, content, add_lines=True):
        if isinstance(content, str):
            lines = 1
            content = [content]
        else:
            lines = len(content)
        if add_lines and 'Lines' not in headers:
            headers['Lines'] = lines
        return self.parse_to_post(
            ['%s: %s' % (k, v) for k, v in headers.iteritems()]
        +
            ['']
        +
            content
        )
        return self.parse_to_post('%s\n\n%s' % (
            '\n'.join('%s: %s' % (k, v) for k, v in headers.iteritems()),
            '\n'.join(content),
        ))

    def parse_to_post(self, content):
        return RawPost(None, [None, None, None, content])


class EncodingParsingTest(ParsingTest):

    def test_parse_html_entity(self):
        post = self.construct(dict(
            Subject='dokter van een patiënt',
        ), 'blaat')
        self.assertEqual(post.subject, u'dokter van een pati\xebnt')

    def test_parse_different_encoding_header(self):
        post = self.construct(dict(
            Subject='=?ISO-8859-7?B?1PHp4e303Pb16+vv8iDM4ezc6u/y?=',
        ), 'blaat')
        self.assertEqual(post.subject, u'\u03a4\u03c1\u03b9\u03b1\u03bd\u03c4\u03ac\u03c6\u03c5\u03bb\u03bb\u03bf\u03c2 \u039c\u03b1\u03bc\u03ac\u03ba\u03bf\u03c2')