aboutsummaryrefslogtreecommitdiff
path: root/src/routes/mod.rs
blob: e0b17cf47466d6291fb849c806b833caa5167c7b (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use actix_web::{web::Json, web::Query, HttpRequest, Result};
use std::collections::HashMap;

pub mod messages;
use messages::*;

type JsonMessage<U> = Result<Json<OutgoingMsg<U>>>;

// Sends a default response message when requested an undefined resource.
pub fn not_understood(req: HttpRequest) -> JsonMessage<NotUnderstood> {
    let message = NotUnderstood {
        path: destruct_path(req.path()),
    };

    Ok(Json(message.as_outgoing()))
}

// Sends Callback message with information from HttpRequest.
pub fn callback(req: HttpRequest, query: Query<HashMap<String, String>>) -> JsonMessage<Callback> {
    let path = req.path();
    let method = req.method().as_str();

    let callback = Callback {
        path: destruct_path(path),
        request: String::from(method),
        content: query.into_inner(),
    };

    Ok(Json(callback.as_outgoing()))
}

// Takes an HttpRequest path and splits it into an array.
fn destruct_path(path: &str) -> Vec<String> {
    path.split_terminator("/")
        // first element is always blank due to link starting with "/api"
        .skip(1)
        .map(String::from)
        .collect::<Vec<String>>()
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::{http::Method, test::TestRequest};

    #[test]
    fn test_not_understood() {
        // Arrange
        let uri = "/api/phpmyadmin/index.rs";
        let req = gen_request(uri, None);

        // Act
        let result = not_understood(req);

        // Assert
        assert_eq!(result.is_ok(), true);

        let val = result.unwrap().into_inner();
        assert_eq!(val.result_type, "NOT_UNDERSTOOD");
        assert_eq!(val.content.path, vec!["api", "phpmyadmin", "index.rs"]);
    }

    #[test]
    fn test_not_understood_blank() {
        // Arrange
        let uri = "/";
        let req = gen_request(uri, None);

        // Act
        let result = not_understood(req);

        // Assert
        assert_eq!(result.is_ok(), true);

        let val = result.unwrap().into_inner();
        assert_eq!(val.result_type, "NOT_UNDERSTOOD");
        assert_eq!(val.content.path.is_empty(), true);
    }

    #[test]
    fn test_callback_get() {
        // Arrange
        let uri = "/api/phpmyadmin/index.rs";
        let req = gen_request(uri, None);

        let mut ref_map = HashMap::new();
        ref_map.insert("hello".to_string(), "world".to_string());
        ref_map.insert("id".to_string(), "10011".to_string());
        let query = gen_query(&ref_map);

        // Act
        let result = callback(req, query);

        // Assert
        assert_eq!(result.is_ok(), true);

        let val = result.unwrap().into_inner();
        assert_eq!(val.result_type, "CALLBACK");
        assert_eq!(val.content.path, vec!["api", "phpmyadmin", "index.rs"]);
        assert_eq!(val.content.request, "GET");
        assert_eq!(val.content.content, ref_map);
    }

    #[test]
    fn test_callback_post() {
        // Arrange
        let uri = "/api/phpmyadmin/index.rs";
        let req = gen_request(uri, Some(Method::POST));

        let mut ref_map = HashMap::new();
        ref_map.insert("hello".to_string(), "world".to_string());
        ref_map.insert("id".to_string(), "10012".to_string());
        let query = gen_query(&ref_map);

        // Act
        let result = callback(req, query);

        // Assert
        assert_eq!(result.is_ok(), true);

        let val = result.unwrap().into_inner();
        assert_eq!(val.result_type, "CALLBACK");
        assert_eq!(val.content.path, vec!["api", "phpmyadmin", "index.rs"]);
        assert_eq!(val.content.request, "POST");
        assert_eq!(val.content.content, ref_map);
    }

    #[test]
    fn test_callback_blank() {
        // Arrange
        let uri = "/";
        let req = gen_request(uri, None);
        let query = Query::from_query("").unwrap();

        // Act
        let result = callback(req, query);

        // Assert
        assert_eq!(result.is_ok(), true);

        let val = result.unwrap().into_inner();
        assert_eq!(val.result_type, "CALLBACK");
        assert_eq!(val.content.path.is_empty(), true);
        assert_eq!(val.content.request, "GET");
        assert_eq!(val.content.content.is_empty(), true);
    }

    #[test]
    fn test_destruct_path() {
        // Arrange
        let path = "/api/storm/breaking";

        // Act
        let result = destruct_path(path);

        // Assert
        assert_eq!(result, vec!["api", "storm", "breaking"]);
    }

    #[test]
    fn test_destruct_path_blank() {
        // Arrange
        let path = "/";

        // Act
        let result = destruct_path(path);

        // Assert
        assert_eq!(result.is_empty(), true);
    }

    #[test]
    fn test_destruct_path_empty() {
        // Arrange
        let path = "";

        // Act
        let result = destruct_path(path);

        // Assert
        assert_eq!(result.is_empty(), true);
    }

    fn gen_request(path: &str, method: Option<Method>) -> HttpRequest {
        TestRequest::with_uri(path)
            .method(method.unwrap_or(Method::GET))
            .to_http_request()
    }

    fn gen_query(map: &HashMap<String, String>) -> Query<HashMap<String, String>> {
        let mut query_str = String::new();
        for (key, val) in map.iter() {
            query_str.push_str(&format!("&{}={}", key, val));
        }

        Query::from_query(&query_str).unwrap()
    }
}