Struct axum::extract::connect_info::MockConnectInfo
source · pub struct MockConnectInfo<T>(pub T);
Expand description
Middleware used to mock ConnectInfo
during tests.
If you’re accidentally using MockConnectInfo
and
Router::into_make_service_with_connect_info
at the same time then
Router::into_make_service_with_connect_info
takes precedence.
§Example
use axum::{
Router,
extract::connect_info::{MockConnectInfo, ConnectInfo},
body::Body,
routing::get,
http::{Request, StatusCode},
};
use std::net::SocketAddr;
use tower::ServiceExt;
async fn handler(ConnectInfo(addr): ConnectInfo<SocketAddr>) {}
// this router you can run with `app.into_make_service_with_connect_info::<SocketAddr>()`
fn app() -> Router {
Router::new().route("/", get(handler))
}
// use this router for tests
fn test_app() -> Router {
app().layer(MockConnectInfo(SocketAddr::from(([0, 0, 0, 0], 1337))))
}
// #[tokio::test]
async fn some_test() {
let app = test_app();
let request = Request::new(Body::empty());
let response = app.oneshot(request).await.unwrap();
assert_eq!(response.status(), StatusCode::OK);
}
Tuple Fields§
§0: T
Trait Implementations§
source§impl<T: Clone> Clone for MockConnectInfo<T>
impl<T: Clone> Clone for MockConnectInfo<T>
source§fn clone(&self) -> MockConnectInfo<T>
fn clone(&self) -> MockConnectInfo<T>
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl<T: Debug> Debug for MockConnectInfo<T>
impl<T: Debug> Debug for MockConnectInfo<T>
source§impl<S, T> Layer<S> for MockConnectInfo<T>
impl<S, T> Layer<S> for MockConnectInfo<T>
impl<T: Copy> Copy for MockConnectInfo<T>
Auto Trait Implementations§
impl<T> Freeze for MockConnectInfo<T>where
T: Freeze,
impl<T> RefUnwindSafe for MockConnectInfo<T>where
T: RefUnwindSafe,
impl<T> Send for MockConnectInfo<T>where
T: Send,
impl<T> Sync for MockConnectInfo<T>where
T: Sync,
impl<T> Unpin for MockConnectInfo<T>where
T: Unpin,
impl<T> UnwindSafe for MockConnectInfo<T>where
T: UnwindSafe,
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)source§impl<T> CloneToUninit for Twhere
T: Copy,
impl<T> CloneToUninit for Twhere
T: Copy,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
🔬This is a nightly-only experimental API. (
clone_to_uninit
)