Skip to content

Commit

Permalink
adds tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Jerboa-app committed Jun 30, 2024
1 parent be046d0 commit f94f412
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 24 deletions.
62 changes: 38 additions & 24 deletions src/server/relay/request.rs
Original file line number Diff line number Diff line change
@@ -1,61 +1,75 @@
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};

#[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<axum::body::Body>,
next: Next
) -> Result<Response, StatusCode>
/// 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<axum::body::Body>) -> Option<RelayRequest>
{
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<axum::body::Body>,
next: Next
) -> Result<Response, StatusCode>
{

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();
Expand Down
19 changes: 19 additions & 0 deletions tests/test_relay.rs
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit f94f412

Please sign in to comment.