#![cfg(feature = "client")]
use axum::{
body::{to_bytes, Body},
extract::Request,
http::StatusCode,
response::Response,
routing::any,
Router,
};
use http_query::Client;
use tokio::net::TcpListener;
async fn spawn(app: Router) -> String {
let listener = TcpListener::bind("127.0.0.1:0").await.unwrap();
let addr = listener.local_addr().unwrap();
tokio::spawn(async move {
axum::serve(listener, app).await.unwrap();
});
format!("http://{addr}")
}
#[tokio::test]
async fn query_round_trip_preserves_method_and_body() {
let app = Router::new().route(
"/search",
any(|req: Request| async move {
let method = req.method().to_string();
let body = to_bytes(req.into_body(), 1 << 20).await.unwrap();
Response::builder()
.header("x-method", method)
.header("content-type", "application/json")
.body(Body::from(body))
.unwrap()
}),
);
let base = spawn(app).await;
let client = Client::new();
let resp = client
.query_string(&format!("{base}/search"), "application/sql", "select 1")
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
assert_eq!(resp.headers()["x-method"], "QUERY");
assert_eq!(resp.text().await.unwrap(), "select 1");
}
#[tokio::test]
async fn options_reports_query_support() {
let app = Router::new().route(
"/search",
any(|| async {
Response::builder()
.status(StatusCode::NO_CONTENT)
.header("allow", "GET, QUERY, OPTIONS")
.header("accept-query", "application/json, application/sql")
.body(Body::empty())
.unwrap()
}),
);
let base = spawn(app).await;
let client = Client::new();
let discovery = client.options(&format!("{base}/search")).await.unwrap();
assert!(discovery.supports_query);
assert!(discovery.accepts_media_type("application/json"));
assert!(discovery.accepts_media_type("application/sql"));
}
#[tokio::test]
async fn fetch_result_follows_content_location() {
let app = Router::new()
.route(
"/search",
any(|| async {
Response::builder()
.header("content-location", "/results/1")
.body(Body::from("inline"))
.unwrap()
}),
)
.route("/results/1", any(|| async { "the-result" }));
let base = spawn(app).await;
let client = Client::new();
let resp = client
.query_string(&format!("{base}/search"), "application/json", "{}")
.await
.unwrap();
let result = client.fetch_result(&resp).await.unwrap();
assert_eq!(result.text().await.unwrap(), "the-result");
}
#[tokio::test]
async fn redirect_302_preserves_query_and_body() {
let app = Router::new()
.route(
"/a",
any(|| async {
Response::builder()
.status(StatusCode::FOUND)
.header("location", "/b")
.body(Body::empty())
.unwrap()
}),
)
.route(
"/b",
any(|req: Request| async move {
let method = req.method().to_string();
let body = to_bytes(req.into_body(), 1 << 20).await.unwrap();
Response::builder()
.header("x-method", method)
.body(Body::from(body))
.unwrap()
}),
);
let base = spawn(app).await;
let client = Client::new();
let resp = client
.query_string(&format!("{base}/a"), "application/sql", "select 1")
.await
.unwrap();
assert_eq!(resp.status().as_u16(), 200);
assert_eq!(resp.headers()["x-method"], "QUERY");
assert_eq!(resp.text().await.unwrap(), "select 1");
}
#[tokio::test]
async fn redirect_303_switches_to_get() {
let app = Router::new()
.route(
"/a",
any(|| async {
Response::builder()
.status(StatusCode::SEE_OTHER)
.header("location", "/b")
.body(Body::empty())
.unwrap()
}),
)
.route(
"/b",
any(|req: Request| async move {
let method = req.method().to_string();
let body = to_bytes(req.into_body(), 1 << 20).await.unwrap();
Response::builder()
.header("x-method", method)
.header("x-bodylen", body.len().to_string())
.body(Body::empty())
.unwrap()
}),
);
let base = spawn(app).await;
let client = Client::new();
let resp = client
.query_string(&format!("{base}/a"), "application/sql", "select 1")
.await
.unwrap();
assert_eq!(resp.headers()["x-method"], "GET");
assert_eq!(resp.headers()["x-bodylen"], "0");
}