Skip to main content

TestRequest

Struct TestRequest 

Source
pub struct TestRequest(/* private fields */);
Expand description

Test Request builder

use ntex::http::test::TestRequest;
use ntex::http::{header, Request, Response, StatusCode, HttpMessage};

fn index(req: Request) -> Response {
    if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
        Response::Ok().into()
    } else {
        Response::BadRequest().into()
    }
}

let resp = index(
    TestRequest::with_header("content-type", "text/plain").build()
);
assert_eq!(resp.status(), StatusCode::OK);

let resp = index(
    TestRequest::default().build()
);
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);

Implementations§

Source§

impl TestRequest

Source

pub fn builder() -> TestRequest

Creates a test request builder with a GET / request.

Source

pub fn with_uri(path: &str) -> TestRequest

Create TestRequest and set request uri.

Source

pub fn with_header<K, V>(key: K, value: V) -> TestRequest

Create TestRequest and append header.

Source

pub fn version(&mut self, ver: Version) -> &mut Self

Set HTTP version of this request.

Source

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

Set HTTP method of this request.

Source

pub fn uri(&mut self, path: &str) -> &mut Self

Set HTTP Uri of this request.

Source

pub fn header<K, V>(&mut self, key: K, value: V) -> &mut Self

Append a header.

Existing values for the same header name are kept.

Source

pub fn cookie<C>(&mut self, cookie: C) -> &mut Self
where C: Into<Cookie<'static>>,

Set cookie for this request.

Source

pub fn set_payload<B: Into<Bytes>>(&mut self, data: B) -> &mut Self

Set request payload.

Source

pub fn take(&mut self) -> TestRequest

Take test request.

Source

pub fn build(&mut self) -> Request

Complete request creation and generate Request instance.

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 Default for TestRequest

Source§

fn default() -> TestRequest

Returns the “default value” for a type. Read more

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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