Struct MockConnectInfo

struct MockConnectInfo<T>(661)

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);
}
#
# #[tokio::main]
# async fn main() {
#     some_test().await;
# }

Implementations

impl<S, T> Layer for MockConnectInfo<T>

fn layer(self: &Self, inner: S) -> <Self as >::Service

impl<T> Any for MockConnectInfo<T>

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for MockConnectInfo<T>

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for MockConnectInfo<T>

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for MockConnectInfo<T>

unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)

impl<T> Freeze for MockConnectInfo<T>

impl<T> From for MockConnectInfo<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FromRef for MockConnectInfo<T>

fn from_ref(input: &T) -> T

impl<T> Instrument for MockConnectInfo<T>

impl<T> RefUnwindSafe for MockConnectInfo<T>

impl<T> Same for MockConnectInfo<T>

impl<T> Send for MockConnectInfo<T>

impl<T> Sync for MockConnectInfo<T>

impl<T> ToOwned for MockConnectInfo<T>

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> Unpin for MockConnectInfo<T>

impl<T> UnwindSafe for MockConnectInfo<T>

impl<T> WithSubscriber for MockConnectInfo<T>

impl<T, U> Into for MockConnectInfo<T>

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for MockConnectInfo<T>

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for MockConnectInfo<T>

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>

impl<T: $crate::clone::Clone> Clone for MockConnectInfo<T>

fn clone(self: &Self) -> MockConnectInfo<T>

impl<T: $crate::fmt::Debug> Debug for MockConnectInfo<T>

fn fmt(self: &Self, f: &mut $crate::fmt::Formatter<'_>) -> $crate::fmt::Result

impl<T: $crate::marker::Copy> Copy for MockConnectInfo<T>

impl<V, T> VZip for MockConnectInfo<T>

fn vzip(self: Self) -> V