Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 10 additions & 6 deletions tests/configuration/tests/suite/examples/ai/model_to_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@ fn model_to_header_routes_by_model_field() {
let proxy_port = free_port();
let yaml = make_yaml(proxy_port, "mistral-large-latest", port_a, port_default);
let config = Config::from_yaml(&yaml).unwrap();
let addr = start_proxy(&config);
let (status, body) = http_post(&addr, "/v1/chat", r#"{"model":"mistral-large-latest","messages":[]}"#);
let proxy = start_proxy(&config);
let (status, body) = http_post(
proxy.addr(),
"/v1/chat",
r#"{"model":"mistral-large-latest","messages":[]}"#,
);
assert_eq!(status, 200, "matching model should return 200");
assert_eq!(
body, "model-a-response",
Expand All @@ -33,8 +37,8 @@ fn model_to_header_falls_through_on_unknown_model() {
let proxy_port = free_port();
let yaml = make_yaml(proxy_port, "mistral-large-latest", port_a, port_default);
let config = Config::from_yaml(&yaml).unwrap();
let addr = start_proxy(&config);
let (status, body) = http_post(&addr, "/v1/chat", r#"{"model":"unknown","messages":[]}"#);
let proxy = start_proxy(&config);
let (status, body) = http_post(proxy.addr(), "/v1/chat", r#"{"model":"unknown","messages":[]}"#);
assert_eq!(status, 200, "unknown model should return 200");
assert_eq!(body, "default-response", "unknown model should fall through to default");
}
Expand All @@ -46,8 +50,8 @@ fn model_to_header_continues_without_model_field() {
let proxy_port = free_port();
let yaml = make_yaml(proxy_port, "mistral-large-latest", port_a, port_default);
let config = Config::from_yaml(&yaml).unwrap();
let addr = start_proxy(&config);
let (status, body) = http_post(&addr, "/v1/chat", r#"{"messages":[]}"#);
let proxy = start_proxy(&config);
let (status, body) = http_post(proxy.addr(), "/v1/chat", r#"{"messages":[]}"#);
assert_eq!(status, 200, "missing model field should return 200");
assert_eq!(body, "default-response", "missing model should fall through to default");
}
Expand Down
8 changes: 4 additions & 4 deletions tests/configuration/tests/suite/examples/api_key_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ filter_chains:
registry
.register("api_key", FilterFactory::Http(Arc::new(ApiKeyFilter::from_config)))
.expect("duplicate filter name");
let addr = start_proxy_with_registry(&config, &registry);
let proxy = start_proxy_with_registry(&config, &registry);

let raw = http_send(
&addr,
proxy.addr(),
"GET / HTTP/1.1\r\n\
Host: localhost\r\n\
X-Api-Key: secret-1\r\n\
Expand All @@ -61,15 +61,15 @@ filter_chains:
assert_eq!(parse_body(&raw), "protected", "valid API key should reach backend");

let raw = http_send(
&addr,
proxy.addr(),
"GET / HTTP/1.1\r\n\
Host: localhost\r\n\
X-Api-Key: wrong\r\n\
Connection: close\r\n\r\n",
);
assert_eq!(parse_status(&raw), 401, "invalid API key should return 401");

let (status, _) = http_get(&addr, "/", None);
let (status, _) = http_get(proxy.addr(), "/", None);
assert_eq!(status, 401, "missing API key should return 401");
}

Expand Down
8 changes: 4 additions & 4 deletions tests/configuration/tests/suite/examples/max_body_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ filter_chains:
FilterFactory::Http(Arc::new(MaxBodyGuard::from_config)),
)
.expect("duplicate filter name");
let addr = start_proxy_with_registry(&config, &registry);
let proxy = start_proxy_with_registry(&config, &registry);

let raw = http_send(
&addr,
proxy.addr(),
"POST / HTTP/1.1\r\n\
Host: localhost\r\n\
Content-Length: 5\r\n\
Expand All @@ -65,15 +65,15 @@ filter_chains:
assert_eq!(parse_body(&raw), "accepted", "small body response should match backend");

let raw = http_send(
&addr,
proxy.addr(),
"POST / HTTP/1.1\r\n\
Host: localhost\r\n\
Content-Length: 2048\r\n\
Connection: close\r\n\r\n",
);
assert_eq!(parse_status(&raw), 413, "large body should be rejected with 413");

let (status, body) = http_get(&addr, "/", None);
let (status, body) = http_get(proxy.addr(), "/", None);
assert_eq!(status, 200, "GET without content-length should be accepted");
assert_eq!(body, "accepted", "GET response should match backend");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,17 @@ fn access_logging() {
proxy_port,
HashMap::from([("127.0.0.1:3000", backend_port)]),
);
let addr = start_proxy(&config);
let proxy = start_proxy(&config);

let raw = http_send(&addr, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
let raw = http_send(
proxy.addr(),
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n",
);
assert_eq!(parse_status(&raw), 200, "basic request should return 200");
assert_eq!(parse_body(&raw), "logged", "response body should match backend");

let raw = http_send(
&addr,
proxy.addr(),
"GET / HTTP/1.1\r\n\
Host: localhost\r\n\
X-Request-Id: trace-abc\r\n\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ fn logging() {
proxy_port,
HashMap::from([("127.0.0.1:3000", backend_port)]),
);
let addr = start_proxy(&config);
let proxy = start_proxy(&config);

let raw = http_send(&addr, "GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n");
let raw = http_send(
proxy.addr(),
"GET / HTTP/1.1\r\nHost: localhost\r\nConnection: close\r\n\r\n",
);
assert_eq!(parse_status(&raw), 200, "request without X-Trace-Id should succeed");

let raw = http_send(
&addr,
proxy.addr(),
"GET / HTTP/1.1\r\n\
Host: localhost\r\n\
X-Trace-Id: my-trace-42\r\n\
Expand All @@ -40,7 +43,7 @@ fn logging() {
);

let raw = http_send(
&addr,
proxy.addr(),
"GET / HTTP/1.1\r\n\
Host: localhost\r\n\
X-Trace-Id: other-99\r\n\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,49 +10,51 @@ use praxis_core::config::Config;
use praxis_filter::{
BodyAccess, BodyMode, FilterAction, FilterError, FilterFactory, FilterRegistry, HttpFilter, HttpFilterContext,
};
use praxis_test_utils::{free_port, http_post, http_send, parse_status, start_backend, start_proxy_with_registry};
use praxis_test_utils::{
ProxyGuard, free_port, http_post, http_send, parse_status, start_backend, start_proxy_with_registry,
};

// -----------------------------------------------------------------------------
// Tests
// -----------------------------------------------------------------------------

#[test]
fn stream_buffer_within_limit_succeeds() {
let addr = setup(256);
let proxy = setup(256);
let body = "a".repeat(100);
let (status, _) = http_post(&addr, "/", &body);
let (status, _) = http_post(proxy.addr(), "/", &body);
assert_eq!(status, 200, "body within limit should be accepted");
}

#[test]
fn stream_buffer_at_exact_limit_succeeds() {
let addr = setup(64);
let proxy = setup(64);
let body = "b".repeat(64);
let (status, _) = http_post(&addr, "/", &body);
let (status, _) = http_post(proxy.addr(), "/", &body);
assert_eq!(status, 200, "body at exact limit should be accepted");
}

#[test]
fn stream_buffer_exceeding_limit_returns_413() {
let addr = setup(64);
let proxy = setup(64);
let body = "c".repeat(128);
let (status, _) = http_post(&addr, "/", &body);
let (status, _) = http_post(proxy.addr(), "/", &body);
assert_eq!(status, 413, "body exceeding limit should be rejected with 413");
}

#[test]
fn stream_buffer_one_byte_over_returns_413() {
let addr = setup(64);
let proxy = setup(64);
let body = "d".repeat(65);
let (status, _) = http_post(&addr, "/", &body);
let (status, _) = http_post(proxy.addr(), "/", &body);
assert_eq!(status, 413, "body one byte over limit should be rejected with 413");
}

#[test]
fn stream_buffer_empty_body_succeeds() {
let addr = setup(64);
let proxy = setup(64);
let raw = http_send(
&addr,
proxy.addr(),
"POST / HTTP/1.1\r\n\
Host: localhost\r\n\
Content-Length: 0\r\n\
Expand Down Expand Up @@ -113,8 +115,8 @@ impl HttpFilter for TinyStreamBufferFilter {
}
}

/// Start a proxy with a tiny stream buffer filter and return the address.
fn setup(max_bytes: usize) -> String {
/// Start a proxy with a tiny stream buffer filter and return the proxy guard.
fn setup(max_bytes: usize) -> ProxyGuard {
let backend_port = start_backend("ok");
let proxy_port = free_port();
let yaml = format!(
Comment thread
esnible marked this conversation as resolved.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ fn conditional_filters() {
proxy_port,
HashMap::from([("127.0.0.1:3000", backend_port)]),
);
let addr = start_proxy(&config);
let proxy = start_proxy(&config);
let raw = http_send(
&addr,
proxy.addr(),
"POST /api/items HTTP/1.1\r\n\
Host: localhost\r\n\
Content-Length: 0\r\n\
Expand All @@ -35,7 +35,7 @@ fn conditional_filters() {
);
assert_eq!(parse_body(&raw), "ok", "POST response body should match backend");
let raw_get = http_send(
&addr,
proxy.addr(),
"GET /api/items HTTP/1.1\r\n\
Host: localhost\r\n\
Connection: close\r\n\r\n",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ fn default_config_root_returns_200() {
let proxy_port = free_port();
let yaml = DEFAULT_CONFIG.replace("0.0.0.0:8080", &format!("127.0.0.1:{proxy_port}"));
let config = Config::from_yaml(&yaml).unwrap();
let addr = start_proxy(&config);
let (status, body) = http_get(&addr, "/", None);
let proxy = start_proxy(&config);
let (status, body) = http_get(proxy.addr(), "/", None);
assert_eq!(status, 200, "default config root should return 200");
let json: Value = serde_json::from_str(&body).expect("response body should be valid JSON");
assert_eq!(json["status"], "ok", "status field should be 'ok'");
Expand All @@ -36,8 +36,8 @@ fn default_config_other_path_returns_404() {
let proxy_port = free_port();
let yaml = DEFAULT_CONFIG.replace("0.0.0.0:8080", &format!("127.0.0.1:{proxy_port}"));
let config = Config::from_yaml(&yaml).unwrap();
let addr = start_proxy(&config);
let (status, body) = http_get(&addr, "/anything", None);
let proxy = start_proxy(&config);
let (status, body) = http_get(proxy.addr(), "/anything", None);
assert_eq!(status, 404, "non-root path should return 404");
assert!(body.contains("not found"), "404 body should contain 'not found'");
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ fn basic_reverse_proxy() {
proxy_port,
HashMap::from([("127.0.0.1:3000", backend_port)]),
);
let addr = praxis_test_utils::start_proxy(&config);
let (status, body) = http_get(&addr, "/", None);
let proxy = praxis_test_utils::start_proxy(&config);
let (status, body) = http_get(proxy.addr(), "/", None);
assert_eq!(status, 200, "basic reverse proxy should return 200");
assert_eq!(body, "hello", "proxy should forward backend response");
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ fn canary_routing() {
proxy_port,
HashMap::from([("127.0.0.1:3001", port_stable), ("127.0.0.1:3002", port_canary)]),
);
let addr = start_proxy(&config);
let proxy = start_proxy(&config);
let total = 200u32;
let mut stable_count = 0u32;
let mut canary_count = 0u32;
for _ in 0..total {
let (status, body) = http_get(&addr, "/", None);
let (status, body) = http_get(proxy.addr(), "/", None);
assert_eq!(status, 200, "canary request should return 200");
match body.as_str() {
"stable" => stable_count += 1,
Expand Down
Loading
Loading