Struct ajars_actix_web::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_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()
}
}
#[test]
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 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.
pub fn to_request(
self
) -> Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
pub fn to_request(
self
) -> Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>
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
pub fn to_http_parts(
self
) -> (HttpRequest, Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>)
pub fn to_http_parts(
self
) -> (HttpRequest, Payload<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>)
Complete request creation and generate HttpRequest
and Payload
instances
pub async fn send_request<S, B, E>(
self,
app: &'_ S
) -> <S as Service<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>>>::Response where
E: Debug,
S: Service<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>, Response = ServiceResponse<B>, Error = E>,
pub async fn send_request<S, B, E>(
self,
app: &'_ S
) -> <S as Service<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>>>::Response where
E: Debug,
S: Service<Request<Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>> + 'static, Global>>>, Response = ServiceResponse<B>, Error = E>,
Complete request creation, calls service and waits for response future completion.
Trait Implementations
Returns the “default value” for a type. Read more