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/hooks/url.rs') 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 '> 2025-12-17release 1.8.01.8.0Max Audron-2/+3 - add twitter cancel 2025-12-17add twitter cancel featureMax Audron-7/+44 yeah nitter 2025-12-14update debug config filesMax Audron-7/+6 2025-12-14release 1.7.11.7.1Max Audron-2/+2 - fix non-sasl connection registration 2025-12-14fix non-sasl connection registrationMax Audron-9/+35 2025-12-14fix readme badge linksMax Audron-3/+3 2025-12-14update readme and remotesMax Audron-1/+14 2025-12-14release 1.7.01.7.0Max Audron-2/+2 - update dependencies - add server password 2025-12-14add server passwordMax Audron-1/+6 2025-12-14update cargo dependenciesMax Audron-670/+505 2025-12-14update flake inputs to nixpkgs 25.11Max Audron-36/+41 2025-05-06fix rustls missing cryptoproviderMax Audron-1/+6 2025-05-06remove too commonly used used for shifty_eyesMax Audron-1/+1 2025-05-06fix truncate on unicodeMax Audron-10/+20 fuck unicode all my homies stan ascii 2025-05-06update dependenciesMax Audron-691/+1659 2024-08-12add nix build and moduleMax Audron-5/+427 2022-02-19remove jsonnet lock fileMax Audron-36/+0 2022-02-19fix deploy to work with gitlab agentMax Audron-4/+4 2021-10-22write tons of documentation and reorganize some modulesMax Audron-65/+300 2021-10-22remove wolfram alpha url shorteningMax Audron-1/+2 2021-10-20remove failing wolfram alpha test casesMax Audron-105/+55 this is due to the url shorterner dying randomly and also just generally bad idea to call external services during unit tests. 2021-10-20bump version to 1.6.2Max Audron-3/+2 2021-10-20prepare for release on crates.ioMax Audron-39/+65 2021-10-20add async docs to macro crate and bump versionMax Audron-9/+10 2021-10-20change hook errors to be logged as warningsMax Audron-3/+3 they in nearly all cases aren't critical enough to warrant an actual error messages 2021-10-20fix configuration not loading correctly on release buildsMax Audron-8/+23 2021-10-19replace sedregex crate8-rework-sedMax Audron-20/+358 This replaces the sedregex crate with our own implementation for multiple reasons: 1. We required to access the parsed regex, this required a patch to the sedregex crate which did not get merged due to an inactive dev, blocking us from publishing on crates.Io 2. We wanted to highlight the changes done in bold 3. We want to add execution of multiple chained sed commands in the future which would require more modification 2021-10-19add formatting trait for irc codesMax Audron-0/+129 add an impl off the formatting trait on String to format Strings with the typical irc formatting codes for bold, italic etc 2021-10-17fix links in readmeMax Audron-2/+2