# Path: via/views/debug.py
# @view.view_config(route_name="debug_headers")
# def debug_headers(_context, request):
#     """Dump the headers as we receive them for debugging."""
# 
#     if request.GET.get("raw"):
#         headers = OrderedDict(request.headers)
#     else:
#         headers = clean_headers(request.headers)
# 
#     self_url = request.route_url("debug_headers")
# 
#     return Response(
#         body=f"""
#             <h1>Instructions</h1>
#             <ol>
#                 <li>Access the service directly (not thru *.hypothes.is)
#                 <li>Enable Do-Not-Track if supported</li>
#                 <li>
#                     <a href="{self_url}">
#                         Click here to get referer
#                     </a>
#                 </li>
#                 <li>Press F5 to get 'Cache-Control'</li>
#             </ol>
# 
#             <a href="{self_url}?raw=1">Show all headers</a>
# 
#             <hr>
# 
#             <h1>Headers received</h1>
#             <pre>{json.dumps(headers, indent=4)}</pre><br>
#         """,
#         status=200,
#     )


from unittest.mock import create_autospec, sentinel
from h_matchers import Any
from pyramid.testing import DummyRequest
from via.views.debug import debug_headers
import pytest

class TestDebugHeaders:
    def test_it(self, pyramid_request):
        pyramid_request.headers = {"Key": "Value"}
        response = debug_headers(sentinel.context, pyramid_request)

        pyramid_request.route_url.assert_called_once_with("debug_headers")

        assert response.status_code == 200
        assert response.text == Any.string.containing(
            pyramid_request.route_url.return_value
        )
        assert response.text == Any.string.containing("Key")
        assert response.text == Any.string.containing("Value")

    def test_it_does_not_clean_headers_with_raw_true(
        self, pyramid_request, clean_headers, OrderedDict
    ):
        pyramid_request.GET["raw"] = "1"

        debug_headers(sentinel.context, pyramid_request)

        clean_headers.assert_not_called()
        OrderedDict.assert_called_once_with(pyramid_request.headers)

    def test_it_cleans_headers_with_raw_false(
        self, pyramid_request, clean_headers, OrderedDict
    ):
        pyramid_request.GET["raw"] = ""


# Path: via/views/debug.py
# @view.view_config(route_name="debug_headers")
# def debug_headers(_context, request):
#     """Dump the headers as we receive them for debugging."""
# 
#     if request.GET.get("raw"):
#         headers = OrderedDict(request.headers)
#     else:
#         headers = clean_headers(request.headers)
# 
#     self_url = request.route_url("debug_headers")
# 
#     return Response(
#         body=f"""
#             <h1>Instructions</h1>
#             <ol>
#                 <li>Access the service directly (not thru *.hypothes.is)
#                 <li>Enable Do-Not-Track if supported</li>
#                 <li>
#                     <a href="{self_url}">
#                         Click here to get referer
#                     </a>
#                 </li>
#                 <li>Press F5 to get 'Cache-Control'</li>
#             </ol>
# 
#             <a href="{self_url}?raw=1">Show all headers</a>
# 
#             <hr>
# 
#             <h1>Headers received</h1>
#             <pre>{json.dumps(headers, indent=4)}</pre><br>
#         """,
#         status=200,
#     )


# Path: tests/unit/via/views/debug_test.py
from unittest.mock import create_autospec, sentinel
from h_matchers import Any
from pyramid.testing import DummyRequest
from via.views.debug import debug_headers
import pytest


class TestDebugHeaders:
    def test_it(self, pyramid_request):
        pyramid_request.headers = {"Key": "Value"}
        response = debug_headers(sentinel.context, pyramid_request)

        pyramid_request.route_url.assert_called_once_with("debug_headers")

        assert response.status_code == 200
        assert response.text == Any.string.containing(
            pyramid_request.route_url.return_value
        )
        assert response.text == Any.string.containing("Key")
        assert response.text == Any.string.containing("Value")

    def test_it_does_not_clean_headers_with_raw_true(
        self, pyramid_request, clean_headers, OrderedDict
    ):
        pyramid_request.GET["raw"] = "1"

        debug_headers(sentinel.context, pyramid_request)

        clean_headers.assert_not_called()
        OrderedDict.assert_called_once_with(pyramid_request.headers)

    def test_it_cleans_headers_with_raw_false(
        self, pyramid_request, clean_headers, OrderedDict
    ):
        pyramid_request.GET["raw"] = ""


        debug_headers(sentinel.context, pyramid_request)