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(uri: &str) -> TestRequest

Constructs test request and sets request URI.

source

pub fn get() -> TestRequest

Constructs test request with GET method.

source

pub fn post() -> TestRequest

Constructs test request with POST method.

source

pub fn put() -> TestRequest

Constructs test request with PUT method.

source

pub fn patch() -> TestRequest

Constructs test request with PATCH method.

source

pub fn delete() -> TestRequest

Constructs test request with DELETE method.

source

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

Sets HTTP version of this request.

source

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

Sets method of this request.

source

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

Sets URI of this request.

source

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

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

source

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

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

source

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

Available on crate feature cookies only.

Sets cookie for this request.

source

pub fn param( self, name: impl Into<Cow<'static, str>>, value: impl Into<Cow<'static, str>>, ) -> Self

Sets 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

Sets peer address.

source

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

Sets request payload.

source

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

Serializes 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

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

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

source

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

Inserts application data.

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

source

pub fn to_request(self) -> Request

Finalizes request creation and returns Request instance.

source

pub fn to_srv_request(self) -> ServiceRequest

Finalizes request creation and returns ServiceRequest instance.

source

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

Finalizes request creation and returns ServiceResponse instance.

source

pub fn to_http_request(self) -> HttpRequest

Finalizes request creation and returns HttpRequest instance.

source

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

Finalizes request creation and returns HttpRequest and Payload pair.

source

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

Finalizes 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 T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where 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 T
where 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 T
where 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