From 9d2e044e4e6374d51e4f6bc684ad3ad022f53d99 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 25 Feb 2021 00:36:30 +0000 Subject: [PATCH] tests: provide HtmlExt and ::e_attr Signed-off-by: Ian Jackson --- apitest/at-otter.rs | 37 +++++++++++++++++++++++++++++++------ 1 file changed, 31 insertions(+), 6 deletions(-) diff --git a/apitest/at-otter.rs b/apitest/at-otter.rs index c4f18bce..d5884c48 100644 --- a/apitest/at-otter.rs +++ b/apitest/at-otter.rs @@ -22,10 +22,39 @@ struct Player { } struct Session { - pub dom: scraper::html::Html, + pub dom: scraper::Html, pub updates: UnixStream, } +mod scraper_ext { + use super::*; + use scraper::*; + use scraper::html::*; + + pub trait HtmlExt { + fn select<'a,'b>(&'a self, selector: &'b Selector) -> Select<'a, 'b>; + + #[throws(as Option)] + fn e_attr(&self, sel: S, attr: &str) -> &str + where S: TryInto, + >::Error: Debug, + { + self + .select(&sel.try_into().unwrap()) + .next()? + .value().attr(attr)? + } + } + + impl HtmlExt for Html { + fn select<'a,'b>(&'a self, selector: &'b Selector) -> Select<'a, 'b> { + self.select(selector) + } + } +} + +use scraper_ext::HtmlExt; + impl Ctx { #[throws(AE)] fn connect_player(&self, player: &Player) -> Session { @@ -36,11 +65,7 @@ impl Ctx { let body = resp.text()?; let loading = scraper::Html::parse_document(&body); //dbg!(&body, &dom); - let ptoken = loading - .select(&"#loading_token".try_into().unwrap()) - .next().unwrap() - .value().attr("data-ptoken") - .unwrap(); + let ptoken = loading.e_attr("#loading_token", "data-ptoken").unwrap(); dbg!(&ptoken); let resp = client.post(&self.ds.subst("@url@/_/session/Portrait")?) -- 2.30.2