# actix_web::test::TestRequest - Rust
API documentation for the Rust `TestRequest` struct in crate `actix_web`.
Keywords: rust, rustlang, rust-lang, TestRequest

# [−] [src] Struct actix_web::test::TestRequest

    pub struct TestRequest<S> { /* fields omitted */ }

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);
    }

## Methods

###

`impl TestRequest<()>`

[src]

####

`pub fn with_uri(path: &str) -> TestRequest<()>`

[src]

Create TestRequest and set request uri

####

`pub fn with_hdr<H: Header>(hdr: H) -> TestRequest<()>`

[src]

Create TestRequest and set header

####

`pub fn with_header<K, V>(key: K, value: V) -> TestRequest<()> where HeaderName: HttpTryFrom<K>, V: IntoHeaderValue,`

[src]

Create TestRequest and set header

###

`impl<S: 'static> TestRequest<S>`

[src]

####

`pub fn with_state(state: S) -> TestRequest<S>`

[src]

Start HttpRequest build process with application state

####

`pub fn version(self, ver: Version) -> Self`

[src]

Set HTTP version of this request

####

`pub fn method(self, meth: Method) -> Self`

[src]

Set HTTP method of this request

####

`pub fn uri(self, path: &str) -> Self`

[src]

Set HTTP Uri of this request

####

`pub fn set<H: Header>(self, hdr: H) -> Self`

[src]

Set a header

####

`pub fn header<K, V>(self, key: K, value: V) -> Self where HeaderName: HttpTryFrom<K>, V: IntoHeaderValue,`

[src]

Set a header

####

`pub fn param(self, name: &'static str, value: &'static str) -> Self`

[src]

Set request path pattern parameter

####

`pub fn set_payload<B: Into<Binary>>(self, data: B) -> Self`

[src]

Set request payload

####

`pub fn prefix(self, prefix: u16) -> Self`

[src]

Set request's prefix

####

`pub fn finish(self) -> HttpRequest<S>`

[src]

Complete request creation and generate `HttpRequest` instance

####

`pub fn request(self) -> Request`

[src]

Complete request creation and generate server `Request` instance

####

`pub fn run<H: Handler<S>>(self, h: &H) -> Result<HttpResponse, Error>`

[src]

This method generates `HttpRequest` instance and runs handler with generated request.

####

`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,`

[src]

This method generates `HttpRequest` instance and runs handler with generated request.

This method panics is handler returns actor.

####

`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>>,`

[src]

This method generates `HttpRequest` instance and executes handler

####

`pub fn execute<F, R>(self, f: F) -> Result<HttpResponse, Error> where F: FnOnce(&HttpRequest<S>) -> R, R: Responder + 'static,`

[src]

This method generates `HttpRequest` instance and executes handler

## Trait Implementations

###

`impl Default for TestRequest<()>`

[src]

####

`fn default() -> TestRequest<()>`

[src]

Returns the "default value" for a type. Read more

## Auto Trait Implementations

###

`impl<S> !Send for TestRequest<S>`

###

`impl<S> !Sync for TestRequest<S>`

## Blanket Implementations

###

`impl<T> From for T`

[src]

####

`fn from(t: T) -> T`

[src]

Performs the conversion.

###

`impl<T, U> Into for T where U: From<T>,`

[src]

####

`fn into(self) -> U`

[src]

Performs the conversion.

###

`impl<T, U> TryFrom for T where T: From<U>,`

[src]

#### `type Error = !`

🔬 This is a nightly-only experimental API. (`try_from`)

The type returned in the event of a conversion error.

####

`fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>`

[src]

🔬 This is a nightly-only experimental API. (`try_from`)

Performs the conversion.

###

`impl<T> Borrow for T where T: ?Sized,`

[src]

####

`fn borrow(&self) -> &T`

[src]

Immutably borrows from an owned value. Read more

###

`impl<T> BorrowMut for T where T: ?Sized,`

[src]

####

`fn borrow_mut(&mut self) -> &mut T`

[src]

Mutably borrows from an owned value. Read more

###

`impl<T, U> TryInto for T where U: TryFrom<T>,`

[src]

#### `type Error = <U as TryFrom<T>>::Error`

🔬 This is a nightly-only experimental API. (`try_from`)

The type returned in the event of a conversion error.

####

`fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>`

[src]

🔬 This is a nightly-only experimental API. (`try_from`)

Performs the conversion.

###

`impl<T> Any for T where T: 'static + ?Sized,`

[src]

####

`fn get_type_id(&self) -> TypeId`

[src]

🔬 This is a nightly-only experimental API. (`get_type_id`)

this method will likely be replaced by an associated static

Gets the `TypeId` of `self`. Read more

###

`impl<T> Erased for T`

# Help

## Keyboard Shortcuts

`?`
  Show this help dialog
`S`
  Focus the search field
`↑`
  Move up in search results
`↓`
  Move down in search results
`↹`
  Switch tab
`⏎`
  Go to active search result
`+`
  Expand all sections
`-`
  Collapse all sections

## Search Tricks

Prefix searches with a type followed by a colon (e.g. `fn:`) to restrict the search to a given type.

Accepted types are: `fn`, `mod`, `struct`, `enum`, `trait`, `type`, `macro`, and `const`.

Search functions by type signature (e.g. `vec -> usize` or `* -> vec`)

Search multiple things at once by splitting your query with comma (e.g. `str,u8` or `String,struct:Vec,test`)