Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
35 changes: 25 additions & 10 deletions tests/configuration/tests/suite/examples/ai/model_to_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,23 @@
//! Model to header filter example tests.

use praxis_core::config::Config;
use praxis_test_utils::{free_port, http_post, start_backend, start_proxy};
use praxis_test_utils::{free_port, http_post, start_backend_with_shutdown, start_proxy};

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

#[test]
fn model_to_header_routes_by_model_field() {
let port_a = start_backend("model-a-response");
let port_default = start_backend("default-response");
let backend_a = start_backend_with_shutdown("model-a-response");
let backend_default = start_backend_with_shutdown("default-response");
let proxy_port = free_port();
let yaml = make_yaml(proxy_port, "mistral-large-latest", port_a, port_default);
let yaml = make_yaml(
proxy_port,
"mistral-large-latest",
backend_a.port(),
backend_default.port(),
);
let config = Config::from_yaml(&yaml).unwrap();
let proxy = start_proxy(&config);
let (status, body) = http_post(
Expand All @@ -32,10 +37,15 @@ fn model_to_header_routes_by_model_field() {

#[test]
fn model_to_header_falls_through_on_unknown_model() {
let port_a = start_backend("model-a-response");
let port_default = start_backend("default-response");
let backend_a = start_backend_with_shutdown("model-a-response");
let backend_default = start_backend_with_shutdown("default-response");
let proxy_port = free_port();
let yaml = make_yaml(proxy_port, "mistral-large-latest", port_a, port_default);
let yaml = make_yaml(
proxy_port,
"mistral-large-latest",
backend_a.port(),
backend_default.port(),
);
let config = Config::from_yaml(&yaml).unwrap();
let proxy = start_proxy(&config);
let (status, body) = http_post(proxy.addr(), "/v1/chat", r#"{"model":"unknown","messages":[]}"#);
Expand All @@ -45,10 +55,15 @@ fn model_to_header_falls_through_on_unknown_model() {

#[test]
fn model_to_header_continues_without_model_field() {
let port_a = start_backend("model-a-response");
let port_default = start_backend("default-response");
let backend_a = start_backend_with_shutdown("model-a-response");
let backend_default = start_backend_with_shutdown("default-response");
let proxy_port = free_port();
let yaml = make_yaml(proxy_port, "mistral-large-latest", port_a, port_default);
let yaml = make_yaml(
proxy_port,
"mistral-large-latest",
backend_a.port(),
backend_default.port(),
);
let config = Config::from_yaml(&yaml).unwrap();
let proxy = start_proxy(&config);
let (status, body) = http_post(proxy.addr(), "/v1/chat", r#"{"messages":[]}"#);
Expand Down
5 changes: 3 additions & 2 deletions tests/configuration/tests/suite/examples/api_key_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use praxis_filter::{
FilterAction, FilterError, FilterFactory, FilterRegistry, HttpFilter, HttpFilterContext, Rejection,
};
use praxis_test_utils::{
free_port, http_get, http_send, parse_body, parse_status, start_backend, start_proxy_with_registry,
free_port, http_get, http_send, parse_body, parse_status, start_backend_with_shutdown, start_proxy_with_registry,
};

// -----------------------------------------------------------------------------
Expand All @@ -19,7 +19,8 @@ use praxis_test_utils::{

#[test]
fn api_key_filter() {
let backend_port = start_backend("protected");
let backend = start_backend_with_shutdown("protected");
let backend_port = backend.port();
let proxy_port = free_port();
let yaml = format!(
r#"
Expand Down
5 changes: 3 additions & 2 deletions tests/configuration/tests/suite/examples/max_body_guard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use praxis_filter::{
FilterAction, FilterError, FilterFactory, FilterRegistry, HttpFilter, HttpFilterContext, Rejection,
};
use praxis_test_utils::{
free_port, http_get, http_send, parse_body, parse_status, start_backend, start_proxy_with_registry,
free_port, http_get, http_send, parse_body, parse_status, start_backend_with_shutdown, start_proxy_with_registry,
};

// -----------------------------------------------------------------------------
Expand All @@ -19,7 +19,8 @@ use praxis_test_utils::{

#[test]
fn max_body_guard() {
let backend_port = start_backend("accepted");
let backend = start_backend_with_shutdown("accepted");
let backend_port = backend.port();
let proxy_port = free_port();
let yaml = format!(
r#"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,22 @@

use std::collections::HashMap;

use praxis_test_utils::{free_port, http_send, parse_body, parse_header, parse_status, start_backend, start_proxy};
use praxis_test_utils::{
free_port, http_send, parse_body, parse_header, parse_status, start_backend_with_shutdown, start_proxy,
};

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

#[test]
fn access_logging() {
let backend_port = start_backend("logged");
let backend = start_backend_with_shutdown("logged");
let proxy_port = free_port();
let config = crate::example_utils::load_example_config(
"observability/access-logging.yaml",
proxy_port,
HashMap::from([("127.0.0.1:3000", backend_port)]),
HashMap::from([("127.0.0.1:3000", backend.port())]),
);
let proxy = start_proxy(&config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

use std::collections::HashMap;

use praxis_test_utils::{free_port, http_send, parse_header, parse_status, start_backend, start_proxy};
use praxis_test_utils::{free_port, http_send, parse_header, parse_status, start_backend_with_shutdown, start_proxy};

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

#[test]
fn logging() {
let backend_port = start_backend("ok");
let backend = start_backend_with_shutdown("ok");
let proxy_port = free_port();
let config = crate::example_utils::load_example_config(
"observability/logging.yaml",
proxy_port,
HashMap::from([("127.0.0.1:3000", backend_port)]),
HashMap::from([("127.0.0.1:3000", backend.port())]),
);
let proxy = start_proxy(&config);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@
//! Multi-listener example tests.

use praxis_core::config::Config;
use praxis_test_utils::{free_port, http_get, start_backend, start_proxy, wait_for_tcp};
use praxis_test_utils::{free_port, http_get, start_backend_with_shutdown, start_proxy, wait_for_tcp};

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

#[test]
fn multi_listener() {
let api_port = start_backend("api");
let web_port = start_backend("web");
let api_backend = start_backend_with_shutdown("api");
let web_backend = start_backend_with_shutdown("web");
let api_port = api_backend.port();
let web_port = web_backend.port();
let http_port = free_port();
let admin_port = free_port();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ use praxis_filter::{
BodyAccess, BodyMode, FilterAction, FilterError, FilterFactory, FilterRegistry, HttpFilter, HttpFilterContext,
};
use praxis_test_utils::{
ProxyGuard, free_port, http_post, http_send, parse_status, start_backend, start_proxy_with_registry,
BackendGuard, ProxyGuard, free_port, http_post, http_send, parse_status, start_backend_with_shutdown,
start_proxy_with_registry,
};

// -----------------------------------------------------------------------------
Expand All @@ -20,39 +21,39 @@ use praxis_test_utils::{

#[test]
fn stream_buffer_within_limit_succeeds() {
let proxy = setup(256);
let (_backend, proxy) = setup(256);
let body = "a".repeat(100);
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 proxy = setup(64);
let (_backend, proxy) = setup(64);
let body = "b".repeat(64);
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 proxy = setup(64);
let (_backend, proxy) = setup(64);
let body = "c".repeat(128);
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 proxy = setup(64);
let (_backend, proxy) = setup(64);
let body = "d".repeat(65);
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 proxy = setup(64);
let (_backend, proxy) = setup(64);
let raw = http_send(
proxy.addr(),
"POST / HTTP/1.1\r\n\
Expand Down Expand Up @@ -115,9 +116,10 @@ impl HttpFilter for TinyStreamBufferFilter {
}
}

/// 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");
/// Start a proxy with a tiny stream buffer filter and return the backend and proxy guards.
fn setup(max_bytes: usize) -> (BackendGuard, ProxyGuard) {
let backend = start_backend_with_shutdown("ok");
let backend_port = backend.port();
let proxy_port = free_port();
let yaml = format!(
r#"
Expand Down Expand Up @@ -149,5 +151,5 @@ filter_chains:
FilterFactory::Http(Arc::new(TinyStreamBufferFilter::from_config)),
)
.expect("duplicate filter name");
start_proxy_with_registry(&config, &registry)
(backend, start_proxy_with_registry(&config, &registry))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

use std::collections::HashMap;

use praxis_test_utils::{free_port, http_send, parse_body, parse_status, start_backend, start_proxy};
use praxis_test_utils::{free_port, http_send, parse_body, parse_status, start_backend_with_shutdown, start_proxy};

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

#[test]
fn conditional_filters() {
let backend_port = start_backend("ok");
let backend = start_backend_with_shutdown("ok");
let proxy_port = free_port();
let config = crate::example_utils::load_example_config(
"pipeline/conditional-filters.yaml",
proxy_port,
HashMap::from([("127.0.0.1:3000", backend_port)]),
HashMap::from([("127.0.0.1:3000", backend.port())]),
);
let proxy = start_proxy(&config);
let raw = http_send(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@

use std::collections::HashMap;

use praxis_test_utils::{free_port, http_get, start_backend};
use praxis_test_utils::{free_port, http_get, start_backend_with_shutdown};

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

#[test]
fn basic_reverse_proxy() {
let backend_port = start_backend("hello");
let backend = start_backend_with_shutdown("hello");
let proxy_port = free_port();
let config = crate::example_utils::load_example_config(
"traffic-management/basic-reverse-proxy.yaml",
proxy_port,
HashMap::from([("127.0.0.1:3000", backend_port)]),
HashMap::from([("127.0.0.1:3000", backend.port())]),
);
let proxy = praxis_test_utils::start_proxy(&config);
let (status, body) = http_get(proxy.addr(), "/", None);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,24 @@

use std::collections::HashMap;

use praxis_test_utils::{free_port, http_get, start_backend, start_proxy};
use praxis_test_utils::{free_port, http_get, start_backend_with_shutdown, start_proxy};

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

#[test]
fn canary_routing() {
let port_stable = start_backend("stable");
let port_canary = start_backend("canary");
let backend_stable = start_backend_with_shutdown("stable");
let backend_canary = start_backend_with_shutdown("canary");
let proxy_port = free_port();
let config = crate::example_utils::load_example_config(
"traffic-management/canary-routing.yaml",
proxy_port,
HashMap::from([("127.0.0.1:3001", port_stable), ("127.0.0.1:3002", port_canary)]),
HashMap::from([
("127.0.0.1:3001", backend_stable.port()),
("127.0.0.1:3002", backend_canary.port()),
]),
);
let proxy = start_proxy(&config);
let total = 200u32;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,25 @@

use std::{collections::HashMap, thread, time::Duration};

use praxis_test_utils::{free_port, http_get, start_backend, start_proxy, start_slow_backend};
use praxis_test_utils::{free_port, http_get, start_backend_with_shutdown, start_proxy, start_slow_backend};

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

#[test]
fn least_connections() {
let port_a = start_backend("lc-a");
let port_b = start_backend("lc-b");
let port_c = start_backend("lc-c");
let backend_a = start_backend_with_shutdown("lc-a");
let backend_b = start_backend_with_shutdown("lc-b");
let backend_c = start_backend_with_shutdown("lc-c");
let proxy_port = free_port();
let config = crate::example_utils::load_example_config(
"traffic-management/least-connections.yaml",
proxy_port,
HashMap::from([
("127.0.0.1:3001", port_a),
("127.0.0.1:3002", port_b),
("127.0.0.1:3003", port_c),
("127.0.0.1:3001", backend_a.port()),
("127.0.0.1:3002", backend_b.port()),
("127.0.0.1:3003", backend_c.port()),
]),
);
let proxy = start_proxy(&config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,27 +5,27 @@

use std::collections::HashMap;

use praxis_test_utils::{free_port, http_get_retry, start_backend, start_proxy};
use praxis_test_utils::{free_port, http_get_retry, start_backend_with_shutdown, start_proxy};

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

#[test]
fn path_based_routing() {
let api_port = start_backend("api");
let static_port = start_backend("static");
let default_port = start_backend("default");
let api_backend = start_backend_with_shutdown("api");
let static_backend = start_backend_with_shutdown("static");
let default_backend = start_backend_with_shutdown("default");
let proxy_port = free_port();
let config = crate::example_utils::load_example_config(
"traffic-management/path-based-routing.yaml",
proxy_port,
HashMap::from([
("127.0.0.1:3001", api_port),
("127.0.0.1:3002", api_port),
("127.0.0.1:3003", api_port),
("127.0.0.1:4000", static_port),
("127.0.0.1:5000", default_port),
("127.0.0.1:3001", api_backend.port()),
("127.0.0.1:3002", api_backend.port()),
("127.0.0.1:3003", api_backend.port()),
("127.0.0.1:4000", static_backend.port()),
("127.0.0.1:5000", default_backend.port()),
]),
);
let proxy = start_proxy(&config);
Expand Down
Loading
Loading