Skip to main content

TestRequest

Struct TestRequest 

Source
pub struct TestRequest<St = ()> { /* private fields */ }
Expand description

Test Request builder.

For unit testing, ntex provides a request builder type and a simple handler runner. TestRequest implements a builder-like pattern. You can generate various types of request via TestRequest’s methods:

  • TestRequest::to_request creates ntex::http::Request instance.
  • TestRequest::to_srv_request creates WebRequest instance, which is used for testing middlewares and chain adapters.
  • TestRequest::to_srv_response creates WebResponse instance.
  • TestRequest::to_http_request creates HttpRequest instance, which is used for testing handlers.
use ntex::http::{header, StatusCode, HttpMessage};
use ntex::web::{self, test, HttpRequest, HttpResponse};

async fn index(req: HttpRequest) -> HttpResponse {
    if req.headers().contains_key(header::CONTENT_TYPE) {
        HttpResponse::Ok().into()
    } else {
        HttpResponse::BadRequest().into()
    }
}

#[ntex::test]
async fn test_index() {
    let req = test::TestRequest::with_header("content-type", "text/plain")
        .to_http_request();

    let resp = index(req).await;
    assert_eq!(resp.status(), StatusCode::OK);

    let req = test::TestRequest::default().to_http_request();
    let resp = index(req).await;
    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

Implementations§

Source§

impl TestRequest

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 set header.

Source

pub fn get() -> TestRequest

Create TestRequest and set method to Method::GET.

Source

pub fn post() -> TestRequest

Create TestRequest and set method to Method::POST.

Source

pub fn put() -> TestRequest

Create TestRequest and set method to Method::PUT.

Source

pub fn patch() -> TestRequest

Create TestRequest and set method to Method::PATCH.

Source

pub fn delete() -> TestRequest

Create TestRequest and set method to Method::DELETE.

Source§

impl<St> TestRequest<St>

Source

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

Set HTTP version of this request.

Source

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

Set HTTP method of this request.

Source

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

Set HTTP Uri of this request.

Source

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

Set a header.

Source

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

Set cookie for this request.

Source

pub fn param(self, name: &'static str, value: &'static str) -> Self

Set request path pattern parameter.

Source

pub fn peer_addr(self, addr: SocketAddr) -> Self

Set peer addr.

The address is returned by peer_addr() of the generated request. Requests with a peer address must be created inside the ntex runtime.

Source

pub fn payload<B: Into<Bytes>>(self, data: B) -> Self

Set request payload.

Source

pub fn form<T: Serialize>(self, data: &T) -> Self

Serialize data to a URL encoded form and set it as the request payload.

The Content-Type header is set to application/x-www-form-urlencoded.

Source

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

Serialize data to JSON and set it as the request payload.

The Content-Type header is set to application/json.

Source

pub fn state<NewSt: 'static>(self, state: NewSt) -> TestRequest<NewSt>

Set request state.

Source

pub fn app_state<T: Send + Sync + 'static>(self, data: T) -> Self

Set application state.

This is equivalent of WebAppConfig::set_state() method for testing purpose. The value is available via HttpRequest::app_state().

Source

pub fn to_request(self) -> Request

Complete request creation and generate Request instance.

Source

pub fn to_srv_request(self) -> WebRequest<St>

Complete request creation and generate WebRequest instance.

Source

pub fn to_srv_response(self, res: HttpResponse) -> WebResponse

Complete request creation and generate WebResponse instance.

Source

pub fn to_http_request(self) -> HttpRequest

Complete request creation and generate HttpRequest instance.

Source

pub fn to_http_parts(self) -> (HttpRequest, Payload, St)

Complete request creation and generate HttpRequest and Payload instances.

Trait Implementations§

Source§

impl<St: Debug> Debug for TestRequest<St>

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§

§

impl<St = ()> !Freeze for TestRequest<St>

§

impl<St = ()> !RefUnwindSafe for TestRequest<St>

§

impl<St = ()> !Send for TestRequest<St>

§

impl<St = ()> !Sync for TestRequest<St>

§

impl<St = ()> !UnwindSafe for TestRequest<St>

§

impl<St> Unpin for TestRequest<St>
where St: Unpin,

§

impl<St> UnsafeUnpin for TestRequest<St>
where St: UnsafeUnpin,

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.