Patch errors if page is missing.
This commit is contained in:
69
src/main.rs
69
src/main.rs
@@ -45,7 +45,7 @@ async fn create_app(state: MoreThanText) -> Router {
|
||||
Router::new()
|
||||
.route("/", get(mtt_conn))
|
||||
.route("/{document}", get(mtt_conn))
|
||||
.route("/api/{document}", post(mtt_conn))
|
||||
.route("/api/{document}", post(mtt_conn).patch(mtt_conn))
|
||||
.layer(CookieManagerLayer::new())
|
||||
.layer(Extension(state.clone()))
|
||||
.with_state(state)
|
||||
@@ -91,6 +91,7 @@ async fn mtt_conn(
|
||||
let action = match method {
|
||||
Method::GET => ActionType::Get,
|
||||
Method::POST => ActionType::Add,
|
||||
Method::PATCH => ActionType::Update,
|
||||
_ => unreachable!("reouter should prevent this"),
|
||||
};
|
||||
let doc = match path.get("document") {
|
||||
@@ -305,4 +306,70 @@ mod servers {
|
||||
"do not allow post to existing documents"
|
||||
);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn patch_root() {
|
||||
let content = format!("content-{}", Uuid::new_v4());
|
||||
let document = json!({
|
||||
"template": content.clone()
|
||||
});
|
||||
let app = create_app(MoreThanText::new()).await;
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method(Method::PATCH)
|
||||
.uri("/api/root".to_string())
|
||||
.body(document.to_string())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
response.status(),
|
||||
StatusCode::OK,
|
||||
"failed to patch /api/root",
|
||||
);
|
||||
let response = app
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.uri("/".to_string())
|
||||
.body(Body::empty())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
response.status(),
|
||||
StatusCode::OK,
|
||||
"failed to get to home page",
|
||||
);
|
||||
let body = response.into_body().collect().await.unwrap().to_bytes();
|
||||
assert_eq!(body, content);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn patch_missing_page() {
|
||||
let content = format!("content-{}", Uuid::new_v4());
|
||||
let document = json!({
|
||||
"template": content.clone()
|
||||
});
|
||||
let app = create_app(MoreThanText::new()).await;
|
||||
let response = app
|
||||
.clone()
|
||||
.oneshot(
|
||||
Request::builder()
|
||||
.method(Method::PATCH)
|
||||
.uri("/api/something".to_string())
|
||||
.body(document.to_string())
|
||||
.unwrap(),
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(
|
||||
response.status(),
|
||||
StatusCode::NOT_FOUND,
|
||||
"failed to patch /api/somethingt",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user