diff --git a/src/layman/util.py b/src/layman/util.py index d21f08136..182c132af 100644 --- a/src/layman/util.py +++ b/src/layman/util.py @@ -277,7 +277,7 @@ def call_modules_fn(modules, fn_name, args=None, kwargs=None, omit_duplicate_cal DUMB_MAP_ADAPTER_DICT = {} -def url_for(endpoint, *, internal=False, x_forwarded_prefix=None, **values): +def _url_for(endpoint, *, server_name, proxy_server_name, internal=False, x_forwarded_prefix=None, **values): assert not (internal and values.get('_external')) assert not (internal and x_forwarded_prefix) x_forwarded_prefix = x_forwarded_prefix or '' @@ -285,21 +285,28 @@ def url_for(endpoint, *, internal=False, x_forwarded_prefix=None, **values): assert (current_app.config.get('SERVER_NAME', None) is not None) == (current_app.config['TESTING'] is True) # Flask does not accept SERVER_NAME without dot, and without SERVER_NAME url_for cannot be used # therefore DUMB_MAP_ADAPTER_DICT is created manually ... - dumb_map_adapter = DUMB_MAP_ADAPTER_DICT.get(x_forwarded_prefix) + dict_key = f"{proxy_server_name} {x_forwarded_prefix}" + dumb_map_adapter = DUMB_MAP_ADAPTER_DICT.get(dict_key) if dumb_map_adapter is None: dumb_map_adapter = current_app.url_map.bind( - settings.LAYMAN_PROXY_SERVER_NAME + x_forwarded_prefix, + proxy_server_name + x_forwarded_prefix, url_scheme=current_app.config['PREFERRED_URL_SCHEME'] ) - DUMB_MAP_ADAPTER_DICT[x_forwarded_prefix] = dumb_map_adapter + DUMB_MAP_ADAPTER_DICT[dict_key] = dumb_map_adapter result = dumb_map_adapter.build(endpoint, values=values, force_external=True) if internal: _, netloc, path, query, fragment = urllib.parse.urlsplit(result) - netloc = settings.LAYMAN_SERVER_NAME + netloc = server_name result = urllib.parse.urlunsplit(('http', netloc, path, query, fragment)) return result +def url_for(endpoint, *, internal=False, x_forwarded_prefix=None, **values): + return _url_for(endpoint, server_name=settings.LAYMAN_SERVER_NAME, + proxy_server_name=settings.LAYMAN_PROXY_SERVER_NAME, + internal=internal, x_forwarded_prefix=x_forwarded_prefix, **values) + + def get_internal_sources(publ_type): return get_modules_from_names(get_publication_types()[publ_type]['internal_sources']) diff --git a/src/layman/util_test.py b/src/layman/util_test.py index 438d0754f..3535f809c 100644 --- a/src/layman/util_test.py +++ b/src/layman/util_test.py @@ -117,3 +117,20 @@ def test_publication_interface_methods(): def test_url_for(endpoint, internal, params, expected_url): with app.app_context(): assert util.url_for(endpoint, internal=internal, **params) == expected_url + + +@pytest.mark.parametrize('endpoint, internal, params, expected_url', [ + ('rest_workspace_maps.get', False, {'workspace': 'workspace_name'}, + f'http://enjoychallenge.tech/rest/{settings.REST_WORKSPACES_PREFIX}/workspace_name/maps'), + ('rest_workspace_layers.get', False, {'workspace': 'workspace_name'}, + f'http://enjoychallenge.tech/rest/{settings.REST_WORKSPACES_PREFIX}/workspace_name/layers'), + ('rest_about.get_version', True, {}, 'http://layman:8000/rest/about/version'), + ('rest_about.get_version', False, {}, 'http://enjoychallenge.tech/rest/about/version'), +]) +def test__url_for(endpoint, internal, params, expected_url): + server_name = 'layman:8000' + proxy_server_name = 'enjoychallenge.tech' + with app.app_context(): + # pylint: disable=protected-access + assert util._url_for(endpoint, server_name=server_name, proxy_server_name=proxy_server_name, internal=internal, + **params) == expected_url