From 5a8a1d84a9654e38ad2582ca4aa35758d2d88904 Mon Sep 17 00:00:00 2001 From: LucaCappelletti94 Date: Tue, 27 Oct 2026 20:58:23 +0100 Subject: [PATCH] Show a Web Push token on the examples' status card as its endpoint's host and tail --- examples/support/src/lib.rs | 49 +++++++++++++++++++++++++++++-------- 1 file changed, 39 insertions(+), 10 deletions(-) diff --git a/examples/support/src/lib.rs b/examples/support/src/lib.rs index ae96285..dbc7362 100644 --- a/examples/support/src/lib.rs +++ b/examples/support/src/lib.rs @@ -207,9 +207,12 @@ pub fn transport(token: &Token) -> &'static str { } /// The token line cut to the page's width, the first and last 8 characters of [`token`] around -/// an ellipsis. +/// an ellipsis, or for Web Push the endpoint's host and the last 8 characters of its path. #[must_use] pub fn shortened(token: &Token) -> String { + if let Token::WebPush { endpoint, .. } = token { + return shortened_endpoint(endpoint); + } let line = self::token(token); let chars = line.chars().count(); if chars <= 16 { @@ -220,6 +223,20 @@ pub fn shortened(token: &Token) -> String { format!("{head}...{tail}") } +/// The endpoint without its scheme, its path cut to the last 8 characters after the host. +fn shortened_endpoint(endpoint: &str) -> String { + let rest = endpoint + .split_once("://") + .map_or(endpoint, |(_, rest)| rest); + let (host, path) = rest.split_once('/').unwrap_or((rest, "")); + let chars = path.chars().count(); + if chars <= 8 { + return rest.to_owned(); + } + let tail: String = path.chars().skip(chars - 8).collect(); + format!("{host}...{tail}") +} + /// The wall-clock milliseconds since 1970, from the standard clock on the native targets and /// from `Date.now()` on `wasm32-unknown-unknown`, whose `SystemTime` has no clock. #[must_use] @@ -479,15 +496,7 @@ mod tests { "APNs" ); #[cfg(not(target_arch = "wasm32"))] - assert_eq!( - transport(&Token::WebPush { - endpoint: "https://example.com".into(), - p256dh: [0; 65], - auth: [0; 16], - expires: None - }), - "UnifiedPush" - ); + assert_eq!(transport(&web_push("https://example.com")), "UnifiedPush"); } #[test] @@ -507,6 +516,26 @@ mod tests { assert_eq!(shortened(&token), super::token(&token)); } + fn web_push(endpoint: &str) -> Token { + Token::WebPush { + endpoint: endpoint.into(), + p256dh: [0; 65], + auth: [0; 16], + expires: None, + } + } + + #[test] + fn shortened_names_a_web_push_endpoint_by_its_host_and_tail() { + let token = web_push("https://fcm.googleapis.com/fcm/send/abcdefgh:12345678"); + assert_eq!(shortened(&token), "fcm.googleapis.com...12345678"); + } + + #[test] + fn shortened_leaves_a_short_web_push_path_whole() { + assert_eq!(shortened(&web_push("https://ntfy.sh/up1")), "ntfy.sh/up1"); + } + #[test] fn push_view_reads_the_payload_fields_and_the_delay() { let payload = br#"{"title":"t","body":"b","sent_at_ms":"1000"}"#;