1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
//! Various helpers for Actix applications to use during testing.
//!
//! # Creating A Test Service
//! - [`init_service`]
//!
//! # Off-The-Shelf Test Services
//! - [`ok_service`]
//! - [`status_service`]
//!
//! # Calling Test Service
//! - [`TestRequest`]
//! - [`call_service`]
//! - [`try_call_service`]
//! - [`call_and_read_body`]
//! - [`call_and_read_body_json`]
//! - [`try_call_and_read_body_json`]
//!
//! # Reading Response Payloads
//! - [`read_body`]
//! - [`try_read_body`]
//! - [`read_body_json`]
//! - [`try_read_body_json`]
// TODO: more docs on generally how testing works with these parts
pub use actix_http::test::TestBuffer;
mod test_request;
mod test_services;
mod test_utils;
#[allow(deprecated)]
pub use self::test_services::{default_service, ok_service, simple_service, status_service};
#[cfg(test)]
pub(crate) use self::test_utils::try_init_service;
#[allow(deprecated)]
pub use self::test_utils::{read_response, read_response_json};
pub use self::{
test_request::TestRequest,
test_utils::{
call_and_read_body, call_and_read_body_json, call_service, init_service, read_body,
read_body_json, try_call_and_read_body_json, try_call_service, try_read_body,
try_read_body_json,
},
};
/// Reduces boilerplate code when testing expected response payloads.
///
/// Must be used inside an async test. Works for both `ServiceRequest` and `HttpRequest`.
///
/// # Examples
/// ```
/// use actix_web::{http::StatusCode, HttpResponse};
///
/// let res = HttpResponse::with_body(StatusCode::OK, "http response");
/// assert_body_eq!(res, b"http response");
/// ```
#[cfg(test)]
macro_rules! assert_body_eq {
($res:ident, $expected:expr) => {
assert_eq!(
::actix_http::body::to_bytes($res.into_body())
.await
.expect("error reading test response body"),
::bytes::Bytes::from_static($expected),
)
};
}
#[cfg(test)]
pub(crate) use assert_body_eq;
#[cfg(test)]
mod tests {
use super::*;
use crate::{http::StatusCode, service::ServiceResponse, HttpResponse};
#[actix_rt::test]
async fn assert_body_works_for_service_and_regular_response() {
let res = HttpResponse::with_body(StatusCode::OK, "http response");
assert_body_eq!(res, b"http response");
let req = TestRequest::default().to_http_request();
let res = HttpResponse::with_body(StatusCode::OK, "service response");
let res = ServiceResponse::new(req, res);
assert_body_eq!(res, b"service response");
}
}