diff --git a/Cargo.lock b/Cargo.lock index 4de13f2..01a67b4 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -555,6 +555,8 @@ dependencies = [ "axum", "chrono", "clap", + "http-body-util", + "serde_json", "tokio", "tower", "tower-cookies", diff --git a/Cargo.toml b/Cargo.toml index a83a5f3..f0b210c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,9 +9,11 @@ edition = "2021" axum = ">=0.8.0" chrono = { version = ">=0.4.40", features = ["now"] } clap = { version = ">=4.5.1", features = ["derive"] } +serde_json = ">=1.0.140" tokio = { version = ">=1.36.0", features = ["full"] } tower-cookies = ">=0.11.0" uuid = { version = ">=1.8.0", features = ["v4"] } [dev-dependencies] +http-body-util = ">=0.1.3" tower = { version = ">=0.5.2", features = ["util"] } diff --git a/src/client.rs b/src/client.rs index af663e2..cee723b 100644 --- a/src/client.rs +++ b/src/client.rs @@ -9,7 +9,12 @@ use std::{ }; use uuid::Uuid; -const RESPONS_TO: [MsgType; 4] = [MsgType::ActionOk, MsgType::Document, MsgType::Error, MsgType::SessionValidated]; +const RESPONS_TO: [MsgType; 4] = [ + MsgType::ActionOk, + MsgType::Document, + MsgType::Error, + MsgType::SessionValidated, +]; #[derive(Clone)] pub struct ClientChannel { diff --git a/src/document.rs b/src/document.rs index 4b6e152..120726b 100644 --- a/src/document.rs +++ b/src/document.rs @@ -2,6 +2,7 @@ use crate::{ queue::{Message, MsgType, Queue}, ActionType, ErrorType, }; +use serde_json::Value; use std::{ collections::HashMap, sync::mpsc::{channel, Receiver}, @@ -51,8 +52,9 @@ impl Document { fn add(&mut self, msg: Message) { let name = msg.get_data("name").unwrap().to_string(); - let doc = msg.get_data("doc").unwrap().to_string(); - self.data.insert(name, doc); + let doc: Value = serde_json::from_str(&msg.get_data("doc").unwrap().to_string()).unwrap(); + self.data + .insert(name, doc["template"].as_str().unwrap().to_string()); self.queue.send(msg.reply(MsgType::ActionOk)).unwrap(); } @@ -66,7 +68,7 @@ impl Document { let mut holder = msg.reply(MsgType::Document); holder.add_data("doc", data.clone()); holder - }, + } None => { let mut holder = msg.reply(MsgType::Error); holder.add_data("error_type", ErrorType::DocumentNotFound); @@ -80,6 +82,7 @@ impl Document { #[cfg(test)] pub mod documents { use super::*; + use serde_json::json; use std::time::Duration; use uuid::Uuid; @@ -88,7 +91,10 @@ pub mod documents { fn setup_document() -> (Queue, Receiver) { let queue = Queue::new(); let (tx, rx) = channel(); - queue.add(tx, [MsgType::ActionOk, MsgType::Document, MsgType::Error].to_vec()); + queue.add( + tx, + [MsgType::ActionOk, MsgType::Document, MsgType::Error].to_vec(), + ); Document::start(queue.clone()); (queue, rx) } @@ -153,10 +159,13 @@ pub mod documents { let (queue, rx) = setup_document(); let name = format!("name-{}", Uuid::new_v4()); let content = format!("content-{}", Uuid::new_v4()); + let input = json!({ + "template": content.clone() + }); let mut msg1 = Message::new(MsgType::DocumentRequest); msg1.add_data("name", name.clone()); msg1.add_data("action", ActionType::Add); - msg1.add_data("doc", content.clone()); + msg1.add_data("doc", input.to_string()); queue.send(msg1.clone()).unwrap(); let reply1 = rx.recv_timeout(TIMEOUT).unwrap(); assert_eq!(reply1.get_id(), msg1.get_id()); diff --git a/src/field.rs b/src/field.rs index f1de2c3..4b5a662 100644 --- a/src/field.rs +++ b/src/field.rs @@ -282,7 +282,7 @@ mod fields { let field: Field = Uuid::new_v4().into(); match field.to_action() { Ok(_) => unreachable!("should have returned an error"), - Err(_) => {}, + Err(_) => {} } } } diff --git a/src/lib.rs b/src/lib.rs index 82dedc8..ec6594e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -132,7 +132,13 @@ impl MoreThanText { reply.get_data("sess_id").unwrap().to_uuid().unwrap() } - pub fn get_document(&self, sess_id: Uuid, action: ActionType, doc_name: S) -> MTTReply + pub fn get_document( + &self, + sess_id: Uuid, + action: ActionType, + doc_name: S, + data: String, + ) -> MTTReply where S: Into, { @@ -140,7 +146,7 @@ impl MoreThanText { msg.add_data("sess_id", sess_id); msg.add_data("action", action); msg.add_data("name", doc_name.into()); - msg.add_data("doc", "splat"); + msg.add_data("doc", data); let rx = self.client_channel.send(msg); let reply = rx.recv().unwrap(); MTTReply::new(reply) @@ -176,7 +182,7 @@ mod mtt { fn get_root_document_with_str() { let mut mtt = MoreThanText::new(); let id = mtt.validate_session(Some(Uuid::new_v4())); - let output = mtt.get_document(id, ActionType::Get, "root"); + let output = mtt.get_document(id, ActionType::Get, "root", "".to_string()); assert!(output.get_error().is_none()); } @@ -184,7 +190,7 @@ mod mtt { fn get_root_document_with_string() { let mut mtt = MoreThanText::new(); let id = mtt.validate_session(Some(Uuid::new_v4())); - let output = mtt.get_document(id, ActionType::Get, "root".to_string()); + let output = mtt.get_document(id, ActionType::Get, "root".to_string(), "".to_string()); assert!(output.get_error().is_none()); } } diff --git a/src/main.rs b/src/main.rs index b733fc0..1f37996 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,6 +85,7 @@ async fn mtt_conn( method: Method, path: Path>, state: State, + body: String, ) -> impl IntoResponse { let (tx, mut rx) = channel(1); let action = match method { @@ -97,7 +98,7 @@ async fn mtt_conn( None => "root".to_string(), }; spawn(async move { - tx.send(state.get_document(sess_id.0, action, doc)) + tx.send(state.get_document(sess_id.0, action, doc, body)) .await .unwrap(); }); @@ -112,7 +113,6 @@ async fn mtt_conn( #[cfg(test)] mod servers { use super::*; - use std::time::Duration; use axum::{ body::Body, http::{ @@ -120,6 +120,9 @@ mod servers { Method, Request, }, }; + use http_body_util::BodyExt; + use serde_json::json; + use std::time::Duration; use tower::ServiceExt; #[tokio::test] @@ -197,6 +200,10 @@ mod servers { async fn add_new_page() { let base = "/something".to_string(); let api = format!("/api{}", &base); + let content = format!("content-{}", Uuid::new_v4()); + let document = json!({ + "template": content.clone() + }); let app = create_app(MoreThanText::new()).await; let response = app .clone() @@ -204,7 +211,7 @@ mod servers { Request::builder() .method(Method::POST) .uri(&api) - .body(Body::empty()) + .body(document.to_string()) .unwrap(), ) .await @@ -225,5 +232,7 @@ mod servers { "failed to get ro {:?}", base ); + let body = response.into_body().collect().await.unwrap().to_bytes(); + assert_eq!(body, content); } }