Struct actix_web::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_requestcreatesactix_http::Requestinstance.TestRequest::to_srv_requestcreatesServiceRequestinstance, which is used for testing middlewares and chain adapters.TestRequest::to_srv_responsecreatesServiceResponseinstance.TestRequest::to_http_requestcreatesHttpRequestinstance, 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
Create TestRequest and set request uri
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,
pub fn with_header<K, V>(key: K, value: V) -> TestRequest where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
Create TestRequest and set header
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
pub fn header<K, V>(self, key: K, value: V) -> Self where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
pub fn header<K, V>(self, key: K, value: V) -> Self where
HeaderName: TryFrom<K>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
V: IntoHeaderValue,
Set a header
Set cookie for this request
Set request path pattern parameter
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: &mut S) -> S::Response where
S: Service<Request = Request, Response = ServiceResponse<B>, Error = E>,
E: Debug,
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,
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
Instruments this type with the provided Span, returning an
Instrumented wrapper. 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
