Struct actix_web::test::TestRequest
source · pub struct TestRequest<S> { /* private fields */ }
Expand description
Test HttpRequest
builder
use actix_web::test::TestRequest;
fn index(req: &HttpRequest) -> HttpResponse {
if let Some(hdr) = req.headers().get(header::CONTENT_TYPE) {
HttpResponse::Ok().into()
} else {
HttpResponse::BadRequest().into()
}
}
fn main() {
let resp = TestRequest::with_header("content-type", "text/plain")
.run(&index)
.unwrap();
assert_eq!(resp.status(), StatusCode::OK);
let resp = TestRequest::default().run(&index).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 with_hdr<H: Header>(hdr: H) -> TestRequest<()>
pub fn with_hdr<H: Header>(hdr: H) -> TestRequest<()>
Create TestRequest and set header
sourcepub fn with_header<K, V>(key: K, value: V) -> TestRequest<()>where
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
pub fn with_header<K, V>(key: K, value: V) -> TestRequest<()>where
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
Create TestRequest and set header
sourceimpl<S: 'static> TestRequest<S>
impl<S: 'static> TestRequest<S>
sourcepub fn with_state(state: S) -> TestRequest<S>
pub fn with_state(state: S) -> TestRequest<S>
Start HttpRequest build process with application state
sourcepub fn header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
pub fn header<K, V>(self, key: K, value: V) -> Selfwhere
HeaderName: HttpTryFrom<K>,
V: IntoHeaderValue,
Set a header
sourcepub fn param(self, name: &'static str, value: &'static str) -> Self
pub fn param(self, name: &'static str, value: &'static str) -> Self
Set request path pattern parameter
sourcepub fn set_payload<B: Into<Binary>>(self, data: B) -> Self
pub fn set_payload<B: Into<Binary>>(self, data: B) -> Self
Set request payload
sourcepub fn finish(self) -> HttpRequest<S>
pub fn finish(self) -> HttpRequest<S>
Complete request creation and generate HttpRequest
instance
sourcepub fn run<H: Handler<S>>(self, h: &H) -> Result<HttpResponse, Error>
pub fn run<H: Handler<S>>(self, h: &H) -> Result<HttpResponse, Error>
This method generates HttpRequest
instance and runs handler
with generated request.
sourcepub fn run_async<H, R, F, E>(self, h: H) -> Result<HttpResponse, E>where
H: Fn(HttpRequest<S>) -> F + 'static,
F: Future<Item = R, Error = E> + 'static,
R: Responder<Error = E> + 'static,
E: Into<Error> + 'static,
pub fn run_async<H, R, F, E>(self, h: H) -> Result<HttpResponse, E>where
H: Fn(HttpRequest<S>) -> F + 'static,
F: Future<Item = R, Error = E> + 'static,
R: Responder<Error = E> + 'static,
E: Into<Error> + 'static,
This method generates HttpRequest
instance and runs handler
with generated request.
This method panics is handler returns actor.
sourcepub fn run_async_result<F, R, I, E>(self, f: F) -> Result<I, E>where
F: FnOnce(&HttpRequest<S>) -> R,
R: Into<AsyncResult<I, E>>,
pub fn run_async_result<F, R, I, E>(self, f: F) -> Result<I, E>where
F: FnOnce(&HttpRequest<S>) -> R,
R: Into<AsyncResult<I, E>>,
This method generates HttpRequest
instance and executes handler
sourcepub fn execute<F, R>(self, f: F) -> Result<HttpResponse, Error>where
F: FnOnce(&HttpRequest<S>) -> R,
R: Responder + 'static,
pub fn execute<F, R>(self, f: F) -> Result<HttpResponse, Error>where
F: FnOnce(&HttpRequest<S>) -> R,
R: Responder + 'static,
This method generates HttpRequest
instance and executes handler
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<S> !RefUnwindSafe for TestRequest<S>
impl<S> !Send for TestRequest<S>
impl<S> !Sync for TestRequest<S>
impl<S> Unpin for TestRequest<S>where
S: Unpin,
impl<S> !UnwindSafe for TestRequest<S>
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
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