import unittest import zlib from io import BytesIO from urllib3.response import HTTPResponse class TestLegacyResponse(unittest.TestCase): def test_getheaders(self): headers = {'host': 'example.com'} r = HTTPResponse(headers=headers) self.assertEqual(r.getheaders(), headers) def test_getheader(self): headers = {'host': 'example.com'} r = HTTPResponse(headers=headers) self.assertEqual(r.getheader('host'), 'example.com') class TestResponse(unittest.TestCase): def test_cache_content(self): r = HTTPResponse('foo') self.assertEqual(r.data, 'foo') self.assertEqual(r._body, 'foo') def test_default(self): r = HTTPResponse() self.assertEqual(r.data, None) def test_none(self): r = HTTPResponse(None) self.assertEqual(r.data, None) def test_preload(self): fp = BytesIO(b'foo') r = HTTPResponse(fp, preload_content=True) self.assertEqual(fp.tell(), len(b'foo')) self.assertEqual(r.data, b'foo') def test_no_preload(self): fp = BytesIO(b'foo') r = HTTPResponse(fp, preload_content=False) self.assertEqual(fp.tell(), 0) self.assertEqual(r.data, b'foo') self.assertEqual(fp.tell(), len(b'foo')) def test_decode_bad_data(self): fp = BytesIO(b'\x00' * 10) self.assertRaises(zlib.error, HTTPResponse, fp, headers={ 'content-encoding': 'deflate' }) def test_decode_deflate(self): import zlib data = zlib.compress(b'foo') fp = BytesIO(data) r = HTTPResponse(fp, headers={'content-encoding': 'deflate'}) self.assertEqual(r.data, b'foo') if __name__ == '__main__': unittest.main()