Skip to content

feat: Make method and MethodExactMatcher case in-sensitive #165

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -65,20 +65,17 @@ pub struct MethodExactMatcher(Method);
/// Shorthand for [`MethodExactMatcher::new`].
pub fn method<T>(method: T) -> MethodExactMatcher
where
T: TryInto<Method>,
<T as TryInto<Method>>::Error: std::fmt::Debug,
T: AsRef<str>,
{
MethodExactMatcher::new(method)
}

impl MethodExactMatcher {
pub fn new<T>(method: T) -> Self
where
T: TryInto<Method>,
<T as TryInto<Method>>::Error: std::fmt::Debug,
T: AsRef<str>,
{
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)
}
Expand Down
41 changes: 41 additions & 0 deletions tests/mocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}