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_request(self, responses):
self.client.logger.setLevel('DEBUG')
self.assertDictEqual(self.client.get("/v1/"), responses.calls[0].response.json())
self.assertIsNone(self.client.get("/no-content/"))
self.assertIsNone(self.client.get("/invalid-json/"))
with self.assertRaises(ClientError) as ctx:
self.client.get("/not-found/")
self.assertEqual(ctx.exception.message, responses.calls[3].response.json())
with self.assertRaises(ServerError) as ctx:
self.client.get("/server-error/")
self.assertEqual(ctx.exception.message, responses.calls[4].response.json())
with self.assertRaises(ServerError) as ctx:
self.client.get("/no-json/")
self.assertEqual(ctx.exception.message, responses.calls[5].response.content)
# Test headers
self.client.authenticate(client_id='client_id', client_secret='client_secret', scope=['account'])
self.assertEqual(responses.calls[6].request.headers['Authorization'], 'Basic Y2xpZW50X2lkOmNsaWVudF9zZWNyZXQ=')
self.assertEqual(responses.calls[6].request.headers['Content-Type'], 'application/x-www-form-urlencoded')
def request(self, method, path, **kwargs):
headers = self.get_headers(kwargs.pop("headers", {}))
response = requests.request(method, self.build_url(path), headers=headers, **kwargs)
self.logger.debug(response.request.url)
try:
response.raise_for_status()
except requests.HTTPError:
try:
error = response.json()
except ValueError:
error = response.content
if 400 <= response.status_code <= 499:
raise ClientError(error)
else:
raise ServerError(error)
try:
result = response.json() or None
except:
result = None
if self.logger.getEffectiveLevel() == logging.DEBUG:
self.logger.debug(json.dumps(result, indent=True))
return result