def test_netbytes2str(self): from mobilize.httputil import netbytes2str kungfu = '功夫' # b'\xe5\x8a\x9f\xe5\xa4\xab' src = b'<html><body><p>\xe5\x8a\x9f\xe5\xa4\xab means:\r\n<ul>\r\n<li>hard work</li>\r\n<li>great skill</li>\r\n</ul>' expected = '<html><body><p>功夫 means:\n<ul>\n<li>hard work</li>\n<li>great skill</li>\n</ul>' actual = netbytes2str(src, 'utf-8') self.assertEquals(expected, actual)
def test_netbytes2str_wrongencoding(self): '''Test for when the guessed encoding is wrong, and we need to automatically try different ones''' from mobilize.httputil import netbytes2str # iso-8859-1 encoding, but we'll initially parse it as utf-8 kungfu = '功夫' # b'\xe5\x8a\x9f\xe5\xa4\xab' src = b'<p>a \xa0 b</p>' expected = '<p>a b</p>' actual = netbytes2str(src, 'utf-8') self.assertEquals(expected, actual)