Skip to content

Commit

Permalink
add exception test
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcSkovMadsen committed Sep 15, 2024
1 parent 19025e1 commit db11e11
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 28 deletions.
58 changes: 31 additions & 27 deletions panel/io/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,36 @@
from bokeh.server.contexts import BokehSessionContext
from pyviz_comms import Comm

def _get_location_params(protocol: str|None, host: str| None, uri: str| None)->dict:
params = {}
href = ''
if protocol:
params['protocol'] = href = f'{protocol}:'
if host:
href += f'//{host}'
if ':' in host:
params['hostname'], params['port'] = host.split(':')
else:
params['hostname'] = host
if uri:
search = hash = None
href += uri
if '?' in uri and '#' in uri:
params['pathname'], query = uri.split('?')
search, hash = query.split('#')
elif '?' in uri:
params['pathname'], search = uri.split('?')
elif '#' in uri:
params['pathname'], hash = uri.split('#')
else:
params['pathname'] = uri
if search:
params['search'] = f'?{search}'
if hash:
params['hash'] = f'#{hash}'
params['href'] = href
return params

class Location(Syncable):
"""
The Location component can be made available in a server context
Expand Down Expand Up @@ -70,33 +100,7 @@ def from_request(cls, request):
except ImportError:
return cls()

params = {}
href = ''
if request.protocol:
params['protocol'] = href = f'{request.protocol}:'
if request.host:
href += f'//{request.host}'
if ':' in request.host:
params['hostname'], params['port'] = request.host.split(':')
else:
params['hostname'] = request.host
if request.uri:
search = hash = None
href += request.uri
if '?' in request.uri and '#' in request.uri:
params['pathname'], query = request.uri.split('?')
search, hash = query.split('#')
elif '?' in request.uri:
params['pathname'], search = request.uri.split('?')
elif '#' in request.uri:
params['pathname'], hash = request.uri.split('#')
else:
params['pathname'] = request.uri
if search:
params['search'] = f'?{search}'
if hash:
params['hash'] = f'#{hash}'
params['href'] = href
params = _get_location_params(request.protocol, request.host, request.uri)
loc = cls()
with edit_readonly(loc):
loc.param.update(params)
Expand Down
10 changes: 9 additions & 1 deletion panel/tests/io/test_location.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import param
import pytest

from panel.io.location import Location
from panel.io.location import Location, _get_location_params
from panel.io.state import state
from panel.tests.util import serve_and_request, wait_until
from panel.util import edit_readonly
Expand Down Expand Up @@ -188,3 +188,11 @@ def test_location_sync_to_dataframe_with_initial_value(location, dataframe):
location.search = "?dataframe=%5B%7B%22x%22%3A+1%7D%5D"
location.sync(p)
pd.testing.assert_frame_equal(p.dataframe, dataframe)

@pytest.mark.parametrize(("protocol", "host", "uri"), [
# Started with the command fastapi dev script.py --root-path /some/path in VS Code terminal on JupyterHub
("http", "::ffff:172.20.0.233", "https,http://sub.domain.dk/some/path/panel")
])
def test_get_location_params(protocol, host, uri):
params = _get_location_params(protocol, host, uri)

Check failure on line 197 in panel/tests/io/test_location.py

View workflow job for this annotation

GitHub Actions / core:test-core:ubuntu-latest

test_get_location_params[http-::ffff:172.20.0.233-https,http://sub.domain.dk/some/path/panel] ValueError: too many values to unpack (expected 2)

Check failure on line 197 in panel/tests/io/test_location.py

View workflow job for this annotation

GitHub Actions / unit:test-310:ubuntu-latest

test_get_location_params[http-::ffff:172.20.0.233-https,http://sub.domain.dk/some/path/panel] ValueError: too many values to unpack (expected 2)

Check failure on line 197 in panel/tests/io/test_location.py

View workflow job for this annotation

GitHub Actions / unit:test-312:ubuntu-latest

test_get_location_params[http-::ffff:172.20.0.233-https,http://sub.domain.dk/some/path/panel] ValueError: too many values to unpack (expected 2)
assert params

0 comments on commit db11e11

Please sign in to comment.