Add LemmyResult to request tests

This commit is contained in:
netbrum 2024-09-29 17:14:06 +02:00
parent 44593d9e52
commit 53a46d0917

View File

@ -471,13 +471,13 @@ pub async fn replace_image(
} }
#[cfg(test)] #[cfg(test)]
#[expect(clippy::unwrap_used)]
mod tests { mod tests {
use crate::{ use crate::{
context::LemmyContext, context::LemmyContext,
request::{extract_opengraph_data, fetch_link_metadata}, request::{extract_opengraph_data, fetch_link_metadata},
}; };
use lemmy_utils::error::LemmyResult;
use pretty_assertions::assert_eq; use pretty_assertions::assert_eq;
use serial_test::serial; use serial_test::serial;
use url::Url; use url::Url;
@ -485,10 +485,10 @@ mod tests {
// These helped with testing // These helped with testing
#[tokio::test] #[tokio::test]
#[serial] #[serial]
async fn test_link_metadata() { async fn test_link_metadata() -> LemmyResult<()> {
let context = LemmyContext::init_test_context().await; let context = LemmyContext::init_test_context().await;
let sample_url = Url::parse("https://gitlab.com/IzzyOnDroid/repo/-/wikis/FAQ").unwrap(); let sample_url = Url::parse("https://gitlab.com/IzzyOnDroid/repo/-/wikis/FAQ")?;
let sample_res = fetch_link_metadata(&sample_url, &context).await.unwrap(); let sample_res = fetch_link_metadata(&sample_url, &context).await?;
assert_eq!( assert_eq!(
Some("FAQ · Wiki · IzzyOnDroid / repo · GitLab".to_string()), Some("FAQ · Wiki · IzzyOnDroid / repo · GitLab".to_string()),
sample_res.opengraph_data.title sample_res.opengraph_data.title
@ -499,8 +499,7 @@ mod tests {
); );
assert_eq!( assert_eq!(
Some( Some(
Url::parse("https://gitlab.com/uploads/-/system/project/avatar/4877469/iod_logo.png") Url::parse("https://gitlab.com/uploads/-/system/project/avatar/4877469/iod_logo.png")?
.unwrap()
.into() .into()
), ),
sample_res.opengraph_data.image sample_res.opengraph_data.image
@ -510,19 +509,21 @@ mod tests {
Some(mime::TEXT_HTML_UTF_8.to_string()), Some(mime::TEXT_HTML_UTF_8.to_string()),
sample_res.content_type sample_res.content_type
); );
Ok(())
} }
#[test] #[test]
fn test_resolve_image_url() { fn test_resolve_image_url() -> LemmyResult<()> {
// url that lists the opengraph fields // url that lists the opengraph fields
let url = Url::parse("https://example.com/one/two.html").unwrap(); let url = Url::parse("https://example.com/one/two.html")?;
// root relative url // root relative url
let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='/image.jpg'></head><body></body></html>"; let html_bytes = b"<!DOCTYPE html><html><head><meta property='og:image' content='/image.jpg'></head><body></body></html>";
let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata"); let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata");
assert_eq!( assert_eq!(
metadata.image, metadata.image,
Some(Url::parse("https://example.com/image.jpg").unwrap().into()) Some(Url::parse("https://example.com/image.jpg")?.into())
); );
// base relative url // base relative url
@ -530,11 +531,7 @@ mod tests {
let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata"); let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata");
assert_eq!( assert_eq!(
metadata.image, metadata.image,
Some( Some(Url::parse("https://example.com/one/image.jpg")?.into())
Url::parse("https://example.com/one/image.jpg")
.unwrap()
.into()
)
); );
// absolute url // absolute url
@ -542,7 +539,7 @@ mod tests {
let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata"); let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata");
assert_eq!( assert_eq!(
metadata.image, metadata.image,
Some(Url::parse("https://cdn.host.com/image.jpg").unwrap().into()) Some(Url::parse("https://cdn.host.com/image.jpg")?.into())
); );
// protocol relative url // protocol relative url
@ -550,7 +547,9 @@ mod tests {
let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata"); let metadata = extract_opengraph_data(html_bytes, &url).expect("Unable to parse metadata");
assert_eq!( assert_eq!(
metadata.image, metadata.image,
Some(Url::parse("https://example.com/image.jpg").unwrap().into()) Some(Url::parse("https://example.com/image.jpg")?.into())
); );
Ok(())
} }
} }