def test_xml_encoding(self): """A character encoding is found via the meta tag.""" encoding = get_html_media_encoding( b""" <?xml version="1.0" encoding="ascii"?> <html> </html> """, "text/html", ) self.assertEqual(encoding, "ascii")
def test_meta_charset_underscores(self): """A character encoding contains underscore.""" encoding = get_html_media_encoding( b""" <html> <head><meta charset="Shift_JIS"> </head> </html> """, "text/html", ) self.assertEqual(encoding, "Shift_JIS")
def test_meta_xml_encoding(self): """Meta tags take precedence over XML encoding.""" encoding = get_html_media_encoding( b""" <?xml version="1.0" encoding="ascii"?> <html> <head><meta charset="UTF-16"> </head> </html> """, "text/html", ) self.assertEqual(encoding, "UTF-16")
def test_content_type(self): """A character encoding is found via the Content-Type header.""" # Test a few variations of the header. headers = ( 'text/html; charset="ascii";', "text/html;charset=ascii;", 'text/html; charset="ascii"', "text/html; charset=ascii", 'text/html; charset="ascii;', 'text/html; charset=ascii";', ) for header in headers: encoding = get_html_media_encoding(b"", header) self.assertEqual(encoding, "ascii")
def test_meta_charset(self): """A character encoding is found via the meta tag.""" encoding = get_html_media_encoding( b""" <html> <head><meta charset="ascii"> </head> </html> """, "text/html", ) self.assertEqual(encoding, "ascii") # A less well-formed version. encoding = get_html_media_encoding( b""" <html> <head>< meta charset = ascii> </head> </html> """, "text/html", ) self.assertEqual(encoding, "ascii")
def test_fallback(self): """A character encoding cannot be found in the body or header.""" encoding = get_html_media_encoding(b"", "text/html") self.assertEqual(encoding, "utf-8")