mock_fetch_as_text_with_rss_url takes a map of responses

This commit is contained in:
Paul Campbell 2023-08-04 07:45:05 +01:00
parent 2a3c6b5be0
commit e44da378c6
2 changed files with 20 additions and 11 deletions

View file

@ -23,6 +23,8 @@ pub fn find(site: &str, channel_name: &str, e: &NetworkEnv) -> Result<String> {
#[cfg(test)]
mod tests {
use std::collections::HashMap;
use crate::test_utils::{
mock_fetch_as_text_with_rss_url, stub_network_download_as_mp3, stub_network_fetch_as_bytes,
};
@ -32,7 +34,10 @@ mod tests {
fn finds_rss_url() -> Result<()> {
//given
let network_env = NetworkEnv {
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
fetch_as_text: mock_fetch_as_text_with_rss_url(HashMap::from([(
"site@channel",
"the-rss-url",
)])),
fetch_as_bytes: stub_network_fetch_as_bytes(),
download_as_mp3: stub_network_download_as_mp3(),
};
@ -47,7 +52,7 @@ mod tests {
fn error_if_channel_name_is_invalid() -> Result<()> {
//given
let network_env = NetworkEnv {
fetch_as_text: mock_fetch_as_text_with_rss_url("the-rss-url"),
fetch_as_text: mock_fetch_as_text_with_rss_url(HashMap::from([])),
fetch_as_bytes: stub_network_fetch_as_bytes(),
download_as_mp3: stub_network_download_as_mp3(),
};

View file

@ -29,17 +29,21 @@ pub fn read_text_file(file_name: &str) -> Result<Vec<String>> {
.map(String::from)
.collect())
}
pub fn mock_fetch_as_text_with_rss_url(url: &'static str) -> NetworkFetchAsTextFn {
// TODO: returned function must return different values each time it is called
Box::new(move |_url: &str| {
Ok(format!(
pub fn mock_fetch_as_text_with_rss_url(
map: HashMap<&'static str, &'static str>,
) -> NetworkFetchAsTextFn {
Box::new(move |url: &str| match map.get(url) {
Some(url) => Ok(format!(
r#"
<html>
<link title="RSS" href="{}">
</html>
"#,
<html>
<link title="RSS" href="{}">
</html>
"#,
url
))
)),
None => Err(Error::message(
format!("Unexpected request for {}", url).as_str(),
)),
})
}
pub fn mock_network_fetch_as_bytes_with_rss_entries(