Skip to main content

rumtk_web/utils/
testdata.rs

1/*
2 *     rumtk attempts to implement HL7 and medical protocols for interoperability in medicine.
3 *     This toolkit aims to be reliable, simple, performant, and standards compliant.
4 *     Copyright (C) 2026  Luis M. Santos, M.D.
5 *     Copyright (C) 2026  MedicalMasses L.L.C.
6 *
7 *     This program is free software: you can redistribute it and/or modify
8 *     it under the terms of the GNU General Public License as published by
9 *     the Free Software Foundation, either version 3 of the License, or
10 *     (at your option) any later version.
11 *
12 *     This program is distributed in the hope that it will be useful,
13 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
14 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 *     GNU General Public License for more details.
16 *
17 *     You should have received a copy of the GNU General Public License
18 *     along with this program.  If not, see <https://www.gnu.org/licenses/>.
19 */
20
21pub mod data {
22    use crate::form_data::{compile_form_data, FormResult};
23    use crate::{FormData, RouterForm};
24    use axum::extract::FromRequest;
25    use axum::extract::Request;
26    use axum::http::header::{CONTENT_LENGTH, CONTENT_TYPE, HOST};
27    use axum::http::Method;
28    use rumtk_core::core::RUMVec;
29    use rumtk_core::rumtk_resolve_task;
30    use std::io::Write;
31
32    type TESTDATA_REQUEST_FUNCTION = fn() -> Request;
33    type TESTDATA_REQUEST_BODY_FUNCTION = fn() -> RUMVec<u8>;
34    type TESTDATA_FORMDATA_FUNCTION = fn() -> FormData;
35
36    /// Credit: https://users.rust-lang.org/t/testing-multipart-form-data-fails-with-cryptic-error-message/100633
37    const TESTDATA_FORM_BODY: TESTDATA_REQUEST_BODY_FUNCTION = || {
38        let mut buffer = RUMVec::<u8>::new();
39
40        write!(buffer, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n").unwrap();
41        write!(
42            buffer,
43            "Content-Disposition: form-data; name=\"username\"\r\n"
44        )
45            .unwrap();
46        write!(buffer, "\r\n").unwrap();
47        write!(buffer, "JohnDoe\r\n").unwrap();
48        write!(buffer, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n").unwrap();
49        write!(
50            buffer,
51            "Content-Disposition: form-data; name=\"profile_pic\"; filename=\"avatar.png\"\r\n"
52        )
53            .unwrap();
54        write!(buffer, "Content-Type: image/png\r\n").unwrap();
55        write!(buffer, "\r\n").unwrap();
56        write!(buffer, "[Binary Data of the Image File]\r\n").unwrap();
57        write!(buffer, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n").unwrap();
58
59        buffer
60    };
61
62    const TESTDATA_FORM_BODY_EMPTY_BOUNDARY: TESTDATA_REQUEST_BODY_FUNCTION = || {
63        let mut buffer = RUMVec::<u8>::new();
64
65        write!(buffer, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n").unwrap();
66
67        buffer
68    };
69
70    pub const TESTDATA_FORMDATA_REQUEST: TESTDATA_REQUEST_FUNCTION = || -> Request {
71        let body_buffer = TESTDATA_FORM_BODY();
72        Request::builder()
73            .method(Method::POST)
74            .uri("/upload")
75            .header(HOST, "localhost")
76            .header(
77                CONTENT_TYPE,
78                "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
79            )
80            .body(body_buffer.into())
81            .unwrap()
82    };
83
84    pub const TESTDATA_FORMDATA_EMPTY_REQUEST: TESTDATA_REQUEST_FUNCTION = || -> Request {
85        let body_buffer = RUMVec::<u8>::new();
86        Request::builder()
87            .method(Method::POST)
88            .uri("/upload")
89            .header(HOST, "localhost")
90            .header(
91                CONTENT_TYPE,
92                "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
93            )
94            .body(body_buffer.into())
95            .unwrap()
96    };
97
98    pub const TESTDATA_FORMDATA_EMPTY_REQUEST_WITH_BOUNDARIES: TESTDATA_REQUEST_FUNCTION = || -> Request {
99        let body_buffer = TESTDATA_FORM_BODY_EMPTY_BOUNDARY();
100        Request::builder()
101            .method(Method::POST)
102            .uri("/upload")
103            .header(HOST, "localhost")
104            .header(
105                CONTENT_TYPE,
106                "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
107            )
108            .header(CONTENT_LENGTH, "38")
109            .body(body_buffer.into())
110            .unwrap()
111    };
112
113    pub const TESTDATA_EXPECTED_FORMDATA: TESTDATA_FORMDATA_FUNCTION = || -> FormData {
114        let mut expected_form = FormData::default();
115
116        expected_form
117            .form
118            .insert("username".into(), "JohnDoe".into());
119        expected_form.form.insert(
120            "profile_pic".into(),
121            "[Binary Data of the Image File]".into(),
122        );
123
124        expected_form
125    };
126
127    pub const TESTDATA_EXPECTED_FORMDATA_EMPTY: TESTDATA_FORMDATA_FUNCTION = || -> FormData {
128        let mut expected_form = FormData::default();
129
130        expected_form
131    };
132
133    async fn create_form(form_fxn: TESTDATA_REQUEST_FUNCTION) -> FormResult {
134        let req = form_fxn();
135        let mut raw_form = RouterForm::from_request(req, &())
136            .await
137            .expect("Multipart form expected.");
138        compile_form_data(&mut raw_form).await
139    }
140
141    pub fn create_test_form(form_fxn: TESTDATA_REQUEST_FUNCTION) -> FormResult {
142        let handle = create_form(form_fxn);
143        rumtk_resolve_task!(handle)
144    }
145
146    pub const UNTRIMMED_HTML_RENDER: &str = "\n        \n           \n        \n        <div class='div-default'>default</div>\n    \n        \n    ";
147    pub const RAW_HTML_PREFORMATTED: &str = "\n        <div>\n            <pre>\n                # started on Thu Apr 30 18:10:00 2026\n\n\n Performance counter stats for &#39;../target/debug/rumtk-hl7-v2-parse&#39;:\n\n         9,894,221      cache-references:u                                                    \n           386,828      cache-misses:u                   #    3.91% of all cache refs         \n       883,435,175      cycles:u                                                              \n     2,319,594,505      instructions:u                   #    2.63  insn per cycle            \n       353,631,271      branches:u                                                            \n             5,549      faults:u                                                              \n                 0      migrations:u                                                          \n\n       7.212089850 seconds time elapsed\n\n       0.208060000 seconds user\n       0.021617000 seconds sys\n\n\n\n            </pre>\n        </div>\n    ";
148    pub const TRIMMED_HTML_RENDER: &str = "<div class='div-default'>default</div>";
149    pub const TRIMMED_HTML_RENDER_META: &str = "<meta charset='UTF-8'><meta http-equiv='Content-Type' content='text/html; charset=utf-8' /><meta name='viewport' content='width=device-width, initial-scale=1.0' /><meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'/><meta name='description' content=''><title></title><link rel='icon' type='image/png' href='/static/img/icon.png'>";
150    pub const TRIMMED_HTML_TITLE_RENDER: &str = "<div class='f14 centered title-default-container'><a id='default'><h2 class='title-default'>DEFAULT</h2><h2 class='title-default-overlay no-select'>DEFAULT</h2></a></div>";
151    pub const JOB_LOADER_TEST_PATTERN: &str = "hx-trigger='every 2s' hx-swap='outerHTML' hx-target='#loader-";
152    pub const TRIMMED_HTML_PREFORMATTED: &str = "<div><pre>\n                # started on Thu Apr 30 18:10:00 2026\n\n\n Performance counter stats for &#39;../target/debug/rumtk-hl7-v2-parse&#39;:\n\n         9,894,221      cache-references:u                                                    \n           386,828      cache-misses:u                   #    3.91% of all cache refs         \n       883,435,175      cycles:u                                                              \n     2,319,594,505      instructions:u                   #    2.63  insn per cycle            \n       353,631,271      branches:u                                                            \n             5,549      faults:u                                                              \n                 0      migrations:u                                                          \n\n       7.212089850 seconds time elapsed\n\n       0.208060000 seconds user\n       0.021617000 seconds sys\n\n\n\n            </pre></div>";
153}
154