aboutsummaryrefslogtreecommitdiff
path: root/src/routes/format_msg.rs
blob: 22f0691380410dffa2ff2e63c229c662df3d6676 (plain) (blame)
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
use actix_web::{body::BoxBody, http::StatusCode, HttpRequest, HttpResponse, Responder};
use serde::Serialize;

pub struct FormatMsg<T> {
    pub message: T,
    pub code: StatusCode,
}

impl<T> FormatMsg<T> {
    /// Deconstruct to an inner value
    pub fn into_inner(self) -> T {
        self.message
    }

    pub fn ok(message: T) -> Self {
        FormatMsg {
            message: message,
            code: StatusCode::OK,
        }
    }
}

impl<T: Serialize> Responder for FormatMsg<T> {
    type Body = BoxBody;

    fn respond_to(self, _: &HttpRequest) -> HttpResponse<BoxBody> {
        let body = match serde_json::to_string(&self.message) {
            Ok(serstr) => serstr,
            Err(e) => return HttpResponse::from_error(e),
        };

        HttpResponse::build(self.code)
            .content_type("application/json")
            .body(body)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::routes::*;
    use crate::routes::tests::*;
    use serde::ser::{Error, Serializer};

    #[test]
    fn test_into_inner() {
        // Arrange
        let msg = NotUnderstood {path: vec![]};
        let msg_ref = msg.clone();
        let formatted = FormatMsg {
            message: msg.as_outgoing(),
            code: StatusCode::OK,
        };

        // Act
        let result = formatted.into_inner();

        // Assert
        assert_eq!(result.result_type, "NOT_UNDERSTOOD");
        assert_eq!(result.content, msg_ref);
    }

    #[test]
    fn test_ok() {
        // Arrange
        let msg = NotUnderstood {path: vec![]};
        let msg_ref = msg.clone();

        // Act
        let result = FormatMsg::ok(msg);

        // Assert
        assert_eq!(result.message, msg_ref);
        assert_eq!(result.code, StatusCode::OK);
    }

    #[test]
    fn test_responder() {
        // Arrange
        let msg = NotUnderstood {path: vec![]};
        let msg_ref = msg.clone();
        let formatted = FormatMsg {
            message: msg,
            code: StatusCode::NOT_FOUND,
        };
        let request = gen_request("/api/404", None);

        // Act
        let result = formatted.respond_to(&request);

        // Assert
        assert_eq!(result.status(), StatusCode::NOT_FOUND);
        assert_eq!(result.headers().get("content-type").unwrap(), "application/json");

        let resp = get_message::<NotUnderstood>(result);
        assert!(resp.is_ok());
        let content = resp.unwrap();
        assert_eq!(content, msg_ref);
    }

    struct InvalidMessage {}

    impl Serialize for InvalidMessage {
        fn serialize<S>(&self, _: S) -> Result<S::Ok, S::Error> where S: Serializer {
            Err(Error::custom("oops".to_string()))
        }
    }

    #[test]
    fn test_responder_serde_error() {
        // Arrange
        let msg = InvalidMessage {};
        let formatted = FormatMsg {
            message: msg,
            code: StatusCode::NOT_FOUND,
        };
        let request = gen_request("/api/404", None);

        // Act
        let result = formatted.respond_to(&request);

        // Assert
        assert!(result.error().is_some());
    }
}