diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..4c9aee3 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,6 @@ +{ + "rust-analyzer.linkedProjects": [ + "./Cargo.toml" + ], + "rust-analyzer.showUnlinkedFileNotification": false +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index bfc0814..e8fdc27 100644 --- a/src/main.rs +++ b/src/main.rs @@ -3,6 +3,9 @@ mod db; mod misc; mod response; +#[cfg(test)] +mod tests; + use actix_web::middleware::Logger; use actix_web::{web, App, HttpServer}; use config::AppConfig; diff --git a/src/response.rs b/src/response.rs index 8ad4afa..ff6c44a 100644 --- a/src/response.rs +++ b/src/response.rs @@ -13,8 +13,8 @@ pub type AppResult = actix_web::Result>, AppError>; /// # Error JSON response model #[derive(Serialize)] pub struct ErrModel { - success: bool, - err: &'static str, + pub success: bool, + pub err: &'static str, } /// # Success JSON response model @@ -23,8 +23,8 @@ pub struct OkModel where T: Serialize, { - success: bool, - data: T, + pub success: bool, + pub data: T, } /// # App Errors diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..dc8562e --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1 @@ +mod response; diff --git a/src/tests/response.rs b/src/tests/response.rs new file mode 100644 index 0000000..19d7e4b --- /dev/null +++ b/src/tests/response.rs @@ -0,0 +1,35 @@ +use actix_web::{http::StatusCode, web, ResponseError}; + +use crate::response::{AppError, AppResponse, AppResult}; + +#[test] +fn success() { + let response: AppResult<&str> = AppResponse::Success("success").response(); + let web::Json(model) = response.unwrap(); + assert_eq!(model.success, true); + assert_eq!(model.data, "success"); +} + +#[test] +fn bad_request() { + let response: AppResult<()> = AppResponse::Invalid("bad").response(); + if let Err(e) = response { + if let AppError::Invalid { err } = e { + assert_eq!(err, "bad"); + } + let resp = e.error_response(); + assert_eq!(resp.status(), StatusCode::BAD_REQUEST); + } else { + panic!("response is not Err") + } +} + +#[test] +fn internal_server_error() { + let response: AppResult<()> = AppResponse::InternalError("500").response(); + if let Err(e) = response { + if let AppError::InternalError = e {} + let resp = e.error_response(); + assert_eq!(resp.status(), StatusCode::INTERNAL_SERVER_ERROR); + } +}