Lines
96.3 %
Functions
81.82 %
Branches
100 %
//! Helpers of the account and commodity edit tests: the rendered form's own
//! fields in, the production routes behind.
use axum::{Router, body::Body, http::Request};
use web::model::User;
use crate::common::controls;
use crate::common::new_page::{Reply, send_to};
/// The rendered form `id`, from its id to its end.
pub fn form_html<'a>(html: &'a str, id: &str) -> &'a str {
let start = html
.find(&format!("id=\"{id}\""))
.unwrap_or_else(|| panic!("no {id} in {html}"));
let form = &html[start..];
&form[..form.find("</form>").expect("form end")]
}
/// What a browser posts from the rendered form `id`.
pub fn form_pairs(html: &str, id: &str) -> Vec<(String, String)> {
controls::pairs(form_html(html, id))
pub async fn get_from(
router: Router<std::sync::Arc<web::AppState>>,
user: &User,
uri: &str,
) -> Reply {
let request = Request::builder()
.uri(uri)
.body(Body::empty())
.expect("request");
send_to(router, user, request).await
pub async fn post_to(
pairs: &[(String, String)],
let body = serde_urlencoded::to_string(pairs).expect("urlencoded");
.method("POST")
.header("content-type", "application/x-www-form-urlencoded")
.header("hx-request", "true")
.body(Body::from(body))
/// `pairs` without the rows named `prefix`, plus the tag rows `tags`.
pub fn with_tags(pairs: &[(String, String)], tags: &[(&str, &str)]) -> Vec<(String, String)> {
let mut kept: Vec<(String, String)> = pairs
.iter()
.filter(|(name, _)| !name.starts_with("tags."))
.cloned()
.collect();
for (index, (name, value)) in tags.iter().enumerate() {
kept.push((format!("tags.{index}.name"), name.to_string()));
kept.push((format!("tags.{index}.value"), value.to_string()));
kept
/// The revision a save reply swaps into the form.
pub fn fresh_revision(body: &str) -> String {
let named = body
.find("name=\"revision\"")
.unwrap_or_else(|| panic!("no fresh revision in {body}"));
let rest = &body[named..];
let start = rest.find("value=\"").expect("value") + "value=\"".len();
rest[start..][..rest[start..].find('"').expect("quote")].to_string()