Skip to content
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

# Next

- Fixes:
- `header` matcher now accepts a single header value containing commas (e.g. an RFC 2822 date such as `Sun, 01 Jan 2023 00:00:00 GMT`). Previously the received value was always split on `,`, which caused such matchers to fail.

# `0.5.8`

- Features:
Expand Down
12 changes: 8 additions & 4 deletions src/matchers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,13 @@ impl HeaderExactMatcher {

impl Match for HeaderExactMatcher {
fn matches(&self, request: &Request) -> bool {
let values = request
.headers
.get_all(&self.0)
let raw_values: Vec<&HeaderValue> = request.headers.get_all(&self.0).iter().collect();
if raw_values.len() == self.1.len()
&& raw_values.iter().zip(self.1.iter()).all(|(a, b)| *a == b)
{
return true;
}
let split_values = raw_values
.iter()
.filter_map(|v| v.to_str().ok())
.flat_map(|v| {
Expand All @@ -407,7 +411,7 @@ impl Match for HeaderExactMatcher {
.filter_map(|v| HeaderValue::from_str(v).ok())
})
.collect::<Vec<_>>();
values == self.1 // order matters
split_values == self.1
}
}

Expand Down
22 changes: 22 additions & 0 deletions tests/request_header_matching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,28 @@ async fn should_not_match_multi_request_header_upon_incomplete_values() {
assert_eq!(should_fail_incomplete_values.status(), 404);
}

#[async_std::test]
async fn should_match_single_header_value_containing_comma() {
// Arrange
let mock_server = MockServer::start().await;
let mock = Mock::given(method("GET"))
.and(header("x-ms-date", "Sun, 01 Jan 2023 00:00:00 GMT"))
.respond_with(ResponseTemplate::new(200));
mock_server.register(mock).await;

// Act
let client = reqwest::Client::new();
let should_match = client
.get(mock_server.uri())
.header("x-ms-date", "Sun, 01 Jan 2023 00:00:00 GMT")
.send()
.await
.unwrap();

// Assert
assert_eq!(should_match.status(), 200);
}

#[async_std::test]
async fn should_match_regex_single_header_value() {
// Arrange
Expand Down