Struct poem::test::TestClient
source · [−]pub struct TestClient<E> { /* private fields */ }
Available on crate feature
test
only.Expand description
A client for testing.
Implementations
sourceimpl<E: Endpoint> TestClient<E>
impl<E: Endpoint> TestClient<E>
sourcepub fn new<T>(ep: T) -> TestClient<T::Endpoint>where
T: IntoEndpoint<Endpoint = E>,
pub fn new<T>(ep: T) -> TestClient<T::Endpoint>where
T: IntoEndpoint<Endpoint = E>,
Create a new client for the specified endpoint.
sourcepub fn default_header<K, V>(self, key: K, value: V) -> Selfwhere
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
pub fn default_header<K, V>(self, key: K, value: V) -> Selfwhere
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Sets the default header for each requests.
Examples
use poem::{handler, http::HeaderMap, test::TestClient, Route};
#[handler]
fn index(headers: &HeaderMap) -> String {
headers
.get("X-Custom-Header")
.and_then(|value| value.to_str().ok())
.unwrap_or_default()
.to_string()
}
let app = Route::new().at("/", index);
let cli = TestClient::new(app).default_header("X-Custom-Header", "test");
let resp = cli.get("/").send().await;
resp.assert_status_is_ok();
resp.assert_text("test").await;
sourcepub fn default_content_type(self, content_type: impl AsRef<str>) -> Self
pub fn default_content_type(self, content_type: impl AsRef<str>) -> Self
Sets the default content type for each requests.
sourcepub fn request(
&self,
method: Method,
uri: impl Into<String>
) -> TestRequestBuilder<'_, E>
pub fn request(
&self,
method: Method,
uri: impl Into<String>
) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
.
sourcepub fn get(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn get(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with GET
method.
sourcepub fn post(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn post(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with POST
method.
sourcepub fn put(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn put(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with PUT
method.
sourcepub fn delete(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn delete(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with DELETE
method.
sourcepub fn head(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn head(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with HEAD
method.
sourcepub fn options(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn options(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with OPTIONS
method.
sourcepub fn connect(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn connect(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with CONNECT
method.
sourcepub fn patch(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn patch(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with PATCH
method.
sourcepub fn trace(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
pub fn trace(&self, uri: impl Into<String>) -> TestRequestBuilder<'_, E>
Create a TestRequestBuilder
with TRACE
method.
Auto Trait Implementations
impl<E> RefUnwindSafe for TestClient<E>where
E: RefUnwindSafe,
impl<E> Send for TestClient<E>where
E: Send,
impl<E> Sync for TestClient<E>where
E: Sync,
impl<E> Unpin for TestClient<E>where
E: Unpin,
impl<E> UnwindSafe for TestClient<E>where
E: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more