pub struct Conn { /* private fields */ }
Expand description
a client connection, representing both an outbound http request and a http response
Implementations§
Source§impl Conn
impl Conn
Sourcepub fn request_headers(&self) -> &Headers
pub fn request_headers(&self) -> &Headers
borrow the request headers
Sourcepub fn with_request_header(
self,
name: impl Into<HeaderName<'static>>,
value: impl Into<HeaderValues>,
) -> Conn
pub fn with_request_header( self, name: impl Into<HeaderName<'static>>, value: impl Into<HeaderValues>, ) -> Conn
chainable setter for inserting
a request header
use trillium_testing::ClientConfig;
let handler = |conn: trillium::Conn| async move {
let header = conn.request_headers().get_str("some-request-header").unwrap_or_default();
let response = format!("some-request-header was {}", header);
conn.ok(response)
};
let client = trillium_client::Client::new(ClientConfig::new());
trillium_testing::with_server(handler, |url| async move {
let mut conn = client.get(url)
.with_request_header("some-request-header", "header-value") // <--
.await?;
assert_eq!(
conn.response_body().read_string().await?,
"some-request-header was header-value"
);
Ok(())
})
Sourcepub fn with_header(
self,
name: impl Into<HeaderName<'static>>,
value: impl Into<HeaderValues>,
) -> Conn
👎Deprecated: use Conn::with_request_header
pub fn with_header( self, name: impl Into<HeaderName<'static>>, value: impl Into<HeaderValues>, ) -> Conn
see [`with_request_header]
Sourcepub fn with_request_headers<HN, HV, I>(self, headers: I) -> Connwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'static>>,
HV: Into<HeaderValues>,
pub fn with_request_headers<HN, HV, I>(self, headers: I) -> Connwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'static>>,
HV: Into<HeaderValues>,
chainable setter for extending
request headers
let handler = |conn: trillium::Conn| async move {
let header = conn.request_headers().get_str("some-request-header").unwrap_or_default();
let response = format!("some-request-header was {}", header);
conn.ok(response)
};
use trillium_testing::ClientConfig;
let client = trillium_client::client(ClientConfig::new());
trillium_testing::with_server(handler, move |url| async move {
let mut conn = client.get(url)
.with_request_headers([ // <--
("some-request-header", "header-value"),
("some-other-req-header", "other-header-value")
])
.await?;
assert_eq!(
conn.response_body().read_string().await?,
"some-request-header was header-value"
);
Ok(())
})
Sourcepub fn with_headers<HN, HV, I>(self, headers: I) -> Connwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'static>>,
HV: Into<HeaderValues>,
👎Deprecated: use Conn::with_request_headers
pub fn with_headers<HN, HV, I>(self, headers: I) -> Connwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'static>>,
HV: Into<HeaderValues>,
see [with_request_headers
]
Sourcepub fn without_request_header(
self,
name: impl Into<HeaderName<'static>>,
) -> Conn
pub fn without_request_header( self, name: impl Into<HeaderName<'static>>, ) -> Conn
Chainable method to remove a request header if present
Sourcepub fn without_header(self, name: impl Into<HeaderName<'static>>) -> Conn
👎Deprecated: use Conn::without_request_header
pub fn without_header(self, name: impl Into<HeaderName<'static>>) -> Conn
see [without_request_header
]
Sourcepub fn response_headers(&self) -> &Headers
pub fn response_headers(&self) -> &Headers
let handler = |conn: trillium::Conn| async move {
conn.with_response_header("some-header", "some-value")
.with_status(200)
};
use trillium_client::Client;
use trillium_testing::ClientConfig;
trillium_testing::with_server(handler, move |url| async move {
let client = Client::new(ClientConfig::new());
let conn = client.get(url).await?;
let headers = conn.response_headers(); //<-
assert_eq!(headers.get_str("some-header"), Some("some-value"));
Ok(())
})
Sourcepub fn request_headers_mut(&mut self) -> &mut Headers
pub fn request_headers_mut(&mut self) -> &mut Headers
retrieves a mutable borrow of the request headers, suitable for appending a header. generally, prefer using chainable methods on Conn
use trillium_testing::ClientConfig;
use trillium_client::Client;
let handler = |conn: trillium::Conn| async move {
let header = conn.request_headers().get_str("some-request-header").unwrap_or_default();
let response = format!("some-request-header was {}", header);
conn.ok(response)
};
let client = Client::new(ClientConfig::new());
trillium_testing::with_server(handler, move |url| async move {
let mut conn = client.get(url);
conn.request_headers_mut() //<-
.insert("some-request-header", "header-value");
let mut conn = conn.await?;
assert_eq!(
conn.response_body().read_string().await?,
"some-request-header was header-value"
);
Ok(())
})
Sourcepub fn response_headers_mut(&mut self) -> &mut Headers
pub fn response_headers_mut(&mut self) -> &mut Headers
get a mutable borrow of the response headers
Sourcepub fn set_request_body(&mut self, body: impl Into<Body>)
pub fn set_request_body(&mut self, body: impl Into<Body>)
sets the request body on a mutable reference. prefer the chainable
Conn::with_body
wherever possible
env_logger::init();
use trillium_client::Client;
use trillium_testing::ClientConfig;
let handler = |mut conn: trillium::Conn| async move {
let body = conn.request_body_string().await.unwrap();
conn.ok(format!("request body was: {}", body))
};
trillium_testing::with_server(handler, move |url| async move {
let client = Client::new(ClientConfig::new());
let mut conn = client.post(url);
conn.set_request_body("body"); //<-
(&mut conn).await?;
assert_eq!(conn.response_body().read_string().await?, "request body was: body");
Ok(())
});
Sourcepub fn with_body(self, body: impl Into<Body>) -> Conn
pub fn with_body(self, body: impl Into<Body>) -> Conn
chainable setter for the request body
env_logger::init();
use trillium_testing::ClientConfig;
use trillium_client::Client;
let handler = |mut conn: trillium::Conn| async move {
let body = conn.request_body_string().await.unwrap();
conn.ok(format!("request body was: {}", body))
};
trillium_testing::with_server(handler, |url| async move {
let client = Client::from(ClientConfig::default());
let mut conn = client.post(url)
.with_body("body") //<-
.await?;
assert_eq!(
conn.response_body().read_string().await?,
"request body was: body"
);
Ok(())
});
Sourcepub fn with_json_body(self, body: &impl Serialize) -> Result<Conn, Error>
pub fn with_json_body(self, body: &impl Serialize) -> Result<Conn, Error>
chainable setter for json body. this requires the json
crate feature to be enabled.
Sourcepub fn url(&self) -> &Url
pub fn url(&self) -> &Url
retrieves the url for this conn.
use trillium_testing::ClientConfig;
use trillium_client::Client;
let client = Client::from(ClientConfig::new());
let conn = client.get("http://localhost:9080");
let url = conn.url(); //<-
assert_eq!(url.host_str().unwrap(), "localhost");
Sourcepub fn method(&self) -> Method
pub fn method(&self) -> Method
retrieves the url for this conn.
use trillium_testing::ClientConfig;
use trillium_client::Client;
use trillium_testing::prelude::*;
let client = Client::from(ClientConfig::new());
let conn = client.get("http://localhost:9080");
let method = conn.method(); //<-
assert_eq!(method, Method::Get);
Sourcepub fn response_body(&mut self) -> ReceivedBody<'_, BoxedTransport>
pub fn response_body(&mut self) -> ReceivedBody<'_, BoxedTransport>
returns a ReceivedBody
that borrows the connection inside this conn.
env_logger::init();
use trillium_testing::ClientConfig;
use trillium_client::Client;
let handler = |mut conn: trillium::Conn| async move {
conn.ok("hello from trillium")
};
trillium_testing::with_server(handler, |url| async move {
let client = Client::from(ClientConfig::new());
let mut conn = client.get(url).await?;
let response_body = conn.response_body(); //<-
assert_eq!(19, response_body.content_length().unwrap());
let string = response_body.read_string().await?;
assert_eq!("hello from trillium", string);
Ok(())
});
Sourcepub async fn response_json<T>(&mut self) -> Result<T, ClientSerdeError>where
T: DeserializeOwned,
pub async fn response_json<T>(&mut self) -> Result<T, ClientSerdeError>where
T: DeserializeOwned,
Attempt to deserialize the response body. Note that this consumes the body content.
Sourcepub fn status(&self) -> Option<Status>
pub fn status(&self) -> Option<Status>
returns the status code for this conn. if the conn has not yet been sent, this will be None.
use trillium_testing::ClientConfig;
use trillium_client::Client;
use trillium_testing::prelude::*;
async fn handler(conn: trillium::Conn) -> trillium::Conn {
conn.with_status(418)
}
trillium_testing::with_server(handler, |url| async move {
let client = Client::new(ClientConfig::new());
let conn = client.get(url).await?;
assert_eq!(Status::ImATeapot, conn.status().unwrap());
Ok(())
});
Sourcepub fn success(self) -> Result<Conn, UnexpectedStatusError>
pub fn success(self) -> Result<Conn, UnexpectedStatusError>
Returns the conn or an UnexpectedStatusError
that contains the conn
use trillium_testing::ClientConfig;
trillium_testing::with_server(trillium::Status::NotFound, |url| async move {
let client = trillium_client::Client::new(ClientConfig::new());
assert_eq!(
client.get(url).await?.success().unwrap_err().to_string(),
"expected a success (2xx) status code, but got 404 Not Found"
);
Ok(())
});
trillium_testing::with_server(trillium::Status::Ok, |url| async move {
let client = trillium_client::Client::new(ClientConfig::new());
assert!(client.get(url).await?.success().is_ok());
Ok(())
});
Sourcepub async fn recycle(self)
pub async fn recycle(self)
Returns this conn to the connection pool if it is keepalive, and closes it otherwise. This will happen asynchronously as a spawned task when the conn is dropped, but calling it explicitly allows you to block on it and control where it happens.
Sourcepub fn peer_addr(&self) -> Option<SocketAddr>
pub fn peer_addr(&self) -> Option<SocketAddr>
attempts to retrieve the connected peer address
Trait Implementations§
Source§impl ClientConnExt for Conn
impl ClientConnExt for Conn
fn success_or_error( self, ) -> Pin<Box<dyn Future<Output = ClientResult<Self>> + Send + 'static>>
Source§impl From<UnexpectedStatusError> for Conn
impl From<UnexpectedStatusError> for Conn
Source§fn from(value: UnexpectedStatusError) -> Conn
fn from(value: UnexpectedStatusError) -> Conn
Source§impl<'conn> IntoFuture for &'conn mut Conn
impl<'conn> IntoFuture for &'conn mut Conn
Source§type IntoFuture = Pin<Box<dyn Future<Output = <&'conn mut Conn as IntoFuture>::Output> + Send + 'conn>>
type IntoFuture = Pin<Box<dyn Future<Output = <&'conn mut Conn as IntoFuture>::Output> + Send + 'conn>>
Source§fn into_future(self) -> <&'conn mut Conn as IntoFuture>::IntoFuture
fn into_future(self) -> <&'conn mut Conn as IntoFuture>::IntoFuture
Source§impl IntoFuture for Conn
impl IntoFuture for Conn
Source§type IntoFuture = Pin<Box<dyn Future<Output = <Conn as IntoFuture>::Output> + Send>>
type IntoFuture = Pin<Box<dyn Future<Output = <Conn as IntoFuture>::Output> + Send>>
Source§fn into_future(self) -> <Conn as IntoFuture>::IntoFuture
fn into_future(self) -> <Conn as IntoFuture>::IntoFuture
Auto Trait Implementations§
impl Freeze for Conn
impl !RefUnwindSafe for Conn
impl Send for Conn
impl Sync for Conn
impl Unpin for Conn
impl !UnwindSafe for Conn
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
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> FmtForward for T
impl<T> FmtForward for T
Source§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self
to use its Binary
implementation when Debug
-formatted.Source§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self
to use its Display
implementation when
Debug
-formatted.Source§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self
to use its LowerExp
implementation when
Debug
-formatted.Source§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self
to use its LowerHex
implementation when
Debug
-formatted.Source§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self
to use its Octal
implementation when Debug
-formatted.Source§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self
to use its Pointer
implementation when
Debug
-formatted.Source§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self
to use its UpperExp
implementation when
Debug
-formatted.Source§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self
to use its UpperHex
implementation when
Debug
-formatted.Source§impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
impl<Src, Dst> LosslessTryInto<Dst> for Srcwhere
Dst: LosslessTryFrom<Src>,
Source§fn lossless_try_into(self) -> Option<Dst>
fn lossless_try_into(self) -> Option<Dst>
Source§impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
impl<Src, Dst> LossyInto<Dst> for Srcwhere
Dst: LossyFrom<Src>,
Source§fn lossy_into(self) -> Dst
fn lossy_into(self) -> Dst
Source§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
Source§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
Source§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
self
and passes that borrow into the pipe function. Read moreSource§fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
Source§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
Source§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
self
, then passes self.as_ref()
into the pipe function.Source§fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
self
, then passes self.as_mut()
into the pipe
function.Source§fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self
, then passes self.deref()
into the pipe function.Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<T> Tap for T
impl<T> Tap for T
Source§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B>
of a value. Read moreSource§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B>
of a value. Read moreSource§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R>
view of a value. Read moreSource§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R>
view of a value. Read moreSource§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target
of a value. Read moreSource§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap()
only in debug builds, and is erased in release builds.Source§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.tap_borrow()
only in debug builds, and is erased in release
builds.Source§fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.tap_ref()
only in debug builds, and is erased in release
builds.Source§fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut()
only in debug builds, and is erased in release
builds.Source§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref()
only in debug builds, and is erased in release
builds.