# Path: via/exceptions.py
# class BadURL(RequestBasedException):
#     """An invalid URL was discovered."""
# 
#     def __init__(self, message, requests_err=None, url=None):
#         super().__init__(message, requests_err)
#         self.url = url
# 
#     status_int = 400
# 
# Path: via/resources.py
# class PathURLResource(_URLResource):
#     """Resource for routes expecting urls from the path."""
# 
#     def url_from_path(self):
#         """Get the 'url' parameter from the path.
# 
#         :return: The URL as a string
#         :raise HTTPBadRequest: If the URL is missing or empty
#         :raise BadURL: If the URL is malformed
#         """
# 
#         url = self._request.path_qs[1:].strip()
#         if not url:
#             raise HTTPBadRequest("Required path part 'url` is missing")
# 
#         return self._normalise_url(url)
# 
# 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)
# 
# def get_original_url(context):
#     """Get the original URL from a provided context object (if any)."""
# 
#     if isinstance(context, QueryURLResource):
#         getter = context.url_from_query
#     elif isinstance(context, PathURLResource):
#         getter = context.url_from_path
#     else:
#         return None
# 
#     try:
#         return getter()
#     except BadURL as err:
#         return err.url
#     except HTTPBadRequest:
#         return None


from unittest.mock import create_autospec
from pyramid.httpexceptions import HTTPBadRequest
from via.exceptions import BadURL
from via.resources import PathURLResource, QueryURLResource, get_original_url
import pytest
        return PathURLResource(pyramid_request)


class TestGetOriginalURL:
    def test_it_with_paths(self, path_url_resource):
        assert (
            get_original_url(path_url_resource)
            == path_url_resource.url_from_path.return_value
        )

    @pytest.mark.parametrize(
        "exc,expected", ((HTTPBadRequest, None), (BadURL("message", url="url"), "url"))
    )
    def test_it_with_bad_paths(self, path_url_resource, exc, expected):
        path_url_resource.url_from_path.side_effect = exc

        assert get_original_url(path_url_resource) == expected

    def test_it_with_queries(self, query_url_resource):
        assert (
            get_original_url(query_url_resource)
            == query_url_resource.url_from_query.return_value
        )

    @pytest.mark.parametrize(
        "exc,expected", ((HTTPBadRequest, None), (BadURL("message", url="url"), "url"))
    )
    def test_it_with_bad_queries(self, query_url_resource, exc, expected):
        query_url_resource.url_from_query.side_effect = exc


# Path: via/exceptions.py
# class BadURL(RequestBasedException):
#     """An invalid URL was discovered."""
# 
#     def __init__(self, message, requests_err=None, url=None):
#         super().__init__(message, requests_err)
#         self.url = url
# 
#     status_int = 400
# 
# Path: via/resources.py
# class PathURLResource(_URLResource):
#     """Resource for routes expecting urls from the path."""
# 
#     def url_from_path(self):
#         """Get the 'url' parameter from the path.
# 
#         :return: The URL as a string
#         :raise HTTPBadRequest: If the URL is missing or empty
#         :raise BadURL: If the URL is malformed
#         """
# 
#         url = self._request.path_qs[1:].strip()
#         if not url:
#             raise HTTPBadRequest("Required path part 'url` is missing")
# 
#         return self._normalise_url(url)
# 
# 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)
# 
# def get_original_url(context):
#     """Get the original URL from a provided context object (if any)."""
# 
#     if isinstance(context, QueryURLResource):
#         getter = context.url_from_query
#     elif isinstance(context, PathURLResource):
#         getter = context.url_from_path
#     else:
#         return None
# 
#     try:
#         return getter()
#     except BadURL as err:
#         return err.url
#     except HTTPBadRequest:
#         return None


# Path: tests/unit/via/resources_test.py
from unittest.mock import create_autospec
from pyramid.httpexceptions import HTTPBadRequest
from via.exceptions import BadURL
from via.resources import PathURLResource, QueryURLResource, get_original_url
import pytest

        return PathURLResource(pyramid_request)


class TestGetOriginalURL:
    def test_it_with_paths(self, path_url_resource):
        assert (
            get_original_url(path_url_resource)
            == path_url_resource.url_from_path.return_value
        )

    @pytest.mark.parametrize(
        "exc,expected", ((HTTPBadRequest, None), (BadURL("message", url="url"), "url"))
    )
    def test_it_with_bad_paths(self, path_url_resource, exc, expected):
        path_url_resource.url_from_path.side_effect = exc

        assert get_original_url(path_url_resource) == expected

    def test_it_with_queries(self, query_url_resource):
        assert (
            get_original_url(query_url_resource)
            == query_url_resource.url_from_query.return_value
        )

    @pytest.mark.parametrize(
        "exc,expected", ((HTTPBadRequest, None), (BadURL("message", url="url"), "url"))
    )
    def test_it_with_bad_queries(self, query_url_resource, exc, expected):
        query_url_resource.url_from_query.side_effect = exc


        assert get_original_url(query_url_resource) == expected