Files
Radicale/radicale/tests/test_web.py
TowyTowy 651e30211c Fix: serve_resource/serve_folder ignore mimetypes and fallback_mimetype parameters
httputils._serve_traversable looked up the Content-Type in the
module-level MIMETYPES/FALLBACK_MIMETYPE constants instead of the
mimetypes/fallback_mimetype parameters that serve_resource() and
serve_folder() accept and pass through. The parameters exist since the
helper was extracted for use by web plugins (33fcda7c, "Extract
httputils.serve_folder"), and the sibling parameters path_prefix and
index_file are honored, but a custom web plugin passing its own
mimetype mapping (e.g. to serve .json, .ico or .mjs files with a
correct Content-Type) silently got the built-in mapping and
application/octet-stream fallback instead.

Use the parameters for the lookup. No behavior change for the built-in
web module, which relies on the defaults.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-18 12:10:37 +02:00

73 lines
2.9 KiB
Python

# This file is part of Radicale - CalDAV and CardDAV server
# Copyright © 2018-2019 Unrud <unrud@outlook.com>
#
# This library is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Radicale. If not, see <http://www.gnu.org/licenses/>.
"""
Test web plugin.
"""
from radicale import httputils
from radicale.tests import BaseTest
class TestBaseWebRequests(BaseTest):
"""Test web plugin."""
def test_internal(self) -> None:
_, headers, _ = self.request("GET", "/.web", check=302)
assert headers.get("Location") == "/.web/"
_, answer = self.get("/.web/")
assert answer
self.post("/.web", check=405)
_, answer = self.get("/.web/js/config.js")
assert "export const PREFER_BROWSER_LOGIN = false;" in answer
self.configure({"web": {"prefer_browser_login": "True"}})
_, answer = self.get("/.web/js/config.js")
assert "export const PREFER_BROWSER_LOGIN = true;" in answer
def test_none(self) -> None:
self.configure({"web": {"type": "none"}})
_, answer = self.get("/.web")
assert answer
_, headers, _ = self.request("GET", "/.web/", check=302)
assert headers.get("Location") == "/.web"
self.post("/.web", check=405)
def test_custom(self) -> None:
"""Custom web plugin."""
self.configure({"web": {"type": "radicale.tests.custom.web"}})
_, answer = self.get("/.web")
assert answer == "custom"
_, answer = self.post("/.web", "body content")
assert answer == "echo:body content"
def test_serve_resource_custom_mimetypes(self) -> None:
"""serve_resource must honor mimetypes and fallback_mimetype."""
status, headers, _, _ = httputils.serve_resource(
"radicale.web", "internal_data", "", "/.web/index.html")
assert status == 200
assert dict(headers)["Content-Type"] == "text/html"
status, headers, _, _ = httputils.serve_resource(
"radicale.web", "internal_data", "", "/.web/index.html",
mimetypes={".html": "text/x-custom"})
assert status == 200
assert dict(headers)["Content-Type"] == "text/x-custom"
status, headers, _, _ = httputils.serve_resource(
"radicale.web", "internal_data", "", "/.web/index.html",
mimetypes={}, fallback_mimetype="application/x-fallback")
assert status == 200
assert dict(headers)["Content-Type"] == "application/x-fallback"