def test_empty_bad_hash(self): wrapped = HashingInput(BytesIO(b''), 0, hashlib.sha256, 'nope') with self.assertRaises(swob.HTTPException) as raised: wrapped.read(3) self.assertEqual(raised.exception.status, '422 Unprocessable Entity') # the error causes us to close the input self.assertTrue(wrapped._input.closed)
def test_bad_hash(self): raw = b'123456789' wrapped = HashingInput(BytesIO(raw), 9, hashlib.sha256, hashlib.md5(raw).hexdigest()) self.assertEqual(b'1234', wrapped.read(4)) self.assertEqual(b'5678', wrapped.read(4)) with self.assertRaises(swob.HTTPException) as raised: wrapped.read(4) self.assertEqual(raised.exception.status, '422 Unprocessable Entity') self.assertTrue(wrapped._input.closed)
def test_too_short(self): raw = b'123456789' wrapped = HashingInput(BytesIO(raw), 10, hashlib.md5, hashlib.md5(raw).hexdigest()) self.assertEqual(b'1234', wrapped.read(4)) self.assertEqual(b'56', wrapped.read(2)) # even though the hash matches, there was more data than we expected with self.assertRaises(swob.HTTPException) as raised: wrapped.read(4) self.assertEqual(raised.exception.status, '422 Unprocessable Entity') self.assertTrue(wrapped._input.closed)
def test_too_long(self): raw = b'123456789' wrapped = HashingInput(BytesIO(raw), 8, hashlib.md5, hashlib.md5(raw).hexdigest()) self.assertEqual(b'1234', wrapped.read(4)) self.assertEqual(b'56', wrapped.read(2)) # even though the hash matches, there was more data than we expected with self.assertRaises(swob.Response) as raised: wrapped.read(3) self.assertEqual(raised.exception.status, '422 Unprocessable Entity') # the error causes us to close the input self.assertTrue(wrapped._input.closed)
def test_good(self): raw = b'123456789' wrapped = HashingInput(BytesIO(raw), 9, hashlib.md5, hashlib.md5(raw).hexdigest()) self.assertEqual(b'1234', wrapped.read(4)) self.assertEqual(b'56', wrapped.read(2)) # trying to read past the end gets us whatever's left self.assertEqual(b'789', wrapped.read(4)) # can continue trying to read -- but it'll be empty self.assertEqual(b'', wrapped.read(2)) self.assertFalse(wrapped._input.closed) wrapped.close() self.assertTrue(wrapped._input.closed)
def test_empty(self): wrapped = HashingInput(BytesIO(b''), 0, hashlib.sha256, hashlib.sha256(b'').hexdigest()) self.assertEqual(b'', wrapped.read(4)) self.assertEqual(b'', wrapped.read(2)) self.assertFalse(wrapped._input.closed) wrapped.close() self.assertTrue(wrapped._input.closed)