|
2 | 2 |
|
3 | 3 | import pytest |
4 | 4 | import dash |
5 | | -from dash import Dash, _pages |
| 5 | +from dash import Dash, _pages, html |
6 | 6 | from mock import patch |
7 | 7 |
|
8 | 8 |
|
@@ -76,3 +76,49 @@ def test_import_layouts_from_pages( |
76 | 76 |
|
77 | 77 | page_entry = list(dash.page_registry.values())[0] |
78 | 78 | assert page_entry["module"] == expected_module_name |
| 79 | + |
| 80 | + |
| 81 | +def _route(client, pathname): |
| 82 | + response = client.post( |
| 83 | + "/_dash-update-component", |
| 84 | + json={ |
| 85 | + "output": ".._pages_content.children..._pages_store.data..", |
| 86 | + "outputs": [ |
| 87 | + {"id": "_pages_content", "property": "children"}, |
| 88 | + {"id": "_pages_store", "property": "data"}, |
| 89 | + ], |
| 90 | + "inputs": [ |
| 91 | + {"id": "_pages_location", "property": "pathname", "value": pathname}, |
| 92 | + {"id": "_pages_location", "property": "search", "value": ""}, |
| 93 | + ], |
| 94 | + "state": [], |
| 95 | + "changedPropIds": ["_pages_location.pathname"], |
| 96 | + }, |
| 97 | + ) |
| 98 | + return response.get_json()["response"]["_pages_content"]["children"] |
| 99 | + |
| 100 | + |
| 101 | +@pytest.mark.parametrize("use_async", [False, True]) |
| 102 | +def test_router_decodes_pathname(clear_pages_state, use_async): |
| 103 | + if use_async: |
| 104 | + pytest.importorskip("asgiref") |
| 105 | + app = Dash(__name__, use_pages=True, pages_folder="", use_async=use_async) |
| 106 | + dash.register_page( |
| 107 | + "report", |
| 108 | + path_template="/report/<name>", |
| 109 | + layout=lambda name=None, **_: html.Div(name, id="name"), |
| 110 | + ) |
| 111 | + dash.register_page("cafe", path="/caf\u00e9", layout=html.Div(id="cafe")) |
| 112 | + app.layout = html.Div(dash.page_container) |
| 113 | + client = app.server.test_client() |
| 114 | + client.get("/") |
| 115 | + |
| 116 | + # dcc.Location sends window.location.pathname, which browsers percent-encode |
| 117 | + assert _route(client, "/report/hello%20world")["props"]["children"] == ( |
| 118 | + "hello world" |
| 119 | + ) |
| 120 | + assert _route(client, "/report/%E6%97%A5%E6%9C%AC")["props"]["children"] == ( |
| 121 | + "\u65e5\u672c" |
| 122 | + ) |
| 123 | + assert _route(client, "/report/a%0Ab")["props"]["children"] == "a\nb" |
| 124 | + assert _route(client, "/caf%C3%A9")["props"]["id"] == "cafe" |
0 commit comments