Struct ntex::web::test::TestRequest [−][src]
pub struct TestRequest { /* fields omitted */ }
Expand description
Test Request
builder.
For unit testing, ntex 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
createsntex::http::Request
instance.TestRequest::to_srv_request
createsWebRequest
instance, which is used for testing middlewares and chain adapters.TestRequest::to_srv_response
createsWebResponse
instance.TestRequest::to_http_request
createsHttpRequest
instance, which is used for testing handlers.
use ntex::http::{header, StatusCode, HttpMessage}; use ntex::web::{self, test, HttpRequest, HttpResponse}; 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
impl TestRequest
[src]
impl TestRequest
[src]pub fn with_uri(path: &str) -> TestRequest
[src]
pub fn with_uri(path: &str) -> TestRequest
[src]Create TestRequest and set request uri
pub fn with_header<K, V>(key: K, value: V) -> TestRequest where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
[src]
pub fn with_header<K, V>(key: K, value: V) -> TestRequest where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
[src]Create TestRequest and set header
pub fn get() -> TestRequest
[src]
pub fn get() -> TestRequest
[src]Create TestRequest and set method to Method::GET
pub fn post() -> TestRequest
[src]
pub fn post() -> TestRequest
[src]Create TestRequest and set method to Method::POST
pub fn put() -> TestRequest
[src]
pub fn put() -> TestRequest
[src]Create TestRequest and set method to Method::PUT
pub fn patch() -> TestRequest
[src]
pub fn patch() -> TestRequest
[src]Create TestRequest and set method to Method::PATCH
pub fn delete() -> TestRequest
[src]
pub fn delete() -> TestRequest
[src]Create TestRequest and set method to Method::DELETE
pub fn header<K, V>(self, key: K, value: V) -> Self where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
[src]
pub fn header<K, V>(self, key: K, value: V) -> Self where
HeaderName: TryFrom<K>,
HeaderValue: TryFrom<V>,
<HeaderName as TryFrom<K>>::Error: Into<HttpError>,
[src]Set a header
pub fn param(self, name: &'static str, value: &'static str) -> Self
[src]
pub fn param(self, name: &'static str, value: &'static str) -> Self
[src]Set request path pattern parameter
pub fn peer_addr(self, addr: SocketAddr) -> Self
[src]
pub fn peer_addr(self, addr: SocketAddr) -> Self
[src]Set peer addr
pub fn set_payload<B: Into<Bytes>>(self, data: B) -> Self
[src]
pub fn set_payload<B: Into<Bytes>>(self, data: B) -> Self
[src]Set request payload
pub fn set_form<T: Serialize>(self, data: &T) -> Self
[src]
pub fn set_form<T: Serialize>(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>(self, data: &T) -> Self
[src]
pub fn set_json<T: Serialize>(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>(self, data: T) -> Self
[src]
pub fn data<T: 'static>(self, data: T) -> Self
[src]Set application data. This is equivalent of App::data()
method
for testing purpose.
pub fn to_request(self) -> Request
[src]
pub fn to_request(self) -> Request
[src]Complete request creation and generate Request
instance
pub fn to_srv_request(self) -> WebRequest<DefaultError>
[src]
pub fn to_srv_request(self) -> WebRequest<DefaultError>
[src]Complete request creation and generate WebRequest
instance
pub fn to_srv_response(self, res: HttpResponse) -> WebResponse
[src]
pub fn to_srv_response(self, res: HttpResponse) -> WebResponse
[src]Complete request creation and generate WebResponse
instance
pub fn to_http_request(self) -> HttpRequest
[src]
pub fn to_http_request(self) -> HttpRequest
[src]Complete request creation and generate HttpRequest
instance
pub fn to_http_parts(self) -> (HttpRequest, Payload)
[src]
pub fn to_http_parts(self) -> (HttpRequest, Payload)
[src]Complete request creation and generate HttpRequest
and Payload
instances
Trait Implementations
impl Default for TestRequest
[src]
impl Default for TestRequest
[src]fn default() -> TestRequest
[src]
fn default() -> TestRequest
[src]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
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> Instrument for T
[src]
impl<T> Instrument for T
[src]fn instrument(self, span: Span) -> Instrumented<Self>
[src]
fn instrument(self, span: Span) -> Instrumented<Self>
[src]Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
fn in_current_span(self) -> Instrumented<Self>
[src]
fn in_current_span(self) -> Instrumented<Self>
[src]impl<T> Same<T> for T
impl<T> Same<T> for T
type Output = T
type Output = T
Should always be Self