Skip to content

Commit

Permalink
chore: preserve original behaviour for text/plain content-type header…
Browse files Browse the repository at this point in the history
… with application/json body
  • Loading branch information
YOU54F committed May 29, 2024
1 parent 57b16b8 commit 72c6e57
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 14 deletions.
29 changes: 17 additions & 12 deletions rust/pact_matching/src/binary_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,14 @@ where
}

let mut magic_content_type = "";
let mut magic_match = false;
if inferred_content_type == "" {
// Use tree_magic_mini crate to detect via magic bytes using mime-db (requires user to install)
magic_content_type = tree_magic_mini::from_u8(data);
let magic_match = magic_content_type == expected;
magic_match = magic_content_type == expected;
debug!("Matching binary contents by content type: expected '{}', detection method: tree_magic_mini '{}' -> {}",
expected, magic_content_type, magic_match);
if magic_match && magic_content_type != "text/plain" {
if magic_match {
return Ok(());
}
}
Expand All @@ -72,17 +73,21 @@ where
}
};

if detected_content_type == "text/plain" {
let matched = magic_match == true || inferred_match == true;
let unmatched = magic_match == false && inferred_match == false;
let detected_text_plain = detected_content_type == "text/plain";

if unmatched && detected_text_plain {
let bytes_detected_content_type = detect_content_type_from_bytes(data);
if bytes_detected_content_type.is_some() {
let bytes_detected_content_type = bytes_detected_content_type.unwrap();
let bytes_match = bytes_detected_content_type == ContentType::from(&expected);
debug!("Matching binary contents by content type: expected '{}', detection method: detect_content_type_from_bytes '{}' -> {}",
expected, bytes_detected_content_type, bytes_match);
if bytes_match {
return Ok(());
}
}
let bytes_detected_content_type = bytes_detected_content_type.unwrap();
let bytes_match = if bytes_detected_content_type == ContentType::from(&expected) { Some(()) } else { None };
debug!("Matching binary contents by content type: expected '{}', detection method: detect_content_type_from_bytes '{}' -> {:?}",
expected, bytes_detected_content_type, bytes_match);
return bytes_match.ok_or_else(|| anyhow!(
"Expected binary contents to have content type '{}' but detected contents was '{}'",
expected,
bytes_detected_content_type
));
}

return Err(anyhow!(
Expand Down
4 changes: 2 additions & 2 deletions rust/pact_matching/src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -975,8 +975,8 @@ mod tests {
let matcher = MatchingRule::ContentType("text/plain".to_string());
expect!(Value::String("plain text".into()).matches_with(&Value::String("plain text".into()), &matcher, false)).to(be_ok());
expect!(Value::String("plain text".into()).matches_with(&Value::String("different text".into()), &matcher, false)).to(be_ok());
expect!(Value::String("plain text".into()).matches_with(&json!(100), &matcher, false)).to(be_err());
expect!(Value::String("plain text".into()).matches_with(&json!(100.01), &matcher, false)).to(be_err());
expect!(Value::String("plain text".into()).matches_with(&json!(100), &matcher, false)).to(be_ok());
expect!(Value::String("plain text".into()).matches_with(&json!(100.01), &matcher, false)).to(be_ok());
{
let xml = r#"<?xml version="1.0" encoding="UTF-8"?>
<note>
Expand Down

0 comments on commit 72c6e57

Please sign in to comment.