From d586d19bf239e27922eb896c6c496e931598ae78 Mon Sep 17 00:00:00 2001 From: Harald Eilertsen Date: Mon, 29 Jul 2019 21:32:27 +0200 Subject: Update to Mockito 0.19 for smoother tests. Provides matching against query params, so we don't need the ugly regexes anymore. --- Cargo.toml | 2 +- tests/zotapi.rs | 23 +++++++++++------------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index c8115e0..77f1fb9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -28,5 +28,5 @@ serde_urlencoded = "0.5.1" [dev-dependencies] clap = "2.33.0" dotenv = "0.13" -mockito = "0.14" +mockito = "0.19" serde_json = "1.0" diff --git a/tests/zotapi.rs b/tests/zotapi.rs index 8c9d0b0..38bdf8a 100644 --- a/tests/zotapi.rs +++ b/tests/zotapi.rs @@ -29,8 +29,7 @@ fn default_mock(method: &str, url: &str) -> Mock { } fn client() -> zotapi::Client { - let listen_url = format!("http://{}", mockito::SERVER_ADDRESS); - zotapi::client(&listen_url, "testuser", "test1234") + zotapi::client(&mockito::server_url(), "testuser", "test1234") } #[test] @@ -80,14 +79,14 @@ fn create_new_post() { #[test] fn create_new_post_with_title() { - // Note this regex is not as good as it could be. But since the rust regex crate - // don't support back references it's the best we can do and not care about the - // oder of the entries. - let expected_body = Matcher::Regex(r"(title=A\+title&?|body=This\+is\+a\+test&?){2}".into()); - let m = mock("POST", "/api/z/1.0/item/update") .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) - .match_body(expected_body) + .match_body( + Matcher::AllOf(vec![ + Matcher::UrlEncoded("title".to_string(), "A+title".to_string()), + Matcher::UrlEncoded("body".to_string(), "This+is+a+test".to_string()), + ]) + ) .with_status(200) .with_header("content-type", "application/json") .with_body("{}") @@ -209,8 +208,8 @@ fn fetch_privacy_groups() { #[test] fn fetch_members_of_group_by_group_id() { - let m = mock("GET", Matcher::Regex( - r"^/api/z/1.0/group_members\?([^&]*&)*group_id=42".into())) + let m = mock("GET", "/api/z/1.0/group_members") + .match_query(Matcher::UrlEncoded("group_id".to_string(), "42".to_string())) .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) .with_status(200) .with_header("content-type", "application/json") @@ -223,8 +222,8 @@ fn fetch_members_of_group_by_group_id() { #[test] fn fetch_members_of_group_by_group_name() { - let m = mock("GET", Matcher::Regex( - r"^/api/z/1.0/group_members\?([^&]*&)*group_name=Friends\+of\+pain".into())) + let m = mock("GET", "/api/z/1.0/group_members") + .match_query(Matcher::UrlEncoded("group_name".into(), "Friends+of+pain".into())) .match_header("Authorization", Matcher::Regex(r"Basic \w+".into())) .with_status(200) .with_header("content-type", "application/json") -- cgit v1.2.3