2017-02-01 11:12:24 +00:00
|
|
|
use super::Route;
|
|
|
|
|
|
|
|
use http::uri::URI;
|
2017-03-28 07:12:59 +00:00
|
|
|
use http::MediaType;
|
2017-02-01 11:12:24 +00:00
|
|
|
use request::Request;
|
|
|
|
|
2016-09-08 07:02:17 +00:00
|
|
|
/// The Collider trait is used to determine if two items that can be routed on
|
|
|
|
/// can match against a given request. That is, if two items `collide`, they
|
|
|
|
/// will both match against _some_ request.
|
2016-03-21 04:27:17 +00:00
|
|
|
pub trait Collider<T: ?Sized = Self> {
|
2016-03-19 02:05:29 +00:00
|
|
|
fn collides_with(&self, other: &T) -> bool;
|
2016-03-18 02:43:58 +00:00
|
|
|
}
|
|
|
|
|
2016-09-30 22:20:11 +00:00
|
|
|
pub fn index_match_until(break_c: char,
|
|
|
|
a: &str,
|
|
|
|
b: &str,
|
|
|
|
dir: bool)
|
|
|
|
-> Option<(isize, isize)> {
|
2016-03-19 02:05:29 +00:00
|
|
|
let (a_len, b_len) = (a.len() as isize, b.len() as isize);
|
|
|
|
let (mut i, mut j, delta) = if dir {
|
|
|
|
(0, 0, 1)
|
|
|
|
} else {
|
|
|
|
(a_len - 1, b_len - 1, -1)
|
|
|
|
};
|
|
|
|
|
2016-04-23 02:48:03 +00:00
|
|
|
let break_b = break_c as u8;
|
2016-03-19 02:05:29 +00:00
|
|
|
while i >= 0 && j >= 0 && i < a_len && j < b_len {
|
2016-04-23 02:48:03 +00:00
|
|
|
let (c1, c2) = (a.as_bytes()[i as usize], b.as_bytes()[j as usize]);
|
|
|
|
if c1 == break_b || c2 == break_b {
|
2016-03-19 02:05:29 +00:00
|
|
|
break;
|
|
|
|
} else if c1 != c2 {
|
2016-03-22 05:04:39 +00:00
|
|
|
return None;
|
2016-03-19 02:05:29 +00:00
|
|
|
} else {
|
|
|
|
i += delta;
|
|
|
|
j += delta;
|
|
|
|
}
|
|
|
|
}
|
2016-03-18 02:43:58 +00:00
|
|
|
|
2016-04-23 02:48:03 +00:00
|
|
|
Some((i, j))
|
2016-03-22 05:04:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn do_match_until(break_c: char, a: &str, b: &str, dir: bool) -> bool {
|
|
|
|
index_match_until(break_c, a, b, dir).is_some()
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
2016-03-18 02:43:58 +00:00
|
|
|
|
2016-04-01 23:54:53 +00:00
|
|
|
impl<'a> Collider<str> for &'a str {
|
|
|
|
fn collides_with(&self, other: &str) -> bool {
|
2016-04-02 08:46:41 +00:00
|
|
|
let (a, b) = (self, other);
|
|
|
|
do_match_until('<', a, b, true) && do_match_until('>', a, b, false)
|
2016-04-01 23:54:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-27 10:52:26 +00:00
|
|
|
// This _only_ checks the `path` component of the URI.
|
2017-02-01 11:12:24 +00:00
|
|
|
impl<'a, 'b> Collider<URI<'b>> for URI<'a> {
|
|
|
|
fn collides_with(&self, other: &URI<'b>) -> bool {
|
|
|
|
for (seg_a, seg_b) in self.segments().zip(other.segments()) {
|
|
|
|
if seg_a.ends_with("..>") || seg_b.ends_with("..>") {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if !seg_a.collides_with(seg_b) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if self.segment_count() != other.segment_count() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 07:12:59 +00:00
|
|
|
impl Collider for MediaType {
|
|
|
|
fn collides_with(&self, other: &MediaType) -> bool {
|
2017-02-01 11:12:24 +00:00
|
|
|
let collide = |a, b| a == "*" || b == "*" || a == b;
|
2017-03-24 05:41:42 +00:00
|
|
|
collide(self.top(), other.top()) && collide(self.sub(), other.sub())
|
2017-02-01 11:12:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This implementation is used at initialization to check if two user routes
|
|
|
|
// collide before launching. Format collisions works like this:
|
2017-03-27 10:52:26 +00:00
|
|
|
// * If route specifies format, it only gets requests for that format.
|
|
|
|
// * If route doesn't specify format, it gets requests for any format.
|
|
|
|
// Query collisions work like this:
|
|
|
|
// * If route specifies qeury, it only gets request that have queries.
|
|
|
|
// * If route doesn't specify qeury, requests with and without queries match.
|
|
|
|
// As a result, as long as everything else collides, whether a route has a query
|
|
|
|
// or not is irrelevant: it will collide.
|
2017-02-01 11:12:24 +00:00
|
|
|
impl Collider for Route {
|
|
|
|
fn collides_with(&self, b: &Route) -> bool {
|
|
|
|
self.method == b.method
|
|
|
|
&& self.rank == b.rank
|
|
|
|
&& self.path.collides_with(&b.path)
|
|
|
|
&& match (self.format.as_ref(), b.format.as_ref()) {
|
2017-03-28 07:12:59 +00:00
|
|
|
(Some(mt_a), Some(mt_b)) => mt_a.collides_with(mt_b),
|
2017-02-01 11:12:24 +00:00
|
|
|
(Some(_), None) => true,
|
|
|
|
(None, Some(_)) => true,
|
|
|
|
(None, None) => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// This implementation is used at runtime to check if a given request is
|
|
|
|
// intended for this Route. Format collisions works like this:
|
2017-03-27 10:52:26 +00:00
|
|
|
// * If route specifies format, it only gets requests for that format.
|
|
|
|
// * If route doesn't specify format, it gets requests for any format.
|
|
|
|
// Query collisions work like this:
|
|
|
|
// * If route specifies a query, it only gets request that have queries.
|
|
|
|
// * If route doesn't specify query, requests with & without queries collide.
|
2017-02-01 11:12:24 +00:00
|
|
|
impl<'r> Collider<Request<'r>> for Route {
|
|
|
|
fn collides_with(&self, req: &Request<'r>) -> bool {
|
|
|
|
self.method == req.method()
|
2017-03-27 10:52:26 +00:00
|
|
|
&& self.path.collides_with(req.uri())
|
|
|
|
&& self.path.query().map_or(true, |_| req.uri().query().is_some())
|
2017-02-01 11:12:24 +00:00
|
|
|
// FIXME: On payload requests, check Content-Type, else Accept.
|
|
|
|
&& match (req.content_type().as_ref(), self.format.as_ref()) {
|
2017-03-28 07:12:59 +00:00
|
|
|
(Some(mt_a), Some(mt_b)) => mt_a.collides_with(mt_b),
|
2017-02-01 11:12:24 +00:00
|
|
|
(Some(_), None) => true,
|
|
|
|
(None, Some(_)) => false,
|
|
|
|
(None, None) => true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-19 02:05:29 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2016-08-26 08:55:11 +00:00
|
|
|
use std::str::FromStr;
|
2016-10-04 00:09:13 +00:00
|
|
|
|
2017-02-01 11:12:24 +00:00
|
|
|
use super::Collider;
|
2016-10-25 11:24:07 +00:00
|
|
|
use request::Request;
|
|
|
|
use data::Data;
|
2016-12-15 08:47:31 +00:00
|
|
|
use handler::Outcome;
|
2016-10-04 00:09:13 +00:00
|
|
|
use router::route::Route;
|
2017-03-28 07:12:59 +00:00
|
|
|
use http::{Method, MediaType, ContentType};
|
2016-10-04 00:25:27 +00:00
|
|
|
use http::uri::URI;
|
2016-10-04 00:09:13 +00:00
|
|
|
use http::Method::*;
|
2016-03-19 02:05:29 +00:00
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
type SimpleRoute = (Method, &'static str);
|
|
|
|
|
2016-12-15 08:47:31 +00:00
|
|
|
fn dummy_handler(_req: &Request, _: Data) -> Outcome<'static> {
|
|
|
|
Outcome::of("hi")
|
2016-03-22 05:04:39 +00:00
|
|
|
}
|
|
|
|
|
2016-03-21 04:27:17 +00:00
|
|
|
fn m_collide(a: SimpleRoute, b: SimpleRoute) -> bool {
|
2016-04-02 07:51:40 +00:00
|
|
|
let route_a = Route::new(a.0, a.1.to_string(), dummy_handler);
|
|
|
|
route_a.collides_with(&Route::new(b.0, b.1.to_string(), dummy_handler))
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-02 08:46:41 +00:00
|
|
|
fn unranked_collide(a: &'static str, b: &'static str) -> bool {
|
|
|
|
let route_a = Route::ranked(0, Get, a.to_string(), dummy_handler);
|
|
|
|
route_a.collides_with(&Route::ranked(0, Get, b.to_string(), dummy_handler))
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
2016-04-01 23:54:53 +00:00
|
|
|
fn s_s_collide(a: &'static str, b: &'static str) -> bool {
|
2016-09-08 07:02:17 +00:00
|
|
|
URI::new(a).collides_with(&URI::new(b))
|
2016-04-01 23:54:53 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 02:05:29 +00:00
|
|
|
#[test]
|
|
|
|
fn simple_collisions() {
|
2016-04-02 08:46:41 +00:00
|
|
|
assert!(unranked_collide("a", "a"));
|
|
|
|
assert!(unranked_collide("/a", "/a"));
|
|
|
|
assert!(unranked_collide("/hello", "/hello"));
|
|
|
|
assert!(unranked_collide("/hello", "/hello/"));
|
|
|
|
assert!(unranked_collide("/hello/there/how/ar", "/hello/there/how/ar"));
|
|
|
|
assert!(unranked_collide("/hello/there", "/hello/there/"));
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn simple_param_collisions() {
|
2016-04-02 08:46:41 +00:00
|
|
|
assert!(unranked_collide("/hello/<name>", "/hello/<person>"));
|
|
|
|
assert!(unranked_collide("/hello/<name>/hi", "/hello/<person>/hi"));
|
|
|
|
assert!(unranked_collide("/hello/<name>/hi/there", "/hello/<person>/hi/there"));
|
|
|
|
assert!(unranked_collide("/<name>/hi/there", "/<person>/hi/there"));
|
|
|
|
assert!(unranked_collide("/<name>/hi/there", "/dude/<name>/there"));
|
|
|
|
assert!(unranked_collide("/<name>/<a>/<b>", "/<a>/<b>/<c>"));
|
|
|
|
assert!(unranked_collide("/<name>/<a>/<b>/", "/<a>/<b>/<c>/"));
|
2016-09-08 07:02:17 +00:00
|
|
|
assert!(unranked_collide("/<a..>", "/hi"));
|
|
|
|
assert!(unranked_collide("/<a..>", "/hi/hey"));
|
|
|
|
assert!(unranked_collide("/<a..>", "/hi/hey/hayo"));
|
|
|
|
assert!(unranked_collide("/a/<a..>", "/a/hi/hey/hayo"));
|
|
|
|
assert!(unranked_collide("/a/<b>/<a..>", "/a/hi/hey/hayo"));
|
|
|
|
assert!(unranked_collide("/a/<b>/<c>/<a..>", "/a/hi/hey/hayo"));
|
|
|
|
assert!(unranked_collide("/<b>/<c>/<a..>", "/a/hi/hey/hayo"));
|
|
|
|
assert!(unranked_collide("/<b>/<c>/hey/hayo", "/a/hi/hey/hayo"));
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn medium_param_collisions() {
|
2016-04-02 08:46:41 +00:00
|
|
|
assert!(unranked_collide("/hello/<name>", "/hello/bob"));
|
|
|
|
assert!(unranked_collide("/<name>", "//bob"));
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn hard_param_collisions() {
|
2016-04-02 08:46:41 +00:00
|
|
|
assert!(unranked_collide("/<name>bob", "/<name>b"));
|
|
|
|
assert!(unranked_collide("/a<b>c", "/abc"));
|
|
|
|
assert!(unranked_collide("/a<b>c", "/azooc"));
|
|
|
|
assert!(unranked_collide("/a<b>", "/a"));
|
|
|
|
assert!(unranked_collide("/<b>", "/a"));
|
|
|
|
assert!(unranked_collide("/<a>/<b>", "/a/b<c>"));
|
|
|
|
assert!(unranked_collide("/<a>/bc<b>", "/a/b<c>"));
|
|
|
|
assert!(unranked_collide("/<a>/bc<b>d", "/a/b<c>"));
|
2016-09-08 07:02:17 +00:00
|
|
|
assert!(unranked_collide("/<a..>", "///a///"));
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 09:43:53 +00:00
|
|
|
#[test]
|
|
|
|
fn query_collisions() {
|
|
|
|
assert!(unranked_collide("/?<a>", "/?<a>"));
|
|
|
|
assert!(unranked_collide("/a/?<a>", "/a/?<a>"));
|
|
|
|
assert!(unranked_collide("/a?<a>", "/a?<a>"));
|
|
|
|
assert!(unranked_collide("/<r>?<a>", "/<r>?<a>"));
|
|
|
|
assert!(unranked_collide("/a/b/c?<a>", "/a/b/c?<a>"));
|
|
|
|
assert!(unranked_collide("/<a>/b/c?<d>", "/a/b/<c>?<d>"));
|
2017-03-27 10:52:26 +00:00
|
|
|
assert!(unranked_collide("/?<a>", "/"));
|
|
|
|
assert!(unranked_collide("/a?<a>", "/a"));
|
|
|
|
assert!(unranked_collide("/a?<a>", "/a"));
|
|
|
|
assert!(unranked_collide("/a/b?<a>", "/a/b"));
|
|
|
|
assert!(unranked_collide("/a/b", "/a/b?<c>"));
|
2017-02-05 09:43:53 +00:00
|
|
|
}
|
|
|
|
|
2016-03-19 02:05:29 +00:00
|
|
|
#[test]
|
|
|
|
fn non_collisions() {
|
2016-04-02 08:46:41 +00:00
|
|
|
assert!(!unranked_collide("/a", "/b"));
|
|
|
|
assert!(!unranked_collide("/a/b", "/a"));
|
|
|
|
assert!(!unranked_collide("/a/b", "/a/c"));
|
|
|
|
assert!(!unranked_collide("/a/hello", "/a/c"));
|
|
|
|
assert!(!unranked_collide("/hello", "/a/c"));
|
|
|
|
assert!(!unranked_collide("/hello/there", "/hello/there/guy"));
|
|
|
|
assert!(!unranked_collide("/b<a>/there", "/hi/there"));
|
|
|
|
assert!(!unranked_collide("/<a>/<b>c", "/hi/person"));
|
|
|
|
assert!(!unranked_collide("/<a>/<b>cd", "/hi/<a>e"));
|
|
|
|
assert!(!unranked_collide("/a<a>/<b>", "/b<b>/<a>"));
|
|
|
|
assert!(!unranked_collide("/a/<b>", "/b/<b>"));
|
|
|
|
assert!(!unranked_collide("/a<a>/<b>", "/b/<b>"));
|
2016-09-08 07:02:17 +00:00
|
|
|
assert!(!unranked_collide("/<a..>", "/"));
|
|
|
|
assert!(!unranked_collide("/hi/<a..>", "/hi"));
|
|
|
|
assert!(!unranked_collide("/hi/<a..>", "/hi/"));
|
|
|
|
assert!(!unranked_collide("/<a..>", "//////"));
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|
|
|
|
|
2017-02-05 09:43:53 +00:00
|
|
|
#[test]
|
|
|
|
fn query_non_collisions() {
|
2017-03-27 10:52:26 +00:00
|
|
|
assert!(!unranked_collide("/a?<b>", "/b"));
|
|
|
|
assert!(!unranked_collide("/a/b", "/a?<b>"));
|
|
|
|
assert!(!unranked_collide("/a/b/c?<d>", "/a/b/c/d"));
|
|
|
|
assert!(!unranked_collide("/a/hello", "/a/?<hello>"));
|
2017-02-05 09:43:53 +00:00
|
|
|
assert!(!unranked_collide("/?<a>", "/hi"));
|
|
|
|
}
|
|
|
|
|
2016-03-19 02:05:29 +00:00
|
|
|
#[test]
|
|
|
|
fn method_dependent_non_collisions() {
|
|
|
|
assert!(!m_collide((Get, "/"), (Post, "/")));
|
|
|
|
assert!(!m_collide((Post, "/"), (Put, "/")));
|
|
|
|
assert!(!m_collide((Put, "/a"), (Put, "/")));
|
|
|
|
assert!(!m_collide((Post, "/a"), (Put, "/")));
|
|
|
|
assert!(!m_collide((Get, "/a"), (Put, "/")));
|
|
|
|
assert!(!m_collide((Get, "/hello"), (Put, "/hello")));
|
|
|
|
}
|
2016-03-21 04:27:17 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_str_non_collisions() {
|
2016-04-01 23:54:53 +00:00
|
|
|
assert!(!s_s_collide("/a", "/b"));
|
|
|
|
assert!(!s_s_collide("/a/b", "/a"));
|
|
|
|
assert!(!s_s_collide("/a/b", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/a/hello", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/hello", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/hello/there", "/hello/there/guy"));
|
|
|
|
assert!(!s_s_collide("/b<a>/there", "/hi/there"));
|
|
|
|
assert!(!s_s_collide("/<a>/<b>c", "/hi/person"));
|
|
|
|
assert!(!s_s_collide("/<a>/<b>cd", "/hi/<a>e"));
|
|
|
|
assert!(!s_s_collide("/a<a>/<b>", "/b<b>/<a>"));
|
|
|
|
assert!(!s_s_collide("/a/<b>", "/b/<b>"));
|
|
|
|
assert!(!s_s_collide("/a<a>/<b>", "/b/<b>"));
|
2016-09-08 07:02:17 +00:00
|
|
|
assert!(!s_s_collide("/a", "/b"));
|
|
|
|
assert!(!s_s_collide("/a/b", "/a"));
|
|
|
|
assert!(!s_s_collide("/a/b", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/a/hello", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/hello", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/hello/there", "/hello/there/guy"));
|
|
|
|
assert!(!s_s_collide("/b<a>/there", "/hi/there"));
|
|
|
|
assert!(!s_s_collide("/<a>/<b>c", "/hi/person"));
|
|
|
|
assert!(!s_s_collide("/<a>/<b>cd", "/hi/<a>e"));
|
|
|
|
assert!(!s_s_collide("/a<a>/<b>", "/b<b>/<a>"));
|
|
|
|
assert!(!s_s_collide("/a/<b>", "/b/<b>"));
|
|
|
|
assert!(!s_s_collide("/a<a>/<b>", "/b/<b>"));
|
|
|
|
assert!(!s_s_collide("/a", "/b"));
|
|
|
|
assert!(!s_s_collide("/a/b", "/a"));
|
|
|
|
assert!(!s_s_collide("/a/b", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/a/hello", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/hello", "/a/c"));
|
|
|
|
assert!(!s_s_collide("/hello/there", "/hello/there/guy"));
|
|
|
|
assert!(!s_s_collide("/b<a>/there", "/hi/there"));
|
|
|
|
assert!(!s_s_collide("/<a>/<b>c", "/hi/person"));
|
|
|
|
assert!(!s_s_collide("/<a>/<b>cd", "/hi/<a>e"));
|
|
|
|
assert!(!s_s_collide("/a<a>/<b>", "/b<b>/<a>"));
|
|
|
|
assert!(!s_s_collide("/a/<b>", "/b/<b>"));
|
|
|
|
assert!(!s_s_collide("/a<a>/<b>", "/b/<b>"));
|
|
|
|
assert!(!s_s_collide("/<a..>", "/"));
|
|
|
|
assert!(!s_s_collide("/hi/<a..>", "/hi/"));
|
|
|
|
assert!(!s_s_collide("/a/hi/<a..>", "/a/hi/"));
|
2016-03-21 04:27:17 +00:00
|
|
|
}
|
2016-08-26 08:55:11 +00:00
|
|
|
|
2017-03-28 07:12:59 +00:00
|
|
|
fn mt_mt_collide(mt1: &str, mt2: &str) -> bool {
|
|
|
|
let mt_a = MediaType::from_str(mt1).expect(mt1);
|
|
|
|
let mt_b = MediaType::from_str(mt2).expect(mt2);
|
|
|
|
mt_a.collides_with(&mt_b)
|
2016-08-26 08:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_content_type_colliions() {
|
2017-03-28 07:12:59 +00:00
|
|
|
assert!(mt_mt_collide("application/json", "application/json"));
|
|
|
|
assert!(mt_mt_collide("*/json", "application/json"));
|
|
|
|
assert!(mt_mt_collide("*/*", "application/json"));
|
|
|
|
assert!(mt_mt_collide("application/*", "application/json"));
|
|
|
|
assert!(mt_mt_collide("application/*", "*/json"));
|
|
|
|
assert!(mt_mt_collide("something/random", "something/random"));
|
|
|
|
|
|
|
|
assert!(!mt_mt_collide("text/*", "application/*"));
|
|
|
|
assert!(!mt_mt_collide("*/text", "*/json"));
|
|
|
|
assert!(!mt_mt_collide("*/text", "application/test"));
|
|
|
|
assert!(!mt_mt_collide("something/random", "something_else/random"));
|
|
|
|
assert!(!mt_mt_collide("something/random", "*/else"));
|
|
|
|
assert!(!mt_mt_collide("*/random", "*/else"));
|
|
|
|
assert!(!mt_mt_collide("something/*", "random/else"));
|
2016-08-26 08:55:11 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 07:12:59 +00:00
|
|
|
fn r_mt_mt_collide<S1, S2>(m1: Method, mt1: S1, m2: Method, mt2: S2) -> bool
|
2017-02-01 11:12:24 +00:00
|
|
|
where S1: Into<Option<&'static str>>, S2: Into<Option<&'static str>>
|
|
|
|
{
|
|
|
|
let mut route_a = Route::new(m1, "/", dummy_handler);
|
2017-03-28 07:12:59 +00:00
|
|
|
if let Some(mt_str) = mt1.into() {
|
|
|
|
route_a.format = Some(mt_str.parse::<MediaType>().unwrap());
|
2017-02-01 11:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut route_b = Route::new(m2, "/", dummy_handler);
|
2017-03-28 07:12:59 +00:00
|
|
|
if let Some(mt_str) = mt2.into() {
|
|
|
|
route_b.format = Some(mt_str.parse::<MediaType>().unwrap());
|
2017-02-01 11:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
route_a.collides_with(&route_b)
|
2016-08-26 08:55:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_route_content_type_colliions() {
|
2017-03-28 07:12:59 +00:00
|
|
|
assert!(r_mt_mt_collide(Get, "application/json", Get, "application/json"));
|
|
|
|
assert!(r_mt_mt_collide(Get, "*/json", Get, "application/json"));
|
|
|
|
assert!(r_mt_mt_collide(Get, "*/json", Get, "application/*"));
|
|
|
|
assert!(r_mt_mt_collide(Get, "text/html", Get, "text/*"));
|
|
|
|
assert!(r_mt_mt_collide(Get, "any/thing", Get, "*/*"));
|
|
|
|
|
|
|
|
assert!(r_mt_mt_collide(Get, None, Get, "text/*"));
|
|
|
|
assert!(r_mt_mt_collide(Get, None, Get, "text/html"));
|
|
|
|
assert!(r_mt_mt_collide(Get, None, Get, "*/*"));
|
|
|
|
assert!(r_mt_mt_collide(Get, "text/html", Get, None));
|
|
|
|
assert!(r_mt_mt_collide(Get, "*/*", Get, None));
|
|
|
|
assert!(r_mt_mt_collide(Get, "application/json", Get, None));
|
|
|
|
|
|
|
|
assert!(!r_mt_mt_collide(Get, "text/html", Get, "application/*"));
|
|
|
|
assert!(!r_mt_mt_collide(Get, "application/html", Get, "text/*"));
|
|
|
|
assert!(!r_mt_mt_collide(Get, "*/json", Get, "text/html"));
|
|
|
|
assert!(!r_mt_mt_collide(Get, "text/html", Get, "text/css"));
|
2017-02-01 11:12:24 +00:00
|
|
|
}
|
|
|
|
|
2017-03-28 07:12:59 +00:00
|
|
|
fn req_route_mt_collide<S1, S2>(m1: Method, mt1: S1, m2: Method, mt2: S2) -> bool
|
2017-02-01 11:12:24 +00:00
|
|
|
where S1: Into<Option<&'static str>>, S2: Into<Option<&'static str>>
|
|
|
|
{
|
|
|
|
let mut req = Request::new(m1, "/");
|
2017-03-28 07:12:59 +00:00
|
|
|
if let Some(mt_str) = mt1.into() {
|
|
|
|
req.replace_header(mt_str.parse::<ContentType>().unwrap());
|
2017-02-01 11:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let mut route = Route::new(m2, "/", dummy_handler);
|
2017-03-28 07:12:59 +00:00
|
|
|
if let Some(mt_str) = mt2.into() {
|
|
|
|
route.format = Some(mt_str.parse::<MediaType>().unwrap());
|
2017-02-01 11:12:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
route.collides_with(&req)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2017-03-28 07:12:59 +00:00
|
|
|
fn test_req_route_mt_collisions() {
|
|
|
|
assert!(req_route_mt_collide(Get, "application/json", Get, "application/json"));
|
|
|
|
assert!(req_route_mt_collide(Get, "application/json", Get, "application/*"));
|
|
|
|
assert!(req_route_mt_collide(Get, "application/json", Get, "*/json"));
|
|
|
|
assert!(req_route_mt_collide(Get, "text/html", Get, "text/html"));
|
|
|
|
assert!(req_route_mt_collide(Get, "text/html", Get, "*/*"));
|
|
|
|
|
|
|
|
assert!(req_route_mt_collide(Get, "text/html", Get, None));
|
|
|
|
assert!(req_route_mt_collide(Get, None, Get, None));
|
|
|
|
assert!(req_route_mt_collide(Get, "application/json", Get, None));
|
|
|
|
assert!(req_route_mt_collide(Get, "x-custom/anything", Get, None));
|
|
|
|
|
|
|
|
assert!(!req_route_mt_collide(Get, "application/json", Get, "text/html"));
|
|
|
|
assert!(!req_route_mt_collide(Get, "application/json", Get, "text/*"));
|
|
|
|
assert!(!req_route_mt_collide(Get, "application/json", Get, "*/xml"));
|
|
|
|
|
|
|
|
assert!(!req_route_mt_collide(Get, None, Get, "text/html"));
|
|
|
|
assert!(!req_route_mt_collide(Get, None, Get, "*/*"));
|
|
|
|
assert!(!req_route_mt_collide(Get, None, Get, "application/json"));
|
2017-03-27 10:52:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn req_route_path_collide(a: &'static str, b: &'static str) -> bool {
|
|
|
|
let req = Request::new(Get, a.to_string());
|
|
|
|
let route = Route::ranked(0, Get, b.to_string(), dummy_handler);
|
|
|
|
route.collides_with(&req)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_req_route_query_collisions() {
|
|
|
|
assert!(req_route_path_collide("/a/b?a=b", "/a/b?<c>"));
|
|
|
|
assert!(req_route_path_collide("/a/b?a=b", "/<a>/b?<c>"));
|
|
|
|
assert!(req_route_path_collide("/a/b?a=b", "/<a>/<b>?<c>"));
|
|
|
|
assert!(req_route_path_collide("/a/b?a=b", "/a/<b>?<c>"));
|
|
|
|
assert!(req_route_path_collide("/?b=c", "/?<b>"));
|
|
|
|
|
|
|
|
assert!(req_route_path_collide("/a/b?a=b", "/a/b"));
|
|
|
|
assert!(req_route_path_collide("/a/b", "/a/b"));
|
|
|
|
assert!(req_route_path_collide("/a/b/c/d?", "/a/b/c/d"));
|
|
|
|
assert!(req_route_path_collide("/a/b/c/d?v=1&v=2", "/a/b/c/d"));
|
|
|
|
|
|
|
|
assert!(!req_route_path_collide("/a/b", "/a/b?<c>"));
|
|
|
|
assert!(!req_route_path_collide("/a/b/c", "/a/b?<c>"));
|
|
|
|
assert!(!req_route_path_collide("/a?b=c", "/a/b?<c>"));
|
|
|
|
assert!(!req_route_path_collide("/?b=c", "/a/b?<c>"));
|
|
|
|
assert!(!req_route_path_collide("/?b=c", "/a?<c>"));
|
2016-08-26 08:55:11 +00:00
|
|
|
}
|
2016-03-19 02:05:29 +00:00
|
|
|
}
|