mirror of
https://gitlab.com/msrd0/gotham-restful.git
synced 2025-02-23 13:02:28 +00:00
tests for the access control request method matcher
This commit is contained in:
parent
e5e9cd5d3c
commit
8321b63982
1 changed files with 50 additions and 1 deletions
|
@ -36,6 +36,10 @@ pub struct AccessControlRequestMethodMatcher
|
||||||
|
|
||||||
impl AccessControlRequestMethodMatcher
|
impl AccessControlRequestMethodMatcher
|
||||||
{
|
{
|
||||||
|
/// Construct a new matcher that matches if the `Access-Control-Request-Method` header matches `method`.
|
||||||
|
/// Note that during matching the method is normalized according to the fetch specification, that is,
|
||||||
|
/// byte-uppercased. This means that when using a custom `method` instead of a predefined one, make sure
|
||||||
|
/// it is uppercased or this matcher will never succeed.
|
||||||
pub fn new(method : Method) -> Self
|
pub fn new(method : Method) -> Self
|
||||||
{
|
{
|
||||||
Self { method }
|
Self { method }
|
||||||
|
@ -46,12 +50,57 @@ impl RouteMatcher for AccessControlRequestMethodMatcher
|
||||||
{
|
{
|
||||||
fn is_match(&self, state : &State) -> Result<(), RouteNonMatch>
|
fn is_match(&self, state : &State) -> Result<(), RouteNonMatch>
|
||||||
{
|
{
|
||||||
|
// according to the fetch specification, methods should be normalized by byte-uppercase
|
||||||
|
// https://fetch.spec.whatwg.org/#concept-method
|
||||||
match HeaderMap::borrow_from(state).get(ACCESS_CONTROL_REQUEST_METHOD)
|
match HeaderMap::borrow_from(state).get(ACCESS_CONTROL_REQUEST_METHOD)
|
||||||
.and_then(|value| value.to_str().ok())
|
.and_then(|value| value.to_str().ok())
|
||||||
.and_then(|str| str.parse::<Method>().ok())
|
.and_then(|str| str.to_ascii_uppercase().parse::<Method>().ok())
|
||||||
{
|
{
|
||||||
Some(m) if m == self.method => Ok(()),
|
Some(m) if m == self.method => Ok(()),
|
||||||
_ => Err(RouteNonMatch::new(StatusCode::NOT_FOUND))
|
_ => Err(RouteNonMatch::new(StatusCode::NOT_FOUND))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod test
|
||||||
|
{
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
fn with_state<F>(accept : Option<&str>, block : F)
|
||||||
|
where F : FnOnce(&mut State) -> ()
|
||||||
|
{
|
||||||
|
State::with_new(|state| {
|
||||||
|
let mut headers = HeaderMap::new();
|
||||||
|
if let Some(acc) = accept
|
||||||
|
{
|
||||||
|
headers.insert(ACCESS_CONTROL_REQUEST_METHOD, acc.parse().unwrap());
|
||||||
|
}
|
||||||
|
state.put(headers);
|
||||||
|
block(state);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn no_acrm_header()
|
||||||
|
{
|
||||||
|
let matcher = AccessControlRequestMethodMatcher::new(Method::PUT);
|
||||||
|
with_state(None, |state| assert!(matcher.is_match(&state).is_err()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn correct_acrm_header()
|
||||||
|
{
|
||||||
|
let matcher = AccessControlRequestMethodMatcher::new(Method::PUT);
|
||||||
|
with_state(Some("PUT"), |state| assert!(matcher.is_match(&state).is_ok()));
|
||||||
|
with_state(Some("put"), |state| assert!(matcher.is_match(&state).is_ok()));
|
||||||
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn incorrect_acrm_header()
|
||||||
|
{
|
||||||
|
let matcher = AccessControlRequestMethodMatcher::new(Method::PUT);
|
||||||
|
with_state(Some("DELETE"), |state| assert!(matcher.is_match(&state).is_err()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue