How to do testing? #168
-
Hi there, in a FLOSS software I write I'd like to test routes protected by let app: Router = get_app().await.expect("could not create app");
let mut rq = http::Request::builder().uri("/foo").method(http::Method::GET);
let response = app.oneshot(rq.body(Body::empty()).unwrap()).await.unwrap();
// … What is the suggested approach to run the test with a user logged in? Edit: Sorry, forgot to add the axum-login setup. It boils down to this: async fn get_app() -> Router {
let session_layer = SessionManagerLayer::new(caching_store)
.with_expiry(Expiry::OnInactivity(
axum_login::tower_sessions::cookie::time::Duration::hours(1),
));
Router::new()
.route("/", get(example))
.layer(AuthManagerLayerBuilder::new(state.clone(), session_layer.clone()).build())
.layer(session_layer)
.with_state(state)
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
In the integration tests we've used reqwest with cookie management enabled. If you're using |
Beta Was this translation helpful? Give feedback.
In the integration tests we've used reqwest with cookie management enabled. If you're using
oneshot
, you could manage the cookies manually but pulling them off the response headers and then attaching them to request headers. It's a little tedious so it's probably worth writing some test helpers for that. It might even make sense to consider incorporating some of those facilities into the crate itself.