def test_process_response_encoding(self): headers = {"Content-Type": "text/html", "Content-Encoding": "gzip"} f = StringIO() plain_body = "<html><head><title>Some page</title>" zf = GzipFile(fileobj=f, mode="wb") zf.write(plain_body) zf.close() response = Response("http://github.com/", headers=headers, body=f.getvalue()) new_response = self.mw.process_response(response) self.assertIsInstance(new_response, HtmlResponse) self.assertEqual(new_response.body, plain_body) self.assertEqual(new_response.encoding, normalize_encoding("utf-8"))
def test_process_response_force_recalculate_encoding(self): headers = {"Content-Type": "text/html", "Content-Encoding": "gzip"} f = StringIO() plainbody = """<html><head><title>Some page</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312">""" zf = GzipFile(fileobj=f, mode="wb") zf.write(plainbody) zf.close() response = HtmlResponse("http;//www.example.com/page.html", headers=headers, body=f.getvalue()) new_response = self.mw.process_response(response) self.assertIsInstance(new_response, HtmlResponse) self.assertEqual(new_response.body, plainbody) self.assertEqual(new_response.encoding, normalize_encoding("gb2312"))
def test_process_response_encoding(self): headers = { 'Content-Type': 'text/html', 'Content-Encoding': 'gzip', } f = StringIO() plain_body = '<html><head><title>Some page</title>' zf = GzipFile(fileobj=f, mode='wb') zf.write(plain_body) zf.close() response = Response('http://github.com/', headers=headers, body=f.getvalue()) new_response = self.mw.process_response(response) self.assertIsInstance(new_response, HtmlResponse) self.assertEqual(new_response.body, plain_body) self.assertEqual(new_response.encoding, normalize_encoding('utf-8'))
def test_process_response_force_recalculate_encoding(self): headers = { 'Content-Type': 'text/html', 'Content-Encoding': 'gzip', } f = StringIO() plainbody = '''<html><head><title>Some page</title><meta http-equiv="Content-Type" content="text/html; charset=gb2312">''' zf = GzipFile(fileobj=f, mode='wb') zf.write(plainbody) zf.close() response = HtmlResponse('http;//www.example.com/page.html', headers=headers, body=f.getvalue()) new_response = self.mw.process_response(response) self.assertIsInstance(new_response, HtmlResponse) self.assertEqual(new_response.body, plainbody) self.assertEqual(new_response.encoding, normalize_encoding('gb2312'))
def test_resolve_encoding(self): self.assertEqual(normalize_encoding('latin1'), 'cp1252') self.assertEqual(normalize_encoding(' Latin-1'), 'cp1252') self.assertEqual(normalize_encoding('gb_2312-80'), 'gb18030') self.assertEqual(normalize_encoding('unknown encoding'), None)