stubr/model/request/body/
binary_eq.rs

1use crate::error::StubrResult;
2use crate::wiremock::{Match, Request};
3use crate::StubrError;
4
5use super::BodyMatcherStub;
6
7pub struct BinaryExactMatcher(Vec<u8>);
8
9impl Match for BinaryExactMatcher {
10    fn matches(&self, req: &Request) -> bool {
11        self.matching_binary(&req.body)
12    }
13}
14
15impl BinaryExactMatcher {
16    pub fn matching_binary(&self, bytes: &[u8]) -> bool {
17        self.0 == bytes
18    }
19}
20
21impl TryFrom<&BodyMatcherStub> for BinaryExactMatcher {
22    type Error = StubrError;
23
24    fn try_from(body: &BodyMatcherStub) -> StubrResult<Self> {
25        use base64::Engine as _;
26        body.binary_equal_to
27            .as_ref()
28            .filter(|_| body.is_by_binary_equality())
29            .and_then(|it| base64::prelude::BASE64_STANDARD.decode(it).ok())
30            .map(Self)
31            .ok_or_else(|| StubrError::QuietError)
32    }
33}