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_long_resource():
url_expr = re.compile(r'https?://api\.test\.tst/v1/json/error\?.*')
responses.add(
responses.GET, url_expr,
status=200,
content_type='application/json',
body='({"json": "error"})')
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
with pytest.raises(jwplatform.errors.JWPlatformUnknownError) as err:
jwp_client.json.error()
assert err.value.message == 'Not a valid JSON string: ({"json": "error"})'
import json
import pytest
import jwplatform
import responses
SUPPORTED_ERROR_CASES = [
{
'http_status': 400,
'response': {
'status': 'error',
'code': 'UnknownError',
'title': 'An Unknown Error occurred',
'message': ''
},
'expected_exception': jwplatform.errors.JWPlatformUnknownError
},
{
'http_status': 404,
'response': {
'status': 'error',
'code': 'NotFound',
'title': 'Not Found',
'message': 'Item not found'
},
'expected_exception': jwplatform.errors.JWPlatformNotFoundError
},
{
'http_status': 400,
'response': {
'status': 'error',
'code': 'NoMethod',
def test_non_json_response_parsing():
url_expr = re.compile(r'https?://api\.test\.tst/v1/error\?.*')
responses.add(
responses.GET, url_expr,
status=502,
content_type='text/html',
body='502 Bad Gateway')
jwp_client = jwplatform.Client('api_key', 'api_secret', host='api.test.tst')
with pytest.raises(jwplatform.errors.JWPlatformUnknownError) as err:
jwp_client.error()
assert err.value.message == 'Not a valid JSON string: 502 Bad Gateway'
use_body = use_body if use_body is not None else http_method == 'POST'
url, params = self._client._build_request(self.path, kwargs)
if use_body:
_request_params['data'] = params
else:
_request_params['params'] = params
response = self._client._connection.request(
http_method, url, **_request_params)
try:
_response = response.json()
except ValueError:
raise errors.JWPlatformUnknownError(
'Not a valid JSON string: {}'.format(response.text))
except:
raise
if response.status_code != 200:
if _response['status'] == 'error':
try:
error_class = getattr(errors, 'JWPlatform{}Error'.format(
_response['code'].rstrip('Error')))
except AttributeError:
error_class = errors.JWPlatformUnknownError
raise error_class(_response['message'])
else:
errors.JWPlatformUnknownError(response.text)
else:
return _response
try:
_response = response.json()
except ValueError:
raise errors.JWPlatformUnknownError(
'Not a valid JSON string: {}'.format(response.text))
except:
raise
if response.status_code != 200:
if _response['status'] == 'error':
try:
error_class = getattr(errors, 'JWPlatform{}Error'.format(
_response['code'].rstrip('Error')))
except AttributeError:
error_class = errors.JWPlatformUnknownError
raise error_class(_response['message'])
else:
errors.JWPlatformUnknownError(response.text)
else:
return _response
except ValueError:
raise errors.JWPlatformUnknownError(
'Not a valid JSON string: {}'.format(response.text))
except:
raise
if response.status_code != 200:
if _response['status'] == 'error':
try:
error_class = getattr(errors, 'JWPlatform{}Error'.format(
_response['code'].rstrip('Error')))
except AttributeError:
error_class = errors.JWPlatformUnknownError
raise error_class(_response['message'])
else:
errors.JWPlatformUnknownError(response.text)
else:
return _response