use axum::body::Body;
use axum::http::{Request, StatusCode};
use tower::ServiceExt as _;
async fn json(res: axum::response::Response) -> serde_json::Value {
let bytes = axum::body::to_bytes(res.into_body(), 64 * 1024)
.await
.expect("body");
serde_json::from_slice(&bytes).expect("the refusal must be JSON")
}
#[tokio::test]
async fn a_write_to_a_read_endpoint_is_refused_in_json() {
let res = rustango_cms::api::router()
.oneshot(
Request::builder()
.method("POST")
.uri("/api/v2/pages/")
.body(Body::empty())
.unwrap(),
)
.await
.expect("oneshot");
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
assert_eq!(
res.headers()
.get(axum::http::header::CONTENT_TYPE)
.and_then(|v| v.to_str().ok())
.unwrap_or_default()
.split(';')
.next()
.unwrap_or_default(),
"application/json",
"a JSON API must not refuse in a format its clients cannot parse",
);
let allow = res
.headers()
.get(axum::http::header::ALLOW)
.and_then(|v| v.to_str().ok())
.expect("405 must carry Allow")
.to_owned();
assert!(allow.contains("GET"), "got {allow}");
let body = json(res).await;
assert_eq!(body["error"]["code"], "method_not_allowed");
assert!(
body["error"]["message"].is_string(),
"the envelope carries a message like every other error: {body}",
);
}
#[tokio::test]
async fn the_allow_header_reflects_the_route_not_a_constant() {
let res = rustango_cms::api::router()
.oneshot(
Request::builder()
.method("GET")
.uri("/api/v2/auth/login")
.body(Body::empty())
.unwrap(),
)
.await
.expect("oneshot");
assert_eq!(res.status(), StatusCode::METHOD_NOT_ALLOWED);
let allow = res
.headers()
.get(axum::http::header::ALLOW)
.and_then(|v| v.to_str().ok())
.expect("Allow")
.to_owned();
assert!(allow.contains("POST"), "got {allow}");
assert!(
!allow.contains("GET"),
"login does not serve GET, so Allow must not claim it: {allow}",
);
let body = json(res).await;
assert_eq!(body["error"]["code"], "method_not_allowed");
}
#[tokio::test]
async fn a_successful_read_is_untouched_by_the_middleware() {
let res = rustango_cms::api::router()
.oneshot(
Request::builder()
.uri("/api/v2/openapi.json")
.body(Body::empty())
.unwrap(),
)
.await
.expect("oneshot");
assert_eq!(res.status(), StatusCode::OK);
let body = json(res).await;
assert_eq!(body["openapi"].as_str().unwrap_or_default().get(..1), Some("3"));
}
#[tokio::test]
async fn cors_exposes_the_validator_and_allow_headers() {
use rustango_cms::api::Cors;
let cors = Cors::Origins(vec!["https://spa.example.com".to_owned()]);
let res = rustango_cms::api::router_with(&cors)
.oneshot(
Request::builder()
.uri("/api/v2/openapi.json")
.header("Origin", "https://spa.example.com")
.body(Body::empty())
.unwrap(),
)
.await
.expect("oneshot");
let exposed = res
.headers()
.get("access-control-expose-headers")
.and_then(|v| v.to_str().ok())
.unwrap_or_default()
.to_ascii_lowercase();
assert!(
exposed.contains("etag"),
"a client that cannot read ETag cannot ask for a 304: {exposed:?}",
);
assert!(
exposed.contains("allow"),
"Allow names the verbs that would work: {exposed:?}",
);
}