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 the HTTP request-target without validation or normalization, as received by the server.

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.

Applies to to_srv_request, to_http_request, and to_http_parts. It is ignored by to_request, because the application router sets path parameters for routed requests.

Source

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

Set peer addr.

The address is returned by peer_addr() of every generated request, including one built with to_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.

It is ignored by to_request, because the application supplies the state for routed requests.

Source

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

Store a typed configuration value.

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

It is ignored by to_request, because the application configuration applies to routed requests; set the value with App::with_config() instead.

Source

pub fn to_request(self) -> Request

Complete request creation and generate Request instance.

The request is meant to be passed through an application service, for example with call_service. It keeps the method, URI, version, headers, cookies, payload, and peer address. Path parameters, state, and application values are supplied by the application instead.

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
§

impl<'a, T, E> AsTaggedExplicit<'a, E> for T
where T: 'a,

§

fn explicit(self, class: Class, tag: u32) -> TaggedParser<'a, Explicit, Self, E>

§

impl<'a, T, E> AsTaggedImplicit<'a, E> for T
where T: 'a,

§

fn implicit( self, class: Class, constructed: bool, tag: u32, ) -> TaggedParser<'a, Implicit, Self, E>

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
§

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

§

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

§

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

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 = !

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

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

§

fn vzip(self) -> V