Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion mkdocs_rss_plugin/templates/rss.xml.jinja2
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
{% if item.link is not none %}<source url="{{ feed.rss_url }}">{{ feed.title }}</source>{% endif %}
{% if item.comments_url is not none %}<comments>{{ item.comments_url|e }}</comments>{% endif %}
{% if item.guid is not none %}<guid isPermaLink="true">{{ item.guid }}</guid>{% endif %}
{% if item.image is not none %}
{% if item.image is not none and item.image[2] is not none and item.image[2] > 0 %}
<enclosure url="{{ item.image[0] }}" type="{{ item.image[1] }}" length="{{ item.image[2] }}" />
{% endif %}
</item>
Expand Down
78 changes: 78 additions & 0 deletions tests/test_enclosure_guard.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#! python3 # noqa: E265

"""Test that <enclosure> is omitted when image length is None or non-positive.

When a remote image returns 404, the length field becomes None,
which would produce invalid XML like <enclosure length="None" />.

Usage from the repo root folder:

python -m unittest tests.test_enclosure_guard

"""

# #############################################################################
# ########## Libraries #############
# ##################################

# Standard library
import logging
import tempfile
from pathlib import Path
from traceback import format_exception

# test suite
from tests.base import BaseTest

# -- Globals --
logger = logging.getLogger(__name__)
logger.setLevel(logging.DEBUG)

OUTPUT_RSS_FEED_CREATED = "feed_rss_created.xml"

# #############################################################################
# ########## Classes ###############
# ##################################


class TestEnclosureGuard(BaseTest):
"""Test that <enclosure> is properly guarded against invalid length values."""

def test_enclosure_guard_none_length(self):
"""Verify that <enclosure> is omitted when image length is None or non-positive.

When a remote image returns 404, the length field becomes None,
which would produce invalid XML like <enclosure length="None" />.
"""
with tempfile.TemporaryDirectory() as tmpdirname:
cli_result = self.build_docs_setup(
testproject_path="docs",
mkdocs_yml_filepath=Path("tests/fixtures/mkdocs_complete.yml"),
output_path=tmpdirname,
strict=True,
)

if cli_result.exception is not None:
e = cli_result.exception
logger.debug(format_exception(type(e), e, e.__traceback__))

self.assertEqual(cli_result.exit_code, 0)
self.assertIsNone(cli_result.exception)

# Read raw XML content
rss_path = Path(tmpdirname) / OUTPUT_RSS_FEED_CREATED
rss_content = rss_path.read_text()

# Check that no enclosure has length="None" or length="0" or negative
self.assertNotIn(
'length="None"',
rss_content,
"Found enclosure with length='None'. "
"The template should guard against None length values.",
)
self.assertNotIn(
'length="0"',
rss_content,
"Found enclosure with length='0'. "
"The template should guard against zero length values.",
)