From d8861d6f7e9c266d5d15dac0b43d690b0d8000f9 Mon Sep 17 00:00:00 2001 From: Paul Campbell Date: Tue, 25 Jul 2023 19:57:31 +0100 Subject: [PATCH] Add tests for feed::find --- src/feed/find.rs | 37 ++++++++++++++++++++++++++++++++----- src/feed/mod.rs | 4 ++-- src/fetch.rs | 4 ++-- src/lib.rs | 2 +- 4 files changed, 37 insertions(+), 10 deletions(-) diff --git a/src/feed/find.rs b/src/feed/find.rs index c6ba1ba..537776f 100644 --- a/src/feed/find.rs +++ b/src/feed/find.rs @@ -1,16 +1,16 @@ use crate::prelude::*; -use crate::fetch::FetchEnv; +use crate::fetch::FetchGet; -pub fn find(site: &str, channel_name: &str, e: &FetchEnv) -> Result { +pub fn find(site: &str, channel_name: &str, e: &FetchGet) -> Result { if let Some(channel_prefix) = channel_name.chars().next() { if channel_prefix != '@' { return Err(format!("Channel Name must begin with an '@': {}", channel_name).into()); } } let channel_url = format!("{}{}", site, channel_name); - let response = (e.get)(&channel_url)?; - let rss_url = scraper::Html::parse_document(&response.text()?) + let response = (e)(&channel_url)?; + let rss_url = scraper::Html::parse_document(&response) .select(&scraper::Selector::parse("link[title='RSS']").unwrap()) .next() .unwrap() @@ -23,10 +23,37 @@ pub fn find(site: &str, channel_name: &str, e: &FetchEnv) -> Result { #[cfg(test)] mod tests { + use crate::fetch::Response; + use super::*; #[test] fn finds_rss_url() -> Result<()> { - // TODO: need to inject wrapper for reqwest::blocking::get + //given + let fetch_get = &(get as FetchGet); + //when + let result = find("site", "@channel", &fetch_get)?; + //then + assert_eq!(result, "the-rss-url"); Ok(()) } + + #[test] + fn error_if_channel_name_is_invalid() -> Result<()> { + //given + let fetch_get = &(get as FetchGet); + //when + let result = find("site", "invalid-channel-name", &fetch_get); + //then + assert!(result.is_err()); + Ok(()) + } + + fn get(_url: &str) -> Result { + Ok(r#" + + + + "# + .to_string()) + } } diff --git a/src/feed/mod.rs b/src/feed/mod.rs index 24ee1c9..8246422 100644 --- a/src/feed/mod.rs +++ b/src/feed/mod.rs @@ -1,6 +1,6 @@ use crate::prelude::*; -use crate::FetchEnv; +use crate::fetch::FetchGet; mod find; mod get; @@ -15,5 +15,5 @@ pub struct FeedEnv { pub get: FeedGet, } -pub type FeedFind = fn(&str, &str, &FetchEnv) -> Result; +pub type FeedFind = fn(&str, &str, &FetchGet) -> Result; pub type FeedGet = fn(&str) -> Result; diff --git a/src/fetch.rs b/src/fetch.rs index a1aaeac..b0d47fd 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -27,8 +27,8 @@ pub fn download(link: &Link) -> Result<()> { Ok(()) } -pub type Response = reqwest::blocking::Response; +pub type Response = String; pub fn get(url: &str) -> Result { - Ok(reqwest::blocking::get(url)?) + Ok(reqwest::blocking::get(url)?.text()?) } diff --git a/src/lib.rs b/src/lib.rs index 8718f1b..243cbd3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -19,7 +19,7 @@ pub struct Env { pub fn run(subscriptions: &str, history: &str, site: &str, e: Env) -> Result<()> { for channel_name in subscriptions::lines_from(subscriptions)? { println!("Channel: {}", channel_name); - let feed_url = (e.feed.find)(site, &channel_name, &e.fetch)?; + let feed_url = (e.feed.find)(site, &channel_name, &e.fetch.get)?; for entry in (e.feed.get)(&feed_url)?.entries() { if let Some(link) = entry.links().get(0).cloned() { if !(e.history.find)(&link, history)? {