[][src]Function testax::post_json

pub async fn post_json<'a, SERVICE, BODY, SERDE, E>(
    app: &'a mut SERVICE,
    json: SERDE,
    url: &'a str
) -> Result<RespBody> where
    BODY: MessageBody + Unpin,
    SERVICE: Service<Request = Request, Response = ServiceResponse<BODY>, Error = E>,
    SERDE: Serialize,
    E: Debug

Post test method for Actix server

use actix_web::{dev::ServiceResponse, post, web, Responder, test};
use actix_service::Service;
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct User {
    name: String, 
}
 
#[post("/api/users")]
async fn post_user(
    user: web::Json<User>,
) -> impl Responder {
    format!("Name is: {}!", user.name)
}
 
#[actix_rt::test]
async fn test_minimal() {
    let mut app = test::init_service(App::new().service(post_users)).await;
 
    let user = User { name: "Filip".to_string() };
    let resp = post_json(&mut app, user, "/api/users").await;
    assert_eq!(resp.status.as_u16(), 200);
    assert_eq!(resp.body, "Name is: Filip!");
}