mocktail/
response.rs

1//! Mock response
2use super::{body::Body, headers::Headers, status::StatusCode};
3
4/// Represents a HTTP response.
5#[derive(Debug, Clone, PartialEq)]
6pub struct Response {
7    pub status: StatusCode,
8    pub headers: Headers,
9    pub body: Body,
10    pub message: Option<String>,
11}
12
13impl Response {
14    pub fn new(body: impl Into<Body>) -> Self {
15        Self {
16            status: StatusCode::default(),
17            headers: Headers::default(),
18            body: body.into(),
19            message: None,
20        }
21    }
22
23    pub fn with_status(mut self, status: impl Into<StatusCode>) -> Self {
24        self.status = status.into();
25        self
26    }
27
28    pub fn with_headers(mut self, headers: Headers) -> Self {
29        self.headers = headers;
30        self
31    }
32
33    pub fn with_message(mut self, message: impl Into<String>) -> Self {
34        self.message = Some(message.into());
35        self
36    }
37
38    pub fn status(&self) -> &StatusCode {
39        &self.status
40    }
41
42    pub fn headers(&self) -> &Headers {
43        &self.headers
44    }
45
46    pub fn body(&self) -> &Body {
47        &self.body
48    }
49
50    pub fn message(&self) -> Option<&str> {
51        self.message.as_deref()
52    }
53
54    pub fn is_ok(&self) -> bool {
55        self.status.is_ok()
56    }
57
58    pub fn is_error(&self) -> bool {
59        self.status.is_error()
60    }
61}
62
63impl Default for Response {
64    fn default() -> Self {
65        Self {
66            status: StatusCode::OK,
67            headers: Headers::default(),
68            body: Body::default(),
69            message: None,
70        }
71    }
72}