Verified Commit c9018b4f authored by onyinyang's avatar onyinyang 🔒
Browse files

Add bridge blocking endpoint

parent ef14d93f
Loading
Loading
Loading
Loading
+28 −0
Original line number Diff line number Diff line
use bytes::Bytes;
#[cfg(feature = "test-branch")]
use http_body_util::Empty;
use http_body_util::{combinators::BoxBody, BodyExt, Full};
use hyper::{
    header::{HeaderValue, ACCEPT},
@@ -857,12 +859,38 @@ impl LoxServerContext {
        self.advance_days_test(req);
        self.send_today()
    }

    #[cfg(feature = "test-branch")]
    /// For testing only: manually advance the day by the given number
    /// of days and send back the current day.
    pub fn block_bridges_test(self, request: Bytes) -> Response<BoxBody<Bytes, Infallible>> {
        let req: BridgeLine = match serde_json::from_slice(&request) {
            Ok(req) => req,
            Err(e) => {
                let response = json!({"error": e.to_string()});
                let val = serde_json::to_string(&response).unwrap();
                return prepare_header(val);
            }
        };
        self.mark_blocked(req);
        let builder = Response::builder()
            .header("Access-Control-Allow-Origin", HeaderValue::from_static("*"))
            .status(200);
        builder.body(empty()).unwrap()
    }
}

fn full<T: Into<Bytes>>(chunk: T) -> BoxBody<Bytes, Infallible> {
    Full::new(chunk.into()).boxed()
}

#[cfg(feature = "test-branch")]
fn empty() -> BoxBody<Bytes, Infallible> {
    Empty::<Bytes>::new()
        .map_err(|never| match never {})
        .boxed()
}

// Prepare HTTP Response for successful Server Request
fn prepare_header(response: String) -> Response<BoxBody<Bytes, Infallible>> {
    let mut resp = Response::new(full(response));
+5 −0
Original line number Diff line number Diff line
@@ -30,6 +30,11 @@ where
                let bytes = req.into_body().collect().await.unwrap().to_bytes();
                cloned_context.advance_days_with_response_test(bytes)
            }),
            #[cfg(feature = "test-branch")]
            (&Method::POST, "/blockbridges") => Ok::<_, Infallible>({
                let bytes = req.into_body().collect().await.unwrap().to_bytes();
                cloned_context.block_bridges_test(bytes)
            }),
            (&Method::POST, "/invite") => Ok::<_, Infallible>(cloned_context.generate_invite()),
            (&Method::POST, "/reachability") => {
                Ok::<_, Infallible>(cloned_context.send_reachability_cred())