Struct axum_test::TestRequest

source ·
pub struct TestRequest { /* private fields */ }
Expand description

A TestRequest represents a HTTP request to the test server.

Creating

Requests are created by the TestServer. You do not create them yourself.

The TestServer has functions corresponding to specific requests. For example calling TestServer::get to create a new HTTP GET request, or `TestServer::post to create a HTTP POST request.

Customising

The TestRequest allows the caller to fill in the rest of the request to be sent to the server. Including the headers, the body, cookies, the content type, and other relevant details.

The TestRequest struct provides a number of methods to set up the request, such as json, text, bytes, expect_failure, content_type, etc. The do_save_cookies and do_not_save_cookies methods are used to control cookie handling.

Sending

Once fully configured you send the rquest by awaiting the request object.

let request = server.get(&"/user");
let response = request.await;

You will receive back a TestResponse.

Implementations§

source§

impl TestRequest

source

pub fn do_save_cookies(self) -> Self

Any cookies returned will be saved to the TestServer that created this, which will continue to use those cookies on future requests.

source

pub fn do_not_save_cookies(self) -> Self

Cookies returned by this will not be saved to the TestServer. For use by future requests.

This is the default behaviour. You can change that default in TestServerConfig.

source

pub fn clear_cookies(self) -> Self

Clears all cookies used internally within this Request.

Adds a Cookie to be sent with this request.

source

pub fn expect_failure(self) -> Self

Marks that this request should expect to fail. Failiure is deemend as any response that isn’t a 200.

By default, requests are expct to always succeed.

source

pub fn expect_success(self) -> Self

Marks that this request should expect to succeed. Success is deemend as returning a 200.

Note this is the default behaviour when creating a new TestRequest.

source

pub fn json<J>(self, body: &J) -> Selfwhere J: ?Sized + Serialize,

Set the body of the request to send up as Json.

source

pub fn text<T>(self, raw_text: T) -> Selfwhere T: Display,

Set raw text as the body of the request.

If there isn’t a content type set, this will default to text/plain.

source

pub fn bytes(self, body_bytes: Bytes) -> Self

Set raw bytes as the body of the request.

The content type is left unchanged.

source

pub fn content_type(self, content_type: &str) -> Self

Set the content type to use for this request in the header.

Trait Implementations§

source§

impl Debug for TestRequest

source§

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

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

impl IntoFuture for TestRequest

§

type Output = TestResponse

The output that the future will produce on completion.
§

type IntoFuture = AutoFuture<TestResponse>

Which kind of future are we turning this into?
source§

fn into_future(self) -> Self::IntoFuture

Creates a future from a value. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · 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 Twhere U: From<T>,

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

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

§

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

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

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