Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_b2_error(self):
response = MagicMock()
response.status_code = 503
response.content = six.b('{"status": 503, "code": "server_busy", "message": "busy"}')
with self.assertRaises(ServiceError):
_translate_errors(lambda: response)
def test_broken_pipe(self):
def fcn():
raise requests.ConnectionError(
requests.packages.urllib3.exceptions.ProtocolError(
"dummy", socket.error(20, 'Broken pipe')
)
)
with self.assertRaises(BrokenPipe):
_translate_errors(fcn)
def test_ok(self):
response = MagicMock()
response.status_code = 200
actual = _translate_errors(lambda: response)
self.assertTrue(response is actual) # no assertIs until 2.7
def test_connection_error(self):
def fcn():
raise requests.ConnectionError('a message')
with self.assertRaises(B2ConnectionError):
_translate_errors(fcn)
def test_unknown_error(self):
def fcn():
raise Exception('a message')
with self.assertRaises(UnknownError):
_translate_errors(fcn)
def test_connection_reset(self):
class SysCallError(Exception):
pass
def fcn():
raise SysCallError('(104, ECONNRESET)')
with self.assertRaises(ConnectionReset):
_translate_errors(fcn)
def test_too_many_requests(self):
response = MagicMock()
response.status_code = 429
response.headers = {'retry-after': 1}
response.content = six.b(
'{"status": 429, "code": "Too Many requests", "message": "retry after some time"}'
)
with self.assertRaises(TooManyRequests):
_translate_errors(lambda: response)
def test_partial_content(self):
response = MagicMock()
response.status_code = 206
actual = _translate_errors(lambda: response)
self.assertTrue(response is actual) # no assertIs until 2.7
def test_unknown_host(self):
def fcn():
raise requests.ConnectionError(
requests.packages.urllib3.exceptions.MaxRetryError(
'AAA nodename nor servname provided, or not known AAA', 'http://example.com'
)
)
with self.assertRaises(UnknownHost):
_translate_errors(fcn)