#[cfg(feature = "templates")] #[macro_use] extern crate rocket; #[cfg(feature = "templates")] mod templates_tests { use std::path::{Path, PathBuf}; use rocket::{Rocket, Build}; use rocket::config::Config; use rocket_contrib::templates::{Template, Metadata}; #[get("//")] fn template_check(md: Metadata<'_>, engine: &str, name: &str) -> Option<()> { match md.contains_template(&format!("{}/{}", engine, name)) { true => Some(()), false => None } } #[get("/is_reloading")] fn is_reloading(md: Metadata<'_>) -> Option<()> { if md.reloading() { Some(()) } else { None } } fn template_root() -> PathBuf { Path::new(env!("CARGO_MANIFEST_DIR")).join("tests").join("templates") } fn rocket() -> Rocket { rocket::custom(Config::figment().merge(("template_dir", template_root()))) .attach(Template::fairing()) .mount("/", routes![template_check, is_reloading]) } #[test] fn test_callback_error() { use rocket::{local::blocking::Client, error::ErrorKind::FailedFairings}; let rocket = rocket::build().attach(Template::try_custom(|_| { Err("error reloading templates!".into()) })); let error = Client::debug(rocket).expect_err("client failure"); match error.kind() { FailedFairings(failures) => assert_eq!(failures[0].name, "Templating"), _ => panic!("Wrong kind of launch error"), } } #[test] fn test_sentinel() { use rocket::{local::blocking::Client, error::ErrorKind::SentinelAborts}; let err = Client::debug_with(routes![is_reloading]).unwrap_err(); assert!(matches!(err.kind(), SentinelAborts(vec) if vec.len() == 1)); let err = Client::debug_with(routes![is_reloading, template_check]).unwrap_err(); assert!(matches!(err.kind(), SentinelAborts(vec) if vec.len() == 2)); #[get("/")] fn return_template() -> Template { Template::render("foo", ()) } let err = Client::debug_with(routes![return_template]).unwrap_err(); assert!(matches!(err.kind(), SentinelAborts(vec) if vec.len() == 1)); #[get("/")] fn return_opt_template() -> Option