# Path: tests/conftest.py
# def assert_cache_control(headers, cache_parts):
#     """Assert that all parts of the Cache-Control header are present."""
#     assert dict(headers) == Any.dict.containing({"Cache-Control": Any.string()})
#     assert (
#         headers["Cache-Control"].split(", ") == Any.list.containing(cache_parts).only()
#     )
# 
# Path: tests/unit/matchers.py
# def temporary_redirect_to(location):
#     """Return a matcher for any `HTTP 302 Found` redirect to the given URL."""
#     return Any.instance_of(Response).with_attrs(
#         {"status_code": 302, "location": location}
#     )
# 
# Path: via/resources.py
# class QueryURLResource(_URLResource):
#     """Resource for routes expecting urls from the query."""
# 
#     def url_from_query(self):
#         """Get the 'url' parameter from the query.
# 
#         :return: The URL as a string
#         :raise HTTPBadRequest: If the URL is missing or empty
#         :raise BadURL: If the URL is malformed
#         """
#         try:
#             url = self._request.params["url"].strip()
#         except KeyError as err:
#             raise HTTPBadRequest("Required parameter 'url' missing") from err
# 
#         if not url:
#             raise HTTPBadRequest("Required parameter 'url' is blank")
# 
#         return self._normalise_url(url)
# 
# Path: via/views/route_by_content.py
# @view.view_config(route_name="route_by_content", decorator=(has_secure_url_token,))
# def route_by_content(context, request):
#     """Routes the request according to the Content-Type header."""
#     url = context.url_from_query()
# 
#     request.checkmate.raise_if_blocked(url)
# 
#     mime_type, status_code = get_url_details(
#         request.find_service(HTTPService), url, request.headers
#     )
#     via_client_svc = request.find_service(ViaClientService)
# 
#     if via_client_svc.is_pdf(mime_type):
#         caching_headers = _caching_headers(max_age=300)
#     else:
#         caching_headers = _cache_headers_for_http(status_code)
# 
#     params = dict(request.params)
#     params.pop("url", None)
# 
#     url = via_client_svc.url_for(url, mime_type, params)
# 
#     return exc.HTTPFound(url, headers=caching_headers)


from unittest.mock import create_autospec, sentinel
from tests.conftest import assert_cache_control
from tests.unit.matchers import temporary_redirect_to
from via.resources import QueryURLResource
from via.views.route_by_content import route_by_content
import pytest


@pytest.mark.usefixtures("via_client_service")
class TestRouteByContent:
    @pytest.mark.parametrize(
        "content_type,status_code,expected_cache_control_header",
        [
            ("PDF", 200, "public, max-age=300, stale-while-revalidate=86400"),
            ("HTML", 200, "public, max-age=60, stale-while-revalidate=86400"),
            ("HTML", 401, "public, max-age=60, stale-while-revalidate=86400"),
            ("HTML", 404, "public, max-age=60, stale-while-revalidate=86400"),
            ("HTML", 500, "no-cache"),
            ("HTML", 501, "no-cache"),
        ],
    )
    def test_it(
        self,
        content_type,
        context,
        expected_cache_control_header,
        get_url_details,
        pyramid_request,
        status_code,
        via_client_service,
        http_service,
    ):
        pyramid_request.params = {"url": sentinel.url, "foo": "bar"}
        get_url_details.return_value = (sentinel.mime_type, status_code)
        via_client_service.is_pdf.return_value = content_type == "PDF"


# Path: tests/conftest.py
# def assert_cache_control(headers, cache_parts):
#     """Assert that all parts of the Cache-Control header are present."""
#     assert dict(headers) == Any.dict.containing({"Cache-Control": Any.string()})
#     assert (
#         headers["Cache-Control"].split(", ") == Any.list.containing(cache_parts).only()
#     )
# 
# Path: tests/unit/matchers.py
# def temporary_redirect_to(location):
#     """Return a matcher for any `HTTP 302 Found` redirect to the given URL."""
#     return Any.instance_of(Response).with_attrs(
#         {"status_code": 302, "location": location}
#     )
# 
# Path: via/resources.py
# class QueryURLResource(_URLResource):
#     """Resource for routes expecting urls from the query."""
# 
#     def url_from_query(self):
#         """Get the 'url' parameter from the query.
# 
#         :return: The URL as a string
#         :raise HTTPBadRequest: If the URL is missing or empty
#         :raise BadURL: If the URL is malformed
#         """
#         try:
#             url = self._request.params["url"].strip()
#         except KeyError as err:
#             raise HTTPBadRequest("Required parameter 'url' missing") from err
# 
#         if not url:
#             raise HTTPBadRequest("Required parameter 'url' is blank")
# 
#         return self._normalise_url(url)
# 
# Path: via/views/route_by_content.py
# @view.view_config(route_name="route_by_content", decorator=(has_secure_url_token,))
# def route_by_content(context, request):
#     """Routes the request according to the Content-Type header."""
#     url = context.url_from_query()
# 
#     request.checkmate.raise_if_blocked(url)
# 
#     mime_type, status_code = get_url_details(
#         request.find_service(HTTPService), url, request.headers
#     )
#     via_client_svc = request.find_service(ViaClientService)
# 
#     if via_client_svc.is_pdf(mime_type):
#         caching_headers = _caching_headers(max_age=300)
#     else:
#         caching_headers = _cache_headers_for_http(status_code)
# 
#     params = dict(request.params)
#     params.pop("url", None)
# 
#     url = via_client_svc.url_for(url, mime_type, params)
# 
#     return exc.HTTPFound(url, headers=caching_headers)


# Path: tests/unit/via/views/route_by_content_test.py
from unittest.mock import create_autospec, sentinel
from tests.conftest import assert_cache_control
from tests.unit.matchers import temporary_redirect_to
from via.resources import QueryURLResource
from via.views.route_by_content import route_by_content
import pytest



@pytest.mark.usefixtures("via_client_service")
class TestRouteByContent:
    @pytest.mark.parametrize(
        "content_type,status_code,expected_cache_control_header",
        [
            ("PDF", 200, "public, max-age=300, stale-while-revalidate=86400"),
            ("HTML", 200, "public, max-age=60, stale-while-revalidate=86400"),
            ("HTML", 401, "public, max-age=60, stale-while-revalidate=86400"),
            ("HTML", 404, "public, max-age=60, stale-while-revalidate=86400"),
            ("HTML", 500, "no-cache"),
            ("HTML", 501, "no-cache"),
        ],
    )
    def test_it(
        self,
        content_type,
        context,
        expected_cache_control_header,
        get_url_details,
        pyramid_request,
        status_code,
        via_client_service,
        http_service,
    ):
        pyramid_request.params = {"url": sentinel.url, "foo": "bar"}
        get_url_details.return_value = (sentinel.mime_type, status_code)
        via_client_service.is_pdf.return_value = content_type == "PDF"


        response = route_by_content(context, pyramid_request)