From f1975cedb4f490c4bdd2de5fcec3be236ec071f8 Mon Sep 17 00:00:00 2001 From: BobrImperator Date: Fri, 25 Jul 2025 14:41:37 +0200 Subject: [PATCH 1/2] test: method(Get) is case insensitive --- tests/mocks.rs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) diff --git a/tests/mocks.rs b/tests/mocks.rs index 7ab4b05..50cf3ec 100644 --- a/tests/mocks.rs +++ b/tests/mocks.rs @@ -450,3 +450,44 @@ async fn custom_err() { ]; assert_eq!(actual_err, expected_err); } + +#[async_std::test] +async fn method_matcher_is_case_insensitive() { + // Arrange + let mock_server = MockServer::start().await; + let response = ResponseTemplate::new(200).set_body_bytes("world"); + let mock = Mock::given(method("Get")) + .and(PathExactMatcher::new("hello")) + .respond_with(response); + mock_server.register(mock).await; + + // Act + let response = reqwest::get(format!("{}/hello", &mock_server.uri())) + .await + .unwrap(); + + // Assert + assert_eq!(response.status(), 200); + assert_eq!(response.text().await.unwrap(), "world"); +} + +#[async_std::test] +async fn http_crate_method_can_be_used_directly() { + use http::Method; + // Arrange + let mock_server = MockServer::start().await; + let response = ResponseTemplate::new(200).set_body_bytes("world"); + let mock = Mock::given(method(Method::GET)) + .and(PathExactMatcher::new("hello")) + .respond_with(response); + mock_server.register(mock).await; + + // Act + let response = reqwest::get(format!("{}/hello", &mock_server.uri())) + .await + .unwrap(); + + // Assert + assert_eq!(response.status(), 200); + assert_eq!(response.text().await.unwrap(), "world"); +} From d3751f053fc6707baa66a562b1b95e1fd0bd40e8 Mon Sep 17 00:00:00 2001 From: BobrImperator Date: Fri, 25 Jul 2025 16:42:30 +0200 Subject: [PATCH 2/2] feat: make method matcher case-insensitive --- src/matchers.rs | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/matchers.rs b/src/matchers.rs index 62f688a..69cccfd 100644 --- a/src/matchers.rs +++ b/src/matchers.rs @@ -16,7 +16,7 @@ use regex::Regex; use serde::Serialize; use serde_json::Value; use std::convert::TryInto; -use std::str; +use std::str::{self, FromStr}; use url::Url; /// Implement the `Match` trait for all closures, out of the box, @@ -65,8 +65,7 @@ pub struct MethodExactMatcher(Method); /// Shorthand for [`MethodExactMatcher::new`]. pub fn method(method: T) -> MethodExactMatcher where - T: TryInto, - >::Error: std::fmt::Debug, + T: AsRef, { MethodExactMatcher::new(method) } @@ -74,11 +73,9 @@ where impl MethodExactMatcher { pub fn new(method: T) -> Self where - T: TryInto, - >::Error: std::fmt::Debug, + T: AsRef, { - let method = method - .try_into() + let method = Method::from_str(&method.as_ref().to_ascii_uppercase()) .expect("Failed to convert to HTTP method."); Self(method) }