def test_handle_json_response(self): m = mock.MagicMock() m.raise_for_status.side_effect = Exception("Test") with self.assertRaises(DownstreamError) as ex: utils.handle_json_response(m) m = mock.MagicMock() m.json.side_effect = ValueError with self.assertRaises(DownstreamError) as ex: utils.handle_json_response(m) m = mock.MagicMock() result = utils.handle_json_response(m)
def test_handle_json_response(self): m = mock.MagicMock() m.status_code = 400 m.json.return_value = dict(message='test error') with self.assertRaises(DownstreamError) as ex: utils.handle_json_response(m) self.assertEqual(str(ex.exception), 'test error') m.json.side_effect = Exception('json processing error') m.raise_for_status.side_effect = Exception('http error') with self.assertRaises(Exception) as ex: utils.handle_json_response(m) self.assertEqual(str(ex.exception), 'http error') m = mock.MagicMock() m.json.return_value = dict(key='value') result = utils.handle_json_response(m) self.assertEqual(m.json.return_value, result)