#![cfg(feature = "openapi")]
#![allow(clippy::unwrap_used, clippy::panic, clippy::expect_used)]
use axum::Router;
use axum::body::Body;
use axum::http::{Method, Request, StatusCode};
use tower::ServiceExt as _;
use utoipa::OpenApi;
use rusty_gasket::openapi::OpenApiPlugin;
use rusty_gasket::plugin::GasketApp;
#[derive(OpenApi)]
#[openapi()]
struct ApiDoc;
async fn router() -> Router {
GasketApp::builder()
.plugin(OpenApiPlugin::from_api_doc::<ApiDoc>())
.build()
.await
.expect("app builds")
.build_router()
}
#[tokio::test]
async fn build_router_does_not_panic_with_openapi_plugin() {
let _router = router().await;
}
#[tokio::test]
async fn openapi_json_route_is_served_once() {
let resp = router()
.await
.oneshot(
Request::builder()
.method(Method::GET)
.uri("/openapi.json")
.body(Body::empty())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.status(),
StatusCode::OK,
"GET /openapi.json must still serve the spec after de-duplicating the route"
);
}
#[tokio::test]
async fn swagger_ui_is_served() {
let resp = router()
.await
.oneshot(
Request::builder()
.method(Method::GET)
.uri("/swagger-ui/")
.body(Body::empty())
.expect("request builds"),
)
.await
.expect("router responds");
assert_eq!(
resp.status(),
StatusCode::OK,
"the Swagger UI must still be served at /swagger-ui/"
);
}