Struct trillium_client::Conn
source · 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 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 request_headers(&mut self) -> &mut Headers
pub fn request_headers(&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.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() //<-
.insert("some-request-header", "header-value");
(&mut conn).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>
) -> Self
pub fn with_header( self, name: impl Into<HeaderName<'static>>, value: impl Into<HeaderValues> ) -> Self
chainable setter for inserting a request header
use trillium_testing::ClientConfig;
let handler = |conn: trillium::Conn| async move {
let header = conn.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_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_headers<HN, HV, I>(self, headers: I) -> Selfwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'static>>,
HV: Into<HeaderValues>,
pub fn with_headers<HN, HV, I>(self, headers: I) -> Selfwhere 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.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_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 response_headers(&self) -> &Headers
pub fn response_headers(&self) -> &Headers
let handler = |conn: trillium::Conn| async move {
conn.with_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 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>) -> Self
pub fn with_body(self, body: impl Into<Body>) -> Self
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 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 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<Self, UnexpectedStatusError>
pub fn success(self) -> Result<Self, 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(())
});