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 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
/// Quickly write tests that check various parts of a `ServiceResponse`.
///
/// An async test must be used (e.g., `#[actix_web::test]`) if used to assert on response body.
///
/// # Examples
/// ```
/// use actix_web::{
/// dev::ServiceResponse, http::header::ContentType, test::TestRequest, HttpResponse,
/// };
/// use actix_web_lab::assert_response_matches;
///
/// # actix_web::rt::System::new().block_on(async {
/// let res = ServiceResponse::new(
/// TestRequest::default().to_http_request(),
/// HttpResponse::Created()
/// .insert_header(("date", "today"))
/// .insert_header(("set-cookie", "a=b"))
/// .body("Hello World!"),
/// );
///
/// assert_response_matches!(res, CREATED);
/// assert_response_matches!(res, CREATED; "date" => "today");
/// assert_response_matches!(res, CREATED; @raw "Hello World!");
///
/// let res = ServiceResponse::new(
/// TestRequest::default().to_http_request(),
/// HttpResponse::Created()
/// .insert_header(("date", "today"))
/// .insert_header(("set-cookie", "a=b"))
/// .body("Hello World!"),
/// );
///
/// assert_response_matches!(res, CREATED;
/// "date" => "today"
/// "set-cookie" => "a=b";
/// @raw "Hello World!"
/// );
///
/// let res = ServiceResponse::new(
/// TestRequest::default().to_http_request(),
/// HttpResponse::Created()
/// .content_type(ContentType::json())
/// .insert_header(("date", "today"))
/// .insert_header(("set-cookie", "a=b"))
/// .body(r#"{"abc":"123"}"#),
/// );
///
/// assert_response_matches!(res, CREATED; @json { "abc": "123" });
/// # });
/// ```
#[macro_export]
macro_rules! assert_response_matches {
($res:ident, $status:ident) => {{
assert_eq!($res.status(), ::actix_web::http::StatusCode::$status)
}};
($res:ident, $status:ident; $($hdr_name:expr => $hdr_val:expr)+) => {{
assert_response_matches!($res, $status);
$(
assert_eq!(
$res.headers().get(::actix_web::http::header::HeaderName::from_static($hdr_name)).unwrap(),
&::actix_web::http::header::HeaderValue::from_static($hdr_val),
);
)+
}};
($res:ident, $status:ident; @raw $payload:expr) => {{
assert_response_matches!($res, $status);
assert_eq!(::actix_web::test::read_body($res).await, $payload);
}};
($res:ident, $status:ident; $($hdr_name:expr => $hdr_val:expr)+; @raw $payload:expr) => {{
assert_response_matches!($res, $status; $($hdr_name => $hdr_val)+);
assert_eq!(::actix_web::test::read_body($res).await, $payload);
}};
($res:ident, $status:ident; @json $payload:tt) => {{
assert_response_matches!($res, $status);
assert_eq!(
::actix_web::test::read_body_json::<$crate::__reexports::serde_json::Value, _>($res).await,
$crate::__reexports::serde_json::json!($payload),
);
}};
}
pub use assert_response_matches;
#[cfg(test)]
mod tests {
use actix_web::{
dev::ServiceResponse, http::header::ContentType, test::TestRequest, HttpResponse,
};
use super::*;
#[actix_web::test]
async fn response_matching() {
let res = ServiceResponse::new(
TestRequest::default().to_http_request(),
HttpResponse::Created()
.insert_header(("date", "today"))
.insert_header(("set-cookie", "a=b"))
.body("Hello World!"),
);
assert_response_matches!(res, CREATED);
assert_response_matches!(res, CREATED; "date" => "today");
assert_response_matches!(res, CREATED; @raw "Hello World!");
let res = ServiceResponse::new(
TestRequest::default().to_http_request(),
HttpResponse::Created()
.insert_header(("date", "today"))
.insert_header(("set-cookie", "a=b"))
.body("Hello World!"),
);
assert_response_matches!(res, CREATED;
"date" => "today"
"set-cookie" => "a=b";
@raw "Hello World!"
);
let res = ServiceResponse::new(
TestRequest::default().to_http_request(),
HttpResponse::Created()
.content_type(ContentType::json())
.insert_header(("date", "today"))
.insert_header(("set-cookie", "a=b"))
.body(r#"{"abc":"123"}"#),
);
assert_response_matches!(res, CREATED; @json { "abc": "123" });
}
}