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
pub use error::SpaError;
pub use spa::{Spa, spa};
pub use spa_service::SpaService;

mod error;
mod spa;
mod spa_service;
mod utils;

#[cfg(test)]
mod tests {
    use std::str::from_utf8;

    use actix_web::{App, body::MessageBody, dev::ServiceFactory, Error, http::StatusCode, test};
    use actix_web::dev::{ServiceRequest, ServiceResponse};

    use super::*;

    /// Create a test application with SPA service
    fn test_app() -> App<
        impl ServiceFactory<
            ServiceRequest,
            Response=ServiceResponse<impl MessageBody>,
            Config=(),
            InitError=(),
            Error=Error,
        >,
    > {
        App::new().service(
            Spa::default()
                .index_file("./fixtures/001/index.html")
                .static_resources_location("./fixtures/001")
                .finish(),
        )
    }

    /// Test: Returns the index file for root path
    #[actix_web::test]
    async fn returns_index() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::default().to_request();
        let res = test::call_service(&app, req).await;

        assert_eq!(res.status(), StatusCode::OK);

        let body = test::read_body(res).await;
        let html = from_utf8(&body).unwrap();
        assert!(html.contains("Home page"));
    }

    /// Test: Returns a specific page for a given path
    #[actix_web::test]
    async fn returns_page() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::default().uri("/page").to_request();
        let res = test::call_service(&app, req).await;

        assert_eq!(res.status(), StatusCode::OK);

        let body = test::read_body(res).await;
        let html = from_utf8(&body).unwrap();
        assert!(html.contains("Sample Page"));
    }

    /// Test: Returns an item page for a specific path
    #[actix_web::test]
    async fn returns_item_page() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::default().uri("/dog/items/cat").to_request();
        let res = test::call_service(&app, req).await;

        assert_eq!(res.status(), StatusCode::OK);

        let body = test::read_body(res).await;
        let html = from_utf8(&body).unwrap();
        assert!(html.contains("Item Page"));
    }

    /// Test: Returns the index file for unknown paths
    #[actix_web::test]
    async fn unknown_page_returns_index() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::default().uri("/fsociety").to_request();
        let res = test::call_service(&app, req).await;

        assert_eq!(res.status(), StatusCode::OK);

        let body = test::read_body(res).await;
        let html = from_utf8(&body).unwrap();
        assert!(html.contains("Home page"));
    }

    /// Test: Returns static assets correctly
    #[actix_web::test]
    async fn returns_assets() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::default().uri("/next.svg").to_request();
        let res = test::call_service(&app, req).await;

        assert_eq!(res.status(), StatusCode::OK);

        let body = test::read_body(res).await;
        let svg = from_utf8(&body).unwrap();
        assert!(svg.contains(r#"<svg xmlns="http://www.w3.org/2000/svg" fill="none""#));
    }

    /// Test: Returns a dynamic numeric page
    #[cfg(feature = "wildcards")]
    #[actix_web::test]
    async fn test_returns_dynamic_numeric_page() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::default().uri("/1/items/1").to_request();
        let res = test::call_service(&app, req).await;

        assert_eq!(res.status(), StatusCode::OK);

        let body = test::read_body(res).await;
        let html = from_utf8(&body).unwrap();
        assert!(html.contains("Item Page"));
    }

    /// Test: Returns a dynamic character page
    #[cfg(feature = "wildcards")]
    #[actix_web::test]
    async fn test_returns_dynamic_character_page() {
        let app = test::init_service(test_app()).await;

        let req = test::TestRequest::default().uri("/3b2b6d56-e85b-432d-b555-7113b810a3b7/items/1").to_request();
        let res = test::call_service(&app, req).await;

        assert_eq!(res.status(), StatusCode::OK);

        let body = test::read_body(res).await;
        let html = from_utf8(&body).unwrap();
        assert!(html.contains("Item Page"));
    }

    /// Test: Handles build manifest not found
    #[cfg(feature = "wildcards")]
    #[actix_web::test]
    async fn handles_build_manifest_not_found() {
        let app = test::init_service(
            App::new().service(
                Spa::default()
                    .index_file("./fixtures/001/index.html")
                    .static_resources_location("./fixtures/no_manifest")
                    .finish(),
            )
        ).await;

        let req = test::TestRequest::default().uri("/").to_request();
        let res = test::call_service(&app, req).await;

        assert_eq!(res.status(), StatusCode::OK);

        let body = test::read_body(res).await;
        let html = from_utf8(&body).unwrap();
        assert!(html.contains("Home page"));
    }
}