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:

use actix_web::{test, HttpRequest, HttpResponse, HttpMessage};
use actix_web::http::{header, StatusCode};

async fn handler(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(header::ContentType::plaintext())
        .to_http_request();

    let resp = handler(req).await;
    assert_eq!(resp.status(), StatusCode::OK);

    let req = test::TestRequest::default().to_http_request();
    let resp = handler(req).await;
    assert_eq!(resp.status(), StatusCode::BAD_REQUEST);
}

Implementations§

source§

impl TestRequest

source

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

Create TestRequest and set request uri

source

pub fn get() -> TestRequest

Create TestRequest and set method to Method::GET

source

pub fn post() -> TestRequest

Create TestRequest and set method to Method::POST

source

pub fn put() -> TestRequest

Create TestRequest and set method to Method::PUT

source

pub fn patch() -> TestRequest

Create TestRequest and set method to Method::PATCH

source

pub fn delete() -> TestRequest

Create TestRequest and set method to Method::DELETE

source

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

Set HTTP version of this request

source

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

Set HTTP method of this request

source

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

Set HTTP URI of this request

source

pub fn insert_header(self, header: impl TryIntoHeaderPair) -> Self

Insert a header, replacing any that were set with an equivalent field name.

source

pub fn append_header(self, header: impl TryIntoHeaderPair) -> Self

Append a header, keeping any that were set with an equivalent field name.

source

pub fn cookie(self, cookie: Cookie<'_>) -> Self

Set cookie for this request.

source

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());
source

pub fn peer_addr(self, addr: SocketAddr) -> Self

Set peer addr.

source

pub fn set_payload(self, data: impl Into<Bytes>) -> Self

Set request payload.

source

pub fn set_form(self, data: impl Serialize) -> 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.

source

pub fn set_json(self, data: impl Serialize) -> Self

Serialize data to JSON and set it as the request payload.

The Content-Type header is set to application/json.

source

pub fn data<T: 'static>(self, data: T) -> Self

Set application data. This is equivalent of App::data() method for testing purpose.

source

pub fn app_data<T: 'static>(self, data: T) -> Self

Set application data. This is equivalent of App::app_data() method for testing purpose.

source

pub fn to_request(self) -> Request

Complete request creation and generate Request instance

source

pub fn to_srv_request(self) -> ServiceRequest

Complete request creation and generate ServiceRequest instance

source

pub fn to_srv_response<B>(self, res: HttpResponse<B>) -> ServiceResponse<B>

Complete request creation and generate ServiceResponse instance

source

pub fn to_http_request(self) -> HttpRequest

Complete request creation and generate HttpRequest instance

source

pub fn to_http_parts(self) -> (HttpRequest, Payload)

Complete request creation and generate HttpRequest and Payload instances

source

pub async fn send_request<S, B, E>(self, app: &S) -> S::Responsewhere S: Service<Request, Response = ServiceResponse<B>, Error = E>, E: Debug,

Complete request creation, calls service and waits for response future completion.

Trait Implementations§

source§

impl Default for TestRequest

source§

fn default() -> TestRequest

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

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

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

source§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

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
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more