Skip to content

Commit

Permalink
Add tests for href_to_path
Browse files Browse the repository at this point in the history
  • Loading branch information
jelmer committed Nov 9, 2023
1 parent c2b667a commit 4d16338
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
19 changes: 18 additions & 1 deletion xandikos/tests/test_webdav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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/'))
2 changes: 1 addition & 1 deletion xandikos/webdav.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 4d16338

Please sign in to comment.