api_particulier: catch ValueError or JSONDecodeError (#38781)

And adapt tests.
This commit is contained in:
Emmanuel Cazenave 2020-01-14 19:18:28 +01:00
parent 049266ba2d
commit 4af4d37161
2 changed files with 12 additions and 6 deletions

View File

@ -21,6 +21,12 @@
from collections import OrderedDict
import requests
try:
from json.decoder import JSONDecodeError
except ImportError:
JSONDecodeError = ValueError
from django.db import models
from django.utils import six
from django.utils.six.moves.urllib import parse
@ -83,7 +89,7 @@ class APIParticulier(BaseResource):
})
try:
data = response.json()
except ValueError as e:
except JSONDecodeError as e:
content = repr(response.content[:1000])
raise APIError(
u'API-particulier platform "%s" returned non-JSON content with status %s: %s' %

View File

@ -219,7 +219,7 @@ def test_error(app, resource, mock_api_particulier):
params=params)
assert resp.status_code == 200
assert resp.json['err'] == 1
assert resp.json['data']['exception'] == 'No JSON object could be decoded'
assert 'returned non-JSON content' in resp.json['err_desc']
vector = [
(['impots_svair', 'impots_adresse'], {
'numero_fiscal': 12,
@ -306,8 +306,8 @@ def test_detail_page(app, resource):
'connector': 'api-particulier',
'slug': 'test',
}))
assert 'API Particulier Prod' in response.content
assert 'family allowance' in response.content
assert 'fiscal information' in response.content
assert 'fiscal address' in response.content
assert 'API Particulier Prod' in response.text
assert 'family allowance' in response.text
assert 'fiscal information' in response.text
assert 'fiscal address' in response.text