agama/rust/agama-server/tests/common/mod.rs
Imobach González Sosa c91416139a
Add aide infrastructure to generate the OpenAPI spec
* Use aide::axum::ApiRouter instead of axum::Router.
* Update the xtask openapi-aide to use the ApiRouter.
2026-04-22 21:55:39 +01:00

58 lines
1.7 KiB
Rust

// Copyright (c) [2024] SUSE LLC
//
// All Rights Reserved.
//
// This program is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation; either version 2 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, contact SUSE LLC.
//
// To contact SUSE LLC about this file by physical or electronic mail, you may
// find current contact information at www.suse.com.
use aide::axum::ApiRouter;
use axum::{
body::{to_bytes, Body},
extract::Request,
response::Response,
};
use tower::ServiceExt;
/// Turns a request or response body into a string.
pub async fn body_to_string(body: Body) -> String {
let bytes = to_bytes(body, usize::MAX).await.unwrap();
String::from_utf8(bytes.to_vec()).unwrap()
}
/// Wrapper around a router to send request.
///
/// It hides the details of the communication with the server.
pub struct Client {
router: ApiRouter,
}
impl Client {
/// Creates a new client.
///
/// * `router`: service router.
pub fn new(router: ApiRouter) -> Self {
Self { router }
}
/// Sends a message.
pub async fn send_request(&self, request: Request<String>) -> Response<Body> {
self.router
.clone()
.oneshot(request)
.await
.expect("Could not send the request: {request:?}")
}
}