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 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>,
) -> Self
pub fn with_request_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.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>,
) -> Self
👎Deprecated: use Conn::with_request_header
pub fn with_header( self, name: impl Into<HeaderName<'static>>, value: impl Into<HeaderValues>, ) -> Self
see [`with_request_header]
sourcepub fn with_request_headers<HN, HV, I>(self, headers: I) -> Selfwhere
I: IntoIterator<Item = (HN, HV)> + Send,
HN: Into<HeaderName<'static>>,
HV: Into<HeaderValues>,
pub fn with_request_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.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) -> Selfwhere
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) -> Selfwhere
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>>,
) -> Self
pub fn without_request_header( self, name: impl Into<HeaderName<'static>>, ) -> Self
Chainable method to remove a request header if present
sourcepub fn without_header(self, name: impl Into<HeaderName<'static>>) -> Self
👎Deprecated: use Conn::without_request_header
pub fn without_header(self, name: impl Into<HeaderName<'static>>) -> Self
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>) -> 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(())
});
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