Skip to content

Commit

Permalink
Merge pull request #95 from metno/issue92
Browse files Browse the repository at this point in the history
improve parsing
  • Loading branch information
TAlonglong authored Jan 2, 2025
2 parents a422628 + f8a91c4 commit a11fb79
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 6 deletions.
10 changes: 8 additions & 2 deletions mapgen/modules/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1465,12 +1465,18 @@ def _get_mapfiles_path(regexp_pattern_module):
return "./"

def _parse_request(query_string):
query_string = query_string.replace("&amp%3b", "&")
query_string = query_string.replace("&amp%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
3 changes: 0 additions & 3 deletions tests/test_arome_arctic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 46 additions & 1 deletion tests/test_wsgi_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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&amp%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&amp%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&amp%3Brequest=GetCapabilities%3FSERVICE%3DWMS&amp%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):
Expand Down

0 comments on commit a11fb79

Please sign in to comment.