Struct actix_test::TestRequest[][src]

pub struct TestRequest { /* fields omitted */ }
Expand description

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()
    }
}

#[actix_web::test]
async fn test_index() {
    let req = test::TestRequest::default().insert_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

Create TestRequest and set request uri

Create TestRequest and set method to Method::GET

Create TestRequest and set method to Method::POST

Create TestRequest and set method to Method::PUT

Create TestRequest and set method to Method::PATCH

Create TestRequest and set method to Method::DELETE

Set HTTP version of this request

Set HTTP method of this request

Set HTTP Uri of this request

Insert a header, replacing any that were set with an equivalent field name.

Append a header, keeping any that were set with an equivalent field name.

Set cookie for this request.

Set request path pattern parameter.

Examples
use actix_web::test::TestRequest;

let req = TestRequest::default().param("foo", "bar");
let req = TestRequest::default().param("foo".to_owned(), "bar".to_owned());

Set peer addr.

Set request payload.

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.

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

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

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

Complete request creation and generate Request instance

Complete request creation and generate ServiceRequest instance

Complete request creation and generate ServiceResponse instance

Complete request creation and generate HttpRequest instance

Complete request creation and generate HttpRequest and Payload instances

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

Trait Implementations

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

Performs the conversion.

Should always be Self

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more