Struct MockConnectInfo

pub struct MockConnectInfo<T>(pub T);

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;
# }

Fields

0: T

Trait Implementations

impl<S, T> Layer<S> for MockConnectInfo<T> where T: Clone + Send + Sync + 'static,

type Service = <Extension<MockConnectInfo<T>> as Layer<S>>::Service;
fn layer(&self, inner: S) -> Self::Service

impl<T: Clone> Clone for MockConnectInfo<T>

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

impl<T: Copy> Copy for MockConnectInfo<T>

impl<T: Debug> Debug for MockConnectInfo<T>

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

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> UnsafeUnpin for MockConnectInfo<T> where T: UnsafeUnpin,

impl<T> UnwindSafe for MockConnectInfo<T> where T: UnwindSafe,

Blanket Implementations

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for MockConnectInfo<T> where ST: ?Sized, DT: ?Sized,

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for MockConnectInfo<T> where ST: ?Sized, DT: ?Sized,

impl<T> Any for MockConnectInfo<T> where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for MockConnectInfo<T> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for MockConnectInfo<T> where T: ?Sized,

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

impl<T> CloneToUninit for MockConnectInfo<T> where T: Clone,

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

impl<T> From<T> for MockConnectInfo<T>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> FromRef<T> for MockConnectInfo<T> where T: Clone,

fn from_ref(input: &T) -> T

impl<T> Instrument for MockConnectInfo<T>

impl<T> Read<Exclusive, BecauseExclusive> for MockConnectInfo<T> where T: ?Sized,

impl<T> Same for MockConnectInfo<T>

type Output = T;

impl<T> ToOwned for MockConnectInfo<T> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> WithSubscriber for MockConnectInfo<T>

impl<T, U> Into<U> for MockConnectInfo<T> where U: From<T>,

fn into(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<U> for MockConnectInfo<T> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for MockConnectInfo<T> where U: TryFrom<T>,

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

impl<V, T> VZip<V> for MockConnectInfo<T> where V: MultiLane<T>,

fn vzip(self) -> V