Struct actix_web::test::TestRequest [−][src]
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
impl TestRequest
[src]
pub fn with_uri(path: &str) -> TestRequest
[src]
Create TestRequest and set request uri
pub fn get() -> TestRequest
[src]
Create TestRequest and set method to Method::GET
pub fn post() -> TestRequest
[src]
Create TestRequest and set method to Method::POST
pub fn put() -> TestRequest
[src]
Create TestRequest and set method to Method::PUT
pub fn patch() -> TestRequest
[src]
Create TestRequest and set method to Method::PATCH
pub fn delete() -> TestRequest
[src]
Create TestRequest and set method to Method::DELETE
pub fn version(mut self: Self, ver: Version) -> Self
[src]
Set HTTP version of this request
pub fn method(mut self: Self, meth: Method) -> Self
[src]
Set HTTP method of this request
pub fn uri(mut self: Self, path: &str) -> Self
[src]
Set HTTP Uri of this request
pub fn insert_header<H>(mut self: Self, header: H) -> Self where
H: IntoHeaderPair,
[src]
H: IntoHeaderPair,
Insert a header, replacing any that were set with an equivalent field name.
pub fn append_header<H>(mut self: Self, header: H) -> Self where
H: IntoHeaderPair,
[src]
H: IntoHeaderPair,
Append a header, keeping any that were set with an equivalent field name.
pub fn cookie(mut self: Self, cookie: Cookie<'_>) -> Self
[src]
Set cookie for this request.
pub fn param(mut self: Self, name: &'static str, value: &'static str) -> Self
[src]
Set request path pattern parameter
pub fn peer_addr(mut self: Self, addr: SocketAddr) -> Self
[src]
Set peer addr
pub fn set_payload<B: Into<Bytes>>(mut self: Self, data: B) -> Self
[src]
Set request payload
pub fn set_form<T: Serialize>(mut self: Self, data: &T) -> Self
[src]
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
.
pub fn set_json<T: Serialize>(mut self: Self, data: &T) -> Self
[src]
Serialize data
to JSON and set it as the request payload. The Content-Type
header is
set to application/json
.
pub fn data<T: 'static>(mut self: Self, data: T) -> Self
[src]
Set application data. This is equivalent of App::data()
method
for testing purpose.
pub fn app_data<T: 'static>(mut self: Self, data: T) -> Self
[src]
Set application data. This is equivalent of App::app_data()
method
for testing purpose.
pub fn to_request(mut self: Self) -> Request
[src]
Complete request creation and generate Request
instance
pub fn to_srv_request(mut self: Self) -> ServiceRequest
[src]
Complete request creation and generate ServiceRequest
instance
pub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B>
[src]
Complete request creation and generate ServiceResponse
instance
pub fn to_http_request(mut self: Self) -> HttpRequest
[src]
Complete request creation and generate HttpRequest
instance
pub fn to_http_parts(mut self: Self) -> (HttpRequest, Payload)
[src]
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,
[src]
S: Service<Request, Response = ServiceResponse<B>, Error = E>,
E: Debug,
Complete request creation, calls service and waits for response future completion.
Trait Implementations
impl Default for TestRequest
[src]
fn default() -> TestRequest
[src]
Auto Trait Implementations
impl !RefUnwindSafe for TestRequest
[src]
impl !Send for TestRequest
[src]
impl !Sync for TestRequest
[src]
impl Unpin for TestRequest
[src]
impl !UnwindSafe for TestRequest
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,