From 7e9f5e4d59d2a5671232227245701bd2453e13a5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jelmer=20Vernoo=C4=B3?= Date: Thu, 9 Nov 2023 20:05:58 +0000 Subject: [PATCH] Add tests for href_to_path --- xandikos/tests/test_webdav.py | 19 ++++++++++++++++++- xandikos/webdav.py | 2 +- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/xandikos/tests/test_webdav.py b/xandikos/tests/test_webdav.py index 8f0c797d..d5abe3bd 100644 --- a/xandikos/tests/test_webdav.py +++ b/xandikos/tests/test_webdav.py @@ -24,7 +24,7 @@ from xandikos import webdav -from ..webdav import ET, Collection, Property, Resource, WebDAVApp +from ..webdav import ET, Collection, Property, Resource, WebDAVApp, href_to_path class WebTestCase(unittest.TestCase): @@ -518,3 +518,20 @@ def test_recode(self): "/blü", webdav.path_from_environ({"PATH_INFO": "/bl\xc3\xbc"}, "PATH_INFO"), ) + + +class HrefToPathTests(unittest.TestCase): + + def test_outside(self): + self.assertIs(None, href_to_path({'SCRIPT_NAME': '/dav'}, '/bar')) + + def test_root(self): + self.assertEqual('/', href_to_path({'SCRIPT_NAME': '/dav'}, '/dav')) + self.assertEqual('/', href_to_path({'SCRIPT_NAME': '/dav/'}, '/dav')) + self.assertEqual('/', href_to_path({'SCRIPT_NAME': '/dav/'}, '/dav/')) + self.assertEqual('/', href_to_path({'SCRIPT_NAME': '/dav'}, '/dav/')) + + def test_relpath(self): + self.assertEqual('/foo', href_to_path({'SCRIPT_NAME': '/dav'}, '/dav/foo')) + self.assertEqual('/foo', href_to_path({'SCRIPT_NAME': '/dav/'}, '/dav/foo')) + self.assertEqual('/foo/', href_to_path({'SCRIPT_NAME': '/dav/'}, '/dav/foo/')) diff --git a/xandikos/webdav.py b/xandikos/webdav.py index 41f1ddde..6e4106bc 100644 --- a/xandikos/webdav.py +++ b/xandikos/webdav.py @@ -1478,7 +1478,7 @@ def get_resources(self, relpaths) -> Iterator[tuple[str, Optional[Resource]]]: def href_to_path(environ, href) -> Optional[str]: - script_name = environ["SCRIPT_NAME"] + script_name = environ["SCRIPT_NAME"].rstrip('/') if not href or not href.startswith(script_name): return None else: