Skip to main content

UnexpectedStatusError

Struct 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.

Returned by Conn::success.

Methods from Deref<Target = Conn>§

Source

pub fn transport(&self) -> Option<&dyn Transport>

Borrows the transport for this conn

This should only be used to call your own custom methods on the transport that do not read or write any data. Calling any method that reads from or writes to the transport will disrupt the HTTP protocol.

Source

pub fn transport_mut(&mut self) -> Option<&mut dyn Transport>

Mutably borrow the transport for this conn

This should only be used to call your own custom methods on the transport that do not read or write any data. Calling any method that reads from or writes to the transport will disrupt the HTTP protocol.

Source

pub fn url(&self) -> &Url

Borrows the url for this conn.

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::from(client_config());

let conn = client.get("http://localhost:9080");

let url = conn.url(); //<-

assert_eq!(url.host_str().unwrap(), "localhost");
Source

pub fn url_mut(&mut self) -> &mut Url

Mutably borrow the url for this conn.

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::from(client_config());

let conn = client.get("http://localhost:9080");

let url = conn.url(); //<-

assert_eq!(url.host_str().unwrap(), "localhost");
Source

pub fn set_url(&mut self, url: Url) -> &mut Self

Sets the url for this conn., returning &mut Self for chaining

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::from(client_config());

let conn = client.get("http://localhost:9080");

let url = conn.url(); //<-

assert_eq!(url.host_str().unwrap(), "localhost");
Source

pub fn method(&self) -> Method

Returns a copy of the method for this conn.

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::from(client_config());
let conn = client.get("http://localhost:9080");

let method = conn.method(); //<-

assert_eq!(method, Method::Get);
Source

pub fn set_method(&mut self, method: Method) -> &mut Self

Sets the method for this conn., returning &mut Self for chaining

use trillium_client::{Client, Method};
use trillium_testing::client_config;

let client = Client::from(client_config());
let conn = client.get("http://localhost:9080");

let method = conn.method(); //<-

assert_eq!(method, Method::Get);
Source

pub fn request_headers(&self) -> &Headers

Borrows the request headers

Source

pub fn request_headers_mut(&mut self) -> &mut Headers

Mutably borrow the request headers

Source

pub fn response_headers(&self) -> &Headers

Borrows the response headers

Source

pub fn status(&self) -> Option<Status>

Returns a copy of the status code for this conn.

If the conn has not yet been sent, this will be None.

use trillium_client::{Client, Status};
use trillium_testing::{client_config, with_server};

async fn handler(conn: trillium::Conn) -> trillium::Conn {
conn.with_status(418)
}

with_server(handler, |url| async move {
let client = Client::new(client_config());
let conn = client.get(url).await?;
assert_eq!(Status::ImATeapot, conn.status().unwrap());
Ok(())
});
Source

pub fn request_body(&self) -> Option<&Body>

Borrows the request body

env_logger::init();
use trillium_client::Client;
use trillium_testing::{client_config, with_server};

let handler = |mut conn: trillium::Conn| async move {
let body = conn.request_body_string().await.unwrap();
conn.ok(format!("request body was: {}", body))
};

with_server(handler, |url| async move {
let client = Client::from(client_config());
let mut conn = client
.post(url)
.with_body("body") //<-
.await?;

assert_eq!(
conn.response_body().read_string().await?,
"request body was: body"
);
Ok(())
});
Source

pub fn set_request_body(&mut self, body: impl Into<Body>) -> &mut Self

Sets the request body, returning &mut Self for chaining

env_logger::init();
use trillium_client::Client;
use trillium_testing::{client_config, with_server};

let handler = |mut conn: trillium::Conn| async move {
let body = conn.request_body_string().await.unwrap();
conn.ok(format!("request body was: {}", body))
};

with_server(handler, |url| async move {
let client = Client::from(client_config());
let mut conn = client
.post(url)
.with_body("body") //<-
.await?;

assert_eq!(
conn.response_body().read_string().await?,
"request body was: body"
);
Ok(())
});
Source

pub fn take_request_body(&mut self) -> Option<Body>

Takes the request body, leaving a None in its place

env_logger::init();
use trillium_client::Client;
use trillium_testing::{client_config, with_server};

let handler = |mut conn: trillium::Conn| async move {
let body = conn.request_body_string().await.unwrap();
conn.ok(format!("request body was: {}", body))
};

with_server(handler, |url| async move {
let client = Client::from(client_config());
let mut conn = client
.post(url)
.with_body("body") //<-
.await?;

assert_eq!(
conn.response_body().read_string().await?,
"request body was: body"
);
Ok(())
});
Source

pub fn timeout(&self) -> Option<Duration>

Returns a copy of the timeout for this conn

this can also be set on the client with Client::set_timeout and Client::with_timeout

Source

pub fn timeout_mut(&mut self) -> Option<&mut Duration>

Mutably borrow the timeout for this conn

this can also be set on the client with Client::set_timeout and Client::with_timeout

Source

pub fn set_timeout(&mut self, timeout: Duration) -> &mut Self

Sets the timeout for this conn, returning &mut Self for chaining

this can also be set on the client with Client::set_timeout and Client::with_timeout

Source

pub fn take_timeout(&mut self) -> Option<Duration>

Takes the timeout for this conn, leaving a None in its place

this can also be set on the client with Client::set_timeout and Client::with_timeout

Source

pub fn set_http_version(&mut self, http_version: Version) -> &mut Self

Sets the http version hint for this conn, returning &mut Self for chaining

Pre-execution this is the prior-knowledge hint, not the version that will necessarily be on the wire. None means “no hint, use auto-discovery” (Alt-Svc h3, ALPN/pooled h2); Some(version) names the protocol to try first. Post-execution this is Some(version) reflecting the version the request was actually sent over.

The public http_version accessor resolves None to Version::Http1_1. See the crate-level Protocol selection documentation.

Source

pub fn strict_http_version(&self) -> bool

Returns a copy of Whether a request that cannot be carried by the protocol it was matched to fails rather than being retried on an earlier protocol.

Off by default: a websocket handshake that lands on an h2 or h3 connection whose peer doesn’t support extended CONNECT is retried as an HTTP/1.1 upgrade on a new connection. When on, that peer yields an error instead. A version hint sets the protocol to try first; this flag decides what happens when that protocol can’t carry the request.

This can also be set for every conn on the client with Client::set_strict_http_version.

Source

pub fn set_strict_http_version( &mut self, strict_http_version: bool, ) -> &mut Self

Sets Whether a request that cannot be carried by the protocol it was matched to fails, returning &mut Self for chaining rather than being retried on an earlier protocol.

Off by default: a websocket handshake that lands on an h2 or h3 connection whose peer doesn’t support extended CONNECT is retried as an HTTP/1.1 upgrade on a new connection. When on, that peer yields an error instead. A version hint sets the protocol to try first; this flag decides what happens when that protocol can’t carry the request.

This can also be set for every conn on the client with Client::set_strict_http_version.

Source

pub fn authority(&self) -> Option<&str>

Borrows the :authority pseudo-header, populated during h2 or h3 header finalization

Source

pub fn scheme(&self) -> Option<&str>

Borrows the :scheme pseudo-header, populated during h2 or h3 header finalization

Source

pub fn path(&self) -> Option<&str>

Borrows the :path pseudo-header, populated during h2 or h3 header finalization

Source

pub fn request_target(&self) -> Option<&str>

Borrows an explicit request target override, used only for OPTIONS * and CONNECT host:port

When set and the method is OPTIONS or CONNECT, this value is used as the HTTP request target instead of deriving it from the url. For all other methods, this field is ignored.

Source

pub fn set_request_target( &mut self, request_target: impl Into<Cow<'static, str>>, ) -> &mut Self

Sets an explicit request target override, used only for OPTIONS * and CONNECT host:port, returning &mut Self for chaining

When set and the method is OPTIONS or CONNECT, this value is used as the HTTP request target instead of deriving it from the url. For all other methods, this field is ignored.

Source

pub fn protocol(&self) -> Option<&str>

Borrows The protocol this request switches the connection to, if any: sent as the Upgrade header over HTTP/1.1 and as the :protocol pseudo-header of an extended CONNECT over HTTP/2 and HTTP/3. Either way the stream is left open as a bidirectional byte channel after the response head.

Source

pub fn request_trailers(&self) -> Option<&Headers>

Borrows trailers sent with the request body, populated after the body has been fully sent.

Only present when the request body was constructed with Body::new_with_trailers and the body has been fully sent.

Source

pub fn response_trailers(&self) -> Option<&Headers>

Borrows trailers received with the response body, populated after the response body has been fully read.

Source

pub fn client(&self) -> &Client

Borrows the Client that built this conn.

Source

pub fn http_version(&self) -> Version

the http version for this conn

Pre-execution this resolves the version hint — the default (no hint) reports Version::Http1_1, which means “use auto-discovery,” not “force HTTP/1.1.” An explicit version set via with_http_version is the protocol to try first. Post-execution this reflects the version the request was actually sent over.

See the crate-level Protocol selection documentation.

Source

pub fn response_body(&mut self) -> ResponseBody<'_>

returns a ResponseBody that borrows the connection inside this conn.

use trillium_client::Client;
use trillium_testing::{client_config, with_server};

let handler = |mut conn: trillium::Conn| async move { conn.ok("hello from trillium") };

with_server(handler, |url| async move {
    let client = Client::from(client_config());
    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(())
});
Source

pub async fn response_json<T>(&mut self) -> Result<T, ClientSerdeError>

Available on crate feature sonic-rs only.

Attempt to deserialize the response body. Note that this consumes the body content.

Source

pub fn take_response_body(&mut self) -> Option<ResponseBody<'static>>

Detach the response body as an owned, 'static value.

Returns None if there is no body to take — neither an override has been installed nor a transport-backed body is available. Subsequent calls return None. Callers who want to wrap-and-replace the body (e.g. tee through a cache) compose this with ConnExt::set_response_body; the conn’s body slot is empty between the two calls.

For a transport-backed body, this moves the transport into the returned ResponseBody<'static>. Drop on that value drains-and-pools (keepalive) or closes (otherwise) the transport via a spawned task; ResponseBody::recycle is the await-able variant. For an override body, the inner Body is moved out and any leftover transport on the conn is recycled immediately.

Source

pub fn peer_addr(&self) -> Option<SocketAddr>

attempts to retrieve the connected peer address

Source

pub fn insert_state<T: Send + Sync + 'static>(&mut self, state: T) -> Option<T>

add state to the client conn, returning any previously set state of this type

Source

pub fn state<T: Send + Sync + 'static>(&self) -> Option<&T>

borrow state

Source

pub fn state_mut<T: Send + Sync + 'static>(&mut self) -> Option<&mut T>

borrow state mutably

Source

pub fn take_state<T: Send + Sync + 'static>(&mut self) -> Option<T>

take state

Trait Implementations§

Source§

impl Debug for UnexpectedStatusError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Deref for UnexpectedStatusError

Source§

type Target = Conn

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl DerefMut for UnexpectedStatusError

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl Display for UnexpectedStatusError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for UnexpectedStatusError

1.30.0 · Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Conn> for UnexpectedStatusError

Source§

fn from(value: Conn) -> Self

Converts to this type from the input type.
Source§

impl From<UnexpectedStatusError> for Conn

Source§

fn from(value: UnexpectedStatusError) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more