[][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_srv_request creates ServiceRequest instance, which is used for testing middlewares and chain adapters.
  • TestRequest::to_srv_response creates ServiceResponse instance.
  • TestRequest::to_http_request creates HttpRequest instance, which is used for testing handlers.
use actix_web::{test, HttpRequest, HttpResponse, HttpMessage};
use actix_web::http::{header, StatusCode};

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

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

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

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

Implementations

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: TryFrom<K>,
    <HeaderName as TryFrom<K>>::Error: Into<HttpError>,
    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 put() -> TestRequest[src]

Create TestRequest and set method to Method::PUT

pub fn patch() -> TestRequest[src]

Create TestRequest and set method to Method::PATCH

pub fn delete() -> TestRequest[src]

Create TestRequest and set method to Method::DELETE

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: TryFrom<K>,
    <HeaderName as TryFrom<K>>::Error: Into<HttpError>,
    V: IntoHeaderValue
[src]

Set a header

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

Set cookie for this request

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

Set request path pattern parameter

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

Set peer addr

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

Set request payload

pub fn set_form<T: Serialize>(self, data: &T) -> Self[src]

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.

pub fn set_json<T: Serialize>(self, data: &T) -> Self[src]

Serialize data to JSON and set it as the request payload. The Content-Type header is set to application/json.

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

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

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

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

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

Complete request creation and generate Request instance

pub fn to_srv_request(self) -> ServiceRequest[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_http_request(self) -> HttpRequest[src]

Complete request creation and generate HttpRequest instance

pub fn to_http_parts(self) -> (HttpRequest, Payload)[src]

Complete request creation and generate HttpRequest and Payload instances

pub async fn send_request<S, B, E, '_>(self, app: &'_ mut S) -> S::Response where
    S: Service<Request = Request, Response = ServiceResponse<B>, Error = E>,
    E: Debug
[src]

Complete request creation, calls service and waits for response future completion.

Trait Implementations

impl Default for TestRequest[src]

Auto Trait Implementations

Blanket Implementations

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

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

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

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

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

impl<T> Same<T> for T

type Output = T

Should always be Self

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

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> 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<V, T> VZip<V> for T where
    V: MultiLane<T>,