[][src]Struct actix_web::test::TestRequest

pub struct TestRequest { /* fields omitted */ }

Test Request builder.

For unit testing, actix 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 actix_http::Request instance.
  • TestRequest::to_service creates ServiceRequest instance, which is used for testing middlewares and chain adapters.
  • TestRequest::to_from creates ServiceFromRequest instance, which is used for testing extractors.
  • TestRequest::to_http_request creates HttpRequest instance, which is used for testing handlers.
This example is not tested
use actix_web::{test, HttpRequest, HttpResponse, HttpMessage};
use actix_web::http::{header, StatusCode};

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

fn main() {
    let req = test::TestRequest::with_header("content-type", "text/plain")
        .to_http_request();

    let resp = test::block_on(index(req).into_future()).unwrap();
    assert_eq!(resp.status(), StatusCode::OK);

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

Methods

impl TestRequest[src]

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

Create TestRequest and set request uri

pub fn with_hdr<H: Header>(hdr: H) -> TestRequest[src]

Create TestRequest and set header

pub fn with_header<K, V>(key: K, value: V) -> TestRequest where
    HeaderName: HttpTryFrom<K>,
    V: IntoHeaderValue, 
[src]

Create TestRequest and set header

pub fn get() -> TestRequest[src]

Create TestRequest and set method to Method::GET

pub fn post() -> TestRequest[src]

Create TestRequest and set method to Method::POST

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

Set HTTP version of this request

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

Set HTTP method of this request

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

Set HTTP Uri of this request

pub fn set<H: Header>(self, hdr: H) -> Self[src]

Set a header

pub fn header<K, V>(self, key: K, value: V) -> Self where
    HeaderName: HttpTryFrom<K>,
    V: IntoHeaderValue, 
[src]

Set a header

pub fn cookie(self, cookie: Cookie) -> Self[src]

Set cookie for this request

pub fn set_payload<B: Into<Bytes>>(self, data: B) -> Self[src]

Set request payload

pub fn app_data<T: 'static>(self, data: T) -> Self[src]

Set application data. This is equivalent of App::data() method for testing purpose.

pub fn route_data<T: 'static>(self, data: T) -> Self[src]

Set route data. This is equivalent of Route::data() method for testing purpose.

pub fn to_srv_request(self) -> ServiceRequest<PayloadStream>[src]

Complete request creation and generate ServiceRequest instance

pub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B>[src]

Complete request creation and generate ServiceResponse instance

pub fn to_request(self) -> Request<PayloadStream>[src]

Complete request creation and generate Request instance

pub fn to_http_request(self) -> HttpRequest[src]

Complete request creation and generate HttpRequest instance

pub fn to_from(self) -> ServiceFromRequest<PayloadStream>[src]

Complete request creation and generate ServiceFromRequest instance

pub fn block_on<F>(f: F) -> Result<F::Item, F::Error> where
    F: Future
[src]

Runs the provided future, blocking the current thread until the future completes.

This function can be used to synchronously block the current thread until the provided future has resolved either successfully or with an error. The result of the future is then returned from this function call.

Note that this function is intended to be used only for testing purpose. This function panics on nested call.

Trait Implementations

impl Default for TestRequest[src]

Auto Trait Implementations

impl !Send for TestRequest

impl !Sync for TestRequest

Blanket Implementations

impl<T, U> Into for T where
    U: From<T>, 
[src]

impl<T> From for T[src]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.

impl<T> Erased for T