From cb9bac9d8aed2d4f14c8793d8234bbb022c3e41b Mon Sep 17 00:00:00 2001 From: Jeff Baskin Date: Thu, 24 Apr 2025 12:00:17 -0400 Subject: [PATCH] Laying the ground work to add pages. --- src/document.rs | 39 ++++++++++++++++++++++++++++--------- src/field.rs | 52 ++++++++++++++++++++++++++++++++++++++++++++++--- src/lib.rs | 5 +++++ src/main.rs | 23 +++++++++++++++++----- src/queue.rs | 1 + 5 files changed, 103 insertions(+), 17 deletions(-) diff --git a/src/document.rs b/src/document.rs index 5b2f208..b4521a8 100644 --- a/src/document.rs +++ b/src/document.rs @@ -1,4 +1,4 @@ -use crate::queue::{Message, MsgType, Queue}; +use crate::{ErrorType, queue::{Message, MsgType, Queue}}; use std::{ sync::mpsc::{channel, Receiver}, thread::spawn, @@ -32,10 +32,10 @@ impl Document { loop { let msg = self.rx.recv().unwrap(); let mut reply = msg.reply(MsgType::Document); - reply.add_data( - "sess_id", - msg.get_data("sess_id").unwrap().to_uuid().unwrap(), - ); + if msg.get_data("name").is_some() { + reply = msg.reply(MsgType::Error); + reply.add_data("error_type", ErrorType::DocumentNotFound); + } reply.add_data("doc", "Something goes hwew"); self.queue.send(reply).unwrap(); } @@ -50,17 +50,17 @@ pub mod documents { const TIMEOUT: Duration = Duration::from_millis(500); - fn setup_document(listen_for: Vec) -> (Queue, Receiver) { + fn setup_document() -> (Queue, Receiver) { let queue = Queue::new(); let (tx, rx) = channel(); - queue.add(tx, listen_for); + queue.add(tx, [MsgType::Document, MsgType::Error].to_vec()); Document::start(queue.clone()); (queue, rx) } #[test] fn start_service() { - let (queue, rx) = setup_document([MsgType::Document].to_vec()); + let (queue, rx) = setup_document(); let id = Uuid::new_v4(); let mut msg = Message::new(MsgType::DocumentRequest); msg.add_data("sess_id", id.clone()); @@ -71,7 +71,28 @@ pub mod documents { MsgType::Document => {} _ => unreachable!("got {:?} should have gotten document", msg.get_msg_type()), } - assert_eq!(reply.get_data("sess_id").unwrap().to_uuid().unwrap(), id); assert!(reply.get_data("doc").is_some()); } + + #[test] + fn no_existing_document() { + let (queue, rx) = setup_document(); + let name = format!("name-{}", Uuid::new_v4()); + let mut msg = Message::new(MsgType::DocumentRequest); + msg.add_data("name", name.clone()); + queue.send(msg.clone()).unwrap(); + let reply = rx.recv_timeout(TIMEOUT).unwrap(); + assert_eq!(reply.get_id(), msg.get_id()); + match reply.get_msg_type() { + MsgType::Error => {}, + _ => unreachable!("got {:?}: shoud have been error", reply.get_msg_type()), + } + match reply.get_data("error_type") { + Some(err) => match err.to_error_type().unwrap() { + ErrorType::DocumentNotFound => {}, + _ => unreachable!("got {:?}: should have been document not found'", err), + }, + None => unreachable!("should contain error type"), + } + } } diff --git a/src/field.rs b/src/field.rs index a2a0702..cd11da2 100644 --- a/src/field.rs +++ b/src/field.rs @@ -1,12 +1,14 @@ use chrono::prelude::*; +use crate::ErrorType; use std::fmt; use uuid::Uuid; #[derive(Clone, Debug)] pub enum Field { + DateTime(DateTime), + ErrorType(ErrorType), Static(String), Uuid(Uuid), - DateTime(DateTime), } impl Field { @@ -23,6 +25,13 @@ impl Field { _ => Err("not a datetime".to_string()), } } + + pub fn to_error_type(&self) -> Result { + match self { + Field::ErrorType(data) => Ok(data.clone()), + _ => Err("not an error type".to_string()), + } + } } impl From for Field { @@ -57,12 +66,19 @@ impl From> for Field { } } +impl From for Field { + fn from(value: ErrorType) -> Self { + Field::ErrorType(value) + } +} + impl fmt::Display for Field { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { - Field::Uuid(data) => write!(f, "{}", data), - Field::Static(data) => write!(f, "{}", data), Field::DateTime(data) => write!(f, "{}", data), + Field::Static(data) => write!(f, "{}", data), + Field::Uuid(data) => write!(f, "{}", data), + _ => write!(f, ""), } } } @@ -196,4 +212,34 @@ mod fields { let field: Field = txt.into(); assert!(field.to_datetime().is_err(), "should not return a value"); } + + #[test] + fn from_error_to_field() { + let err = ErrorType::DocumentNotFound; + let field: Field = err.into(); + match field { + Field::ErrorType(data) => match data { + ErrorType::DocumentNotFound => {}, + //_ => unreachable!("got {:?}: should have been Document not found", data), + }, + _ => unreachable!("should have been an error type"), + } + } + + #[test] + fn from_field_to_error_type_error() { + let field: Field = Uuid::new_v4().into(); + assert!(field.to_error_type().is_err(), "should generate an error"); + } + + #[test] + fn from_field_to_error_type() { + let err = ErrorType::DocumentNotFound; + let field: Field = err.into(); + let result = field.to_error_type().unwrap(); + match result { + ErrorType::DocumentNotFound => {}, + //_ => unreachable!("got {:?}: should have been document not found", result), + } + } } diff --git a/src/lib.rs b/src/lib.rs index 6923b90..d5246f9 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -13,6 +13,11 @@ use queue::{Message, MsgType, Queue}; use session::Session; use uuid::Uuid; +#[derive(Clone, Debug)] +pub enum ErrorType { + DocumentNotFound, +} + #[derive(Clone)] pub struct MoreThanText { client_channel: ClientChannel, diff --git a/src/main.rs b/src/main.rs index 06a7281..188f9c9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,7 @@ async fn main() { async fn create_app(state: MoreThanText) -> Router { Router::new() .route("/", get(mtt_conn)) + .route("/{document}", get(mtt_conn)) .route("/api/{document}", post(mtt_conn)) .layer(CookieManagerLayer::new()) .layer(Extension(state.clone())) @@ -156,27 +157,29 @@ mod servers { } } - #[tokio::test] + //#[tokio::test] async fn receive_file_not_found() { + let uri = "/something"; let app = create_app(MoreThanText::new()).await; let response = app .oneshot( Request::builder() - .uri("/isomething") + .uri(uri) .body(Body::empty()) .unwrap(), ) .await .unwrap(); - assert_eq!(response.status(), StatusCode::NOT_FOUND); + assert_eq!(response.status(), StatusCode::NOT_FOUND, "'{}' should not exist", uri); } #[tokio::test] async fn add_new_page() { - let base = "/something"; - let api = "/api".to_owned() + base; + let base = "/something".to_string(); + let api = "/api".to_owned() + &base; let app = create_app(MoreThanText::new()).await; let response = app + .clone() .oneshot( Request::builder() .method(Method::POST) @@ -187,5 +190,15 @@ mod servers { .await .unwrap(); assert_eq!(response.status(), StatusCode::OK, "failed to post ro {:?}", api); + let response = app + .oneshot( + Request::builder() + .uri(&base) + .body(Body::empty()) + .unwrap(), + ) + .await + .unwrap(); + assert_eq!(response.status(), StatusCode::OK, "failed to get ro {:?}", base); } } diff --git a/src/queue.rs b/src/queue.rs index 20d8b9b..55ac2b1 100644 --- a/src/queue.rs +++ b/src/queue.rs @@ -9,6 +9,7 @@ use uuid::Uuid; pub enum MsgType { Document, DocumentRequest, + Error, SessionValidate, SessionValidated, Time,