diff --git a/.github/workflows/ci-generators.yml b/.github/workflows/ci-generators.yml index 1d5bba03..c543f25e 100644 --- a/.github/workflows/ci-generators.yml +++ b/.github/workflows/ci-generators.yml @@ -69,7 +69,7 @@ jobs: DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres_test - name: controller - run: cargo run -- generate controller pages about && cargo build && cargo test pages + run: cargo run -- generate controller pages about --htmx && cargo build && cargo test pages working-directory: ./examples/demo env: REDIS_URL: redis://localhost:${{job.services.redis.ports[6379]}} diff --git a/src/cli.rs b/src/cli.rs index 9c3a9a2c..6ec12883 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -184,9 +184,21 @@ enum ComponentArg { /// Actions actions: Vec, - /// The kind of scaffold to generate - #[clap(short, long, value_enum, default_value_t = gen::ScaffoldKind::Api)] - kind: gen::ScaffoldKind, + /// The kind of controller actions to generate + #[clap(short, long, value_enum, group = "scaffold_kind_group")] + kind: Option, + + /// Use HTMX controller actions + #[clap(long, group = "scaffold_kind_group")] + htmx: bool, + + /// Use HTML controller actions + #[clap(long, group = "scaffold_kind_group")] + html: bool, + + /// Use API controller actions + #[clap(long, group = "scaffold_kind_group")] + api: bool, }, /// Generate a Task based on the given name Task { @@ -256,11 +268,30 @@ impl TryFrom for Component { name, actions, kind, - } => Ok(Self::Controller { - name, - actions, - kind, - }), + htmx, + html, + api, + } => { + let kind = if let Some(kind) = kind { + kind + } else if htmx { + ScaffoldKind::Htmx + } else if html { + ScaffoldKind::Html + } else if api { + ScaffoldKind::Api + } else { + return Err(crate::Error::string( + "Error: One of `kind`, `htmx`, `html`, or `api` must be specified.", + )); + }; + + Ok(Self::Controller { + name, + actions, + kind, + }) + } ComponentArg::Task { name } => Ok(Self::Task { name }), ComponentArg::Scheduler {} => Ok(Self::Scheduler {}), ComponentArg::Worker { name } => Ok(Self::Worker { name }), diff --git a/src/controller/app_routes.rs b/src/controller/app_routes.rs index 1d21940a..6d75d64e 100644 --- a/src/controller/app_routes.rs +++ b/src/controller/app_routes.rs @@ -521,3 +521,110 @@ fn handle_panic(err: Box) -> axum::response: errors::Error::InternalServerError.into_response() } + +#[cfg(test)] +mod tests { + + use super::*; + use crate::prelude::*; + use crate::tests_cfg; + use insta::assert_debug_snapshot; + use rstest::rstest; + use tower::ServiceExt; + + async fn action() -> Result { + format::json("loco") + } + + #[test] + fn can_load_app_route_from_default() { + for route in AppRoutes::with_default_routes().collect() { + assert_debug_snapshot!( + format!("[{}]", route.uri.replace('/', "[slash]")), + format!("{:?} {}", route.actions, route.uri) + ); + } + } + + #[test] + fn can_load_empty_app_routes() { + assert_eq!(AppRoutes::empty().collect().len(), 0); + } + + #[test] + fn can_load_routes() { + let router_without_prefix = Routes::new().add("/", get(action)); + let normalizer = Routes::new() + .prefix("/normalizer") + .add("no-slash", get(action)) + .add("/", post(action)) + .add("//loco///rs//", delete(action)) + .add("//////multiple-start", head(action)) + .add("multiple-end/////", trace(action)); + + let app_router = AppRoutes::empty() + .add_route(router_without_prefix) + .add_route(normalizer) + .add_routes(vec![ + Routes::new().add("multiple1", put(action)), + Routes::new().add("multiple2", options(action)), + Routes::new().add("multiple3", patch(action)), + ]); + + for route in app_router.collect() { + assert_debug_snapshot!( + format!("[{}]", route.uri.replace('/', "[slash]")), + format!("{:?} {}", route.actions, route.uri) + ); + } + } + + #[test] + fn can_load_routes_with_root_prefix() { + let router_without_prefix = Routes::new() + .add("/loco", get(action)) + .add("loco-rs", get(action)); + + let app_router = AppRoutes::empty() + .prefix("api") + .add_route(router_without_prefix); + + for route in app_router.collect() { + assert_debug_snapshot!( + format!("[{}]", route.uri.replace('/', "[slash]")), + format!("{:?} {}", route.actions, route.uri) + ); + } + } + #[rstest] + #[case(axum::http::Method::GET, get(action))] + #[case(axum::http::Method::POST, post(action))] + #[case(axum::http::Method::DELETE, delete(action))] + #[case(axum::http::Method::HEAD, head(action))] + #[case(axum::http::Method::OPTIONS, options(action))] + #[case(axum::http::Method::PATCH, patch(action))] + #[case(axum::http::Method::POST, post(action))] + #[case(axum::http::Method::PUT, put(action))] + #[case(axum::http::Method::TRACE, trace(action))] + #[tokio::test] + async fn can_xx( + #[case] http_method: axum::http::Method, + #[case] method: axum::routing::MethodRouter, + ) { + let router_without_prefix = Routes::new().add("/loco", method); + + let app_router = AppRoutes::empty().add_route(router_without_prefix); + + let ctx = tests_cfg::app::get_app_context().await; + let router = app_router.to_router(ctx, axum::Router::new()).unwrap(); + + let req = axum::http::Request::builder() + .uri("/loco") + .method(http_method) + .body(axum::body::Body::empty()) + .unwrap(); + + let response = router.oneshot(req).await.unwrap(); + assert!(response.status().is_success()); + } +} diff --git a/src/controller/fallback.html b/src/controller/fallback.html index 306e1bfe..610b6aee 100644 --- a/src/controller/fallback.html +++ b/src/controller/fallback.html @@ -11,7 +11,7 @@
-
+
diff --git a/src/controller/format.rs b/src/controller/format.rs index 19c407c2..8acb7038 100644 --- a/src/controller/format.rs +++ b/src/controller/format.rs @@ -367,3 +367,238 @@ impl Default for RenderBuilder { pub fn render() -> RenderBuilder { RenderBuilder::new() } + +#[cfg(test)] +mod tests { + + use super::*; + use crate::{controller::views::engines::TeraView, prelude::*}; + use insta::assert_debug_snapshot; + use tree_fs; + + async fn response_body_to_string(response: hyper::Response) -> String { + let bytes = axum::body::to_bytes(response.into_body(), 200) + .await + .unwrap(); + std::str::from_utf8(&bytes).unwrap().to_string() + } + + pub fn get_header_from_response( + response: &hyper::Response, + header: &str, + ) -> Option { + Some(response.headers().get(header)?.to_str().ok()?.to_string()) + } + + #[tokio::test] + async fn empty_response_format() { + let response: hyper::Response = empty().unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(response_body_to_string(response).await, String::new()); + } + + #[tokio::test] + async fn text_response_format() { + let response_content = "loco"; + let response = text(response_content).unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(response_body_to_string(response).await, response_content); + } + + #[tokio::test] + async fn json_response_format() { + let response_content = serde_json::json!({"loco": "app"}); + let response = json(&response_content).unwrap(); + + assert_debug_snapshot!(response); + assert_eq!( + response_body_to_string(response).await, + response_content.to_string() + ); + } + + #[tokio::test] + async fn empty_json_response_format() { + let response = empty_json().unwrap(); + + assert_debug_snapshot!(response); + assert_eq!( + response_body_to_string(response).await, + serde_json::json!({}).to_string() + ); + } + + #[tokio::test] + async fn html_response_format() { + let response_content: &str = "

loco

"; + let response = html(response_content).unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(response_body_to_string(response).await, response_content); + } + + #[tokio::test] + async fn redirect_response() { + let response = redirect("https://loco.rs").unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(response_body_to_string(response).await, String::new()); + } + + #[tokio::test] + async fn view_response() { + let yaml_content = r" + files: + - path: template/test.html + content: |- + - {{foo}} + "; + + let tree_res = tree_fs::from_yaml_str(yaml_content).unwrap(); + let v = TeraView::from_custom_dir(&tree_res).unwrap(); + + assert_debug_snapshot!(view(&v, "template/none.html", serde_json::json!({}))); + let response = view(&v, "template/test.html", serde_json::json!({"foo": "loco"})).unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(&response_body_to_string(response).await, "- loco"); + } + + #[tokio::test] + async fn template_response() { + let response = template("- {{foo}}", serde_json::json!({"foo": "loco"})).unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(&response_body_to_string(response).await, "- loco"); + } + + #[tokio::test] + async fn builder_set_status_code_response() { + assert_eq!(render().empty().unwrap().status(), 200); + assert_eq!(render().status(202).empty().unwrap().status(), 202); + } + + #[tokio::test] + async fn builder_set_headers_response() { + assert_eq!(render().empty().unwrap().headers().len(), 0); + let response = render() + .header("header-1", "loco") + .header("header-2", "rs") + .empty() + .unwrap(); + + assert_eq!(response.headers().len(), 2); + assert_eq!( + get_header_from_response(&response, "header-1"), + Some("loco".to_string()) + ); + assert_eq!( + get_header_from_response(&response, "header-2"), + Some("rs".to_string()) + ); + } + + #[tokio::test] + async fn builder_etag_response() { + assert_eq!(render().empty().unwrap().headers().len(), 0); + let response = render().etag("foobar").unwrap().empty().unwrap(); + + assert_eq!(response.headers().len(), 1); + assert_eq!( + get_header_from_response(&response, "etag"), + Some("foobar".to_string()) + ); + } + + #[tokio::test] + async fn builder_cookies_response() { + let response = render() + .cookies(&[ + cookie::Cookie::new("foo", "bar"), + cookie::Cookie::new("baz", "qux"), + ]) + .unwrap() + .empty() + .unwrap(); + + assert_debug_snapshot!(response.headers()); + } + + #[tokio::test] + async fn builder_text_response() { + let response = render().text("loco").unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(&response_body_to_string(response).await, "loco"); + } + + #[tokio::test] + async fn builder_empty_response() { + let response = render().empty().unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(response_body_to_string(response).await, String::new()); + } + + #[tokio::test] + async fn builder_view_response() { + let yaml_content = r" + files: + - path: template/test.html + content: |- + - {{foo}} + "; + + let tree_res = tree_fs::from_yaml_str(yaml_content).unwrap(); + let v = TeraView::from_custom_dir(&tree_res).unwrap(); + + assert_debug_snapshot!(view(&v, "template/none.html", serde_json::json!({}))); + let response = render() + .view(&v, "template/test.html", serde_json::json!({"foo": "loco"})) + .unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(&response_body_to_string(response).await, "- loco"); + } + + #[tokio::test] + async fn builder_template_response() { + let response = render() + .template("- {{foo}}", serde_json::json!({"foo": "loco"})) + .unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(&response_body_to_string(response).await, "- loco"); + } + + #[tokio::test] + async fn builder_html_response() { + let response_content = "

loco

"; + let response = render().html(response_content).unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(&response_body_to_string(response).await, response_content); + } + + #[tokio::test] + async fn builder_json_response() { + let response_content = serde_json::json!({"loco": "app"}); + let response = render().json(&response_content).unwrap(); + + assert_debug_snapshot!(response); + assert_eq!( + response_body_to_string(response).await, + response_content.to_string() + ); + } + + #[tokio::test] + async fn builder_redirect_response() { + let response = render().redirect("https://loco.rs").unwrap(); + + assert_debug_snapshot!(response); + assert_eq!(response_body_to_string(response).await, String::new()); + } +} diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]].snap new file mode 100644 index 00000000..c87c43b6 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[GET] /" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]_health].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]_health].snap new file mode 100644 index 00000000..5b9aafae --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]_health].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[GET] /_health" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]_ping].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]_ping].snap new file mode 100644 index 00000000..2e6f05aa --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]_ping].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[GET] /_ping" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]bar].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]bar].snap new file mode 100644 index 00000000..b65e4d39 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]bar].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[GET] /api/bar" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]foo].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]foo].snap new file mode 100644 index 00000000..2f826466 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]foo].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[GET] /api/foo" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]loco-rs].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]loco-rs].snap new file mode 100644 index 00000000..e1e39891 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]loco-rs].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[GET] /api/loco-rs" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]loco].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]loco].snap new file mode 100644 index 00000000..4cfc803c --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]api[slash]loco].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[GET] /api/loco" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple1].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple1].snap new file mode 100644 index 00000000..155bd6f2 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple1].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[PUT] /multiple1" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple2].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple2].snap new file mode 100644 index 00000000..63e06dbd --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple2].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[OPTIONS] /multiple2" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple3].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple3].snap new file mode 100644 index 00000000..f64a4085 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]multiple3].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[PATCH] /multiple3" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]foo[slash]bar].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]foo[slash]bar].snap new file mode 100644 index 00000000..d6ee9be9 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]foo[slash]bar].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[DELETE] /normalizer/foo/bar" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]loco[slash]rs].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]loco[slash]rs].snap new file mode 100644 index 00000000..1e5f17a6 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]loco[slash]rs].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[DELETE] /normalizer/loco/rs" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]multiple-end].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]multiple-end].snap new file mode 100644 index 00000000..06009cbd --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]multiple-end].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[TRACE] /normalizer/multiple-end" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]multiple-start].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]multiple-start].snap new file mode 100644 index 00000000..b685d6fc --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]multiple-start].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[HEAD] /normalizer/multiple-start" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]no-slash].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]no-slash].snap new file mode 100644 index 00000000..c65a0d84 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer[slash]no-slash].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[GET] /normalizer/no-slash" diff --git a/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer].snap b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer].snap new file mode 100644 index 00000000..8c1f9c7c --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__app_routes__tests__[[slash]normalizer].snap @@ -0,0 +1,5 @@ +--- +source: src/controller/app_routes.rs +expression: "format!(\"{:?} {}\", route.actions, route.uri)" +--- +"[POST] /normalizer" diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_cookies_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_cookies_response.snap new file mode 100644 index 00000000..f04b6c4f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_cookies_response.snap @@ -0,0 +1,8 @@ +--- +source: src/controller/format.rs +expression: response.headers() +--- +{ + "set-cookie": "foo=bar", + "set-cookie": "baz=qux", +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_empty_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_empty_response.snap new file mode 100644 index 00000000..f0ae5da0 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_empty_response.snap @@ -0,0 +1,12 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: {}, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_html_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_html_response.snap new file mode 100644 index 00000000..725b8a2f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_html_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "text/html; charset=utf-8", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_json_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_json_response.snap new file mode 100644 index 00000000..2d44c197 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_json_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "application/json", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_redirect_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_redirect_response.snap new file mode 100644 index 00000000..dc2a417d --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_redirect_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 303, + version: HTTP/1.1, + headers: { + "location": "https://loco.rs", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_template_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_template_response.snap new file mode 100644 index 00000000..725b8a2f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_template_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "text/html; charset=utf-8", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_text_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_text_response.snap new file mode 100644 index 00000000..72bd7516 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_text_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "text/plain; charset=utf-8", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_view_response-2.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_view_response-2.snap new file mode 100644 index 00000000..725b8a2f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_view_response-2.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "text/html; charset=utf-8", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__builder_view_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__builder_view_response.snap new file mode 100644 index 00000000..de00916f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__builder_view_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: "view(&v, \"template/none.html\", serde_json::json!({}))" +--- +Err( + Tera( + Error { + kind: TemplateNotFound( + "template/none.html", + ), + source: None, + }, + ), +) diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__empty_json_response_format.snap b/src/controller/snapshots/loco_rs__controller__format__tests__empty_json_response_format.snap new file mode 100644 index 00000000..2d44c197 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__empty_json_response_format.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "application/json", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__empty_response_format.snap b/src/controller/snapshots/loco_rs__controller__format__tests__empty_response_format.snap new file mode 100644 index 00000000..f0ae5da0 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__empty_response_format.snap @@ -0,0 +1,12 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: {}, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__html_response_format.snap b/src/controller/snapshots/loco_rs__controller__format__tests__html_response_format.snap new file mode 100644 index 00000000..725b8a2f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__html_response_format.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "text/html; charset=utf-8", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__json_response_format.snap b/src/controller/snapshots/loco_rs__controller__format__tests__json_response_format.snap new file mode 100644 index 00000000..2d44c197 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__json_response_format.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "application/json", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__redirect_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__redirect_response.snap new file mode 100644 index 00000000..dc2a417d --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__redirect_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 303, + version: HTTP/1.1, + headers: { + "location": "https://loco.rs", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__template_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__template_response.snap new file mode 100644 index 00000000..725b8a2f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__template_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "text/html; charset=utf-8", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__text_response_format.snap b/src/controller/snapshots/loco_rs__controller__format__tests__text_response_format.snap new file mode 100644 index 00000000..72bd7516 --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__text_response_format.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "text/plain; charset=utf-8", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__view_response-2.snap b/src/controller/snapshots/loco_rs__controller__format__tests__view_response-2.snap new file mode 100644 index 00000000..725b8a2f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__view_response-2.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: response +--- +Response { + status: 200, + version: HTTP/1.1, + headers: { + "content-type": "text/html; charset=utf-8", + }, + body: Body( + UnsyncBoxBody, + ), +} diff --git a/src/controller/snapshots/loco_rs__controller__format__tests__view_response.snap b/src/controller/snapshots/loco_rs__controller__format__tests__view_response.snap new file mode 100644 index 00000000..de00916f --- /dev/null +++ b/src/controller/snapshots/loco_rs__controller__format__tests__view_response.snap @@ -0,0 +1,14 @@ +--- +source: src/controller/format.rs +expression: "view(&v, \"template/none.html\", serde_json::json!({}))" +--- +Err( + Tera( + Error { + kind: TemplateNotFound( + "template/none.html", + ), + source: None, + }, + ), +) diff --git a/src/prelude.rs b/src/prelude.rs index 5c6e2338..ea92be2f 100644 --- a/src/prelude.rs +++ b/src/prelude.rs @@ -2,7 +2,7 @@ pub use async_trait::async_trait; pub use axum::{ extract::{Form, Path, State}, response::{IntoResponse, Response}, - routing::{delete, get, post, put}, + routing::{delete, get, head, options, patch, post, put, trace}, }; pub use axum_extra::extract::cookie; pub use chrono::NaiveDateTime as DateTime;