From 9efa696e8e7823094502d13653700a216fe36874 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Sun, 3 Apr 2022 18:42:30 -0300 Subject: [PATCH 1/3] Add crate mockito as a dev-dep to help unit-testing http handlers --- Cargo.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Cargo.toml b/Cargo.toml index d2addc50..58055375 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,6 +36,7 @@ sync = [] env_logger = "0.9" futures-await-test = "0.3" futures = "0.3" +mockito = "0.31.0" meilisearch-test-macro = { path = "meilisearch-test-macro" } tokio = { version = "1", features = ["rt", "macros"] } From b2d8773df32bf9c1a13b6f88862e20b4c69e7559 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Sun, 3 Apr 2022 18:43:00 -0300 Subject: [PATCH 2/3] Send qualified User-Agent to Meilisearch server --- src/client.rs | 40 ++++++++++++++++++++++++++++++++++++++++ src/request.rs | 14 ++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/client.rs b/src/client.rs index 47a1bae9..6b588417 100644 --- a/src/client.rs +++ b/src/client.rs @@ -656,6 +656,46 @@ mod tests { }; use meilisearch_test_macro::meilisearch_test; use time::OffsetDateTime; + use mockito::mock; + use std::mem; + + #[meilisearch_test] + async fn test_methods_has_qualified_version_as_header() { + let mock_server_url = &mockito::server_url(); + let path = "/hello"; + let address = &format!("{}{}", mock_server_url, path); + let user_agent = &*qualified_version(); + + let assertions = vec![ + ( + mock("GET", path).match_header("User-Agent", user_agent).create(), + request::(address, "", Method::Get, 200) + ), + ( + mock("POST", path).match_header("User-Agent", user_agent).create(), + request::(address, "", Method::Post("".to_string()), 200) + ), + ( + mock("DELETE", path).match_header("User-Agent", user_agent).create(), + request::(address, "", Method::Delete, 200) + ), + ( + mock("PUT", path).match_header("User-Agent", user_agent).create(), + request::(address, "", Method::Put("".to_string()), 200) + ), + ( + mock("PATCH", path).match_header("User-Agent", user_agent).create(), + request::(address, "", Method::Patch("".to_string()), 200) + ) + ]; + + for (m, req) in assertions { + let _ = req.await; + + m.assert(); + mem::drop(m); + } + } #[meilisearch_test] async fn test_get_keys(client: Client) { diff --git a/src/request.rs b/src/request.rs index 92483729..d62ccc17 100644 --- a/src/request.rs +++ b/src/request.rs @@ -23,11 +23,13 @@ pub(crate) async fn request { Request::get(url) .header(header::AUTHORIZATION, auth) + .header(header::USER_AGENT, user_agent) .body(()) .map_err(|_| crate::errors::Error::InvalidRequest)? .send_async() @@ -36,6 +38,7 @@ pub(crate) async fn request { Request::delete(url) .header(header::AUTHORIZATION, auth) + .header(header::USER_AGENT, user_agent) .body(()) .map_err(|_| crate::errors::Error::InvalidRequest)? .send_async() @@ -45,6 +48,7 @@ pub(crate) async fn request( Err(e) => Err(Error::ParseError(e)), } } + +pub fn qualified_version() -> String { + const VERSION: Option<&str> = option_env!("CARGO_PKG_VERSION"); + + format!("Meilisearch Rust (v{})", VERSION.unwrap_or("unknown")) +} From 21afaa0e30f1f4653ee1610f2e8181b623ad4dc7 Mon Sep 17 00:00:00 2001 From: Bruno Casali Date: Fri, 6 May 2022 08:09:32 -0300 Subject: [PATCH 3/3] Update src/request.rs Co-authored-by: Tamo --- src/request.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/request.rs b/src/request.rs index d62ccc17..52cd8d05 100644 --- a/src/request.rs +++ b/src/request.rs @@ -101,7 +101,7 @@ pub(crate) async fn request