1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
use crate::header::{key::HeaderKey, value::HeaderValue, values::HeaderValues};

pub type AsserhttpResult<T> = Result<T, AsserhttpError>;

#[derive(Debug, thiserror::Error)]
#[cfg_attr(feature = "internal", derive(Eq, PartialEq))]
pub enum AsserhttpError {
    #[error("expected status to be '{expected}' but was '{actual}'")]
    StatusMismatch { actual: u16, expected: u16 },
    #[error("expected header '{key}' to be equal to '{expected}' but was '{actual}'")]
    HeaderValueMismatch {
        key: HeaderKey,
        actual: HeaderValue,
        expected: HeaderValue,
    },
    #[error("expected one header named '{key}' but none found")]
    HeaderAbsent { key: HeaderKey },
    #[error("expected no header named '{key}' but one found")]
    HeaderPresent { key: HeaderKey },
    #[error("expected header '{key}' to be single valued. Had '{values_count}' values '{actual_values}'. Use 'expect_headers' instead")]
    MultivaluedHeader {
        key: HeaderKey,
        values_count: usize,
        actual_values: HeaderValues,
    },
    #[error("no value expected for header '{key}'. Use 'expect_header_present' instead")]
    InvalidHeaderValuesSupplied { key: HeaderKey },
    #[error("expected header '{key}' to contain values '{expected}' but contained '{actual_values}'")]
    HeaderValuesMismatch {
        key: HeaderKey,
        actual_values: HeaderValues,
        expected: HeaderValues,
    },
    #[error("{0}")]
    JsonBodyMismatch(String),
    #[error("expected body to be '{expected}' but was '{actual}'")]
    TextBodyMismatch { actual: String, expected: String },
    #[error("expected body to be '{expected:?}' but was '{actual:?}'")]
    BytesBodyMismatch { actual: Vec<u8>, expected: Vec<u8> },
    #[error("expected body to match regex '{regex}' but was '{actual}'")]
    RegexBodyMismatch { actual: String, regex: String },
    #[error("expected a response body but none was present")]
    BodyAbsent,
    #[error("expected no response body but one was present")]
    BodyPresent,
    #[error("Error in the http client: {0}")]
    HttpError(String),
    #[error("{0}")]
    RegexError(String),
    #[error("Internal asserhttp error")]
    InternalError,
    #[cfg(not(feature = "internal"))]
    #[error(transparent)]
    AnyError(#[from] anyhow::Error),
    #[cfg(not(feature = "internal"))]
    #[error(transparent)]
    HttpTypesError(anyhow::Error),
    #[cfg(not(feature = "internal"))]
    #[error(transparent)]
    JsonError(#[from] serde_json::Error),
    #[cfg(not(feature = "internal"))]
    #[error(transparent)]
    IoError(#[from] std::io::Error),
    #[cfg(not(feature = "internal"))]
    #[error(transparent)]
    Utf8Error(#[from] std::str::Utf8Error),
    #[cfg(all(not(feature = "internal"), feature = "awc"))]
    #[error(transparent)]
    AwcError(#[from] actix_http::error::PayloadError),
    #[cfg(all(not(feature = "internal"), feature = "reqwest"))]
    #[error(transparent)]
    ReqwestError(#[from] reqwest::Error),
    #[error("Error in a third party crate: {0}")]
    ExternalError(String),
}

#[cfg(feature = "internal")]
impl From<anyhow::Error> for AsserhttpError {
    fn from(e: anyhow::Error) -> Self {
        Self::ExternalError(e.to_string())
    }
}

#[cfg(not(feature = "internal"))]
impl From<http_types::Error> for AsserhttpError {
    fn from(e: http_types::Error) -> Self {
        Self::HttpTypesError(e.into_inner())
    }
}

#[cfg(feature = "internal")]
impl From<http_types::Error> for AsserhttpError {
    fn from(e: http_types::Error) -> Self {
        Self::ExternalError(e.to_string())
    }
}

#[cfg(feature = "internal")]
impl From<serde_json::Error> for AsserhttpError {
    fn from(e: serde_json::Error) -> Self {
        Self::ExternalError(e.to_string())
    }
}

#[cfg(feature = "internal")]
impl From<std::io::Error> for AsserhttpError {
    fn from(e: std::io::Error) -> Self {
        Self::ExternalError(e.to_string())
    }
}

#[cfg(feature = "internal")]
impl From<std::str::Utf8Error> for AsserhttpError {
    fn from(e: std::str::Utf8Error) -> Self {
        Self::ExternalError(e.to_string())
    }
}

#[cfg(all(feature = "internal", feature = "actix"))]
impl From<actix_http::error::PayloadError> for AsserhttpError {
    fn from(e: actix_http::error::PayloadError) -> Self {
        Self::ExternalError(e.to_string())
    }
}

#[cfg(all(feature = "internal", feature = "reqwest"))]
impl From<reqwest::Error> for AsserhttpError {
    fn from(e: reqwest::Error) -> Self {
        Self::ExternalError(e.to_string())
    }
}

#[cfg(feature = "axum")]
impl From<axum::Error> for AsserhttpError {
    fn from(e: axum::Error) -> Self {
        Self::ExternalError(e.to_string())
    }
}

impl From<regex::Error> for AsserhttpError {
    fn from(e: regex::Error) -> Self {
        Self::RegexError(e.to_string())
    }
}