diff options
| author | R0flcopt3r <12752060+R0flcopt3r@users.noreply.github.com> | 2021-10-09 19:31:34 +0200 |
|---|---|---|
| committer | Max Audron <audron@cocaine.farm> | 2021-10-10 16:21:55 +0200 |
| commit | 91bf47caf00badecd935bb3dd5acded5b770d7d3 (patch) | |
| tree | 90e076d31dc6ffd6096c7ca6d4a5b1fb132461ce | |
| parent | fixed tests with new refactor (diff) | |
using mockito for testing instead of real websites
| -rw-r--r-- | Cargo.lock | 57 | ||||
| -rw-r--r-- | Cargo.toml | 2 | ||||
| -rw-r--r-- | src/hooks/url.rs | 69 |
3 files changed, 114 insertions, 14 deletions
@@ -27,6 +27,16 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "28b2cd92db5cbd74e8e5028f7e27dd7aa3090e89e4f2a197cc7c8dfb69c7063b" [[package]] +name = "assert-json-diff" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "50f1c3703dd33532d7f0ca049168930e9099ecac238e23cf932f3a69c42f06da" +dependencies = [ + "serde", + "serde_json", +] + +[[package]] name = "async-stream" version = "0.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -48,6 +58,17 @@ dependencies = [ ] [[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi", + "libc", + "winapi", +] + +[[package]] name = "autocfg" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -103,6 +124,7 @@ dependencies = [ "irc", "irc-proto", "kuchiki", + "mockito", "rand 0.8.3", "regex", "reqwest", @@ -159,6 +181,17 @@ dependencies = [ ] [[package]] +name = "colored" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b3616f750b84d8f0de8a58bda93e08e2a81ad3f523089b05f1dffecab48c6cbd" +dependencies = [ + "atty", + "lazy_static", + "winapi", +] + +[[package]] name = "convert_case" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -240,6 +273,12 @@ dependencies = [ ] [[package]] +name = "difference" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "524cbf6897b527295dff137cec09ecf3a05f4fddffd7dfcd1585403449e74198" + +[[package]] name = "digest" version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -839,6 +878,24 @@ dependencies = [ ] [[package]] +name = "mockito" +version = "0.30.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d10030163d67f681db11810bc486df3149e6d91c8b4f3f96fa8b62b546c2cef8" +dependencies = [ + "assert-json-diff", + "colored", + "difference", + "httparse", + "lazy_static", + "log", + "rand 0.8.3", + "regex", + "serde_json", + "serde_urlencoded", +] + +[[package]] name = "native-tls" version = "0.2.8" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -32,6 +32,8 @@ rand = "0.8.3" reqwest = "0.11" kuchiki = "0.8.1" +mockito = "0.30.0" + [dev-dependencies] tokio-test = "*" 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#" +<html> + <head> + <title>This is test site</title> + </head> + <body> + <h1>some heading</h1> + </body> +</html> +"#, + ) + .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#" +<html> + <head> + <title>test site 1</title> + </head> +</html> +"#, + ) + .create(), + mockito::mock("GET", "/2") + .with_body( + r#" +<html> + <head> + <title>test site 2</title> + </head> +"#, + ) + .create(), + ]; + let mut titles: Vec<String> = 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"); } } |
