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
21use std::io::Write;
22
23use crate::form_data::{compile_form_data, FormResult};
24use crate::{FormData, RouterForm};
25use axum::extract::{FromRequest, Request};
26use axum::http::header::{CONTENT_TYPE, HOST};
27use axum::http::Method;
28use rumtk_core::core::RUMVec;
29use rumtk_core::rumtk_resolve_task;
30
31type TESTDATA_REQUEST_FUNCTION = fn() -> Request;
32type TESTDATA_REQUEST_BODY_FUNCTION = fn() -> RUMVec<u8>;
33type TESTDATA_FORMDATA_FUNCTION = fn() -> FormData;
34
35/// Credit: https://users.rust-lang.org/t/testing-multipart-form-data-fails-with-cryptic-error-message/100633
36const TESTDATA_FORM_BODY: TESTDATA_REQUEST_BODY_FUNCTION = || {
37    let mut buffer = RUMVec::<u8>::new();
38
39    write!(buffer, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n").unwrap();
40    write!(
41        buffer,
42        "Content-Disposition: form-data; name=\"username\"\r\n"
43    )
44    .unwrap();
45    write!(buffer, "\r\n").unwrap();
46    write!(buffer, "JohnDoe\r\n").unwrap();
47    write!(buffer, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n").unwrap();
48    write!(
49        buffer,
50        "Content-Disposition: form-data; name=\"profile_pic\"; filename=\"avatar.png\"\r\n"
51    )
52    .unwrap();
53    write!(buffer, "Content-Type: image/png\r\n").unwrap();
54    write!(buffer, "\r\n").unwrap();
55    write!(buffer, "[Binary Data of the Image File]\r\n").unwrap();
56    write!(buffer, "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\n").unwrap();
57
58    buffer
59};
60
61pub const TESTDATA_FORMDATA_REQUEST: TESTDATA_REQUEST_FUNCTION = || -> Request {
62    let body_buffer = TESTDATA_FORM_BODY();
63    Request::builder()
64        .method(Method::POST)
65        .uri("/upload")
66        .header(HOST, "localhost")
67        .header(
68            CONTENT_TYPE,
69            "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW",
70        )
71        .body(body_buffer.into())
72        .unwrap()
73};
74
75pub const TESTDATA_EXPECTED_FORMDATA: TESTDATA_FORMDATA_FUNCTION = || -> FormData {
76    let mut expected_form = FormData::default();
77
78    expected_form
79        .form
80        .insert("username".into(), "JohnDoe".into());
81    expected_form.form.insert(
82        "profile_pic".into(),
83        "[Binary Data of the Image File]".into(),
84    );
85
86    expected_form
87};
88
89async fn create_form(form_fxn: TESTDATA_REQUEST_FUNCTION) -> FormResult {
90    let req = form_fxn();
91    let mut raw_form = RouterForm::from_request(req, &())
92        .await
93        .expect("Multipart form expected.");
94    compile_form_data(&mut raw_form).await
95}
96
97pub fn create_test_form(form_fxn: TESTDATA_REQUEST_FUNCTION) -> FormResult {
98    let handle = create_form(form_fxn);
99    rumtk_resolve_task!(handle)
100}
101
102pub const UNTRIMMED_HTML_RENDER: &str = "\n        \n           \n        \n        <div class='div-default'>default</div>\n    \n        \n    ";
103pub const TRIMMED_HTML_RENDER: &str = "<div class='div-default'>default</div>";
104pub const TRIMMED_HTML_RENDER_META: &str = "<meta charset='UTF-8'>\n<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />\n<meta name='viewport' content='width=device-width, initial-scale=1.0' />\n<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'/>\n<meta name='description' content=''>\n<title></title>\n<link rel='icon' type='image/png' href='/static/img/icon.png'>";
105pub const TRIMMED_HTML_TITLE_RENDER: &str = "<div class='f14 centered title-default-container'>\n<a id='default'>\n<h2 class='title-default'>DEFAULT</h2>\n<h2 class='title-default-overlay no-select'>DEFAULT</h2>\n</a>\n</div>";