# Path: tests/common/requests_exceptions.py
# def make_requests_exception(
#     error_class, status_code, json_data=None, raw_data=None, **response_kwargs
# ):
#     response = Response()
#     response.status_code = status_code
# 
#     for key, value in response_kwargs.items():
#         setattr(response, key, value)
# 
#     if raw_data:
#         response.raw = BytesIO(raw_data.encode("utf-8"))
# 
#     elif json_data:
#         response.raw = BytesIO(json.dumps(json_data).encode("utf-8"))
# 
#     return error_class(response=response)
# 
# Path: via/exceptions.py
# class RequestBasedException(Exception):
#     """An exception based on a requests error."""
# 
#     def __init__(self, message, requests_err=None):
#         super().__init__(message)
# 
#         self.request = requests_err.request if requests_err else None
#         self.response = requests_err.response if requests_err else None
# 
#     def __str__(self):
#         string = super().__str__()
# 
#         if self.response is None:
#             return string
# 
#         # Log the details of the response. This goes to both Sentry and the
#         # application's logs. It's helpful for debugging to know how the
#         # external service responded.
# 
#         return " ".join(
#             [
#                 part
#                 for part in [
#                     f"{string}:",
#                     str(self.response.status_code or ""),
#                     self.response.reason,
#                     self.response.text,
#                 ]
#                 if part
#             ]
#         )


import pytest
from requests.exceptions import HTTPError
from tests.common.requests_exceptions import make_requests_exception
from via.exceptions import RequestBasedException

class TestRequestBasedException:
    @pytest.mark.parametrize(
        "error_params,expected_string",
        (
            (
                {
                    "status_code": 400,
                    "reason": "Bad request",
                    "json_data": {"test": "data", "more": "data"},
                },
                'message: 400 Bad request {"test": "data", "more": "data"}',
            ),
            (
                {
                    "status_code": 401,
                    "raw_data": "<html>HTML response text!</html>",
                },
                "message: 401 <html>HTML response text!</html>",
            ),
            (
                {
                    "status_code": 402,
                },
                "message: 402",
            ),
        ),
    )
    def test_it(self, error_params, expected_string):
        exception = RequestBasedException(

# Path: tests/common/requests_exceptions.py
# def make_requests_exception(
#     error_class, status_code, json_data=None, raw_data=None, **response_kwargs
# ):
#     response = Response()
#     response.status_code = status_code
# 
#     for key, value in response_kwargs.items():
#         setattr(response, key, value)
# 
#     if raw_data:
#         response.raw = BytesIO(raw_data.encode("utf-8"))
# 
#     elif json_data:
#         response.raw = BytesIO(json.dumps(json_data).encode("utf-8"))
# 
#     return error_class(response=response)
# 
# Path: via/exceptions.py
# class RequestBasedException(Exception):
#     """An exception based on a requests error."""
# 
#     def __init__(self, message, requests_err=None):
#         super().__init__(message)
# 
#         self.request = requests_err.request if requests_err else None
#         self.response = requests_err.response if requests_err else None
# 
#     def __str__(self):
#         string = super().__str__()
# 
#         if self.response is None:
#             return string
# 
#         # Log the details of the response. This goes to both Sentry and the
#         # application's logs. It's helpful for debugging to know how the
#         # external service responded.
# 
#         return " ".join(
#             [
#                 part
#                 for part in [
#                     f"{string}:",
#                     str(self.response.status_code or ""),
#                     self.response.reason,
#                     self.response.text,
#                 ]
#                 if part
#             ]
#         )


# Path: tests/unit/via/exceptions_test.py
import pytest
from requests.exceptions import HTTPError
from tests.common.requests_exceptions import make_requests_exception
from via.exceptions import RequestBasedException


class TestRequestBasedException:
    @pytest.mark.parametrize(
        "error_params,expected_string",
        (
            (
                {
                    "status_code": 400,
                    "reason": "Bad request",
                    "json_data": {"test": "data", "more": "data"},
                },
                'message: 400 Bad request {"test": "data", "more": "data"}',
            ),
            (
                {
                    "status_code": 401,
                    "raw_data": "<html>HTML response text!</html>",
                },
                "message: 401 <html>HTML response text!</html>",
            ),
            (
                {
                    "status_code": 402,
                },
                "message: 402",
            ),
        ),
    )
    def test_it(self, error_params, expected_string):
        exception = RequestBasedException(

            "message", requests_err=make_requests_exception(HTTPError, **error_params)