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_non_2xx_status_code_error():
with pytest.raises(exceptions.ReceivedNon2xxResponseError):
raise exceptions.ReceivedNon2xxResponseError(404)
def test_non_2xx_status_code_error():
with pytest.raises(exceptions.ReceivedNon2xxResponseError):
raise exceptions.ReceivedNon2xxResponseError(404)
def test_InvalidResponse(self):
with self.assertRaises(exceptions.InvalidResponse):
raise exceptions.InvalidResponse
# Log the response
self.logger.debug('<-- '+r.text \
.replace("\n",'') \
.replace(' ', ' ')
.replace('{ ', '{')
)
except (requests.exceptions.InvalidSchema,
requests.exceptions.RequestException):
raise exceptions.ConnectionError()
# Raise exception the HTTP status code was not 200, and there was no
# response body, because this should be handled.
if not len(r.text) and r.status_code != 200:
raise exceptions.StatusCodeError(r.status_code)
return r.text
def handle_response(response_str, expected_response=True):
"""Processes the response from a request"""
# A response was expected, but none was given?
if expected_response and not len(response_str):
raise exceptions.ReceivedNoResponse()
# Was response given?
if len(response_str):
# Attempt to parse the response
try:
response_dict = json.loads(response_str)
except ValueError:
raise exceptions.ParseError()
# A response was *not* expected, but one was given? It may not
# be necessary to raise here. If we receive a response anyway,
# can't we just ignore it?
if not expected_response and 'result' in response_dict:
raise exceptions.InvalidResponse()
# Validate the response against the Response schema
try:
jsonschema.validate(
response_dict,
json.loads(pkgutil.get_data(
__name__, 'response-schema.json').decode('utf-8')))
except jsonschema.ValidationError:
raise exceptions.InvalidResponse()