From 8a242e053ed55b33fc3673d9f76c210de072f26c Mon Sep 17 00:00:00 2001 From: Trygve Aspenes Date: Thu, 2 Jan 2025 14:52:15 +0100 Subject: [PATCH 1/2] improve parsing --- mapgen/modules/helpers.py | 10 +++++++-- tests/test_wsgi_app.py | 47 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 54 insertions(+), 3 deletions(-) diff --git a/mapgen/modules/helpers.py b/mapgen/modules/helpers.py index 9942b64..fd2db1e 100644 --- a/mapgen/modules/helpers.py +++ b/mapgen/modules/helpers.py @@ -1465,12 +1465,18 @@ def _get_mapfiles_path(regexp_pattern_module): return "./" def _parse_request(query_string): + query_string = query_string.replace("&%3b", "&") + query_string = query_string.replace("&%3B", "&") + query_string = query_string.replace("&", "&") + query_string = query_string.replace("%3D", "=") + query_string = query_string.replace("%3d", "=") + query_string = query_string.replace("%3F", "?") + query_string = query_string.replace("%3f", "?") + query_string = query_string.replace("?", "&") full_request = parse_qs(query_string) qp = {k.lower(): v for k, v in full_request.items()} logger.debug(f"QP: {qp}") - qp = {k.replace("amp;","") if k.startswith("amp;") else k:v for k,v in qp.items()} - logger.debug(f"QP after replace amp;: {qp}") qp = {k if (isinstance(v, list) and len(v) == 1) else k:v[0] for k,v in qp.items()} logger.debug(f"QP after flatten lists {qp}") return qp diff --git a/tests/test_wsgi_app.py b/tests/test_wsgi_app.py index d389bfc..fb051eb 100644 --- a/tests/test_wsgi_app.py +++ b/tests/test_wsgi_app.py @@ -9,7 +9,7 @@ from webtest import TestApp from mapgen.main import app from unittest.mock import patch, MagicMock -from mapgen.modules.helpers import HTTPError +from mapgen.modules.helpers import _parse_request, HTTPError from mapgen.modules.get_quicklook import get_quicklook from mapgen.modules.satellite_satpy_quicklook import _upload_geotiff_to_ceph, _exists_on_ceph, _generate_satpy_geotiff @@ -94,6 +94,51 @@ def test_get_quicklook_test1(caplog): assert res.status == '500 Internal Server Error' assert res.body == b"File Not Found: /test1.nc." +def test_parse_request(): + req= "REQUEST=GetCapabilities&SERVICE=WMS&VERSION=1.3.0" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + +def test_parse_request_lowercase(): + req= "request=GetCapabilities&service=WMS&version=1.3.0" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + +def test_parse_request_extra_questionmark(): + req= "request=GetCapabilities&service=WMS&version=1.3.0?service=WMS" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + +def test_parse_request_with_ampersand(): + req= "request=GetCapabilities&service=WMS&version=1.3.0&service=WMS" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + +def test_parse_request_with_ampersand_html(): + req= "request=GetCapabilities&service=WMS&version=1.3.0&%3bservice=WMS" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + +def test_parse_request_with_ampersand_html2(): + req= "request=GetCapabilities&service=WMS&version=1.3.0&%3Bservice=WMS" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + +def test_parse_request_with_question_mark_html(): + req= "request=GetCapabilities&service=WMS&version=1.3.0%3Fservice=WMS" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + +def test_parse_request_with_question_mark_html2(): + req= "request=GetCapabilities&service=WMS&version=1.3.0%3fservice=WMS" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + +def test_parse_request_various(): + req= "REQUEST=GetCapabilities&%3Brequest=GetCapabilities%3FSERVICE%3DWMS&%3Bversion=1.3.0&service=WMS" + result = _parse_request(req) + assert result == {'request': 'GetCapabilities', 'version': '1.3.0', 'service': 'WMS'} + @patch('mapgen.modules.satellite_satpy_quicklook._generate_satpy_geotiff') @patch('mapgen.modules.satellite_satpy_quicklook.rasterio.open') def test_get_quicklook_satpy_ordinary(rasterio_open, generate_satpy_geotiff): From f8a91c421f621d144f73a801c1fd5622514259af Mon Sep 17 00:00:00 2001 From: Trygve Aspenes Date: Thu, 2 Jan 2025 15:20:28 +0100 Subject: [PATCH 2/2] remove NA assert --- tests/test_arome_arctic.py | 3 --- 1 file changed, 3 deletions(-) diff --git a/tests/test_arome_arctic.py b/tests/test_arome_arctic.py index d322d02..4dda2e1 100644 --- a/tests/test_arome_arctic.py +++ b/tests/test_arome_arctic.py @@ -161,9 +161,6 @@ def test_read_dataset_request_getmap(mock_csw, mock_read_config, tmpdir, caplog) caplog.set_level(logging.DEBUG) response_code, result, content_type = get_quicklook(netcdf_path, query_string, http_host, url_scheme, products=[]) assert response_code == '200 OK' - assert ("QP after replace amp;: {'service': ['WMS'], 'version': ['1.3.0'], 'request': ['GetMap'], 'layers': ['air_temperature_2m'], " - "'width': ['767'], 'height': ['880'], 'crs': ['EPSG:3857'], 'bbox': ['-4822584.097826986,7566329.44660393,10215292.880334288,24819695.471091438'], " - "'styles': ['raster'], 'format': ['image/png'], 'transparent': ['TRUE'], 'time': ['2024-11-11T07:00:00Z']}") in caplog.text assert ("QP after flatten lists {'service': 'WMS', 'version': '1.3.0', 'request': 'GetMap', 'layers': 'air_temperature_2m', " "'width': '767', 'height': '880', 'crs': 'EPSG:3857', 'bbox': '-4822584.097826986,7566329.44660393,10215292.880334288,24819695.471091438', " "'styles': 'raster', 'format': 'image/png', 'transparent': 'TRUE', 'time': '2024-11-11T07:00:00Z'}") in caplog.text