test(axum): use dom-aware html assertions
Harden axum response and root fingerprint tests with scraper-based HTML parsing instead of raw rendered-string equality. req: test/005
This commit is contained in:
+21
-5
@@ -533,30 +533,46 @@ mod tests {
|
||||
use super::{encode_sse_batch, html_with_root_fingerprint, sse, BuildFingerprint, InteractionForm, SLHX_SSE_EVENT};
|
||||
use axum::{body::{to_bytes, Body}, extract::FromRequest, http::{header, Request}, response::IntoResponse};
|
||||
use futures_util::stream;
|
||||
use scraper::{Html, Selector};
|
||||
use slhx_core::{EffectBatch, EFFECT_BATCH_ABI_VERSION};
|
||||
use std::convert::Infallible;
|
||||
|
||||
fn selector(value: &str) -> Selector {
|
||||
Selector::parse(value).expect("test selector parses")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn root_fingerprint_is_added_to_initial_root() {
|
||||
// req: test/005
|
||||
let html = html_with_root_fingerprint(
|
||||
"<html><body><main data-slhx-root>Docs</main></body></html>".into(),
|
||||
BuildFingerprint(99),
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
html,
|
||||
"<html><body><main data-slhx-root data-slhx-fp=\"99\">Docs</main></body></html>"
|
||||
);
|
||||
let document = Html::parse_document(&html);
|
||||
let root = document
|
||||
.select(&selector("main[data-slhx-root]"))
|
||||
.next()
|
||||
.expect("root element is rendered");
|
||||
assert_eq!(root.value().attr("data-slhx-fp"), Some("99"));
|
||||
assert_eq!(root.text().collect::<String>(), "Docs");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn existing_root_fingerprint_is_preserved() {
|
||||
// req: test/005
|
||||
let html = html_with_root_fingerprint(
|
||||
"<main data-slhx-root data-slhx-fp=\"1\">Docs</main>".into(),
|
||||
BuildFingerprint(99),
|
||||
);
|
||||
|
||||
assert_eq!(html, "<main data-slhx-root data-slhx-fp=\"1\">Docs</main>");
|
||||
let document = Html::parse_fragment(&html);
|
||||
let root = document
|
||||
.select(&selector("main[data-slhx-root]"))
|
||||
.next()
|
||||
.expect("root element is rendered");
|
||||
assert_eq!(root.value().attr("data-slhx-fp"), Some("1"));
|
||||
assert_eq!(root.text().collect::<String>(), "Docs");
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
||||
Reference in New Issue
Block a user