Skip to content
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

feat: added PartialEq to Cors #486

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 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
2 changes: 2 additions & 0 deletions actix-cors/CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

## Unreleased

- Implement `PartialEq` for `Cors` allowing for better testing. [#486]

## 0.7.0

- `Cors` is now marked `#[must_use]`.
Expand Down
20 changes: 20 additions & 0 deletions actix-cors/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -608,6 +608,19 @@ where
.unwrap()
}

impl PartialEq for Cors {
fn eq(&self, other: &Self) -> bool {
self.inner == other.inner
// Because of the cors-function, checking if the content is equal implies that the errors are equal
//
// Proof by contradiction:
// Lets assume that the inner values are equal, but the error values are not.
// This means there had been an error, which has been fixed.
// This cannot happen as the first call to set the invalid value means that further usages of the cors-function will reject other input.
// => inner has to be in a different state
}
}

#[cfg(test)]
mod test {
use std::convert::Infallible;
Expand Down Expand Up @@ -679,4 +692,11 @@ mod test {

Cors::default().new_transform(srv).await.unwrap();
}

#[test]
fn impl_eq() {
assert_eq!(Cors::default().send_wildcard(), Cors::default());
assert_ne!(Cors::default().send_wildcard(), Cors::default());
assert_ne!(Cors::default(), Cors::permissive());
}
}
8 changes: 7 additions & 1 deletion actix-cors/src/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ impl Default for OriginFn {
}
}

impl PartialEq for OriginFn {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.boxed_fn, &other.boxed_fn)
robjtede marked this conversation as resolved.
Show resolved Hide resolved
}
}

impl fmt::Debug for OriginFn {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("origin_fn")
Expand All @@ -40,7 +46,7 @@ pub(crate) fn header_value_try_into_method(hdr: &HeaderValue) -> Option<Method>
.and_then(|meth| Method::try_from(meth).ok())
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct Inner {
pub(crate) allowed_origins: AllOrSome<HashSet<HeaderValue>>,
pub(crate) allowed_origins_fns: SmallVec<[OriginFn; 4]>,
Expand Down
Loading