Struct trillium_client::UnexpectedStatusError
source · pub struct UnexpectedStatusError(/* private fields */);
Expand description
An unexpected http status code was received. Transform this back
into the conn with From::from
/Into::into
.
Currently only returned by Conn::success
Methods from Deref<Target = Conn>§
sourcepub fn request_headers(&self) -> &Headers
pub fn request_headers(&self) -> &Headers
borrow the request headers
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 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 peer_addr(&self) -> Option<SocketAddr>
pub fn peer_addr(&self) -> Option<SocketAddr>
attempts to retrieve the connected peer address
Trait Implementations§
source§impl Debug for UnexpectedStatusError
impl Debug for UnexpectedStatusError
source§impl Deref for UnexpectedStatusError
impl Deref for UnexpectedStatusError
source§impl DerefMut for UnexpectedStatusError
impl DerefMut for UnexpectedStatusError
source§impl Display for UnexpectedStatusError
impl Display for UnexpectedStatusError
source§impl Error for UnexpectedStatusError
impl Error for UnexpectedStatusError
1.30.0 · source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
The lower-level source of this error, if any. Read more
1.0.0 · source§fn description(&self) -> &str
fn description(&self) -> &str
👎Deprecated since 1.42.0: use the Display impl or to_string()
source§impl From<Conn> for UnexpectedStatusError
impl From<Conn> for UnexpectedStatusError
source§impl From<UnexpectedStatusError> for Conn
impl From<UnexpectedStatusError> for Conn
source§fn from(value: UnexpectedStatusError) -> Self
fn from(value: UnexpectedStatusError) -> Self
Converts to this type from the input type.
Auto Trait Implementations§
impl Freeze for UnexpectedStatusError
impl !RefUnwindSafe for UnexpectedStatusError
impl Send for UnexpectedStatusError
impl Sync for UnexpectedStatusError
impl Unpin for UnexpectedStatusError
impl !UnwindSafe for UnexpectedStatusError
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
Mutably borrows from an owned value. Read more