Struct actix_web::test::TestRequest
source · [−]pub struct TestRequest { /* private fields */ }
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
createsactix_http::Request
instance.TestRequest::to_srv_request
createsServiceRequest
instance, which is used for testing middlewares and chain adapters.TestRequest::to_srv_response
createsServiceResponse
instance.TestRequest::to_http_request
createsHttpRequest
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
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
pub async fn send_request<S, B, E>(self, app: &S) -> S::Response where
S: Service<Request, Response = ServiceResponse<B>, Error = E>,
E: Debug,
pub async fn send_request<S, B, E>(self, app: &S) -> S::Response where
S: Service<Request, Response = ServiceResponse<B>, Error = E>,
E: Debug,
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
impl !RefUnwindSafe for TestRequest
impl !Send for TestRequest
impl !Sync for TestRequest
impl Unpin for TestRequest
impl !UnwindSafe for TestRequest
Blanket Implementations
Mutably borrows from an owned value. Read more
pub fn vzip(self) -> V
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