# Path: via/requests_tools/headers.py
# BANNED_HEADERS = {
#     # Requests needs to set Host to the right thing for us
#     "Host",
#     # Don't pass Cookies
#     "Cookie",
#     # AWS things
#     "X-Amzn-Trace-Id",
#     # AWS NGINX / NGINX things
#     "X-Real-Ip",
#     "X-Forwarded-Proto",
#     "X-Forwarded-Port",
#     # Cloudflare things
#     "Cf-Request-Id",
#     "Cf-Connecting-Ip",
#     "Cf-Ipcountry",
#     "Cf-Ray",
#     "Cf-Visitor",
#     "Cdn-Loop",
# }
# 
# HEADER_DEFAULTS = {
#     # Mimic what it looks like if you got here from a Google search
#     "Referer": "https://www.google.com",
#     "Sec-Fetch-Dest": "document",
#     "Sec-Fetch-Mode": "navigate",
#     "Sec-Fetch-Site": "cross-site",
#     "Sec-Fetch-User": "?1",
#     # This gets overwritten by AWS NGINX
#     "Connection": "keep-alive",
# }
# 
# HEADER_MAP = {"Dnt": "DNT"}
# 
# def add_request_headers(headers):
#     """Add headers for 3rd party providers which we access data from."""
# 
#     # Pass our abuse policy in request headers for third-party site admins.
#     headers["X-Abuse-Policy"] = "https://web.hypothes.is/abuse-policy/"
#     headers["X-Complaints-To"] = "https://web.hypothes.is/report-abuse/"
# 
#     return headers
# 
# def clean_headers(headers):
#     """Remove Cloudflare and other cruft from the headers.
# 
#     This will attempt to present a clean version of the headers which can be
#     used to make a request to the 3rd party site.
# 
#     Also:
# 
#       * Map the headers as if we had come from a Google Search
#       * Remove things added by AWS and Cloudflare etc.
#       * Fix some header names
# 
#     This is intended to return a list of headers that can be passed on to the
#     upstream service.
# 
#     :param headers: A mapping of header values
#     :return: An OrderedDict of cleaned headers
#     """
#     clean = OrderedDict()
# 
#     for header_name, value in headers.items():
#         if header_name in BANNED_HEADERS:
#             continue
# 
#         # Map to standard names for things
#         header_name = HEADER_MAP.get(header_name, header_name)
# 
#         # Add in defaults for certain fields
#         value = HEADER_DEFAULTS.get(header_name, value)
# 
#         clean[header_name] = value
# 
#     return clean


from collections import OrderedDict
from via.requests_tools.headers import (
    BANNED_HEADERS,
    HEADER_DEFAULTS,
    HEADER_MAP,
    add_request_headers,
    clean_headers,
)
import pytest




class TestCleanHeaders:
    def test_basics(self):
        headers = {"Most-Headers": "are passed through", "Preserving": "order"}

        result = clean_headers(headers)

        assert result == headers
        assert list(result.keys()) == ["Most-Headers", "Preserving"]
        assert isinstance(result, OrderedDict)

    # For the following tests we are going to lean heavily on the defined lists
    # of headers, and just test that the function applies them correctly. Other
    # wise we just end-up copy pasting them here to no real benefit

    @pytest.mark.parametrize("header_name", BANNED_HEADERS)
    def test_we_remove_banned_headers(self, header_name):
        result = clean_headers({header_name: "value"})

        assert header_name not in result

    @pytest.mark.parametrize("header_name,default_value", HEADER_DEFAULTS.items())
    def test_we_assign_to_defaults_if_present(self, header_name, default_value):
        result = clean_headers({header_name: "some custom default"})

        assert result[header_name] == default_value


# Path: via/requests_tools/headers.py
# BANNED_HEADERS = {
#     # Requests needs to set Host to the right thing for us
#     "Host",
#     # Don't pass Cookies
#     "Cookie",
#     # AWS things
#     "X-Amzn-Trace-Id",
#     # AWS NGINX / NGINX things
#     "X-Real-Ip",
#     "X-Forwarded-Proto",
#     "X-Forwarded-Port",
#     # Cloudflare things
#     "Cf-Request-Id",
#     "Cf-Connecting-Ip",
#     "Cf-Ipcountry",
#     "Cf-Ray",
#     "Cf-Visitor",
#     "Cdn-Loop",
# }
# 
# HEADER_DEFAULTS = {
#     # Mimic what it looks like if you got here from a Google search
#     "Referer": "https://www.google.com",
#     "Sec-Fetch-Dest": "document",
#     "Sec-Fetch-Mode": "navigate",
#     "Sec-Fetch-Site": "cross-site",
#     "Sec-Fetch-User": "?1",
#     # This gets overwritten by AWS NGINX
#     "Connection": "keep-alive",
# }
# 
# HEADER_MAP = {"Dnt": "DNT"}
# 
# def add_request_headers(headers):
#     """Add headers for 3rd party providers which we access data from."""
# 
#     # Pass our abuse policy in request headers for third-party site admins.
#     headers["X-Abuse-Policy"] = "https://web.hypothes.is/abuse-policy/"
#     headers["X-Complaints-To"] = "https://web.hypothes.is/report-abuse/"
# 
#     return headers
# 
# def clean_headers(headers):
#     """Remove Cloudflare and other cruft from the headers.
# 
#     This will attempt to present a clean version of the headers which can be
#     used to make a request to the 3rd party site.
# 
#     Also:
# 
#       * Map the headers as if we had come from a Google Search
#       * Remove things added by AWS and Cloudflare etc.
#       * Fix some header names
# 
#     This is intended to return a list of headers that can be passed on to the
#     upstream service.
# 
#     :param headers: A mapping of header values
#     :return: An OrderedDict of cleaned headers
#     """
#     clean = OrderedDict()
# 
#     for header_name, value in headers.items():
#         if header_name in BANNED_HEADERS:
#             continue
# 
#         # Map to standard names for things
#         header_name = HEADER_MAP.get(header_name, header_name)
# 
#         # Add in defaults for certain fields
#         value = HEADER_DEFAULTS.get(header_name, value)
# 
#         clean[header_name] = value
# 
#     return clean


# Path: tests/unit/via/requests_tools/headers_test.py
from collections import OrderedDict
from via.requests_tools.headers import (
    BANNED_HEADERS,
    HEADER_DEFAULTS,
    HEADER_MAP,
    add_request_headers,
    clean_headers,
)
import pytest





class TestCleanHeaders:
    def test_basics(self):
        headers = {"Most-Headers": "are passed through", "Preserving": "order"}

        result = clean_headers(headers)

        assert result == headers
        assert list(result.keys()) == ["Most-Headers", "Preserving"]
        assert isinstance(result, OrderedDict)

    # For the following tests we are going to lean heavily on the defined lists
    # of headers, and just test that the function applies them correctly. Other
    # wise we just end-up copy pasting them here to no real benefit

    @pytest.mark.parametrize("header_name", BANNED_HEADERS)
    def test_we_remove_banned_headers(self, header_name):
        result = clean_headers({header_name: "value"})

        assert header_name not in result

    @pytest.mark.parametrize("header_name,default_value", HEADER_DEFAULTS.items())
    def test_we_assign_to_defaults_if_present(self, header_name, default_value):
        result = clean_headers({header_name: "some custom default"})

        assert result[header_name] == default_value


    @pytest.mark.parametrize("header_name", HEADER_DEFAULTS.keys())