Normalize paths in 'route_guard' test.

This commit is contained in:
Sergio Benitez 2018-10-23 01:23:11 -07:00
parent 0493febc58
commit f6325798b1
3 changed files with 41 additions and 4 deletions

View File

@ -1,4 +1,6 @@
use std::io;
use std::{fmt, io};
use std::borrow::Cow;
use std::path::{Path, PathBuf, Component};
pub trait ReadExt: io::Read {
fn read_max(&mut self, mut buf: &mut [u8]) -> io::Result<usize> {
@ -17,3 +19,37 @@ pub trait ReadExt: io::Read {
}
impl<T: io::Read> ReadExt for T { }
pub struct NormalizedPath<'a>(&'a Path);
impl<'a> fmt::Display for NormalizedPath<'a> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
fn to_str<'c>(c: &'c Component) -> Cow<'c, str> {
c.as_os_str().to_string_lossy()
}
let mut components = self.0.components();
match (components.next(), components.next()) {
(Some(Component::RootDir), Some(c)) => write!(f, "/{}", to_str(&c))?,
(Some(a), Some(b)) => write!(f, "{}/{}", to_str(&a), to_str(&b))?,
(Some(c), None) => write!(f, "{}", to_str(&c))?,
_ => return Ok(())
};
for c in components {
write!(f, "/{}", to_str(&c))?;
}
Ok(())
}
}
pub trait Normalize {
fn normalized(&self) -> NormalizedPath;
}
impl<T: AsRef<Path>> Normalize for T {
fn normalized(&self) -> NormalizedPath {
NormalizedPath(self.as_ref())
}
}

View File

@ -116,6 +116,7 @@ extern crate isatty;
#[cfg(test)] #[macro_use] extern crate lazy_static;
#[doc(hidden)] #[macro_use] pub mod logger;
#[doc(hidden)] pub mod ext;
pub mod local;
pub mod request;
pub mod response;
@ -141,7 +142,6 @@ mod router;
mod rocket;
mod codegen;
mod catcher;
mod ext;
#[doc(inline)] pub use response::Response;
#[doc(inline)] pub use handler::{Handler, ErrorHandler};

View File

@ -2,12 +2,13 @@
#[macro_use] extern crate rocket;
use std::path::PathBuf;
use std::path::{Path, PathBuf};
use rocket::ext::Normalize;
use rocket::Route;
#[get("/<path..>")]
fn files(route: &Route, path: PathBuf) -> String {
format!("{}/{}", route.base(), path.to_string_lossy())
Path::new(route.base()).join(path).normalized().to_string()
}
mod route_guard_tests {