async def test_flush_dumps_cache(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertEqual(await io.read(1024), DATA[:1024]) self.data_source = OTHER_DATA await io.seek(0) self.assertEqual(await io.read(1024), DATA[:1024]) await io.flush() await io.seek(0) self.assertEqual(await io.read(1024), OTHER_DATA[:1024])
async def test_random_access(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: await io.seek(1536) self.assertEqual(await io.read(1024), DATA[1536:2560]) await io.seek(10, whence=SEEK_CUR) self.assertEqual(await io.read(1024), DATA[2570:3594]) await io.seek(-20, whence=SEEK_CUR) self.assertEqual(await io.read(1024), DATA[3574:4598]) await io.seek(-1044, whence=SEEK_END) self.assertEqual(await io.read(1024), DATA[-1044:-20])
async def test_read1(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: await io.seek(1024) await io.read(1024) await io.seek(0) self.session.reset_mock() data = await io.read1() self.session.get.assert_called_once() self.assertEqual(data, DATA[:2048]) await io.seek(1536)
def test_seek_and_tell_match(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertEqual(io.seek(1536), 1536) self.assertEqual(io.tell(), 1536) self.assertEqual(io.seek(10, whence=SEEK_CUR), 1546) self.assertEqual(io.tell(), 1546) self.assertEqual(io.seek(-20, whence=SEEK_CUR), 1526) self.assertEqual(io.tell(), 1526) self.assertEqual(io.seek(-20, whence=SEEK_END), len(DATA) - 20) self.assertEqual(io.tell(), len(DATA) - 20)
async def test_readinto1(self): b = bytearray(len(DATA)) async with HTTPIOFile('http://www.example.com/test/', 1024) as io: await io.seek(1024) await io.read(1024) await io.seek(0) self.session.reset_mock() self.assertEqual(await io.readinto1(b), 2048) self.session.get.assert_called_once() self.assertEqual(b[:2048], DATA[:2048]) await io.seek(1536) self.session.reset_mock() self.assertEqual(await io.readinto1(b), len(DATA) - 1536) self.session.get.assert_called_once() self.assertEqual(b[:len(DATA) - 1536], DATA[1536:])
async def test_closed(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertTrue(hasattr(io, 'closed')) self.assertFalse(io.closed) self.assertTrue(io.closed)
def test_isatty(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertFalse(io.isatty())
async def test_aiter(self): self.data_source = ASCII_DATA async with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertEqual([line.decode('ascii') async for line in io], [line for line in ASCII_LINES])
async def test_read_gets_data_without_buffering(self): async with HTTPIOFile('http://www.example.com/test/') as io: data = await io.read(1024) self.assertEqual(data, DATA[0:1024])
def test_truncate(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: with self.assertRaises(IOBaseError): io.truncate()
def test_readlines(self): self.data_source = ASCII_DATA with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertEqual([line.decode('ascii') for line in io.readlines()], [line for line in ASCII_LINES])
def test_readable(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertTrue(io.readable())
def test_read_gets_data_without_buffering(self): with HTTPIOFile('http://www.example.com/test/') as io: self.assertEqual(io.read(), DATA)
def test_read_gets_data(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: data = io.read(1024) self.assertEqual(data, DATA[0:1024])
def test_write(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: with self.assertRaises(IOBaseError): io.write(DATA[:1024])
async def test_peek(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: await io.seek(1500) data = await io.peek(1024) self.assertEqual(data, DATA[1500:1500 + 1024]) self.assertEqual(await io.tell(), 1500)
def test_writable(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertFalse(io.writable())
async def test_throws_exception_when_get_returns_error(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.error_code = 404 with self.assertRaises(HTTPException): await io.read(1024) self.assertEqual(await io.tell(), 0)
def test_writelines(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: with self.assertRaises(IOBaseError): io.writelines([line.encode('ascii') for line in ASCII_LINES])
async def test_readinto(self): b = bytearray(1536) async with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertEqual(await io.readinto(b), len(b)) self.assertEqual(bytes(b), DATA[:1536])
def test_implements_buffered_io_base(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertIsInstance(io, BufferedIOBase)
async def test_readline(self): self.data_source = ASCII_DATA async with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertEqual((await io.readline()).decode('ascii'), ASCII_LINES[0])
async def test_throws_exception_when_head_returns_error(self): self.error_code = 404 with self.assertRaises(HTTPException): async with HTTPIOFile('http://www.example.com/test/', 1024): pass
async def test_tell_starts_at_zero(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertEqual(await io.tell(), 0)
async def test_read_after_close_fails(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: pass with self.assertRaises(IOBaseError): await io.read()
async def test_seekable(self): async with HTTPIOFile('http://www.example.com/test/', 1024) as io: self.assertTrue(await io.seekable())
def test_detach(self): with HTTPIOFile('http://www.example.com/test/', 1024) as io: with self.assertRaises(UnsupportedOperation): io.detach()