Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Test a 401 (should raise Exception)
request.return_value = Response(401, json.dumps({
'error': {
'code': 2006,
'message': "You shall not pass.",
}
}))
self.assertRaises(pdpyras.PDClientError, sess.request, 'get',
'/services')
request.reset_mock()
# Test retry logic;
with patch.object(pdpyras.time, 'sleep') as sleep:
# Test getting a connection error and succeeding the final time.
returns = [
pdpyras.Urllib3Error("D'oh!")
]*sess.max_network_attempts
returns.append(Response(200, json.dumps({'user': user})))
request.side_effect = returns
r = sess.get('/users/P123456')
self.assertEqual(sess.max_network_attempts+1,
request.call_count)
self.assertEqual(sess.max_network_attempts, sleep.call_count)
self.assertTrue(r.ok)
request.reset_mock()
sleep.reset_mock()
# Now test handling a non-transient error:
raises = [pdpyras.RequestsError("D'oh!")]*(
sess.max_network_attempts-1)
raises.extend([pdpyras.Urllib3Error("D'oh!")]*2)
request.side_effect = raises