From f94f412dd7c267acb90f554b188ddb9ed806926b Mon Sep 17 00:00:00 2001 From: Jerboa-app Date: Sun, 30 Jun 2024 17:52:44 +0100 Subject: [PATCH] adds tests --- src/server/relay/request.rs | 62 +++++++++++++++++++++++-------------- tests/test_relay.rs | 19 ++++++++++++ 2 files changed, 57 insertions(+), 24 deletions(-) create mode 100644 tests/test_relay.rs diff --git a/src/server/relay/request.rs b/src/server/relay/request.rs index 6e30760..1aeeb19 100644 --- a/src/server/relay/request.rs +++ b/src/server/relay/request.rs @@ -1,7 +1,7 @@ use std::str::{from_utf8, FromStr}; use axum::{body::Body, http::{HeaderMap, HeaderName, HeaderValue, Request}, middleware::Next, response::Response}; -use reqwest::StatusCode; +use reqwest::{header, StatusCode}; use serde::Deserialize; use crate::{config::{Config, RelayConfig, CONFIG_PATH}, util::extract_bytes}; @@ -9,53 +9,67 @@ use crate::{config::{Config, RelayConfig, CONFIG_PATH}, util::extract_bytes}; #[derive(Deserialize)] /// Information to relay a request, name must match a name of /// a [RelayConfig], see [filter_relay]. -struct RelayRequest +pub struct RelayRequest { body: String, headers: Vec<(String, String)>, name: String } -/// Relay a request, if the header "relay" is present -/// and matches some [RelayConfig] in [Config]. -/// -/// The request body should be json deserializable into -/// a [RelayRequest]. Using this proxy one may hide API -/// tokens and urls behind a request sent to Busser. E.g -/// an AWS lambda CRUD API may be called by proxy hiding its -/// url and token. -pub async fn filter_relay - ( - headers: HeaderMap, - request: Request, - next: Next - ) -> Result +/// Checks for relay header +pub fn is_relay(headers: HeaderMap) -> bool { - if !headers.contains_key("relay") - { - return Ok(next.run(request).await); - } + headers.contains_key("relay") +} +/// Parses the body into a [RelayRequest] or [None]. +pub async fn get_request(request: Request) -> Option +{ let bytes = match extract_bytes(request).await { Ok(b) => b, - Err(_) => return Err(StatusCode::BAD_REQUEST) + Err(_) => return None }; let body = match from_utf8(&bytes) { Ok(b) => b, - Err(_) => return Err(StatusCode::BAD_REQUEST) + Err(_) => return None }; - let req: RelayRequest = match serde_json::from_str(body) + match serde_json::from_str(body) { Ok(r) => r, Err(_) => { crate::debug(format!("Bad JSON body"), Some("Relay")); - return Err(StatusCode::BAD_REQUEST) + return None } + } +} + +/// Relay a request, if the header "relay" is present +/// and matches some [RelayConfig] in [Config]. +/// +/// The request body should be json deserializable into +/// a [RelayRequest]. Using this proxy one may hide API +/// tokens and urls behind a request sent to Busser. E.g +/// an AWS lambda CRUD API may be called by proxy hiding its +/// url and token. +pub async fn filter_relay + ( + headers: HeaderMap, + request: Request, + next: Next + ) -> Result +{ + + if !is_relay(headers) { return Ok(next.run(request).await); } + + let req = match get_request(request).await + { + Some(r) => r, + None => return Err(StatusCode::BAD_REQUEST), }; let mut relay_headers = HeaderMap::new(); diff --git a/tests/test_relay.rs b/tests/test_relay.rs new file mode 100644 index 0000000..79773a1 --- /dev/null +++ b/tests/test_relay.rs @@ -0,0 +1,19 @@ +mod common; + +#[cfg(test)] +mod relay +{ + use axum::{body::{Body, Bytes}, http::{HeaderMap, HeaderValue, Request}, middleware::Next}; + use busser::server::relay::request::{filter_relay, get_request, is_relay}; + use reqwest::StatusCode; + + #[tokio::test] + async fn test_relay() + { + let headers = HeaderMap::new(); + assert!(!is_relay(headers)); + + let request = Request::new(Body::from("")); + assert!(get_request(request).await.is_none()); + } +} \ No newline at end of file