Skip to content

Commit

Permalink
BGDIINF_SB-2197: Returns 415 in case of wrong content-type
Browse files Browse the repository at this point in the history
If the request was done with an invalid content-type, then the service
returned a 400, "No 'geom' given, cannot create a profile without coordinates".

This was kind of misleading, for example when doing a request with curl without
specifying the content-type (curl used application/x-www-form-urlencoded) but
with valid payload.

The issue was that `request.is_json` in flask is set based on the content-type header
and not on the payload being a valid json.
  • Loading branch information
ltshb committed Feb 24, 2022
1 parent e67f887 commit 4b84eb8
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
9 changes: 6 additions & 3 deletions app/helpers/validation/profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,12 @@ def read_linestring():
geom_to_shape = None
if 'geom' in request.args:
linestring = request.args.get('geom')
elif request.is_json and request.content_length is not None and \
0 < request.content_length < max_content_length:
linestring = request.get_data(as_text=True) # read as text
elif request.method == 'POST':
if not request.is_json:
abort(415)
if request.content_length and 0 < request.content_length < max_content_length:
linestring = request.get_data(as_text=True) # read as text

if not linestring:
abort(400, "No 'geom' given, cannot create a profile without coordinates")
try:
Expand Down
9 changes: 9 additions & 0 deletions tests/unit_tests/test_profile.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,15 @@ def test_profile_lv03_layers_post(self, mock_georaster_utils):
)
self.assertEqual(resp.content_type, 'application/json')

@patch('app.routes.georaster_utils')
def test_profile_lv03_layers_post_content_type_With_charset(self, mock_georaster_utils):
params = create_json(4, 21781)
self.headers['Content-Type'] = 'application/json; charset=utf-8'
resp = self.prepare_mock_and_test_post(
mock_georaster_utils=mock_georaster_utils, body=params, expected_status=200
)
self.assertEqual(resp.content_type, 'application/json')

@patch('app.routes.georaster_utils')
def test_profile_lv03_layers_none(self, mock_georaster_utils):
resp = self.prepare_mock_and_test_json_profile(
Expand Down
14 changes: 12 additions & 2 deletions tests/unit_tests/test_profile_validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ class TestProfileValidation(unittest.TestCase):
def setUp(self) -> None:
service_alti.app.config['TESTING'] = True
self.test_instance = service_alti.app.test_client()
self.headers = DEFAULT_HEADERS

def assert_response(self, response, expected_status=200):
self.assertIsNotNone(response)
Expand All @@ -47,7 +46,7 @@ def prepare_mock_and_test(
'nb_points': nb_points,
'offset': offset
},
headers=self.headers
headers=DEFAULT_HEADERS
)

@patch('app.routes.georaster_utils')
Expand Down Expand Up @@ -88,6 +87,17 @@ def test_profile_validation_valid_offset_none(self, mock_georaster_utils):
)
self.assert_response(response)

@patch('app.routes.georaster_utils')
def test_profile_validation_wrong_content_type(self, mock_georaster_utils):
prepare_mock(mock_georaster_utils)
response = self.test_instance.post(
'/rest/services/profile.json',
headers={
**DEFAULT_HEADERS, 'Content-Type': 'text/plain'
}
)
self.assert_response(response, expected_status=415)

@patch('app.routes.georaster_utils')
def test_profile_validation_no_linestring(self, mock_georaster_utils):
response = self.prepare_mock_and_test(
Expand Down

0 comments on commit 4b84eb8

Please sign in to comment.