Skip to content

Commit

Permalink
Split url_for into two methods
Browse files Browse the repository at this point in the history
  • Loading branch information
jirik committed Aug 30, 2023
1 parent 9e7ab02 commit d9d9874
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
17 changes: 12 additions & 5 deletions src/layman/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,29 +277,36 @@ 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 ''
# It seems SERVER_NAME is not None only in some tests. It also seems TESTING is True only in the same tests.
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'])

Expand Down
17 changes: 17 additions & 0 deletions src/layman/util_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit d9d9874

Please sign in to comment.