Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
#[cfg(test)]
mod server_tests {
use crate::core::server::proxy;
use serde_json::json;
#[test]
fn test_get_destination_path_basic() {
let result = proxy::get_destination_path("/v1/messages", "/v1");
assert_eq!(result, "/messages");
}
#[test]
fn test_get_destination_path_with_subpath() {
let result = proxy::get_destination_path("/v1/messages/threads/123", "/v1");
assert_eq!(result, "/messages/threads/123");
}
#[test]
fn test_get_destination_path_no_prefix() {
let result = proxy::get_destination_path("/messages", "");
assert_eq!(result, "/messages");
}
#[test]
fn test_get_destination_path_different_prefix() {
let result = proxy::get_destination_path("/api/v1/messages", "/api/v1");
assert_eq!(result, "/messages");
}
#[test]
fn test_get_destination_path_empty_prefix() {
let result = proxy::get_destination_path("/messages", "/v1");
assert_eq!(result, "/messages");
}
#[test]
fn test_messages_in_cors_whitelist() {
let whitelisted_paths = ["/", "/openapi.json", "/favicon.ico", "/messages"];
assert!(whitelisted_paths.contains(&"/messages"));
}
#[test]
fn test_messages_in_main_whitelist() {
let whitelisted_paths = [
"/",
"/openapi.json",
"/favicon.ico",
"/docs/swagger-ui.css",
"/docs/swagger-ui-bundle.js",
"/docs/swagger-ui-standalone-preset.js",
"/messages",
];
assert!(whitelisted_paths.contains(&"/messages"));
}
#[test]
fn test_messages_subpath_not_in_exact_whitelist() {
let whitelisted_paths = ["/", "/openapi.json", "/favicon.ico", "/messages"];
// Only exact match
assert!(!whitelisted_paths.contains(&"/messages/threads"));
assert!(!whitelisted_paths.contains(&"/messages/api"));
}
#[test]
fn test_proxy_config_creation() {
let config = proxy::ProxyConfig {
prefix: "/v1".to_string(),
proxy_api_key: "test-key".to_string(),
trusted_hosts: vec![vec!["localhost".to_string()]],
host: "localhost".to_string(),
port: 1337,
enable_server_tool_execution: false,
cors_enabled: true,
};
assert_eq!(config.prefix, "/v1");
assert_eq!(config.proxy_api_key, "test-key");
assert_eq!(config.trusted_hosts.len(), 1);
assert_eq!(config.host, "localhost");
assert_eq!(config.port, 1337);
}
#[test]
fn test_proxy_config_default() {
let config = proxy::ProxyConfig {
prefix: "".to_string(),
proxy_api_key: "".to_string(),
trusted_hosts: vec![],
host: "127.0.0.1".to_string(),
port: 8080,
enable_server_tool_execution: false,
cors_enabled: true,
};
assert_eq!(config.prefix, "");
assert_eq!(config.proxy_api_key, "");
assert_eq!(config.trusted_hosts.len(), 0);
assert_eq!(config.host, "127.0.0.1");
assert_eq!(config.port, 8080);
}
#[test]
fn test_allowed_methods() {
let allowed_methods = ["GET", "POST", "PUT", "DELETE", "OPTIONS", "PATCH"];
assert!(allowed_methods.contains(&"POST"));
assert!(allowed_methods.contains(&"GET"));
assert!(allowed_methods.contains(&"OPTIONS"));
}
#[test]
fn test_allowed_headers() {
let allowed_headers = [
"accept",
"authorization",
"content-type",
"host",
"origin",
"user-agent",
"x-api-key",
];
assert!(allowed_headers.contains(&"authorization"));
assert!(allowed_headers.contains(&"content-type"));
assert!(allowed_headers.contains(&"x-api-key"));
}
// Tests for X-Api-Key header authentication support
// The proxy now accepts either Authorization: Bearer <token> or X-Api-Key: <token>
#[test]
fn test_bearer_token_extraction() {
let api_key = "test-secret-key";
let auth_header = "Bearer test-secret-key";
let auth_valid = auth_header
.strip_prefix("Bearer ")
.map(|token| token == api_key)
.unwrap_or(false);
assert!(auth_valid);
}
#[test]
fn test_bearer_token_extraction_invalid() {
let api_key = "test-secret-key";
let auth_header = "Bearer wrong-key";
let auth_valid = auth_header
.strip_prefix("Bearer ")
.map(|token| token == api_key)
.unwrap_or(false);
assert!(!auth_valid);
}
#[test]
fn test_bearer_token_extraction_missing_prefix() {
let api_key = "test-secret-key";
let auth_header = "test-secret-key"; // Missing "Bearer " prefix
let auth_valid = auth_header
.strip_prefix("Bearer ")
.map(|token| token == api_key)
.unwrap_or(false);
assert!(!auth_valid);
}
#[test]
fn test_x_api_key_validation() {
let api_key = "test-secret-key";
let x_api_key_header = "test-secret-key";
let api_key_valid = x_api_key_header == api_key;
assert!(api_key_valid);
}
#[test]
fn test_x_api_key_validation_invalid() {
let api_key = "test-secret-key";
let x_api_key_header = "wrong-key";
let api_key_valid = x_api_key_header == api_key;
assert!(!api_key_valid);
}
#[test]
fn test_auth_either_header_valid_bearer() {
let api_key = "test-secret-key";
let auth_header = Some("Bearer test-secret-key");
let x_api_key_header: Option<&str> = None;
let auth_valid = auth_header
.and_then(|auth_str| auth_str.strip_prefix("Bearer "))
.map(|token| token == api_key)
.unwrap_or(false);
let api_key_valid = x_api_key_header.map(|key| key == api_key).unwrap_or(false);
assert!(auth_valid || api_key_valid);
}
#[test]
fn test_auth_either_header_valid_x_api_key() {
let api_key = "test-secret-key";
let auth_header: Option<&str> = None;
let x_api_key_header = Some("test-secret-key");
let auth_valid = auth_header
.and_then(|auth_str| auth_str.strip_prefix("Bearer "))
.map(|token| token == api_key)
.unwrap_or(false);
let api_key_valid = x_api_key_header.map(|key| key == api_key).unwrap_or(false);
assert!(auth_valid || api_key_valid);
}
#[test]
fn test_auth_both_headers_missing() {
let api_key = "test-secret-key";
let auth_header: Option<&str> = None;
let x_api_key_header: Option<&str> = None;
let auth_valid = auth_header
.and_then(|auth_str| auth_str.strip_prefix("Bearer "))
.map(|token| token == api_key)
.unwrap_or(false);
let api_key_valid = x_api_key_header.map(|key| key == api_key).unwrap_or(false);
assert!(!auth_valid && !api_key_valid);
}
#[test]
fn test_auth_both_headers_invalid() {
let api_key = "test-secret-key";
let auth_header = Some("Bearer wrong-key");
let x_api_key_header = Some("also-wrong-key");
let auth_valid = auth_header
.and_then(|auth_str| auth_str.strip_prefix("Bearer "))
.map(|token| token == api_key)
.unwrap_or(false);
let api_key_valid = x_api_key_header.map(|key| key == api_key).unwrap_or(false);
assert!(!auth_valid && !api_key_valid);
}
#[test]
fn test_auth_both_headers_one_valid() {
let api_key = "test-secret-key";
// Bearer is wrong but X-Api-Key is correct
let auth_header = Some("Bearer wrong-key");
let x_api_key_header = Some("test-secret-key");
let auth_valid = auth_header
.and_then(|auth_str| auth_str.strip_prefix("Bearer "))
.map(|token| token == api_key)
.unwrap_or(false);
let api_key_valid = x_api_key_header.map(|key| key == api_key).unwrap_or(false);
// Should pass if either is valid
assert!(auth_valid || api_key_valid);
}
#[test]
fn test_x_api_key_in_cors_allowed_headers() {
// Verify x-api-key is in the CORS allowed headers list used by the proxy
let allowed_headers = [
"accept",
"accept-language",
"authorization",
"cache-control",
"connection",
"content-type",
"dnt",
"host",
"if-modified-since",
"keep-alive",
"origin",
"user-agent",
"x-api-key",
"x-csrf-token",
"x-forwarded-for",
"x-forwarded-host",
"x-forwarded-proto",
"x-requested-with",
"x-stainless-arch",
"x-stainless-lang",
"x-stainless-os",
"x-stainless-package-version",
"x-stainless-retry-count",
"x-stainless-runtime",
"x-stainless-runtime-version",
"x-stainless-timeout",
];
assert!(allowed_headers.contains(&"x-api-key"));
}
#[test]
fn adds_string_type_for_description_only_leaf() {
let mut schema = json!({
"type": "object",
"properties": {
"url": {
"description": "field property config"
}
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(schema["properties"]["url"]["type"], json!("string"));
}
#[test]
fn adds_empty_properties_for_object_without_properties() {
let mut schema = json!({
"type": "object"
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(schema["properties"], json!({}));
}
#[test]
fn normalizes_nested_object_and_array_leaves() {
let mut schema = json!({
"type": "object",
"properties": {
"payload": {
"type": "object",
"properties": {
"title": {
"description": "Title text"
}
}
},
"filters": {
"type": "object"
},
"items": {
"type": "array",
"items": {
"type": "object"
}
}
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(
schema["properties"]["payload"]["properties"]["title"]["type"],
json!("string")
);
assert_eq!(schema["properties"]["filters"]["properties"], json!({}));
assert_eq!(
schema["properties"]["items"]["items"]["properties"],
json!({})
);
}
#[test]
fn leaves_existing_typed_or_container_nodes_unchanged() {
let mut schema = json!({
"type": "object",
"properties": {
"name": {
"description": "already typed",
"type": "integer"
},
"container": {
"description": "container node",
"properties": {
"child": {
"description": "leaf child"
}
}
}
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(schema["properties"]["name"]["type"], json!("integer"));
assert!(schema["properties"]["container"].get("type").is_none());
assert_eq!(
schema["properties"]["container"]["properties"]["child"]["type"],
json!("string"),
);
}
#[test]
fn expands_bare_string_shorthand_in_properties() {
let mut schema = json!({
"type": "object",
"properties": {
"name": "string",
"count": "integer",
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(schema["properties"]["name"], json!({"type": "string"}));
assert_eq!(schema["properties"]["count"], json!({"type": "integer"}));
}
#[test]
fn expands_bare_string_shorthand_in_array_items() {
let mut schema = json!({
"type": "object",
"properties": {
"tags": { "type": "array", "items": "string" }
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(
schema["properties"]["tags"]["items"],
json!({"type": "string"})
);
}
#[test]
fn expands_bare_string_shorthand_in_any_of() {
let mut schema = json!({
"type": "object",
"properties": {
"value": { "anyOf": ["string", "number"] }
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(
schema["properties"]["value"]["anyOf"],
json!([{"type": "string"}, {"type": "number"}])
);
}
#[test]
fn does_not_coerce_enum_literal_strings() {
let mut schema = json!({
"type": "object",
"properties": {
"mode": { "type": "string", "enum": ["string", "number", "fast"] }
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(
schema["properties"]["mode"]["enum"],
json!(["string", "number", "fast"])
);
}
#[test]
fn leaves_existing_valid_object_schema_unchanged() {
let mut schema = json!({
"type": "object",
"properties": {
"payload": {
"type": "object",
"properties": {
"name": {
"type": "string"
}
},
"required": ["name"]
}
}
});
let original = schema.clone();
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(schema, original);
}
#[test]
fn normalizes_mixed_schema_without_altering_valid_fields() {
let mut schema = json!({
"type": "object",
"properties": {
"count": {
"type": "integer"
},
"title": {
"description": "A title"
},
"metadata": {
"type": "object",
"description": "Optional metadata"
}
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(schema["properties"]["count"]["type"], json!("integer"));
assert_eq!(schema["properties"]["title"]["type"], json!("string"));
assert_eq!(
schema["properties"]["metadata"]["description"],
json!("Optional metadata")
);
assert_eq!(schema["properties"]["metadata"]["properties"], json!({}));
}
#[test]
fn strips_broken_string_formats_for_llamacpp_grammar() {
let mut schema = json!({
"type": "object",
"properties": {
"start": { "type": "string", "format": "date-time" },
"stop": { "type": "string", "format": "date" },
"at": { "type": "string", "format": "time" }
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert!(schema["properties"]["start"].get("format").is_none());
assert!(schema["properties"]["stop"].get("format").is_none());
assert!(schema["properties"]["at"].get("format").is_none());
assert_eq!(schema["properties"]["start"]["type"], json!("string"));
}
#[test]
fn preserves_harmless_string_formats() {
let mut schema = json!({
"type": "object",
"properties": {
"id": { "type": "string", "format": "uuid" },
"url": { "type": "string", "format": "uri" }
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(schema["properties"]["id"]["format"], json!("uuid"));
assert_eq!(schema["properties"]["url"]["format"], json!("uri"));
}
#[test]
fn strips_pcre_shorthand_patterns() {
let mut schema = json!({
"type": "object",
"properties": {
"when": { "type": "string", "pattern": "^\\d{4}-\\d{2}-\\d{2}$" },
"alpha": { "type": "string", "pattern": "^[A-Z]+$" }
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert!(schema["properties"]["when"].get("pattern").is_none());
assert_eq!(schema["properties"]["alpha"]["pattern"], json!("^[A-Z]+$"));
}
#[test]
fn strips_broken_format_when_type_missing_or_non_string() {
let mut schema = json!({
"type": "object",
"properties": {
"until": { "format": "date-time", "description": "iso end" },
"nullable": { "type": ["string", "null"], "format": "date" }
}
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert!(schema["properties"]["until"].get("format").is_none());
assert_eq!(schema["properties"]["until"]["type"], json!("string"));
assert!(schema["properties"]["nullable"].get("format").is_none());
}
// ── Pure helper coverage ───────────────────────────────────────────────
#[test]
fn http_status_indicates_api_key_retry_matrix() {
use hyper::StatusCode;
assert!(crate::core::openai_schema::http_status_indicates_api_key_retry(
StatusCode::UNAUTHORIZED
));
assert!(crate::core::openai_schema::http_status_indicates_api_key_retry(
StatusCode::FORBIDDEN
));
assert!(crate::core::openai_schema::http_status_indicates_api_key_retry(
StatusCode::TOO_MANY_REQUESTS
));
assert!(!crate::core::openai_schema::http_status_indicates_api_key_retry(StatusCode::OK));
assert!(!crate::core::openai_schema::http_status_indicates_api_key_retry(
StatusCode::BAD_REQUEST
));
assert!(!crate::core::openai_schema::http_status_indicates_api_key_retry(
StatusCode::INTERNAL_SERVER_ERROR
));
}
#[test]
fn text_parts_to_content_empty() {
let out = proxy::text_parts_to_content(&[]);
assert_eq!(out, json!(""));
}
#[test]
fn text_parts_to_content_single_text_collapses_to_string() {
let parts = vec![json!({"type": "text", "text": "hello"})];
let out = proxy::text_parts_to_content(&parts);
assert_eq!(out, json!("hello"));
}
#[test]
fn text_parts_to_content_multiple_returns_array() {
let parts = vec![
json!({"type": "text", "text": "a"}),
json!({"type": "image_url", "image_url": {"url": "data:..."}}),
];
let out = proxy::text_parts_to_content(&parts);
assert!(out.is_array());
assert_eq!(out.as_array().unwrap().len(), 2);
}
#[test]
fn extract_tool_result_content_string() {
let v = json!("plain result");
assert_eq!(proxy::extract_tool_result_content(Some(&v)), "plain result");
}
#[test]
fn extract_tool_result_content_array_of_text() {
let v = json!([
{"type": "text", "text": "first"},
{"type": "text", "text": "second"},
{"type": "image", "source": {}}
]);
assert_eq!(
proxy::extract_tool_result_content(Some(&v)),
"first\nsecond"
);
}
#[test]
fn extract_tool_result_content_none_yields_empty() {
assert_eq!(proxy::extract_tool_result_content(None), "");
}
#[test]
fn extract_tool_result_content_other_value_stringified() {
let v = json!({"foo": "bar"});
// Non-string, non-array values get JSON-stringified.
let s = proxy::extract_tool_result_content(Some(&v));
assert!(s.contains("foo"));
}
#[test]
fn convert_media_block_image_with_source() {
let block = json!({
"type": "image",
"source": {
"data": "BASE64DATA",
"media_type": "image/png"
}
});
let mut parts = Vec::new();
proxy::convert_media_block(&block, &mut parts);
assert_eq!(parts.len(), 1);
assert_eq!(parts[0]["type"], "image_url");
assert_eq!(
parts[0]["image_url"]["url"],
"data:image/png;base64,BASE64DATA"
);
}
#[test]
fn convert_media_block_image_missing_data_skipped() {
let block = json!({"type": "image", "source": {"media_type": "image/png"}});
let mut parts = Vec::new();
proxy::convert_media_block(&block, &mut parts);
assert!(parts.is_empty());
}
#[test]
fn convert_media_block_text_fallback() {
let block = json!({"type": "unknown", "text": "fallback"});
let mut parts = Vec::new();
proxy::convert_media_block(&block, &mut parts);
assert_eq!(parts.len(), 1);
assert_eq!(parts[0]["type"], "text");
assert_eq!(parts[0]["text"], "fallback");
}
#[test]
fn convert_messages_simple_string_content() {
let msgs = json!([
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "hello"}
]);
let out = proxy::convert_messages(&msgs, None).unwrap();
let arr = out.as_array().unwrap();
assert_eq!(arr.len(), 2);
assert_eq!(arr[0]["role"], "user");
assert_eq!(arr[0]["content"], "hi");
assert_eq!(arr[1]["role"], "assistant");
}
#[test]
fn convert_messages_with_string_system_prompt_prepends() {
let msgs = json!([{"role": "user", "content": "hi"}]);
let sys = json!("you are helpful");
let out = proxy::convert_messages(&msgs, Some(&sys)).unwrap();
let arr = out.as_array().unwrap();
assert_eq!(arr[0]["role"], "system");
assert_eq!(arr[0]["content"], "you are helpful");
assert_eq!(arr[1]["role"], "user");
}
#[test]
fn convert_messages_with_array_system_prompt_joins_text() {
let msgs = json!([{"role": "user", "content": "hi"}]);
let sys = json!([
{"type": "text", "text": "first"},
{"type": "text", "text": "second"}
]);
let out = proxy::convert_messages(&msgs, Some(&sys)).unwrap();
let arr = out.as_array().unwrap();
assert_eq!(arr[0]["content"], "first\nsecond");
}
#[test]
fn convert_messages_assistant_with_tool_use_emits_tool_calls() {
let msgs = json!([{
"role": "assistant",
"content": [
{"type": "text", "text": "let me check"},
{"type": "tool_use", "id": "tool_1", "name": "search", "input": {"q": "rust"}}
]
}]);
let out = proxy::convert_messages(&msgs, None).unwrap();
let arr = out.as_array().unwrap();
assert_eq!(arr.len(), 1);
assert_eq!(arr[0]["tool_calls"][0]["id"], "tool_1");
assert_eq!(arr[0]["tool_calls"][0]["function"]["name"], "search");
// arguments should be stringified JSON
assert!(arr[0]["tool_calls"][0]["function"]["arguments"]
.as_str()
.unwrap()
.contains("rust"));
}
#[test]
fn convert_messages_user_tool_result_emits_tool_role() {
let msgs = json!([{
"role": "user",
"content": [
{"type": "tool_result", "tool_use_id": "tool_1", "content": "the answer"},
{"type": "text", "text": "thanks"}
]
}]);
let out = proxy::convert_messages(&msgs, None).unwrap();
let arr = out.as_array().unwrap();
assert_eq!(arr.len(), 2);
assert_eq!(arr[0]["role"], "tool");
assert_eq!(arr[0]["tool_call_id"], "tool_1");
assert_eq!(arr[0]["content"], "the answer");
assert_eq!(arr[1]["role"], "user");
}
#[test]
fn convert_messages_unknown_role_skipped() {
let msgs = json!([
{"role": "ghost", "content": "boo"},
{"role": "user", "content": "hi"}
]);
let out = proxy::convert_messages(&msgs, None).unwrap();
assert_eq!(out.as_array().unwrap().len(), 1);
}
#[test]
fn transform_anthropic_to_openai_basic() {
let body = json!({
"model": "claude-x",
"messages": [{"role": "user", "content": "hi"}],
"stream": true,
"temperature": 0.5,
"stop_sequences": ["END"]
});
let out = proxy::transform_anthropic_to_openai(&body).unwrap();
assert_eq!(out["model"], "claude-x");
assert_eq!(out["stream"], true);
assert_eq!(out["temperature"], 0.5);
assert_eq!(out["stop"], json!(["END"]));
assert!(out["messages"].is_array());
}
#[test]
fn transform_anthropic_to_openai_with_tools() {
let body = json!({
"model": "claude-x",
"messages": [{"role": "user", "content": "hi"}],
"tools": [{
"name": "lookup",
"description": "lookup tool",
"input_schema": {"type": "object"}
}]
});
let out = proxy::transform_anthropic_to_openai(&body).unwrap();
assert_eq!(out["tools"][0]["type"], "function");
assert_eq!(out["tools"][0]["function"]["name"], "lookup");
assert_eq!(out["tools"][0]["function"]["description"], "lookup tool");
}
#[test]
fn transform_anthropic_to_openai_missing_model_returns_none() {
let body = json!({"messages": []});
assert!(proxy::transform_anthropic_to_openai(&body).is_none());
}
#[test]
fn transform_openai_response_to_anthropic_text_only() {
let resp = json!({
"id": "chatcmpl-1",
"model": "gpt-4",
"choices": [{
"message": {"role": "assistant", "content": "hello"},
"finish_reason": "stop"
}],
"usage": {"input_tokens": 1, "output_tokens": 1}
});
let out = proxy::transform_openai_response_to_anthropic(&resp);
assert_eq!(out["type"], "message");
assert_eq!(out["role"], "assistant");
assert_eq!(out["stop_reason"], "end_turn");
assert_eq!(out["content"][0]["type"], "text");
assert_eq!(out["content"][0]["text"], "hello");
}
#[test]
fn transform_openai_response_to_anthropic_tool_calls() {
let resp = json!({
"id": "x",
"model": "gpt-4",
"choices": [{
"message": {
"role": "assistant",
"content": null,
"tool_calls": [{
"id": "call_1",
"function": {"name": "search", "arguments": "{\"q\":\"rust\"}"}
}]
},
"finish_reason": "tool_calls"
}]
});
let out = proxy::transform_openai_response_to_anthropic(&resp);
assert_eq!(out["stop_reason"], "tool_use");
assert_eq!(out["content"][0]["type"], "tool_use");
assert_eq!(out["content"][0]["id"], "call_1");
assert_eq!(out["content"][0]["name"], "search");
assert_eq!(out["content"][0]["input"]["q"], "rust");
}
#[test]
fn transform_openai_response_finish_reason_length_maps_max_tokens() {
let resp = json!({
"choices": [{
"message": {"content": "x"},
"finish_reason": "length"
}]
});
let out = proxy::transform_openai_response_to_anthropic(&resp);
assert_eq!(out["stop_reason"], "max_tokens");
}
#[test]
fn parse_openai_messages_ok() {
let msgs = json!([
{"role": "user", "content": "hi"},
{"role": "assistant", "content": "hello"}
]);
let out = proxy::parse_openai_messages(&msgs).unwrap();
assert_eq!(out.len(), 2);
assert_eq!(out[0]["role"], "user");
}
#[test]
fn parse_openai_messages_not_array_errors() {
let msgs = json!({"role": "user"});
assert!(proxy::parse_openai_messages(&msgs).is_err());
}
#[test]
fn parse_openai_messages_missing_role_errors() {
let msgs = json!([{"content": "hi"}]);
assert!(proxy::parse_openai_messages(&msgs).is_err());
}
#[test]
fn parse_openai_messages_passes_multimodal_array_through() {
let msgs = json!([{"role": "user", "content": [
{"type": "text", "text": "hi"},
{"type": "image_url", "image_url": {"url": "data:image/png;base64,AA"}},
]}]);
let out = proxy::parse_openai_messages(&msgs).unwrap();
assert_eq!(out[0]["content"], msgs[0]["content"]);
}
#[test]
fn parse_openai_messages_non_string_non_array_content_errors() {
let msgs = json!([{"role": "user", "content": 42}]);
assert!(proxy::parse_openai_messages(&msgs).is_err());
}
#[test]
fn replace_system_prompt_replaces_existing_system() {
let mut messages = vec![
json!({"role": "system", "content": "old"}),
json!({"role": "user", "content": "hi"}),
];
proxy::replace_system_prompt(&mut messages, "new");
assert_eq!(messages.len(), 2);
assert_eq!(messages[0]["role"], "system");
assert_eq!(messages[0]["content"], "new");
assert_eq!(messages[1]["role"], "user");
}
#[test]
fn replace_system_prompt_inserts_when_absent() {
let mut messages = vec![json!({"role": "user", "content": "hi"})];
proxy::replace_system_prompt(&mut messages, "system!");
assert_eq!(messages.len(), 2);
assert_eq!(messages[0]["role"], "system");
assert_eq!(messages[0]["content"], "system!");
}
#[test]
fn extract_tool_calls_returns_empty_when_absent() {
let resp = json!({"choices": [{"message": {"content": "x"}}]});
assert!(proxy::extract_tool_calls(&resp).is_empty());
}
#[test]
fn extract_tool_calls_returns_array() {
let resp = json!({
"choices": [{
"message": {
"tool_calls": [{"id": "c1"}, {"id": "c2"}]
}
}]
});
let calls = proxy::extract_tool_calls(&resp);
assert_eq!(calls.len(), 2);
assert_eq!(calls[0]["id"], "c1");
}
#[test]
fn extract_choice_message_returns_first_message() {
let resp = json!({"choices": [{"message": {"role": "assistant", "content": "x"}}]});
let msg = proxy::extract_choice_message(&resp).unwrap();
assert_eq!(msg["role"], "assistant");
}
#[test]
fn extract_choice_message_none_when_no_choices() {
let resp = json!({});
assert!(proxy::extract_choice_message(&resp).is_none());
}
#[test]
fn copy_optional_chat_params_copies_only_known_keys() {
let from = json!({
"temperature": 0.7,
"top_p": 0.9,
"max_tokens": 128,
"ignored_field": true
});
let mut into = serde_json::Map::new();
proxy::copy_optional_chat_params(&from, &mut into);
assert_eq!(into.get("temperature").unwrap(), &json!(0.7));
assert_eq!(into.get("top_p").unwrap(), &json!(0.9));
assert_eq!(into.get("max_tokens").unwrap(), &json!(128));
assert!(into.get("ignored_field").is_none());
}
#[test]
fn sse_event_default_message_type() {
let v = json!({"foo": "bar"});
let bytes = proxy::sse_event(&v);
let s = std::str::from_utf8(&bytes).unwrap();
assert!(s.starts_with("event: message\n"));
assert!(s.ends_with("\n\n"));
assert!(s.contains("data: "));
}
#[test]
fn sse_event_uses_type_field() {
let v = json!({"type": "content_block_start", "x": 1});
let bytes = proxy::sse_event(&v);
let s = std::str::from_utf8(&bytes).unwrap();
assert!(s.starts_with("event: content_block_start\n"));
}
#[test]
fn normalize_openai_tools_in_chat_body_patches_parameters() {
let mut body = json!({
"messages": [],
"tools": [{
"type": "function",
"function": {
"name": "x",
"parameters": {
"type": "object",
"properties": {
"url": {"description": "the url"}
}
}
}
}]
});
crate::core::openai_schema::normalize_openai_tools_in_chat_body(&mut body);
assert_eq!(
body["tools"][0]["function"]["parameters"]["properties"]["url"]["type"],
json!("string")
);
}
#[test]
fn normalize_openai_tools_in_chat_body_no_tools_is_noop() {
let mut body = json!({"messages": []});
let original = body.clone();
crate::core::openai_schema::normalize_openai_tools_in_chat_body(&mut body);
assert_eq!(body, original);
}
#[test]
fn add_cors_headers_with_trusted_origin_reflects_origin() {
let trusted = vec![vec!["localhost".to_string()]];
let builder = hyper::Response::builder();
let builder = proxy::add_cors_headers_with_host_and_origin(
builder,
"localhost",
"http://localhost:3000",
&trusted,
);
let resp = builder
.body(http_body_util::Empty::<hyper::body::Bytes>::new())
.unwrap();
let h = resp.headers();
assert!(h.contains_key("access-control-allow-methods"));
assert!(h.contains_key("access-control-allow-headers"));
assert_eq!(h.get("vary").unwrap(), "Origin");
assert_eq!(
h.get("access-control-allow-origin").unwrap(),
"http://localhost:3000"
);
assert_eq!(h.get("access-control-allow-credentials").unwrap(), "true");
}
#[test]
fn add_cors_headers_with_untrusted_origin_omits_origin() {
let trusted = vec![vec!["localhost".to_string()]];
let builder = hyper::Response::builder();
let builder = proxy::add_cors_headers_with_host_and_origin(
builder,
"localhost",
"http://evil.example.com",
&trusted,
);
let resp = builder
.body(http_body_util::Empty::<hyper::body::Bytes>::new())
.unwrap();
let h = resp.headers();
assert!(h.contains_key("access-control-allow-methods"));
assert!(!h.contains_key("access-control-allow-origin"));
assert!(!h.contains_key("access-control-allow-credentials"));
}
#[test]
fn add_cors_headers_reflects_a_listed_opaque_origin() {
for (origin, entry) in [("null", "null"), ("file://", "file://")] {
let resp = proxy::add_cors_headers_with_host_and_origin(
hyper::Response::builder(),
"127.0.0.1:1337",
origin,
&[vec![entry.to_string()]],
)
.body(http_body_util::Empty::<hyper::body::Bytes>::new())
.unwrap();
assert_eq!(resp.headers().get("access-control-allow-origin").unwrap(), origin);
let resp = proxy::add_cors_headers_with_host_and_origin(
hyper::Response::builder(),
"127.0.0.1:1337",
origin,
&[vec![]],
)
.body(http_body_util::Empty::<hyper::body::Bytes>::new())
.unwrap();
assert!(!resp.headers().contains_key("access-control-allow-origin"));
}
}
#[test]
fn add_cors_headers_with_empty_origin_omits_origin() {
let trusted = vec![vec!["localhost".to_string()]];
let builder = hyper::Response::builder();
let builder =
proxy::add_cors_headers_with_host_and_origin(builder, "localhost", "", &trusted);
let resp = builder
.body(http_body_util::Empty::<hyper::body::Bytes>::new())
.unwrap();
let h = resp.headers();
assert!(!h.contains_key("access-control-allow-origin"));
}
#[test]
fn get_destination_path_trailing_slash() {
let result = proxy::get_destination_path("/v1/", "/v1");
// remove_prefix should leave "/" or "" — either way no panic, deterministic.
assert!(result == "/" || result.is_empty());
}
#[test]
fn proxy_config_clone_preserves_fields() {
let cfg = proxy::ProxyConfig {
prefix: "/p".to_string(),
proxy_api_key: "k".to_string(),
trusted_hosts: vec![vec!["a".to_string()]],
host: "h".to_string(),
port: 1,
enable_server_tool_execution: true,
cors_enabled: true,
};
let cloned = cfg.clone();
assert_eq!(cloned.prefix, "/p");
assert_eq!(cloned.proxy_api_key, "k");
assert!(cloned.enable_server_tool_execution);
}
#[test]
fn normalizes_combinator_members_recursively() {
let mut schema = json!({
"anyOf": [
{
"type": "object"
},
{
"description": "fallback string"
}
],
"oneOf": [
{
"type": "object"
}
],
"allOf": [
{
"description": "merged leaf"
}
]
});
crate::core::openai_schema::normalize_openai_tool_parameters_schema(&mut schema);
assert_eq!(schema["anyOf"][0]["properties"], json!({}));
assert_eq!(schema["anyOf"][1]["type"], json!("string"));
assert_eq!(schema["oneOf"][0]["properties"], json!({}));
assert_eq!(schema["allOf"][0]["type"], json!("string"));
}
const PROMPT: &str = "You are Claude Code, Anthropic's official CLI for Claude.";
#[test]
fn strip_billing_header_inline_form() {
let text = format!(
"x-anthropic-billing-header: cc_version=2.1.150.d66; cc_entrypoint=cli; cch=b378b;\n{PROMPT}"
);
assert_eq!(proxy::strip_anthropic_billing_header(&text), PROMPT);
}
#[test]
fn strip_billing_header_wrapped_form() {
let text = format!(
"x-anthropic-billing-header:\n cc_version=2.1.150.d66; cc_entrypoint=cli; cch=934c8;\n{PROMPT}"
);
assert_eq!(proxy::strip_anthropic_billing_header(&text), PROMPT);
}
#[test]
fn strip_billing_header_leaves_regular_content() {
assert_eq!(proxy::strip_anthropic_billing_header(PROMPT), PROMPT);
// A header-like word that isn't the billing prefix is untouched.
let other = "x-anthropic-version: 2023-06-01\nhello";
assert_eq!(proxy::strip_anthropic_billing_header(other), other);
}
#[test]
fn strip_billing_header_in_body_covers_system_and_first_message() {
let header = "x-anthropic-billing-header: cc_version=1; cch=z;\n";
let mut body = json!({
"system": format!("{header}{PROMPT}"),
"messages": [
{ "role": "user", "content": format!("{header}first") },
{ "role": "user", "content": format!("{header}second") }
]
});
proxy::strip_billing_header_in_body(&mut body);
assert_eq!(body["system"], json!(PROMPT));
assert_eq!(body["messages"][0]["content"], json!("first"));
// Only the first message is touched.
assert_eq!(
body["messages"][1]["content"],
json!(format!("{header}second"))
);
}
#[test]
fn inject_sampling_defaults_fills_only_missing_keys() {
let mut body = json!({
"model": "m",
"messages": [],
"temperature": 0.2
});
let defaults = json!({
"temperature": 0.7,
"top_p": 0.9,
"repetition_penalty": 1.1
});
proxy::inject_sampling_defaults(&mut body, &defaults);
// Caller-provided value wins.
assert_eq!(body["temperature"], json!(0.2));
// Omitted defaults are added.
assert_eq!(body["top_p"], json!(0.9));
assert_eq!(body["repetition_penalty"], json!(1.1));
}
#[test]
fn strip_billing_header_in_body_handles_block_content() {
let header = "x-anthropic-billing-header: cc_version=1;\n";
let mut body = json!({
"messages": [
{ "role": "user", "content": [ { "type": "text", "text": format!("{header}{PROMPT}") } ] }
]
});
proxy::strip_billing_header_in_body(&mut body);
assert_eq!(body["messages"][0]["content"][0]["text"], json!(PROMPT));
}
// ---- Origin gate and /orchestrations opt-in, against a running server ----
fn trusted(hosts: &[&str]) -> Vec<Vec<String>> {
vec![hosts.iter().map(|h| h.to_string()).collect()]
}
#[test]
fn request_origin_refuses_a_non_ascii_origin_header() {
let mut headers = hyper::HeaderMap::new();
assert_eq!(proxy::request_origin(&headers), Some(""));
headers.insert(
hyper::header::ORIGIN,
hyper::header::HeaderValue::from_static("http://localhost:3000"),
);
assert_eq!(
proxy::request_origin(&headers),
Some("http://localhost:3000")
);
headers.insert(
hyper::header::ORIGIN,
hyper::header::HeaderValue::from_bytes(b"http://\xffevil.com").unwrap(),
);
assert_eq!(proxy::request_origin(&headers), None);
}
#[test]
fn origin_allowed_absent_origin_always_passes() {
assert!(proxy::origin_allowed("", "127.0.0.1:1337", &trusted(&[]), true));
assert!(proxy::origin_allowed("", "192.168.1.10:1337", &trusted(&[]), false));
}
#[test]
fn origin_allowed_matrix() {
let t = trusted(&[]);
let h = "127.0.0.1:1337";
assert!(proxy::origin_allowed("http://localhost:3000", h, &t, true));
assert!(proxy::origin_allowed("tauri://localhost", h, &t, true));
assert!(!proxy::origin_allowed("https://evil.com", h, &t, true));
assert!(!proxy::origin_allowed("null", h, &t, true));
assert!(!proxy::origin_allowed("file://", h, &t, true));
// Opaque origins only with an explicit entry, and only with CORS on.
assert!(proxy::origin_allowed("null", h, &trusted(&["null"]), true));
assert!(proxy::origin_allowed("file://", h, &trusted(&["file://"]), true));
assert!(!proxy::origin_allowed("file://", h, &trusted(&["null"]), true));
assert!(!proxy::origin_allowed("null", h, &trusted(&["null"]), false));
// Private-IP pages on the LAN are not trusted unless listed.
assert!(!proxy::origin_allowed("http://192.168.1.20:3000", h, &t, true));
assert!(proxy::origin_allowed(
"http://192.168.1.20:3000",
h,
&trusted(&["192.168.1.20"]),
true
));
// CORS off: only the app webview and same-origin pages.
assert!(!proxy::origin_allowed("http://localhost:3000", h, &t, false));
assert!(proxy::origin_allowed("tauri://localhost", h, &t, false));
assert!(proxy::origin_allowed("http://127.0.0.1:1337", h, &t, false));
// Same-origin needs a valid Host: a rebinding hostname does not count.
assert!(!proxy::origin_allowed(
"http://evil.com:1337",
"evil.com:1337",
&t,
true
));
}
#[test]
fn orchestrations_follow_tool_execution_opt_in() {
let mut cfg = proxy::ProxyConfig {
prefix: "/v1".to_string(),
proxy_api_key: String::new(),
trusted_hosts: vec![vec![]],
host: "127.0.0.1".to_string(),
port: 1337,
enable_server_tool_execution: false,
cors_enabled: true,
};
assert!(!proxy::orchestrations_allowed(&cfg));
cfg.enable_server_tool_execution = true;
assert!(proxy::orchestrations_allowed(&cfg));
}
async fn start_test_server(
enable_server_tool_execution: bool,
cors_enabled: bool,
) -> (u16, std::sync::Arc<tokio::sync::Mutex<Option<crate::core::state::ServerHandle>>>) {
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
let port = std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let handle = Arc::new(Mutex::new(None));
let data = std::env::temp_dir().to_string_lossy().into_owned();
proxy::start_server(
handle.clone(),
Arc::new(tauri_plugin_llamacpp::state::LlamacppState::default()),
Arc::new(Mutex::new(HashMap::new())),
"127.0.0.1".to_string(),
port,
"/v1".to_string(),
String::new(),
vec![vec![]],
30,
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(crate::core::mcp::models::McpSettings::default())),
data,
enable_server_tool_execution,
cors_enabled,
)
.await
.expect("server starts");
(port, handle)
}
async fn post(
port: u16,
path: &str,
origin: Option<&str>,
content_type: &str,
) -> reqwest::StatusCode {
let mut req = reqwest::Client::new()
.post(format!("http://127.0.0.1:{port}/v1{path}"))
.header("Content-Type", content_type)
.body(r#"{"model":"nope","messages":[{"role":"user","content":"hi"}]}"#);
if let Some(o) = origin {
req = req.header("Origin", o);
}
req.send().await.expect("request sent").status()
}
#[tokio::test]
async fn untrusted_origin_post_is_refused_before_handlers() {
let (port, handle) = start_test_server(false, true).await;
// A cross-site "simple" request (no preflight) is refused.
for path in ["/chat/completions", "/messages", "/orchestrations", "/models"] {
assert_eq!(
post(port, path, Some("https://evil.example"), "text/plain").await,
reqwest::StatusCode::FORBIDDEN,
"{path}"
);
}
assert_eq!(
post(port, "/chat/completions", Some("http://192.168.1.20:3000"), "text/plain").await,
reqwest::StatusCode::FORBIDDEN
);
// No Origin (curl, SDKs) and loopback origins reach the handler: no
// model is loaded, so the handler answers, just not with the gate's 403.
for origin in [None, Some("http://localhost:3000")] {
let status = post(port, "/chat/completions", origin, "application/json").await;
assert_ne!(status, reqwest::StatusCode::FORBIDDEN, "{origin:?}");
}
proxy::stop_server(handle).await.unwrap();
}
#[tokio::test]
async fn orchestrations_refused_without_opt_in() {
let (port, handle) = start_test_server(false, true).await;
assert_eq!(
post(port, "/orchestrations", None, "application/json").await,
reqwest::StatusCode::FORBIDDEN
);
proxy::stop_server(handle).await.unwrap();
let (port, handle) = start_test_server(true, true).await;
assert_ne!(
post(port, "/orchestrations", None, "application/json").await,
reqwest::StatusCode::FORBIDDEN
);
proxy::stop_server(handle).await.unwrap();
}
#[tokio::test]
async fn cors_toggle_off_refuses_loopback_pages() {
let (port, handle) = start_test_server(false, false).await;
assert_eq!(
post(port, "/chat/completions", Some("http://localhost:3000"), "application/json")
.await,
reqwest::StatusCode::FORBIDDEN
);
assert_ne!(
post(port, "/chat/completions", None, "application/json").await,
reqwest::StatusCode::FORBIDDEN
);
proxy::stop_server(handle).await.unwrap();
}
#[tokio::test]
async fn public_bind_without_api_key_is_refused_before_binding() {
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
let port = std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let handle = Arc::new(Mutex::new(None));
let err = proxy::start_server(
handle.clone(),
Arc::new(tauri_plugin_llamacpp::state::LlamacppState::default()),
Arc::new(Mutex::new(HashMap::new())),
"0.0.0.0".to_string(),
port,
"/v1".to_string(),
String::new(),
vec![vec![]],
30,
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(crate::core::mcp::models::McpSettings::default())),
std::env::temp_dir().to_string_lossy().into_owned(),
false,
true,
)
.await
.expect_err("an open public bind must be refused");
let msg = err.to_string();
assert!(msg.contains("API key") && msg.contains("127.0.0.1"), "{msg}");
// Nothing was started and nothing is listening.
assert!(handle.lock().await.is_none());
assert!(std::net::TcpListener::bind(("127.0.0.1", port)).is_ok());
}
/// A `*` in Trusted Hosts opens the API to any web page (CORS), but the
/// Host header must still name this server: a DNS-rebinding page reaches
/// it under its own hostname.
#[tokio::test]
async fn wildcard_trusted_hosts_still_refuse_a_foreign_host_header() {
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::Mutex;
let port = std::net::TcpListener::bind("127.0.0.1:0")
.unwrap()
.local_addr()
.unwrap()
.port();
let handle = Arc::new(Mutex::new(None));
proxy::start_server(
handle.clone(),
Arc::new(tauri_plugin_llamacpp::state::LlamacppState::default()),
Arc::new(Mutex::new(HashMap::new())),
"127.0.0.1".to_string(),
port,
"/v1".to_string(),
String::new(),
vec![vec!["*".to_string()]],
30,
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(HashMap::new())),
Arc::new(Mutex::new(crate::core::mcp::models::McpSettings::default())),
std::env::temp_dir().to_string_lossy().into_owned(),
false,
true,
)
.await
.expect("server starts");
let get = |host: String, origin: Option<&'static str>| async move {
let mut req = reqwest::Client::new()
.get(format!("http://127.0.0.1:{port}/v1/models"))
.header("Host", host);
if let Some(o) = origin {
req = req.header("Origin", o);
}
req.send().await.expect("request sent").status()
};
// The rebinding shape: attacker hostname in Host and as Origin.
assert_eq!(
get("attacker.example".into(), Some("http://attacker.example")).await,
reqwest::StatusCode::FORBIDDEN
);
assert_eq!(
get("attacker.example:1337".into(), None).await,
reqwest::StatusCode::FORBIDDEN
);
// `*` keeps its CORS meaning: a page on another origin may call the
// API through the server's own address.
assert_ne!(
get(format!("127.0.0.1:{port}"), Some("https://any-web-app.example")).await,
reqwest::StatusCode::FORBIDDEN
);
assert_ne!(
get(format!("127.0.0.1:{port}"), None).await,
reqwest::StatusCode::FORBIDDEN
);
proxy::stop_server(handle).await.unwrap();
}
}