diff options
| author | Arnaud Bailly <arnaud.bailly@iohk.io> | 2024-10-04 08:13:17 +0200 |
|---|---|---|
| committer | Arnaud Bailly <arnaud.bailly@iohk.io> | 2024-10-04 08:13:17 +0200 |
| commit | 2498ba2b3dc57fa7f47fe7eced0dbab1bbfb5336 (patch) | |
| tree | f7dcc6f8bfb6077a41b9363835b646193ed86648 | |
| parent | ad370430ef356fdc0e2d514041560ae5c4185e32 (diff) | |
| download | lambda-nantes-2498ba2b3dc57fa7f47fe7eced0dbab1bbfb5336.tar.gz | |
Generate new id upon receiving registration request
| -rw-r--r-- | rust/Cargo.lock | 83 | ||||
| -rw-r--r-- | rust/Cargo.toml | 6 | ||||
| -rw-r--r-- | rust/src/web.rs | 68 |
3 files changed, 146 insertions, 11 deletions
diff --git a/rust/Cargo.lock b/rust/Cargo.lock index 484a4d6..cd32246 100644 --- a/rust/Cargo.lock +++ b/rust/Cargo.lock @@ -253,6 +253,17 @@ dependencies = [ ] [[package]] +name = "atty" +version = "0.2.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d9b39be18770d11421cdb1b9947a45dd3f37e93092cbf377614828a319d5fee8" +dependencies = [ + "hermit-abi 0.1.19", + "libc", + "winapi", +] + +[[package]] name = "autocfg" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -482,6 +493,19 @@ dependencies = [ ] [[package]] +name = "env_logger" +version = "0.8.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a19187fea3ac7e84da7dacf48de0c45d63c6a76f9490dae389aead16c243fce3" +dependencies = [ + "atty", + "humantime", + "log", + "regex", + "termcolor", +] + +[[package]] name = "equivalent" version = "1.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -612,6 +636,15 @@ checksum = "1e087f84d4f86bf4b218b927129862374b72199ae7d8657835f1e89000eea4fb" [[package]] name = "hermit-abi" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62b467343b94ba476dcb2500d242dadbb39557df889310ac77c5d99100aaac33" +dependencies = [ + "libc", +] + +[[package]] +name = "hermit-abi" version = "0.3.9" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d231dfb89cfffdbc30e7fc41579ed6066ad03abda9e567ccafae602b97ec5024" @@ -640,6 +673,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" [[package]] +name = "humantime" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4" + +[[package]] name = "iana-time-zone" version = "0.1.61" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -802,7 +841,7 @@ version = "1.0.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "80e04d1dcff3aae0704555fe5fee3bcfaf3d1fdf8a7e521d5b9d2b42acb52cec" dependencies = [ - "hermit-abi", + "hermit-abi 0.3.9", "libc", "log", "wasi", @@ -1041,6 +1080,8 @@ version = "0.1.0" dependencies = [ "actix-web", "chrono", + "env_logger", + "log", "proptest", "rand", "serde", @@ -1225,6 +1266,15 @@ dependencies = [ ] [[package]] +name = "termcolor" +version = "1.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" +dependencies = [ + "winapi-util", +] + +[[package]] name = "time" version = "0.3.36" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -1441,6 +1491,37 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c62a0a307cb4a311d3a07867860911ca130c3494e8c2719593806c08bc5d0484" [[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" +dependencies = [ + "windows-sys 0.59.0", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + +[[package]] name = "windows-core" version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" diff --git a/rust/Cargo.toml b/rust/Cargo.toml index 8616834..4deb7ec 100644 --- a/rust/Cargo.toml +++ b/rust/Cargo.toml @@ -9,6 +9,8 @@ serde = { version = "1.0", features = ["derive"] } chrono = { version= "0.4.38", features = ["serde"]} serde_json = "1.0.128" actix-web = "4" +env_logger = "0.8" +log = "0.4" [dev-dependencies] proptest = "1.0.0" @@ -24,3 +26,7 @@ path = "src/main.rs" [[bin]] name = "tester" path = "src/tester.rs" + +[[bin]] +name = "server" +path = "src/web.rs" diff --git a/rust/src/web.rs b/rust/src/web.rs index 2614c06..9c1b2c2 100644 --- a/rust/src/web.rs +++ b/rust/src/web.rs @@ -13,11 +13,34 @@ struct Registration { url: String, } +trait IdGenerator { + fn next_id(&self) -> String; +} + +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] +enum RegistrationResult { + Success(RegistrationSuccess), +} + +#[derive(Debug, PartialEq, Serialize, Deserialize, Clone)] +struct RegistrationSuccess { + id: String, + url: String, +} + #[post("/register")] -async fn register(registration: web::Json<Registration>) -> impl Responder { +async fn register( + id_gen: web::Data<dyn IdGenerator>, + registration: web::Json<Registration>, +) -> impl Responder { + let id = id_gen.next_id(); + let success = RegistrationSuccess { + id, + url: registration.url.clone(), + }; HttpResponse::Ok() .content_type(ContentType::json()) - .body(serde_json::to_string(®istration).unwrap()) + .body(serde_json::to_string(&RegistrationResult::Success(success)).unwrap()) } #[actix_web::main] @@ -30,27 +53,52 @@ async fn main() -> std::io::Result<()> { #[cfg(test)] mod app_tests { - use actix_web::{body, http::header::ContentType, test, App}; + use std::sync::Arc; + + use actix_web::{body, http::header::ContentType, middleware::Logger, test, App}; use super::*; + struct ConstantIdGenerator { + id: String, + } + + impl ConstantIdGenerator { + fn new(id: String) -> Self { + Self { id } + } + } + + impl IdGenerator for ConstantIdGenerator { + fn next_id(&self) -> String { + self.id.clone() + } + } + #[actix_web::test] - async fn post_registration_echoes_back_registration_data_with_unique_id() { - let app = test::init_service(App::new().service(register)).await; - let registration = Registration { - url: "http://192.168.1.1".to_string(), - }; + async fn post_registration_returns_success_with_unique_id() { + let id = "0123456789abcdef0123456789abcdef".to_string(); + let id_generator: Arc<dyn IdGenerator> = Arc::new(ConstantIdGenerator::new(id.clone())); + env_logger::init(); + let logger = Logger::default(); + let data = web::Data::from(id_generator); + let app = + test::init_service(App::new().wrap(logger).app_data(data).service(register)).await; + let url = "http://192.168.1.1".to_string(); let req = test::TestRequest::post() .uri("/register") - .set_json(registration.clone()) + .set_json(Registration { url: url.clone() }) .insert_header(ContentType::json()) .to_request(); + let resp = test::call_service(&app, req).await; + assert!(resp.status().is_success()); + let body = resp.into_body(); let bytes = body::to_bytes(body).await; assert_eq!( - registration, + RegistrationResult::Success(RegistrationSuccess { id, url }), serde_json::from_slice(&bytes.unwrap()).unwrap() ); } |
