From 5d14155ba338651c5ecd43ada5607f716857fe3f Mon Sep 17 00:00:00 2001 From: _ <_@_> Date: Fri, 30 Oct 2020 18:18:42 -0500 Subject: [PATCH] Start adding some tests --- src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ src/relay/mod.rs | 3 +++ 2 files changed, 49 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7d12142..781be02 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,10 @@ pub mod http_serde; +// It's easier if the server can stream its response body +// back to the relay un-changed inside its request body +// So we wrap the server's actual response head +// (status code, headers, etc.) in this one header field. + pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4"; // Basically binaries, but in the lib we can do experimental @@ -7,3 +12,44 @@ pub const PTTH_MAGIC_HEADER: &str = "X-PTTH-2LJYXWC4"; pub mod relay; pub mod server; + +#[cfg (test)] +mod tests { + use std::{ + error::Error, + time::Duration, + }; + + use tokio::{ + prelude::*, + runtime::Runtime, + spawn, + time::delay_for, + }; + + use super::{ + relay, + server, + }; + + #[test] + fn end_to_end () { + use reqwest::Client; + + let mut rt = Runtime::new ().unwrap (); + + // Spawn the root task + rt.block_on (async { + spawn (async { + relay::main ().await.unwrap (); + }); + + let client = Client::new (); + + let resp = client.get ("http://127.0.0.1:4000/relay_up_check") + .send ().await.unwrap ().bytes ().await.unwrap (); + + assert_eq! (resp, "Relay is up\n"); + }); + } +} diff --git a/src/relay/mod.rs b/src/relay/mod.rs index fe8026d..b8b8a00 100644 --- a/src/relay/mod.rs +++ b/src/relay/mod.rs @@ -241,6 +241,9 @@ async fn handle_all (req: Request , state: Arc ) Ok (status_reply (StatusCode::BAD_REQUEST, "Bad URI format")) } } + else if path == "/relay_up_check" { + Ok (status_reply (StatusCode::OK, "Relay is up\n")) + } else { Ok (status_reply (StatusCode::OK, "Hi\n")) }