def test_fix_xmldecl(): # Slow compared to the other tests, but still only a few seconds. for encoding in encodings.aliases.aliases.values(): if encoding in ( "rot_13", "quopri_codec", "zlib_codec", "base64_codec", "uu_codec", "tactis", "hex_codec", "bz2_codec", ): continue try: "".encode(encoding) except LookupError: # not trying to handle unknown encodings yet continue xmldecl = fix_xmldecl(six.u(" <?xml>").encode(encoding), encoding, add_encoding=True) if encoding.lower().startswith("utf"): if "16" in encoding: if "le" in encoding.lower(): assert xmldecl.startswith(codecs.BOM_UTF16_LE) if "be" in encoding.lower(): assert xmldecl.startswith(codecs.BOM_UTF16_BE) sniffed = sniff_encoding(xmldecl) assert sniffed == encoding, (xmldecl, encoding, sniffed) xmldecl = fix_xmldecl(six.u(" <?xml>").encode(encoding), encoding, add_encoding=True) if encoding.lower().startswith("utf"): if "16" in encoding: if "le" in encoding.lower(): assert xmldecl.startswith(codecs.BOM_UTF16_LE) if "be" in encoding.lower(): assert xmldecl.startswith(codecs.BOM_UTF16_BE) sniffed = sniff_encoding(xmldecl) assert sniffed == encoding, (xmldecl, encoding, sniffed)
def test_formfeed_in_xmldecl(): xmldecl = fix_xmldecl(" \f\t\f\n\f\r\f<?xml>".encode("utf16")) assert xmldecl.decode("utf16") == "<?xml version='1.0'?>", xmldecl xmldecl = fix_xmldecl((' \f\t\f\n\f\r\f<?xml\fversion="1.0"' "\f\fstandalone=no\f\f?>").encode("utf16")) assert xmldecl.decode("utf16") == ("""<?xml version="1.0" standalone='no' ?>"""), xmldecl