#![allow(dead_code)]
use std::collections::HashMap;
use std::sync::Arc;
use axum::Router;
use axum::body::Body;
use axum::http::{HeaderMap, Request, StatusCode};
use http_body_util::BodyExt;
use postgres_mcp::config::Config;
use postgres_mcp::{AppState, build_router};
use serde_json::{Value, json};
use tower::ServiceExt;
pub const PORT_18: u16 = 15432;
pub const PORT_19: u16 = 15433;
pub const PORT_15: u16 = 15434;
pub const PORT_TLS: u16 = 15435;
pub const DB_USER: &str = "mcp_ro";
pub const DB_PASSWORD: &str = "test-only-ro";
pub const DB_NAME: &str = "appdb";
pub const PROTOCOL_2025: &str = "2025-11-25";
pub const PROTOCOL_2026: &str = "2026-07-28";
pub fn db_entry(name: &str, port: u16) -> Value {
json!({
"name": name,
"host": "127.0.0.1",
"port": port,
"database": DB_NAME,
"user": DB_USER,
"password": DB_PASSWORD,
"sslmode": "disable",
"max_connections": 1,
})
}
pub fn config_with(databases: &[Value], extra: &[(&str, &str)]) -> Config {
let mut vars: HashMap<String, String> = HashMap::new();
vars.insert(
"PG_DATABASES".to_owned(),
serde_json::to_string(databases).expect("database list serializes"),
);
for (name, value) in extra {
vars.insert((*name).to_owned(), (*value).to_owned());
}
Config::from_lookup(&|name| vars.get(name).cloned()).expect("test configuration is valid")
}
pub fn one_db(port: u16, extra: &[(&str, &str)]) -> Config {
config_with(&[db_entry("appdb", port)], extra)
}
pub fn app(config: Config) -> Router {
build_router(Arc::new(AppState::new(config)))
}
pub struct Reply {
pub status: StatusCode,
pub headers: HeaderMap,
pub body: Vec<u8>,
}
impl Reply {
pub fn json(&self) -> Value {
serde_json::from_slice(&self.body).expect("response body is JSON")
}
pub fn text(&self) -> String {
String::from_utf8_lossy(&self.body).into_owned()
}
}
pub async fn send(app: &Router, request: Request<Body>) -> Reply {
let response = app
.clone()
.oneshot(request)
.await
.expect("router service is infallible");
let status = response.status();
let headers = response.headers().clone();
let body = response
.into_body()
.collect()
.await
.expect("body is readable")
.to_bytes()
.to_vec();
Reply {
status,
headers,
body,
}
}
pub async fn post_mcp(app: &Router, body: &Value, headers: &[(&str, &str)]) -> Reply {
let mut builder = Request::builder()
.method("POST")
.uri("/mcp")
.header("content-type", "application/json")
.header("accept", "application/json, text/event-stream");
if !headers
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case("host"))
{
builder = builder.header("host", "localhost:8432");
}
for (name, value) in headers {
builder = builder.header(*name, *value);
}
let request = builder
.body(Body::from(body.to_string()))
.expect("request is well formed");
send(app, request).await
}
pub async fn call_tool(app: &Router, name: &str, arguments: Value) -> Value {
let body = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "tools/call",
"params": {"name": name, "arguments": arguments},
});
post_mcp(app, &body, &[("mcp-protocol-version", PROTOCOL_2025)])
.await
.json()
}
pub fn result_of(response: &Value) -> &Value {
response
.get("result")
.unwrap_or_else(|| panic!("no result in {response}"))
}