diff --git a/async_ex2/Cargo.toml b/async_ex2/Cargo.toml index d8195f6c..ace032b8 100644 --- a/async_ex2/Cargo.toml +++ b/async_ex2/Cargo.toml @@ -18,3 +18,4 @@ serde_json = "1.0.39" time = "0.1.42" validator = "0.8.0" validator_derive = "0.8.0" +actix-service = "0.3.6" diff --git a/async_ex2/src/handlers/products.rs b/async_ex2/src/handlers/products.rs index 5d0cd2a9..174b8286 100644 --- a/async_ex2/src/handlers/products.rs +++ b/async_ex2/src/handlers/products.rs @@ -23,4 +23,35 @@ pub fn get_product_detail(id: web::Path) pub fn remove_product(id: web::Path) -> impl Future { fut_ok(HttpResponse::Ok().finish()) -} \ No newline at end of file +} + + +#[cfg(test)] +mod tests { + use super::*; + use actix_service::Service; + use actix_web::{test, HttpResponse, HttpRequest, App, + http::{header, StatusCode}, web}; + use crate::appconfig::config_app; + + + #[test] + fn test_add_product() { + let mut app = test::init_service( + App::new() + .configure(config_app) + ); + + let payload = r#"{"id":12345,"product_type":"fancy","name":"test"}"#.as_bytes(); + + let req = test::TestRequest::post() + .uri("/products") + .header(header::CONTENT_TYPE, "application/json") + .set_payload(payload) + .to_request(); + + let resp = test::block_on(app.call(req)).unwrap(); + + assert_eq!(resp.status(), StatusCode::OK); + } +}