From 43855546593185906f8bf460cf628847418958df Mon Sep 17 00:00:00 2001 From: Ben Millar Date: Fri, 7 Mar 2025 16:53:25 +0000 Subject: [PATCH] Add X-Robots-Tag=noindex to all response headers --- app/main/__init__.py | 1 + app/main/middleware.py | 8 ++++++++ tests/unit_tests/test_middleware.py | 19 +++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 app/main/middleware.py create mode 100644 tests/unit_tests/test_middleware.py diff --git a/app/main/__init__.py b/app/main/__init__.py index 83318cadf..335078647 100644 --- a/app/main/__init__.py +++ b/app/main/__init__.py @@ -5,6 +5,7 @@ from app.main import routes # noqa: E402,F401 from app.main import filters # noqa: E402,F401 +from app.main import middleware # noqa: E402,F401 def get_locale(): diff --git a/app/main/middleware.py b/app/main/middleware.py new file mode 100644 index 000000000..2dba9b422 --- /dev/null +++ b/app/main/middleware.py @@ -0,0 +1,8 @@ +from app.main import bp + + +@bp.after_app_request +def add_noindex_header(response): + """Add a noindex header to all responses, to disallow search engines from indexing the page.""" + response.headers["X-Robots-Tag"] = "noindex" + return response diff --git a/tests/unit_tests/test_middleware.py b/tests/unit_tests/test_middleware.py new file mode 100644 index 000000000..eb45972e0 --- /dev/null +++ b/tests/unit_tests/test_middleware.py @@ -0,0 +1,19 @@ +import pytest + + +@pytest.mark.parametrize( + "endpoint", + [ + "/", + "/children-families-relationships", + "/cookies", + "/assets/images/favicon.svg", + "/assets/scripts.js", + "/assets/styles.css", + "/404-page", + "/service-unavailable", + ], +) +def test_noindex_header_on_routes(client, endpoint): + response = client.get(endpoint) + assert response.headers.get("X-Robots-Tag") == "noindex"