2026-04-04 01:40:24 +08:00
|
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
|
|
|
|
|
|
use axum::body::Body;
|
|
|
|
|
use axum::response::Response;
|
|
|
|
|
use axum::routing::any;
|
|
|
|
|
use axum::{extract::Request, Router};
|
|
|
|
|
use http::header::{HeaderName, HeaderValue};
|
|
|
|
|
use http::StatusCode;
|
|
|
|
|
|
|
|
|
|
use crate::gateway::constants::{
|
|
|
|
|
CONTROL_EXECUTED_HEADER, CONTROL_EXECUTE_FALLBACK_HEADER, EXECUTION_PATH_HEADER,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
use super::{build_router, start_server};
|
2026-03-31 19:19:04 +08:00
|
|
|
|
|
|
|
|
#[tokio::test]
|
2026-04-04 01:40:24 +08:00
|
|
|
async fn gateway_locally_denies_video_control_sync_even_with_opt_in_headers_when_execution_runtime_missing(
|
2026-04-03 14:59:58 +08:00
|
|
|
) {
|
2026-03-31 19:19:04 +08:00
|
|
|
let execute_hits = Arc::new(Mutex::new(0usize));
|
|
|
|
|
let execute_hits_clone = Arc::clone(&execute_hits);
|
|
|
|
|
let public_hits = Arc::new(Mutex::new(0usize));
|
|
|
|
|
let public_hits_clone = Arc::clone(&public_hits);
|
|
|
|
|
|
|
|
|
|
let upstream = Router::new()
|
|
|
|
|
.route(
|
|
|
|
|
"/api/internal/gateway/execute-sync",
|
|
|
|
|
any(move |_request: Request| {
|
|
|
|
|
let execute_hits_inner = Arc::clone(&execute_hits_clone);
|
|
|
|
|
async move {
|
|
|
|
|
*execute_hits_inner.lock().expect("mutex should lock") += 1;
|
|
|
|
|
let mut response = Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.body(Body::from("{\"status\":\"queued\"}"))
|
|
|
|
|
.expect("response should build");
|
|
|
|
|
response.headers_mut().insert(
|
|
|
|
|
http::header::CONTENT_TYPE,
|
|
|
|
|
HeaderValue::from_static("application/json"),
|
|
|
|
|
);
|
|
|
|
|
response.headers_mut().insert(
|
|
|
|
|
HeaderName::from_static(CONTROL_EXECUTED_HEADER),
|
|
|
|
|
HeaderValue::from_static("true"),
|
|
|
|
|
);
|
|
|
|
|
response
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.route(
|
|
|
|
|
"/v1/videos/task-123",
|
|
|
|
|
any(move |_request: Request| {
|
|
|
|
|
let public_hits_inner = Arc::clone(&public_hits_clone);
|
|
|
|
|
async move {
|
|
|
|
|
*public_hits_inner.lock().expect("mutex should lock") += 1;
|
|
|
|
|
(StatusCode::IM_A_TEAPOT, Body::from("public-route-hit"))
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
2026-04-04 01:40:24 +08:00
|
|
|
let gateway = build_router().expect("gateway should build");
|
2026-03-31 19:19:04 +08:00
|
|
|
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
|
|
|
|
|
|
|
|
|
let response = reqwest::Client::new()
|
|
|
|
|
.get(format!("{gateway_url}/v1/videos/task-123"))
|
|
|
|
|
.header(CONTROL_EXECUTE_FALLBACK_HEADER, "true")
|
|
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
.expect("request should succeed");
|
|
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
let payload: serde_json::Value = response.json().await.expect("body should parse");
|
|
|
|
|
assert_eq!(payload["error"]["type"], "http_error");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
payload["error"]["message"],
|
2026-04-04 01:40:24 +08:00
|
|
|
"OpenAI video execution runtime miss did not match a Rust execution path"
|
2026-03-31 19:19:04 +08:00
|
|
|
);
|
|
|
|
|
assert_eq!(*execute_hits.lock().expect("mutex should lock"), 0);
|
|
|
|
|
assert_eq!(*public_hits.lock().expect("mutex should lock"), 0);
|
|
|
|
|
|
|
|
|
|
gateway_handle.abort();
|
|
|
|
|
upstream_handle.abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
2026-04-04 01:40:24 +08:00
|
|
|
async fn gateway_locally_denies_video_control_sync_without_opt_in_header_when_execution_runtime_missing(
|
2026-04-03 14:59:58 +08:00
|
|
|
) {
|
2026-03-31 19:19:04 +08:00
|
|
|
let execute_hits = Arc::new(Mutex::new(0usize));
|
|
|
|
|
let execute_hits_clone = Arc::clone(&execute_hits);
|
|
|
|
|
let public_hits = Arc::new(Mutex::new(0usize));
|
|
|
|
|
let public_hits_clone = Arc::clone(&public_hits);
|
|
|
|
|
let public_execution_path = Arc::new(Mutex::new(None::<String>));
|
|
|
|
|
let public_execution_path_clone = Arc::clone(&public_execution_path);
|
|
|
|
|
|
|
|
|
|
let upstream = Router::new()
|
|
|
|
|
.route(
|
|
|
|
|
"/api/internal/gateway/execute-sync",
|
|
|
|
|
any(move |_request: Request| {
|
|
|
|
|
let execute_hits_inner = Arc::clone(&execute_hits_clone);
|
|
|
|
|
async move {
|
|
|
|
|
*execute_hits_inner.lock().expect("mutex should lock") += 1;
|
|
|
|
|
let mut response = Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.body(Body::from("{\"unexpected\":true}"))
|
|
|
|
|
.expect("response should build");
|
|
|
|
|
response.headers_mut().insert(
|
|
|
|
|
HeaderName::from_static(CONTROL_EXECUTED_HEADER),
|
|
|
|
|
HeaderValue::from_static("true"),
|
|
|
|
|
);
|
|
|
|
|
response
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.route(
|
|
|
|
|
"/v1/videos/task-123",
|
|
|
|
|
any(move |request: Request| {
|
|
|
|
|
let public_hits_inner = Arc::clone(&public_hits_clone);
|
|
|
|
|
let public_execution_path_inner = Arc::clone(&public_execution_path_clone);
|
|
|
|
|
async move {
|
|
|
|
|
*public_hits_inner.lock().expect("mutex should lock") += 1;
|
|
|
|
|
*public_execution_path_inner
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("mutex should lock") = Some(
|
|
|
|
|
request
|
|
|
|
|
.headers()
|
|
|
|
|
.get(EXECUTION_PATH_HEADER)
|
|
|
|
|
.and_then(|value| value.to_str().ok())
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
.to_string(),
|
|
|
|
|
);
|
|
|
|
|
(StatusCode::IM_A_TEAPOT, Body::from("public-route-hit"))
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
2026-04-04 01:40:24 +08:00
|
|
|
let gateway = build_router().expect("gateway should build");
|
2026-03-31 19:19:04 +08:00
|
|
|
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
|
|
|
|
|
|
|
|
|
let response = reqwest::Client::new()
|
|
|
|
|
.get(format!("{gateway_url}/v1/videos/task-123"))
|
|
|
|
|
.header(CONTROL_EXECUTE_FALLBACK_HEADER, "true")
|
|
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
.expect("request should succeed");
|
|
|
|
|
|
2026-04-03 14:59:58 +08:00
|
|
|
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
let payload: serde_json::Value = response.json().await.expect("body should parse");
|
|
|
|
|
assert_eq!(payload["error"]["type"], "http_error");
|
2026-03-31 19:19:04 +08:00
|
|
|
assert_eq!(
|
2026-04-03 14:59:58 +08:00
|
|
|
payload["error"]["message"],
|
2026-04-04 01:40:24 +08:00
|
|
|
"OpenAI video execution runtime miss did not match a Rust execution path"
|
2026-03-31 19:19:04 +08:00
|
|
|
);
|
2026-04-03 14:59:58 +08:00
|
|
|
assert_eq!(*execute_hits.lock().expect("mutex should lock"), 0);
|
2026-03-31 19:19:04 +08:00
|
|
|
assert_eq!(*public_hits.lock().expect("mutex should lock"), 0);
|
|
|
|
|
assert_eq!(
|
|
|
|
|
public_execution_path
|
|
|
|
|
.lock()
|
|
|
|
|
.expect("mutex should lock")
|
|
|
|
|
.clone()
|
|
|
|
|
.as_deref(),
|
|
|
|
|
None
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
gateway_handle.abort();
|
|
|
|
|
upstream_handle.abort();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[tokio::test]
|
2026-04-04 01:40:24 +08:00
|
|
|
async fn gateway_skips_video_get_control_sync_without_opt_in_header() {
|
2026-03-31 19:19:04 +08:00
|
|
|
let execute_hits = Arc::new(Mutex::new(0usize));
|
|
|
|
|
let execute_hits_clone = Arc::clone(&execute_hits);
|
|
|
|
|
let public_hits = Arc::new(Mutex::new(0usize));
|
|
|
|
|
let public_hits_clone = Arc::clone(&public_hits);
|
|
|
|
|
|
|
|
|
|
let upstream = Router::new()
|
|
|
|
|
.route(
|
|
|
|
|
"/api/internal/gateway/execute-sync",
|
|
|
|
|
any(move |_request: Request| {
|
|
|
|
|
let execute_hits_inner = Arc::clone(&execute_hits_clone);
|
|
|
|
|
async move {
|
|
|
|
|
*execute_hits_inner.lock().expect("mutex should lock") += 1;
|
|
|
|
|
let mut response = Response::builder()
|
|
|
|
|
.status(StatusCode::OK)
|
|
|
|
|
.body(Body::from("{\"status\":\"queued\"}"))
|
|
|
|
|
.expect("response should build");
|
|
|
|
|
response.headers_mut().insert(
|
|
|
|
|
HeaderName::from_static(CONTROL_EXECUTED_HEADER),
|
|
|
|
|
HeaderValue::from_static("true"),
|
|
|
|
|
);
|
|
|
|
|
response
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
.route(
|
|
|
|
|
"/v1/videos/task-123",
|
|
|
|
|
any(move |_request: Request| {
|
|
|
|
|
let public_hits_inner = Arc::clone(&public_hits_clone);
|
|
|
|
|
async move {
|
|
|
|
|
*public_hits_inner.lock().expect("mutex should lock") += 1;
|
|
|
|
|
(StatusCode::IM_A_TEAPOT, Body::from("public-route-hit"))
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
let (upstream_url, upstream_handle) = start_server(upstream).await;
|
2026-04-04 01:40:24 +08:00
|
|
|
let gateway = build_router().expect("gateway should build");
|
2026-03-31 19:19:04 +08:00
|
|
|
let (gateway_url, gateway_handle) = start_server(gateway).await;
|
|
|
|
|
|
|
|
|
|
let response = reqwest::Client::new()
|
|
|
|
|
.get(format!("{gateway_url}/v1/videos/task-123"))
|
|
|
|
|
.send()
|
|
|
|
|
.await
|
|
|
|
|
.expect("request should succeed");
|
|
|
|
|
|
|
|
|
|
assert_eq!(response.status(), StatusCode::SERVICE_UNAVAILABLE);
|
|
|
|
|
let payload: serde_json::Value = response.json().await.expect("body should parse");
|
|
|
|
|
assert_eq!(payload["error"]["type"], "http_error");
|
|
|
|
|
assert_eq!(
|
|
|
|
|
payload["error"]["message"],
|
2026-04-04 01:40:24 +08:00
|
|
|
"OpenAI video execution runtime miss did not match a Rust execution path"
|
2026-03-31 19:19:04 +08:00
|
|
|
);
|
|
|
|
|
assert_eq!(*execute_hits.lock().expect("mutex should lock"), 0);
|
|
|
|
|
assert_eq!(*public_hits.lock().expect("mutex should lock"), 0);
|
|
|
|
|
|
|
|
|
|
gateway_handle.abort();
|
|
|
|
|
upstream_handle.abort();
|
|
|
|
|
}
|