From 91bf47caf00badecd935bb3dd5acded5b770d7d3 Mon Sep 17 00:00:00 2001 From: R0flcopt3r <12752060+R0flcopt3r@users.noreply.github.com> Date: Sat, 9 Oct 2021 19:31:34 +0200 Subject: using mockito for testing instead of real websites --- src/hooks/url.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 55 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/hooks/url.rs b/src/hooks/url.rs index 7847969..939dc52 100644 --- a/src/hooks/url.rs +++ b/src/hooks/url.rs @@ -89,17 +89,30 @@ mod tests { use super::url_parser; use super::url_title; use anyhow::{Error, Result}; + use mockito; #[test] fn test_url_titel() { - let title: String = - tokio_test::block_on(url_title("https://news.ycombinator.com")).unwrap(); - assert_eq!(title.as_str(), "Hacker News"); - - let title: String = tokio_test::block_on(url_title("https://google.com")).unwrap(); - assert_eq!(title.as_str(), "Google"); - - assert!(tokio_test::block_on(url_title("random_site")).is_err()) + assert!(tokio_test::block_on(url_title(&mockito::server_url())).is_err()); + + let _m = mockito::mock("GET", "/") + .with_body( + r#" + + + This is test site + + +

some heading

+ + +"#, + ) + .create(); + mockito::start(); + + let title: String = tokio_test::block_on(url_title(&mockito::server_url())).unwrap(); + assert_eq!(title.as_str(), "This is test site"); } #[test] fn test_url_parser() { @@ -130,21 +143,49 @@ mod tests { } #[test] + /** + Integration test ish. this tries to replicate url_preview, to make sure + everything works together. + */ fn test_all() { + let _urls = [ + mockito::mock("GET", "/1") + .with_body( + r#" + + + test site 1 + + +"#, + ) + .create(), + mockito::mock("GET", "/2") + .with_body( + r#" + + + test site 2 + +"#, + ) + .create(), + ]; + let mut titles: Vec = Vec::new(); - let text = "https://news.ycombinator.com www.google.com https://youtube.com"; + let text = format!( + "some text {u}/1 other text {u}/2", + u = &mockito::server_url() + ); let urls = url_parser(&text); - assert_eq!(urls.len(), 3); + assert_eq!(urls.len(), 2); for url in &urls { if let Ok(title) = tokio_test::block_on(url_title(&url.as_str())) { titles.push(title); } } - assert_eq!( - msg_builder(&titles), - "Titles: Hacker News --- Google --- YouTube" - ); + assert_eq!(msg_builder(&titles), "Titles: test site 1 --- test site 2"); } } -- cgit v1.2.3