use axum::body::Body;
use axum::extract::Path;
use http::{Request, StatusCode};
use tower::ServiceExt;
mod comments {
use super::Path;
pub async fn index(Path(_post_id): Path<u64>) -> &'static str {
"index"
}
pub async fn new(Path(_post_id): Path<u64>) -> &'static str {
"new"
}
pub async fn create(Path(_post_id): Path<u64>) -> &'static str {
"create"
}
pub async fn show(Path(_id): Path<u64>) -> &'static str {
"show"
}
pub async fn edit(Path(_id): Path<u64>) -> &'static str {
"edit"
}
pub async fn update(Path(_id): Path<u64>) -> &'static str {
"update"
}
pub async fn destroy(Path(_id): Path<u64>) -> &'static str {
"destroy"
}
}
async fn status(app: &axum::Router, method: &str, uri: &str) -> StatusCode {
app.clone()
.oneshot(
Request::builder()
.method(method)
.uri(uri)
.body(Body::empty())
.unwrap(),
)
.await
.unwrap()
.status()
}
#[tokio::test]
async fn shallow_resources_nest_collection_but_not_member() {
let app = doido_controller::routes! {
shallow_resources!(posts, comments, comments);
};
assert_eq!(
status(&app, "GET", "/posts/1/comments").await,
StatusCode::OK
);
assert_eq!(
status(&app, "POST", "/posts/1/comments").await,
StatusCode::OK
);
assert_eq!(
status(&app, "GET", "/posts/1/comments/new").await,
StatusCode::OK
);
assert_eq!(status(&app, "GET", "/comments/9").await, StatusCode::OK);
assert_eq!(
status(&app, "GET", "/comments/9/edit").await,
StatusCode::OK
);
assert_eq!(status(&app, "PATCH", "/comments/9").await, StatusCode::OK);
assert_eq!(status(&app, "DELETE", "/comments/9").await, StatusCode::OK);
assert_eq!(
status(&app, "GET", "/posts/1/comments/9").await,
StatusCode::NOT_FOUND
);
}