我正在使用 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)