-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2eeb3b3
commit 4127690
Showing
5 changed files
with
71 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#![allow(clippy::unused_async)] | ||
|
||
use axum::debug_handler; | ||
use loco_rs::prelude::*; | ||
use serde::{Deserialize, Serialize}; | ||
use tracing::debug; | ||
use wise_units::Measurement; | ||
|
||
#[derive(Clone, Debug, Serialize, Deserialize)] | ||
pub struct Params { | ||
lhs: Measurement, | ||
rhs: Measurement, | ||
} | ||
|
||
#[debug_handler] | ||
pub async fn div(Json(params): Json<Params>) -> impl IntoResponse { | ||
debug!("Dividing {} * {}", ¶ms.lhs, ¶ms.rhs); | ||
|
||
format::json(params.lhs / params.rhs) | ||
} | ||
|
||
pub fn routes() -> Routes { | ||
Routes::new().prefix("div").add("/", post(div)) | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ pub mod home; | |
pub mod convert; | ||
pub mod add; | ||
pub mod sub; | ||
pub mod mul; | ||
pub mod mul; | ||
pub mod div; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
|
||
use loco_rs::testing; | ||
use serial_test::serial; | ||
use wise_units_web::app::App; | ||
|
||
#[tokio::test] | ||
#[serial] | ||
async fn can_post_div_root() { | ||
testing::request::<App, _, _>(|request, _ctx| async move { | ||
let payload = serde_json::json!({ | ||
"lhs": { | ||
"value": 10, | ||
"unit": "km3" | ||
}, | ||
"rhs": { | ||
"value": 5, | ||
"unit": "km2" | ||
} | ||
}); | ||
|
||
let expected = serde_json::json!({ | ||
"value": 2.0, | ||
"unit": "km" | ||
}); | ||
let res = request.post("/api/div").json(&payload).await; | ||
assert_eq!(res.status_code(), 200); | ||
assert_eq!(res.text(), serde_json::to_string(&expected).unwrap()); | ||
}) | ||
.await; | ||
} | ||
|
||
#[tokio::test] | ||
#[serial] | ||
async fn can_not_get_request_root() { | ||
testing::request::<App, _, _>(|request, _ctx| async move { | ||
let res = request.get("/api/div").await; | ||
assert_eq!(res.status_code(), 405); | ||
}) | ||
.await; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ mod home; | |
pub mod convert; | ||
pub mod add; | ||
pub mod sub; | ||
pub mod mul; | ||
pub mod mul; | ||
pub mod div; |