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:
Generated
+1
@@ -1370,6 +1370,7 @@ version = "0.1.0"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"axum",
|
"axum",
|
||||||
"futures-util",
|
"futures-util",
|
||||||
|
"scraper",
|
||||||
"slhx-core",
|
"slhx-core",
|
||||||
"slhx-js",
|
"slhx-js",
|
||||||
"tokio",
|
"tokio",
|
||||||
|
|||||||
@@ -13,4 +13,5 @@ slhx-core = { path = "../slhx-core" }
|
|||||||
slhx-js = { path = "../slhx-js" }
|
slhx-js = { path = "../slhx-js" }
|
||||||
|
|
||||||
[dev-dependencies]
|
[dev-dependencies]
|
||||||
|
scraper = "0.23"
|
||||||
tokio = { version = "1", features = ["macros", "rt"] }
|
tokio = { version = "1", features = ["macros", "rt"] }
|
||||||
|
|||||||
+21
-5
@@ -533,30 +533,46 @@ mod tests {
|
|||||||
use super::{encode_sse_batch, html_with_root_fingerprint, sse, BuildFingerprint, InteractionForm, SLHX_SSE_EVENT};
|
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 axum::{body::{to_bytes, Body}, extract::FromRequest, http::{header, Request}, response::IntoResponse};
|
||||||
use futures_util::stream;
|
use futures_util::stream;
|
||||||
|
use scraper::{Html, Selector};
|
||||||
use slhx_core::{EffectBatch, EFFECT_BATCH_ABI_VERSION};
|
use slhx_core::{EffectBatch, EFFECT_BATCH_ABI_VERSION};
|
||||||
use std::convert::Infallible;
|
use std::convert::Infallible;
|
||||||
|
|
||||||
|
fn selector(value: &str) -> Selector {
|
||||||
|
Selector::parse(value).expect("test selector parses")
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn root_fingerprint_is_added_to_initial_root() {
|
fn root_fingerprint_is_added_to_initial_root() {
|
||||||
|
// req: test/005
|
||||||
let html = html_with_root_fingerprint(
|
let html = html_with_root_fingerprint(
|
||||||
"<html><body><main data-slhx-root>Docs</main></body></html>".into(),
|
"<html><body><main data-slhx-root>Docs</main></body></html>".into(),
|
||||||
BuildFingerprint(99),
|
BuildFingerprint(99),
|
||||||
);
|
);
|
||||||
|
|
||||||
assert_eq!(
|
let document = Html::parse_document(&html);
|
||||||
html,
|
let root = document
|
||||||
"<html><body><main data-slhx-root data-slhx-fp=\"99\">Docs</main></body></html>"
|
.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]
|
#[test]
|
||||||
fn existing_root_fingerprint_is_preserved() {
|
fn existing_root_fingerprint_is_preserved() {
|
||||||
|
// req: test/005
|
||||||
let html = html_with_root_fingerprint(
|
let html = html_with_root_fingerprint(
|
||||||
"<main data-slhx-root data-slhx-fp=\"1\">Docs</main>".into(),
|
"<main data-slhx-root data-slhx-fp=\"1\">Docs</main>".into(),
|
||||||
BuildFingerprint(99),
|
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]
|
#[tokio::test]
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
use axum::http::{header, HeaderMap};
|
use axum::http::{header, HeaderMap};
|
||||||
use axum::response::IntoResponse;
|
use axum::response::IntoResponse;
|
||||||
|
use scraper::{Html, Selector};
|
||||||
use slhx_axum::{
|
use slhx_axum::{
|
||||||
runtime_js, DispatchRejection, EffectResponse, HandlerRegistry, InteractionForm,
|
runtime_js, DispatchRejection, EffectResponse, HandlerRegistry, InteractionForm,
|
||||||
InteractionFormRejection, PageMode, PageRequest, PageResponse, SLHX_CONTENT_TYPE,
|
InteractionFormRejection, PageMode, PageRequest, PageResponse, SLHX_CONTENT_TYPE,
|
||||||
@@ -7,6 +8,10 @@ use slhx_axum::{
|
|||||||
};
|
};
|
||||||
use slhx_core::{push, BuildFingerprint, Slot};
|
use slhx_core::{push, BuildFingerprint, Slot};
|
||||||
|
|
||||||
|
fn selector(value: &str) -> Selector {
|
||||||
|
Selector::parse(value).expect("test selector parses")
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn page_mode_detects_partial_header() {
|
fn page_mode_detects_partial_header() {
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
@@ -18,17 +23,28 @@ fn page_mode_detects_partial_header() {
|
|||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn page_request_wraps_full_pages_and_leaves_partials_unwrapped() {
|
fn page_request_wraps_full_pages_and_leaves_partials_unwrapped() {
|
||||||
let full = PageRequest { mode: PageMode::Full }.page("<main>Docs</main>", |content| {
|
// req: test/005
|
||||||
format!("<html>{content}</html>")
|
let full = PageRequest { mode: PageMode::Full }.page(
|
||||||
});
|
"<main data-page=\"docs\">Docs</main>",
|
||||||
|
|content| format!("<html><body data-shell=\"docs\">{content}</body></html>"),
|
||||||
|
);
|
||||||
assert_eq!(full.mode, PageMode::Full);
|
assert_eq!(full.mode, PageMode::Full);
|
||||||
assert_eq!(full.html, "<html><main>Docs</main></html>");
|
let full_document = Html::parse_document(&full.html);
|
||||||
|
assert_eq!(
|
||||||
|
full_document
|
||||||
|
.select(&selector("body[data-shell=\"docs\"] main[data-page=\"docs\"]"))
|
||||||
|
.count(),
|
||||||
|
1
|
||||||
|
);
|
||||||
|
|
||||||
let partial = PageRequest { mode: PageMode::Partial }.page("<main>Docs</main>", |content| {
|
let partial = PageRequest { mode: PageMode::Partial }.page(
|
||||||
format!("<html>{content}</html>")
|
"<main data-page=\"docs\">Docs</main>",
|
||||||
});
|
|content| format!("<html><body data-shell=\"docs\">{content}</body></html>"),
|
||||||
|
);
|
||||||
assert_eq!(partial.mode, PageMode::Partial);
|
assert_eq!(partial.mode, PageMode::Partial);
|
||||||
assert_eq!(partial.html, "<main>Docs</main>");
|
let partial_fragment = Html::parse_fragment(&partial.html);
|
||||||
|
assert_eq!(partial_fragment.select(&selector("main[data-page=\"docs\"]")).count(), 1);
|
||||||
|
assert_eq!(partial_fragment.select(&selector("body[data-shell=\"docs\"]")).count(), 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
|
|||||||
Reference in New Issue
Block a user