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
sourceimpl TestRequest
impl TestRequest
sourcepub fn with_uri(path: &str) -> TestRequest
pub fn with_uri(path: &str) -> TestRequest
Create TestRequest and set request uri
sourcepub fn get() -> TestRequest
pub fn get() -> TestRequest
Create TestRequest and set method to Method::GET
sourcepub fn post() -> TestRequest
pub fn post() -> TestRequest
Create TestRequest and set method to Method::POST
sourcepub fn put() -> TestRequest
pub fn put() -> TestRequest
Create TestRequest and set method to Method::PUT
sourcepub fn patch() -> TestRequest
pub fn patch() -> TestRequest
Create TestRequest and set method to Method::PATCH
sourcepub fn delete() -> TestRequest
pub fn delete() -> TestRequest
Create TestRequest and set method to Method::DELETE
sourcepub fn insert_header(self, header: impl TryIntoHeaderPair) -> Self
pub fn insert_header(self, header: impl TryIntoHeaderPair) -> Self
Insert 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
Append a header, keeping any that were set with an equivalent field name.
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
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());
sourcepub fn peer_addr(self, addr: SocketAddr) -> Self
pub fn peer_addr(self, addr: SocketAddr) -> Self
Set peer addr.
sourcepub fn set_payload<B: Into<Bytes>>(self, data: B) -> Self
pub fn set_payload<B: Into<Bytes>>(self, data: B) -> Self
Set request payload.
sourcepub fn set_form<T: Serialize>(self, data: &T) -> Self
pub fn set_form<T: Serialize>(self, data: &T) -> Self
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
.
sourcepub fn set_json<T: Serialize>(self, data: &T) -> Self
pub fn set_json<T: Serialize>(self, data: &T) -> Self
Serialize data
to JSON and set it as the request payload. The Content-Type
header is
set to application/json
.
sourcepub fn data<T: 'static>(self, data: T) -> Self
pub fn data<T: 'static>(self, data: T) -> Self
Set application data. This is equivalent of App::data()
method
for testing purpose.
sourcepub fn app_data<T: 'static>(self, data: T) -> Self
pub fn app_data<T: 'static>(self, data: T) -> Self
Set 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
Complete request creation and generate Request
instance
sourcepub fn to_srv_request(self) -> ServiceRequest
pub fn to_srv_request(self) -> ServiceRequest
Complete request creation and generate 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>
Complete request creation and generate ServiceResponse
instance
sourcepub fn to_http_request(self) -> HttpRequest
pub fn to_http_request(self) -> HttpRequest
Complete request creation and generate HttpRequest
instance
sourcepub fn to_http_parts(self) -> (HttpRequest, Payload)
pub fn to_http_parts(self) -> (HttpRequest, Payload)
Complete request creation and generate HttpRequest
and Payload
instances
sourcepub 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
sourceimpl Default for TestRequest
impl Default for TestRequest
sourcefn default() -> TestRequest
fn default() -> TestRequest
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
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> WithSubscriber for T
impl<T> WithSubscriber for T
sourcefn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> where
S: Into<Dispatch>,
Attaches the provided Subscriber
to this type, returning a
WithDispatch
wrapper. Read more
sourcefn with_current_subscriber(self) -> WithDispatch<Self>
fn with_current_subscriber(self) -> WithDispatch<Self>
Attaches the current default Subscriber
to this type, returning a
WithDispatch
wrapper. Read more