aboutsummaryrefslogtreecommitdiff
path: root/src/routes/mod.rs
blob: 759375d82b3bc8ccd16758c8716103dc96192d92 (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
use actix_web::{web::{route, scope, Query}, HttpRequest, Result, Scope};
use actix_web::http::StatusCode;
use messages::*;
use std::collections::HashMap;

pub mod format_msg;
mod callback;
mod not_understood;

pub use self::format_msg::FormatMsg;
use self::callback::callback;
use self::not_understood::not_understood;

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

// Provides the routes for the application
pub fn get_scope() -> Scope {
    scope("/api")
        .service(scope("/callback").default_service(route().to(callback)))
        .default_service(route().to(not_understood))
}

// 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};
    use actix_web::{App, dev::Service, test::{block_on, init_service}};
    use actix_web::{HttpResponse, dev::Body};
    use serde::Deserialize;
    use std::str;

    #[test]
    fn test_get_scope_callback() {
        // Arrange
        let req = TestRequest::with_uri("/api/callback").to_request();
        let scope = get_scope();
        let mut srv = init_service(App::new().service(scope));

        // Act
        let resp = &block_on(srv.call(req)).unwrap();

        // Assert
        assert_eq!(resp.status(), StatusCode::OK);

        let content = get_message::<OutgoingMsg<Callback>>(resp.response());
        assert_eq!(content.result_type, "CALLBACK");
        assert_eq!(content.content.path, vec!["api", "callback"]);
    }

    #[test]
    fn test_get_scope_not_understood() {
        // Arrange
        let req = TestRequest::with_uri("/api/404").to_request();
        let scope = get_scope();
        let mut srv = init_service(App::new().service(scope));

        // Act
        let resp = &block_on(srv.call(req)).unwrap();

        // Assert
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);

        let content = get_message::<OutgoingMsg<NotUnderstood>>(resp.response());
        assert_eq!(content.result_type, "NOT_UNDERSTOOD");
        assert_eq!(content.content.path, vec!["api", "404"]);
    }

    #[test]
    fn test_get_scope_blank() {
        // Arrange
        let req = TestRequest::with_uri("/").to_request();
        let scope = get_scope();
        let mut srv = init_service(App::new().service(scope));

        // Act
        let resp = block_on(srv.call(req)).unwrap();

        // Assert
        assert_eq!(resp.status(), StatusCode::NOT_FOUND);
    }

    #[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!(result.is_empty());
    }

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

        // Act
        let result = destruct_path(path);

        // Assert
        assert!(result.is_empty());
    }

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

    pub 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()
    }

    pub fn get_message<'a, T: Deserialize<'a>>(response: &'a HttpResponse) -> T {
        let body = response.body().as_ref().unwrap();
        let mut array = &[b'0';0][..];
        match body {
            Body::Bytes(b) => {
                array = b.as_ref();
            },
            _ => {},
        };

        let van = str::from_utf8(array).unwrap();
        serde_json::from_str(van).unwrap()
    }
}