map: don't fail on invalid geojson data (#53521)
This commit is contained in:
parent
a71c673437
commit
ea21ad5b00
|
@ -226,8 +226,19 @@ class MapLayer(models.Model):
|
|||
headers={'accept': 'application/json'},
|
||||
)
|
||||
if not response.ok:
|
||||
return []
|
||||
data = response.json()
|
||||
return {
|
||||
'type': 'FeatureCollection',
|
||||
'features': [],
|
||||
'_combo_err_desc': "Bad status code from requested URL",
|
||||
}
|
||||
try:
|
||||
data = response.json()
|
||||
except json.JSONDecodeError:
|
||||
return {
|
||||
'type': 'FeatureCollection',
|
||||
'features': [],
|
||||
'_combo_err_desc': "Non JSON response from requested URL",
|
||||
}
|
||||
if 'features' in data:
|
||||
features = data['features']
|
||||
else:
|
||||
|
|
|
@ -7,6 +7,7 @@ from django.conf import settings
|
|||
from django.contrib.auth.models import Group, User
|
||||
from django.test.client import RequestFactory
|
||||
from django.urls import reverse
|
||||
from requests.models import Response
|
||||
|
||||
from combo.apps.maps.models import Map, MapLayer, MapLayerOptions
|
||||
from combo.data.models import Page
|
||||
|
@ -284,6 +285,20 @@ def test_get_geojson(app, layer, user):
|
|||
|
||||
geojson_url = reverse('mapcell-geojson', kwargs={'cell_id': cell.id, 'layer_slug': layer.slug})
|
||||
|
||||
# invalid content
|
||||
with mock.patch('combo.utils.requests_wrapper.RequestsSession.get') as requests_get:
|
||||
mock_resp = Response()
|
||||
mock_resp.status_code = 200
|
||||
requests_get.return_value = mock_resp
|
||||
resp = app.get(geojson_url)
|
||||
assert len(resp.json['features']) == 0
|
||||
assert resp.json['_combo_err_desc'] == 'Non JSON response from requested URL'
|
||||
|
||||
mock_resp.status_code = 500
|
||||
resp = app.get(geojson_url)
|
||||
assert len(resp.json['features']) == 0
|
||||
assert resp.json['_combo_err_desc'] == 'Bad status code from requested URL'
|
||||
|
||||
# check cache duration
|
||||
with mock.patch('combo.utils.requests_wrapper.RequestsSession.request') as requests_get:
|
||||
requests_get.return_value = mock.Mock(
|
||||
|
|
Loading…
Reference in New Issue