use async_trait::async_trait;
use rustf::middleware::{InboundAction, InboundMiddleware, OutboundMiddleware};
use rustf::prelude::*;
use rustf::Error;
use std::net::SocketAddr;
use std::time::Duration;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::TcpStream;
struct Stamp;
#[async_trait]
impl OutboundMiddleware for Stamp {
async fn process_response(&self, ctx: &mut Context) -> rustf::Result<()> {
if let Some(response) = ctx.res.as_mut() {
response.add_header("X-Stamped", "yes");
}
Ok(())
}
}
#[derive(Clone)]
struct LateDual;
#[async_trait]
impl InboundMiddleware for LateDual {
async fn process_request(&self, _ctx: &mut Context) -> rustf::Result<InboundAction> {
Ok(InboundAction::Capture)
}
fn priority(&self) -> i32 {
50
}
}
#[async_trait]
impl OutboundMiddleware for LateDual {
async fn process_response(&self, ctx: &mut Context) -> rustf::Result<()> {
if let Some(response) = ctx.res.as_mut() {
response.add_header("X-Late-Dual", "yes");
}
Ok(())
}
}
struct RejectGuardedPath;
#[async_trait]
impl InboundMiddleware for RejectGuardedPath {
async fn process_request(&self, ctx: &mut Context) -> rustf::Result<InboundAction> {
match ctx.req.path() {
"/by-middleware" => Err(Error::authentication("Middleware says no")),
"/stopped-by-middleware" => {
ctx.throw403(Some("Stopped here"))?;
Ok(InboundAction::Stop)
}
_ => Ok(InboundAction::Continue),
}
}
fn priority(&self) -> i32 {
-100
}
}
async fn guarded(ctx: &mut Context) -> rustf::Result<()> {
ctx.require_auth()?;
ctx.plain("secret")
}
async fn validated(_ctx: &mut Context) -> rustf::Result<()> {
Err(Error::validation("email is not a valid address"))
}
async fn refused(_ctx: &mut Context) -> rustf::Result<()> {
Err(Error::authorization("administrators only"))
}
async fn boom(_ctx: &mut Context) -> rustf::Result<()> {
Err(Error::internal(
"postgres://admin:hunter2@10.0.0.7/billing is unreachable",
))
}
async fn missing(_ctx: &mut Context) -> rustf::Result<()> {
Err(Error::ModelNotFound("internal_billing_ledger".into()))
}
async fn wrapped(_ctx: &mut Context) -> rustf::Result<()> {
Err(Error::authentication("Session expired")
.with_context("while verifying the admin token against /etc/rustf/keys/admin.pem"))
}
async fn half_written(ctx: &mut Context) -> rustf::Result<()> {
ctx.plain("half of a page")?;
Err(Error::validation("changed my mind"))
}
async fn slow(_ctx: &mut Context) -> rustf::Result<()> {
Err(Error::timeout("Worker 'ledger-sync' timed out after 30s"))
}
async fn db_down(_ctx: &mut Context) -> rustf::Result<()> {
Err(Error::database_query(
"relation \"internal_billing_ledger\" does not exist",
))
}
async fn parse_json(ctx: &mut Context) -> rustf::Result<()> {
let value: serde_json::Value = ctx.body_json()?;
ctx.json(value)
}
#[derive(serde::Deserialize)]
#[allow(dead_code)]
struct Payment {
internal_account_id: u64,
}
async fn parse_typed(ctx: &mut Context) -> rustf::Result<()> {
let _payment: Payment = ctx.body_json()?;
ctx.plain("paid")
}
async fn ok_page(ctx: &mut Context) -> rustf::Result<()> {
ctx.plain("fine")
}
fn install_routes() -> Vec<Route> {
routes![
GET "/guarded" => guarded,
GET "/validated" => validated,
GET "/refused" => refused,
GET "/boom" => boom,
GET "/missing" => missing,
GET "/wrapped" => wrapped,
GET "/half" => half_written,
GET "/ok" => ok_page,
GET "/slow" => slow,
GET "/db" => db_down,
POST "/json" => parse_json,
POST "/pay" => parse_typed,
GET "/by-middleware" => ok_page,
GET "/stopped-by-middleware" => ok_page,
]
}
fn install_hooked_routes() -> Vec<Route> {
async fn before(ctx: &mut Context) -> rustf::Result<BeforeAction> {
ctx.require_auth()?;
Ok(BeforeAction::Continue)
}
routes![
before: before,
GET "/hooked" => ok_page,
]
}
struct HttpResponse {
status: u16,
headers: String,
body: String,
}
async fn http_get(addr: SocketAddr, path: &str, accept: Option<&str>) -> HttpResponse {
let headers: Vec<(&str, &str)> = accept.map(|value| ("Accept", value)).into_iter().collect();
http_send(addr, "GET", path, &headers, "").await
}
async fn http_send(
addr: SocketAddr,
method: &str,
path: &str,
headers: &[(&str, &str)],
body: &str,
) -> HttpResponse {
let mut stream = TcpStream::connect(addr).await.unwrap();
let mut request = format!("{method} {path} HTTP/1.1\r\nHost: {addr}\r\n");
for (name, value) in headers {
request.push_str(&format!("{name}: {value}\r\n"));
}
if !body.is_empty() {
request.push_str(&format!("Content-Length: {}\r\n", body.len()));
}
request.push_str("Connection: close\r\n\r\n");
request.push_str(body);
stream.write_all(request.as_bytes()).await.unwrap();
let read = async {
let mut raw = Vec::new();
let mut byte = [0u8; 1];
loop {
if stream.read(&mut byte).await.unwrap() == 0 {
panic!("connection closed before the header terminator for {path}");
}
raw.push(byte[0]);
if raw.ends_with(b"\r\n\r\n") {
break;
}
}
let head = String::from_utf8_lossy(&raw).into_owned();
let status = head
.lines()
.next()
.and_then(|line| line.split_whitespace().nth(1))
.and_then(|code| code.parse::<u16>().ok())
.unwrap_or_else(|| panic!("no status line in {head}"));
let length = head
.lines()
.find_map(|line| {
let (name, value) = line.split_once(':')?;
name.eq_ignore_ascii_case("content-length")
.then(|| value.trim().parse::<usize>().ok())?
})
.unwrap_or(0);
let mut body = vec![0u8; length];
stream.read_exact(&mut body).await.unwrap();
HttpResponse {
status,
headers: head,
body: String::from_utf8_lossy(&body).into_owned(),
}
};
tokio::time::timeout(Duration::from_secs(5), read)
.await
.unwrap_or_else(|_| panic!("timed out reading the response for {path}"))
}
#[tokio::test]
async fn handler_errors_carry_their_own_status() {
let app = RustF::new()
.middleware_from(|registry| {
registry.register_outbound("stamp", Stamp);
registry.register_inbound("reject_guarded_path", RejectGuardedPath);
registry.register_dual("late_dual", LateDual);
})
.controllers(install_routes())
.controllers(install_hooked_routes());
let running = app.serve_with_handle("127.0.0.1:0").await.unwrap();
let addr = running.local_addr;
let response = http_get(addr, "/ok", None).await;
assert_eq!(response.status, 200, "{}", response.headers);
assert_eq!(response.body, "fine");
assert!(
response.headers.to_lowercase().contains("x-late-dual: yes"),
"on a normal request the dual-phase middleware's outbound runs: {}",
response.headers
);
let response = http_get(addr, "/guarded", None).await;
assert_eq!(
response.status, 401,
"ctx.require_auth()? must be a 401, not a 500: {}",
response.headers
);
assert_eq!(response.body, "Authentication required");
assert!(
response.headers.to_lowercase().contains("text/plain"),
"a browser request gets plain text: {}",
response.headers
);
let response = http_get(addr, "/hooked", None).await;
assert_eq!(
response.status, 401,
"a before hook's error must map like a handler's: {}",
response.headers
);
let response = http_get(addr, "/guarded", Some("application/json")).await;
assert_eq!(response.status, 401);
assert!(
response.headers.to_lowercase().contains("application/json"),
"{}",
response.headers
);
let body: serde_json::Value = serde_json::from_str(&response.body)
.unwrap_or_else(|e| panic!("body was not JSON ({e}): {}", response.body));
assert_eq!(body["error"], "Authentication required");
assert_eq!(body["code"], "E_AUTH");
let response = http_get(
addr,
"/guarded",
Some("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"),
)
.await;
assert!(
response.headers.to_lowercase().contains("text/plain"),
"a browser Accept must not produce JSON: {}",
response.headers
);
let response = http_get(addr, "/validated", None).await;
assert_eq!(response.status, 400, "{}", response.headers);
assert_eq!(response.body, "email is not a valid address");
let response = http_get(addr, "/refused", None).await;
assert_eq!(response.status, 403, "{}", response.headers);
assert_eq!(response.body, "administrators only");
let response = http_get(addr, "/missing", None).await;
assert_eq!(response.status, 404, "{}", response.headers);
assert_eq!(
response.body, "Not Found",
"a 404 must not echo the model name"
);
let response = http_get(addr, "/boom", None).await;
assert_eq!(response.status, 500, "{}", response.headers);
assert_eq!(response.body, "Internal Server Error");
assert!(
!response.body.contains("hunter2") && !response.body.contains("10.0.0.7"),
"a 500 must not disclose its message: {}",
response.body
);
let response = http_get(addr, "/boom", Some("application/json")).await;
assert_eq!(response.status, 500);
let body: serde_json::Value = serde_json::from_str(&response.body).unwrap();
assert_eq!(body["error"], "Internal Server Error");
assert_eq!(
body["code"], "E_INTERNAL",
"a 5xx must not name the variant that failed"
);
let response = http_get(addr, "/wrapped", None).await;
assert_eq!(response.status, 401, "{}", response.headers);
assert_eq!(response.body, "Session expired");
assert!(
!response.body.contains("admin.pem"),
"with_context detail must not be disclosed: {}",
response.body
);
let response = http_get(addr, "/half", None).await;
assert_eq!(response.status, 400, "{}", response.headers);
assert_eq!(
response.body, "changed my mind",
"the half-written body must not survive the error"
);
let response = http_get(addr, "/by-middleware", None).await;
assert_eq!(
response.status, 401,
"an inbound middleware's error must carry its own status: {}",
response.headers
);
assert_eq!(response.body, "Middleware says no");
assert!(
response.headers.to_lowercase().contains("x-stamped: yes"),
"outbound-only middleware after the failing one must still run: {}",
response.headers
);
assert!(
!response.headers.to_lowercase().contains("x-late-dual"),
"a dual-phase middleware whose inbound never ran must not run outbound: {}",
response.headers
);
let response = http_get(addr, "/stopped-by-middleware", None).await;
assert_eq!(response.status, 403, "{}", response.headers);
assert!(
response.headers.to_lowercase().contains("x-stamped: yes"),
"an early inbound Stop must still get outbound-only middleware: {}",
response.headers
);
assert!(
!response.headers.to_lowercase().contains("x-late-dual"),
"a Stop must not run a later dual-phase middleware's outbound: {}",
response.headers
);
assert_eq!(
response
.headers
.to_lowercase()
.matches("x-stamped:")
.count(),
1,
"each outbound middleware runs exactly once: {}",
response.headers
);
let response = http_get(addr, "/slow", None).await;
assert_eq!(response.status, 504, "{}", response.headers);
assert_eq!(response.body, "Gateway Timeout");
let response = http_get(addr, "/slow", Some("application/json")).await;
let body: serde_json::Value = serde_json::from_str(&response.body).unwrap();
assert_eq!(
body["code"], "E_INTERNAL",
"a 5xx must not name the variant"
);
let response = http_get(addr, "/db", None).await;
assert_eq!(response.status, 500, "{}", response.headers);
assert_eq!(response.body, "Internal Server Error");
let response = http_get(addr, "/db", Some("application/json")).await;
assert!(
!response.body.contains("internal_billing_ledger"),
"a database error must not leak through JSON: {}",
response.body
);
let response = http_send(
addr,
"POST",
"/json",
&[("Content-Type", "application/json")],
"{not json",
)
.await;
assert_eq!(response.status, 400, "{}", response.headers);
assert_eq!(response.body, "Invalid JSON body");
let response = http_send(
addr,
"POST",
"/pay",
&[("Content-Type", "application/json")],
"{}",
)
.await;
assert_eq!(response.status, 400, "{}", response.headers);
assert!(
!response.body.contains("internal_account_id"),
"the deserializer's diagnostic must not name server-side fields: {}",
response.body
);
let response = http_send(
addr,
"POST",
"/json",
&[("Content-Type", "application/json")],
r#"{"ok":true}"#,
)
.await;
assert_eq!(response.status, 200, "{}", response.headers);
let response = http_send(addr, "GET", "/guarded", &[("HX-Request", "true")], "").await;
assert_eq!(response.status, 401);
assert!(
response.headers.to_lowercase().contains("text/plain"),
"htmx must not receive a JSON error: {}",
response.headers
);
let response = http_send(
addr,
"GET",
"/guarded",
&[("X-Requested-With", "XMLHttpRequest")],
"",
)
.await;
assert!(
response.headers.to_lowercase().contains("application/json"),
"a classic XHR gets JSON: {}",
response.headers
);
let response = http_get(addr, "/guarded", None).await;
assert!(
response.headers.to_lowercase().contains("x-stamped: yes"),
"a handler error response must go through outbound middleware: {}",
response.headers
);
running.handle.shutdown().await.unwrap();
}