如何在actix_web单元测试中获取响应的主体?

zel*_*3ju 5 rust rust-actix actix-web

我正在使用 Rust 和 actix_web 构建 Web API 服务。

我想测试一条路线并检查收到的响应正文是否符合我的预期。但是我正在努力将接收到的正文ResponseBody<Body>转换为 JSON 或 BSON。被调用的路由实际上返回application/json.

let mut app = test::init_service(App::new()
        .data(AppState { database: db.clone() })
        .route("/recipes/{id}", web::post().to(add_one_recipe))
    ).await;

let payload = create_one_recipe().as_document().unwrap().clone();

let req = test::TestRequest::post()
    .set_json(&payload).uri("/recipes/new").to_request();

let mut resp = test::call_service(&mut app, req).await;
let body: ResponseBody<Body> = resp.take_body(); // Here I want the body as Binary, String, JSON, or BSON. The response is actually application/json.
Run Code Online (Sandbox Code Playgroud)

4_5*_*5_4 6

看看Body和 ResponseBody,这看起来像这样的方法:

use actix_web::{web, App, HttpResponse, HttpServer, Responder};
use serde::{Deserialize, Serialize};

#[derive(Serialize, Deserialize)]
struct Greet {
    name: String,
}

async fn greet() -> impl Responder {
    let body = serde_json::to_string(&Greet {
        name: "Test".to_owned(),
    })
    .unwrap();
    HttpResponse::Ok()
        .content_type("application/json")
        .body(body)
}

#[actix_rt::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| App::new().route("/", web::get().to(greet)))
        .bind("127.0.0.1:8000")?
        .run()
        .await
}

#[cfg(test)]
mod tests {
    use super::*;
    use actix_web::{body::Body, test, web, App};
    use serde_json::json;

    #[actix_rt::test]
    async fn test_greet_get() {
        let mut app = test::init_service(App::new().route("/", web::get().to(greet))).await;
        let req = test::TestRequest::with_header("content-type", "application/json").to_request();
        let mut resp = test::call_service(&mut app, req).await;
        let body = resp.take_body();
        let body = body.as_ref().unwrap();
        assert!(resp.status().is_success());
        assert_eq!(
            &Body::from(json!({"name":"Test"})), // or serde.....
            body
        );
    }
}
Run Code Online (Sandbox Code Playgroud)
running 1 test
test tests::test_greet_get ... ok

test result: ok. 1 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
Run Code Online (Sandbox Code Playgroud)


Sch*_*ice 5

Actix的/例子库通过定义一个新的特点和实现它的实现了这一ResponseBody<Body>类型的内容返回作为一个&str可能的情况:

trait BodyTest {
    fn as_str(&self) -> &str;
}

impl BodyTest for ResponseBody<Body> {
    fn as_str(&self) -> &str {
        match self {
            ResponseBody::Body(ref b) => match b {
                Body::Bytes(ref by) => std::str::from_utf8(&by).unwrap(),
                _ => panic!(),
            },
            ResponseBody::Other(ref b) => match b {
                Body::Bytes(ref by) => std::str::from_utf8(&by).unwrap(),
                _ => panic!(),
            },
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

之后你可以简单地做:

assert_eq!(resp.response().body().as_str(), "Your name is John");
Run Code Online (Sandbox Code Playgroud)

这些摘录的完整代码参考来自:https : //github.com/actix/examples/blob/master/forms/form/src/main.rs