Skip to content

Commit

Permalink
Add custom namespaces definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Anatoliy057 committed Oct 15, 2024
1 parent d3b1b64 commit 0da962e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
15 changes: 15 additions & 0 deletions tests/test_util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@
import logging.handlers
import sys
import unittest
from lxml import etree
from io import StringIO

from wsgidav import xml_tools
from wsgidav.util import (
BASE_LOGGER_NAME,
add_property_response,
check_tags,
checked_etag,
deep_update,
Expand All @@ -25,6 +28,7 @@
pop_path,
removeprefix,
shift_path,
to_str,
update_headers_in_place,
)

Expand Down Expand Up @@ -180,6 +184,17 @@ def testBasics(self):
assert get_dict_value(d, "c", as_dict=True) is False
assert get_dict_value(d, "x", as_dict=True) == {}
self.assertRaises(KeyError, get_dict_value, d, "x", as_dict=False)

multistatus_el = xml_tools.make_multistatus_el()
add_property_response(multistatus_el, "", [
('{custom}name', etree.Element("{custom}name", nsmap={"C": "custom"}))
])
assert to_str(xml_tools.xml_to_bytes(multistatus_el, pretty=False)) == (
'<?xml version=\'1.0\' encoding=\'UTF-8\'?>\n'
'<D:multistatus xmlns:D="DAV:"><D:response>'
'<D:href></D:href><D:propstat><D:prop><C:name xmlns:C="custom"/></D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat>'
'</D:response></D:multistatus>'
)


class LoggerTest(unittest.TestCase):
Expand Down
11 changes: 6 additions & 5 deletions wsgidav/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,7 +1306,7 @@ def add_property_response(multistatus_elem, href, prop_list):
"""
# Split prop_list by status code and build a unique list of namespaces
nsCount = 1
nsDict = {}
nsSet = set()
nsMap = {}
propDict = {}

Expand All @@ -1320,10 +1320,11 @@ def add_property_response(multistatus_elem, href, prop_list):
# Collect namespaces, so we can declare them in the <response> for
# compacter output
ns, _ = split_namespace(name)
if ns != "DAV:" and ns not in nsDict and ns != "":
nsDict[ns] = True
nsMap[f"NS{nsCount}"] = ns
nsCount += 1
if ns != "DAV:" and ns not in nsSet and ns != "":
if not is_etree_element(value) or ns not in value.nsmap.values():
nsMap[f"NS{nsCount}"] = ns
nsCount += 1
nsSet.add(ns)

propDict.setdefault(status, []).append((name, value))

Expand Down

0 comments on commit 0da962e

Please sign in to comment.