1 # SPDX-License-Identifier: GPL-3.0-or-later
3 # This file is part of Nominatim. (https://nominatim.org)
5 # Copyright (C) 2026 by the Nominatim developer community.
6 # For a full list of authors see the git log.
8 Tests for the HTML debug logger.
10 The debug output is served as ``text/html``, so values that come from user input
11 or database should be HTML-escaped, while the markup the logger emits itself
14 from types import SimpleNamespace
16 import nominatim_api.logging as loglib
18 # A value containing HTML-significant characters (&, <, >).
19 HTML_VALUE = 'H-T-M-L & <b>BoldValue</b>'
22 def _fake_result(**kwargs):
23 """ Build a minimal object with the attributes result_dump() reads. """
24 defaults = dict(source_table=SimpleNamespace(name='PLACEX'),
25 names={'name': 'a place'},
27 category=('place', 'hamlet'),
29 osm_object=('N', 123),
32 defaults.update(kwargs)
33 return SimpleNamespace(**defaults)
36 def test_result_dump_escapes_html_in_values():
37 logger = loglib.HTMLLogger()
38 res = _fake_result(names={'name': HTML_VALUE}, country_code='a<b')
39 logger.result_dump('Results', iter([(0.0, res)]))
40 out = logger.get_buffer()
42 assert '<b>BoldValue</b>' not in out
43 assert '<b>BoldValue</b>' in out
44 assert 'a<b' in out
47 def test_result_dump_preserves_own_markup():
48 logger = loglib.HTMLLogger()
49 logger.result_dump('Results', iter([(0.0, _fake_result())]))
50 out = logger.get_buffer()
52 # The OSM link the logger builds itself must not be escaped away.
53 assert '<a href="https://www.openstreetmap.org/node/123">N123</a>' in out