axum_test/transport_layer/into_transport_layer/
router.rsuse anyhow::Result;
use axum::Router;
use crate::transport_layer::IntoTransportLayer;
use crate::transport_layer::TransportLayer;
use crate::transport_layer::TransportLayerBuilder;
impl IntoTransportLayer for Router<()> {
fn into_http_transport_layer(
self,
builder: TransportLayerBuilder,
) -> Result<Box<dyn TransportLayer>> {
self.into_make_service().into_http_transport_layer(builder)
}
fn into_mock_transport_layer(self) -> Result<Box<dyn TransportLayer>> {
self.into_make_service().into_mock_transport_layer()
}
}
#[cfg(test)]
mod test_into_http_transport_layer {
use axum::extract::State;
use axum::routing::get;
use axum::Router;
use crate::TestServer;
async fn get_ping() -> &'static str {
"pong!"
}
async fn get_state(State(count): State<u32>) -> String {
format!("count is {}", count)
}
#[tokio::test]
async fn it_should_create_and_test_with_make_into_service() {
let app: Router = Router::new().route("/ping", get(get_ping));
let server = TestServer::builder()
.http_transport()
.build(app)
.expect("Should create test server");
server.get(&"/ping").await.assert_text(&"pong!");
}
#[tokio::test]
async fn it_should_create_and_test_with_make_into_service_with_state() {
let app: Router = Router::new()
.route("/count", get(get_state))
.with_state(123);
let server = TestServer::builder()
.http_transport()
.build(app)
.expect("Should create test server");
server.get(&"/count").await.assert_text(&"count is 123");
}
}
#[cfg(test)]
mod test_into_mock_transport_layer_for_router {
use axum::extract::State;
use axum::routing::get;
use axum::Router;
use crate::TestServer;
async fn get_ping() -> &'static str {
"pong!"
}
async fn get_state(State(count): State<u32>) -> String {
format!("count is {}", count)
}
#[tokio::test]
async fn it_should_create_and_test_with_make_into_service() {
let app: Router = Router::new().route("/ping", get(get_ping));
let server = TestServer::builder()
.mock_transport()
.build(app)
.expect("Should create test server");
server.get(&"/ping").await.assert_text(&"pong!");
}
#[tokio::test]
async fn it_should_create_and_test_with_make_into_service_with_state() {
let app: Router = Router::new()
.route("/count", get(get_state))
.with_state(123);
let server = TestServer::builder()
.mock_transport()
.build(app)
.expect("Should create test server");
server.get(&"/count").await.assert_text(&"count is 123");
}
}