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
creates anactix_http::Request
.TestRequest::to_srv_request
creates aServiceRequest
, which is used for testing middlewares and chain adapters.TestRequest::to_srv_response
creates aServiceResponse
.TestRequest::to_http_request
creates anHttpRequest
, which is used for testing handlers.
use actix_web::{test, HttpRequest, HttpResponse, HttpMessage};
use actix_web::http::{header, StatusCode};
async fn handler(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(header::ContentType::plaintext())
.to_http_request();
let resp = handler(req).await;
assert_eq!(resp.status(), StatusCode::OK);
let req = test::TestRequest::default().to_http_request();
let resp = handler(req).await;
assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}
Implementations§
Source§impl TestRequest
impl TestRequest
Sourcepub fn with_uri(uri: &str) -> TestRequest
pub fn with_uri(uri: &str) -> TestRequest
Constructs test request and sets request URI.
Sourcepub fn get() -> TestRequest
pub fn get() -> TestRequest
Constructs test request with GET method.
Sourcepub fn post() -> TestRequest
pub fn post() -> TestRequest
Constructs test request with POST method.
Sourcepub fn put() -> TestRequest
pub fn put() -> TestRequest
Constructs test request with PUT method.
Sourcepub fn patch() -> TestRequest
pub fn patch() -> TestRequest
Constructs test request with PATCH method.
Sourcepub fn delete() -> TestRequest
pub fn delete() -> TestRequest
Constructs test request with DELETE method.
Sourcepub fn insert_header(self, header: impl TryIntoHeaderPair) -> Self
pub fn insert_header(self, header: impl TryIntoHeaderPair) -> Self
Inserts a header, replacing any that were set with an equivalent field name.
Sourcepub fn append_header(self, header: impl TryIntoHeaderPair) -> Self
pub fn append_header(self, header: impl TryIntoHeaderPair) -> Self
Appends a header, keeping any that were set with an equivalent field name.
Sets cookie for this request.
Sourcepub fn param(
self,
name: impl Into<Cow<'static, str>>,
value: impl Into<Cow<'static, str>>,
) -> Self
pub fn param( self, name: impl Into<Cow<'static, str>>, value: impl Into<Cow<'static, str>>, ) -> Self
Sets 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());
Sourcepub fn peer_addr(self, addr: SocketAddr) -> Self
pub fn peer_addr(self, addr: SocketAddr) -> Self
Sets peer address.
Sourcepub fn set_payload(self, data: impl Into<Bytes>) -> Self
pub fn set_payload(self, data: impl Into<Bytes>) -> Self
Sets request payload.
Sourcepub fn set_form(self, data: impl Serialize) -> Self
pub fn set_form(self, data: impl Serialize) -> Self
Serializes 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
.
Sourcepub fn set_json(self, data: impl Serialize) -> Self
pub fn set_json(self, data: impl Serialize) -> Self
Serializes data
to JSON and set it as the request payload.
The Content-Type
header is set to application/json
.
Sourcepub fn app_data<T: 'static>(self, data: T) -> Self
pub fn app_data<T: 'static>(self, data: T) -> Self
Inserts application data.
This is equivalent of App::app_data()
method for testing purpose.
Sourcepub fn to_request(self) -> Request
pub fn to_request(self) -> Request
Finalizes request creation and returns Request
instance.
Sourcepub fn to_srv_request(self) -> ServiceRequest
pub fn to_srv_request(self) -> ServiceRequest
Finalizes request creation and returns ServiceRequest
instance.
Sourcepub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B>
pub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B>
Finalizes request creation and returns ServiceResponse
instance.
Sourcepub fn to_http_request(self) -> HttpRequest
pub fn to_http_request(self) -> HttpRequest
Finalizes request creation and returns HttpRequest
instance.
Sourcepub fn to_http_parts(self) -> (HttpRequest, Payload)
pub fn to_http_parts(self) -> (HttpRequest, Payload)
Finalizes request creation and returns HttpRequest
and Payload
pair.
Sourcepub async fn send_request<S, B, E>(self, app: &S) -> S::Response
pub async fn send_request<S, B, E>(self, app: &S) -> S::Response
Finalizes request creation, calls service, and waits for response future completion.
Trait Implementations§
Source§impl Default for TestRequest
impl Default for TestRequest
Source§fn default() -> TestRequest
fn default() -> TestRequest
Auto Trait Implementations§
impl !Freeze for TestRequest
impl !RefUnwindSafe for TestRequest
impl !Send for TestRequest
impl !Sync for TestRequest
impl Unpin for TestRequest
impl !UnwindSafe for TestRequest
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more