Package cssutils :: Package tests :: Package encutils |
[hide private]
[frames] | no frames]

# Source Code for Package cssutils.tests.encutils

      1  """
      2  tests for encutils.py
      3  """
      4  __version__ = '0.8'
      5
      6  import httplib
      7  from StringIO import StringIO
      8  import sys
      9  import unittest
     10
     11  try:
     12      import encutils
     13  except ImportError:
     14      import cssutils.encutils as encutils
     15
     16  # helper log
     17  log = encutils.buildlog(stream=StringIO())
     18
     19 -class AutoEncodingTestCase(unittest.TestCase):
     20
     21 -    def _fakeRes(self, content):
     22          "build a fake HTTP response"
     23          class FakeRes:
     24              def __init__(self, content):
     25                  fp = StringIO(content)
     26                  self._info = httplib.HTTPMessage(fp)
     27
     28              def info(self):
     29                  return self._info
     30          return FakeRes(content)
     31
     32 -    def test_getTextTypeByMediaType(self):
     33          tests = {
     34              'application/xml': encutils._XML_APPLICATION_TYPE,
     35              'application/xml-dtd': encutils._XML_APPLICATION_TYPE,
     36              'application/xml-external-parsed-entity': encutils._XML_APPLICATION_TYPE,
     37              'application/xhtml+xml': encutils._XML_APPLICATION_TYPE,
     38              'text/xml': encutils._XML_TEXT_TYPE,
     39              'text/xml-external-parsed-entity': encutils._XML_TEXT_TYPE,
     40              'text/xhtml+xml': encutils._XML_TEXT_TYPE,
     41              'text/html': encutils._HTML_TEXT_TYPE,
     42              'text/css': encutils._TEXT_TYPE,
     43              'text/plain': encutils._TEXT_TYPE,
     44              'x/x': encutils._OTHER_TYPE,
     45              'ANYTHING': encutils._OTHER_TYPE
     46              }
     47          for test, exp in tests.items():
     48              self.assertEqual(
     49                  exp, encutils._getTextTypeByMediaType(test, log=log))
     50
     51 -    def test_getTextType(self):
     52          tests = {
     53              u'\x00\x00\xFE\xFF<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
     54              u'\xFF\xFE\x00\x00<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
     55              u'\xFE\xFF<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
     56              u'\xFF\xFE<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
     57              u'\xef\xbb\xbf<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
     58              u'<?xml version="1.0"': encutils._XML_APPLICATION_TYPE,
     59              u'\x00\x00\xFE\xFFanything': encutils._OTHER_TYPE,
     60              u'\xFF\xFE\x00\x00anything': encutils._OTHER_TYPE,
     61              u'\xFE\xFFanything': encutils._OTHER_TYPE,
     62              u'\xFF\xFEanything': encutils._OTHER_TYPE,
     63              u'\xef\xbb\xbfanything': encutils._OTHER_TYPE,
     64              u'x/x': encutils._OTHER_TYPE,
     65              u'ANYTHING': encutils._OTHER_TYPE
     66              }
     67          for test, exp in tests.items():
     68              self.assertEqual(
     69                  exp, encutils._getTextType(test, log=log))
     70
     71 -    def test_encodingByMediaType(self):
     72          tests = {
     73              'application/xml': 'utf-8',
     74              'application/xml-dtd': 'utf-8',
     75              'application/xml-external-parsed-entity': 'utf-8',
     76              'application/ANYTHING+xml': 'utf-8',
     77              '  application/xml  ': 'utf-8',
     78              'text/xml': 'ascii',
     79              'text/xml-external-parsed-entity': 'ascii',
     80              'text/ANYTHING+xml': 'ascii',
     81              'text/html': 'iso-8859-1',
     82              'text/css': 'iso-8859-1',
     83              'text/plain': 'iso-8859-1',
     84              'ANYTHING': None
     85              }
     86          for test, exp in tests.items():
     87              self.assertEqual(exp,
     88                               encutils.encodingByMediaType(test, log=log))
     89
     90 -    def test_getMetaInfo(self):
     91          tests = {
     92              """<meta tp-equiv='Content-Type' content='text/html; charset=ascii'>""":
     93                  (None, None),
     94              """<meta http-equiv='ontent-Type' content='text/html; charset=ascii'>""":
     95                  (None, None),
     96
     97              """<meta http-equiv='Content-Type' content='text/html'>""":
     98                  ('text/html', None),
     99
    100              """<meta content='text/html' http-equiv='Content-Type'>""":
    101                  ('text/html', None),
    102              """<meta content='text/html;charset=ascii' http-equiv='Content-Type'>""":
    103                  ('text/html', 'ascii'),
    104
    105              """<meta http-equiv='Content-Type' content='text/html ;charset=ascii'>""":
    106                  ('text/html', 'ascii'),
    107              """<meta content='text/html;charset=iso-8859-1' http-equiv='Content-Type'>""":
    108                  ('text/html', 'iso-8859-1'),
    109              """<meta http-equiv="Content-Type" content="text/html;charset = ascii">""":
    110                  ('text/html', 'ascii'),
    111
    112              """<meta http-equiv="Content-Type" content="text/html;charset=ascii;x=2">""":
    113                  ('text/html', 'ascii'),
    114              """<meta http-equiv="Content-Type" content="text/html;x=2;charset=ascii">""":
    115                  ('text/html', 'ascii'),
    116              """<meta http-equiv="Content-Type" content="text/html;x=2;charset=ascii;y=2">""":
    117                  ('text/html', 'ascii'),
    118
    119              """<meta http-equiv='Content-Type' content="text/html;charset=ascii">""":
    120                  ('text/html', 'ascii'),
    121              """<meta http-equiv='Content-Type' content='text/html;charset=ascii'  />""":
    122                  ('text/html', 'ascii'),
    123              """<meta http-equiv = " Content-Type" content = " text/html;charset=ascii " >""":
    124                  ('text/html', 'ascii'),
    125              """<meta http-equiv = " \n Content-Type " content = "  \t text/html   ;  charset=ascii " >""":
    126                  ('text/html', 'ascii')
    127              }
    128          for test, exp in tests.items():
    129              self.assertEqual(exp, encutils.getMetaInfo(test, log=log))
    130
    131 -    def test_detectXMLEncoding(self):
    132          tests = {
    133              # BOM
    134              ('utf_32_be'): u'\x00\x00\xFE\xFFanything',
    135              ('utf_32_le'): u'\xFF\xFE\x00\x00anything',
    136              ('utf_16_be'): u'\xFE\xFFanything',
    137              ('utf_16_le'): u'\xFF\xFEanything',
    138              ('utf-8'): u'\xef\xbb\xbfanything',
    139              # encoding=
    140              ('ascii'): '<?xml version="1.0" encoding="ascii" ?>',
    141              ('ascii'): "<?xml version='1.0' encoding='ascii' ?>",
    142              ('iso-8859-1'): "<?xml version='1.0' encoding='iso-8859-1' ?>",
    143              # default
    144              ('utf-8'): '<?xml version="1.0" ?>',
    145              ('utf-8'): '<?xml version="1.0"?><x encoding="ascii"/>'
    146              }
    147          for exp, test in tests.items():
    148              self.assertEqual(exp, encutils.detectXMLEncoding(test, log=log))
    149
    150 -    def test_tryEncodings(self):
    151          try:
    152              import chardet
    153              tests = [
    154                  ('ascii', 'abc'),
    155                  ('windows-1252', u'\xf6'),
    156                  ('ascii', u'\u1111')
    157                  ]
    158          except ImportError:
    159              tests = [
    160                  ('ascii', 'abc'),
    161                  ('iso-8859-1', u'\xf6'),
    162                  ('utf-8', u'\u1111')
    163                  ]
    164          for exp, test in tests:
    165              self.assertEqual(exp, encutils.tryEncodings(test))
    166
    167      # (expectedencoding, expectedmismatch): (httpheader, filecontent)
    168      fulltests = {
    169          ('utf-8', False): (
    170              '''NoContentType''', '''OnlyText'''),
    171
    172          # --- application/xhtml+xml ---
    173          # default enc
    174          ('utf-8', False): (
    175              '''Content-Type: application/xhtml+xml''',
    176              '''<?xml version="1.0" ?>
    177                  <example>
    178                      <meta http-equiv="Content-Type"
    179                          content="application/xhtml+xml"/>
    180                  </example>'''),
    181          # header enc
    182          ('iso-h', True): (
    183              '''Content-Type: application/xhtml+xml;charset=iso-H''',
    184              '''<?xml version="1.0" ?>
    185                  <example>
    186                      <meta http-equiv="Content-Type"
    187                          content="application/xhtml+xml"/>
    188                  </example>'''),
    189          # mismatch header - meta, meta ignored
    190          ('iso-h', True): (
    191              '''Content-Type: application/xhtml+xml;charset=iso-H''',
    192              '''<?xml version="1.0" ?>
    193                  <example>
    194                      <meta http-equiv="Content-Type"
    195                          content="application/xhtml+xml;charset=iso_M"/>
    196                  </example>'''),
    197          # mismatch XML - meta, meta ignored
    198          ('iso-x', False): (
    199              '''Content-Type: application/xhtml+xml''',
    200              '''<?xml version="1.0" encoding="iso-X" ?>
    201                  <example>
    202                      <meta http-equiv="Content-Type"
    203                          content="application/xhtml+xml;charset=iso_M"/>
    204                  </example>'''),
    205          # mismatch header and XML, header wins
    206          ('iso-h', True): (
    207              '''Content-Type: application/xhtml+xml;charset=iso-H''',
    208              '''<?xml version="1.0" encoding="iso-X" ?>
    209                  <example/>'''),
    210
    211          # --- text/xml ---
    212          # default enc
    213          ('ascii', False): (
    214              '''Content-Type: text/xml''',
    215              '''<?xml version="1.0" ?>
    216                  <example>
    217                      <meta http-equiv="Content-Type"
    218                          content="text/xml"/>
    219                  </example>'''),
    220          # header enc
    221          ('iso-h', True): (
    222              '''Content-Type: text/xml;charset=iso-H''',
    223              '''<?xml version="1.0" ?>
    224                  <example>
    225                      <meta http-equiv="Content-Type"
    226                          content="text/xml"/>
    227                  </example>'''),
    228          # mismatch header - meta, meta ignored
    229          ('iso-h', True): (
    230              '''Content-Type: text/xml;charset=iso-H''',
    231              '''<?xml version="1.0" ?>
    232                  <example>
    233                      <meta http-equiv="Content-Type"
    234                          content="text/xml;charset=iso_M"/>
    235                  </example>'''),
    236          # XML - meta, both ignored, use HTTP, meta completely ignored
    237          ('ascii', False): (
    238              '''Content-Type: text/xml''',
    239              '''<?xml version="1.0" encoding="iso-X" ?>
    240                  <example>
    241                      <meta http-equiv="Content-Type"
    242                          content="text/xml;charset=iso_M"/>
    243                  </example>'''),
    244          # mismatch header and XML, XML ignored
    245          ('iso-h', True): (
    246              '''Content-Type: text/xml;charset=iso-H''',
    247              '''<?xml version="1.0" encoding="iso-X" ?>
    248                  <example/>'''),
    249
    250          # --- text/html ---
    251          # no default enc
    252          (None, False): ('Content-Type: text/html;',
    253              '''<meta http-equiv="Content-Type"
    254                  content="text/html">'''),
    255          # header enc
    256          ('iso-h', True): ('Content-Type: text/html;charset=iso-H',
    257              '''<meta http-equiv="Content-Type"
    258                  content="text/html">'''),
    259          # meta enc
    260          ('iso-m', False): ('Content-Type: text/html',
    261              '''<meta http-equiv="Content-Type"
    262                  content="text/html;charset=iso-m">'''),
    263          # mismatch header - meta, header wins
    264          ('iso-h', True): ('Content-Type: text/html;charset=iso-H',
    265              '''<meta http-equiv="Content-Type"
    266                  content="text/html;charset=iso-m">'''),
    267
    268          # no header:
    269          (None, False): (None,
    270              '''<meta http-equiv="Content-Type"
    271                  content="text/html;charset=iso-m">'''),
    272            (None, False): (None, '''text'''),
    273            ('utf-8', False): (None, '''<?xml version='''),
    274            ('utf-8', False): (None, '''<?xml version='''),
    275            ('iso-x', False): (None, '''<?xml version="1.0" encoding="iso-X"?>''')
    276          }
    277
    278 -    def test_getEncodingInfo(self):
    279          for exp, test in self.fulltests.items():
    280              header, text = test
    281              if header:
    282                  res = encutils.getEncodingInfo(self._fakeRes(header), text)
    283              else:
    284                  res = encutils.getEncodingInfo(text=text)
    285              res = (res.encoding, res.mismatch)
    286              self.assertEqual(exp, res)
    287
    288
    289  if __name__ == '__main__':
    290      unittest.main()
    291