Skip to main content

RequestBuilder

Struct RequestBuilder 

Source
pub struct RequestBuilder<'a, H> { /* private fields */ }
Expand description

Builder for constructing test requests with a fluent API.

Use the methods on TestClient to create a request builder, then chain configuration methods and call send to execute.

§Example

let response = client
    .post("/api/items")
    .header("Content-Type", "application/json")
    .body(r#"{"name": "Widget"}"#)
    .send();

Implementations§

Source§

impl<'a, H: Handler + 'static> RequestBuilder<'a, H>

Source

pub fn query(self, key: &str, value: &str) -> Self

Sets a query string parameter.

Multiple calls append parameters.

§Example
client.get("/search").query("q", "rust").query("limit", "10").send()
Source

pub fn header(self, name: impl Into<String>, value: impl Into<Vec<u8>>) -> Self

Sets a request header.

§Example
client.get("/api").header("Authorization", "Bearer token").send()
Source

pub fn header_str(self, name: impl Into<String>, value: &str) -> Self

Sets a request header with a string value.

Source

pub fn body(self, body: impl Into<Vec<u8>>) -> Self

Sets the request body as raw bytes.

§Example
client.post("/upload").body(b"binary data".to_vec()).send()
Source

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

Sets the request body as a string.

Source

pub fn json<T: Serialize>(self, value: &T) -> Self

Sets the request body as JSON.

Automatically sets the Content-Type header to application/json.

§Example
#[derive(Serialize)]
struct CreateUser { name: String }

client.post("/users").json(&CreateUser { name: "Alice".into() }).send()
Source

pub fn cookie(self, name: &str, value: &str) -> Self

Sets a cookie for this request only.

This does not affect the client’s cookie jar.

Source

pub fn send(self) -> TestResponse

Sends the request and returns the response.

§Example
let response = client.get("/").send();

Auto Trait Implementations§

§

impl<'a, H> Freeze for RequestBuilder<'a, H>

§

impl<'a, H> !RefUnwindSafe for RequestBuilder<'a, H>

§

impl<'a, H> Send for RequestBuilder<'a, H>
where H: Sync + Send,

§

impl<'a, H> Sync for RequestBuilder<'a, H>
where H: Sync + Send,

§

impl<'a, H> Unpin for RequestBuilder<'a, H>

§

impl<'a, H> !UnwindSafe for RequestBuilder<'a, H>

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<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: NoopSpan) -> Self

Instruments this future with a span (no-op when disabled).
Source§

fn in_current_span(self) -> Self

Instruments this future with the current span (no-op when disabled).
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> Same for T

Source§

type Output = T

Should always be Self
Source§

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

Source§

type Error = Infallible

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

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

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
Source§

impl<T> ResponseProduces<T> for T