use super::{http_response_to_rpc, response_with_status, rpc_response_to_http};
use crate::server::{dispatch_http_request, handle_rpc, ServeAuth};
use crate::server_federation::federation_discovery_response;
use pray_core::ssh_rpc::{RpcRequest, SSH_RPC_SPEC};
use serde_json::json;
use std::fs;
use std::path::PathBuf;
fn temporary_root(name: &str) -> PathBuf {
let path = std::env::temp_dir().join(format!("pray-http-rpc-{name}-{}", std::process::id()));
let _ = fs::remove_dir_all(&path);
fs::create_dir_all(path.join("v1")).expect("v1 directory");
fs::write(
path.join("v1/index.json"),
r#"{"spec":"prayfile-distribution-1","packages":[]}"#,
)
.expect("index");
path
}
#[test]
fn http_discovery_matches_direct_handler() {
let root = temporary_root("discovery");
let direct = federation_discovery_response(&root).expect("direct response");
let auth = ServeAuth::http("127.0.0.1", false);
let bridged = dispatch_http_request(
&root,
&auth,
"GET",
"/.well-known/pray-federation.json",
&[],
)
.expect("bridged response");
assert_eq!(direct.status, bridged.status);
assert_eq!(direct.content_type, bridged.content_type);
let direct_json: serde_json::Value = serde_json::from_slice(&direct.body).expect("direct json");
let bridged_json: serde_json::Value =
serde_json::from_slice(&bridged.body).expect("bridged json");
assert_eq!(direct_json, bridged_json);
let _ = fs::remove_dir_all(&root);
}
#[test]
fn rpc_response_round_trips_through_http_envelope() {
let response = response_with_status(200, "application/json", br#"{"ok":true}"#.to_vec());
let rpc = http_response_to_rpc("1", response);
let http = rpc_response_to_http(&rpc);
assert_eq!(http.status, 200);
assert_eq!(http.content_type, "application/json");
assert_eq!(http.body, br#"{"ok":true}"#);
}
#[test]
fn handle_rpc_and_http_dispatch_share_sync_package_path() {
let root = temporary_root("sync-package");
let metadata_path = root.join("v1/packages/sample/base.json");
fs::create_dir_all(metadata_path.parent().unwrap()).expect("package directory");
fs::write(
&metadata_path,
r#"{"name":"sample/base","versions":[{"version":"1.0.0","artifact":"v1/artifacts/sample/base/1.0.0/package.praypkg"}]}"#,
)
.expect("metadata");
fs::write(
root.join("v1/index.json"),
r#"{"spec":"prayfile-distribution-1","packages":["sample/base"]}"#,
)
.expect("index");
let auth = ServeAuth::http("127.0.0.1", false);
let rpc = handle_rpc(
&root,
&auth,
&RpcRequest::new("1", "sync.package", json!({ "name": "sample/base" })),
)
.expect("rpc response");
assert_eq!(rpc.spec, SSH_RPC_SPEC);
assert_eq!(rpc.status, 200);
let http = dispatch_http_request(&root, &auth, "GET", "/v1/sync/package/sample/base", &[])
.expect("http response");
assert_eq!(http.status, 200);
assert_eq!(rpc_response_to_http(&rpc).body, http.body);
let first = crate::server_http::next_http_request_id();
let second = crate::server_http::next_http_request_id();
assert_ne!(first, second);
let _ = fs::remove_dir_all(&root);
}
#[test]
fn missing_static_file_returns_not_found() {
let root = temporary_root("missing-static");
let auth = ServeAuth::http("127.0.0.1", false);
let response =
dispatch_http_request(&root, &auth, "GET", "/v1/distribution.json", &[]).expect("dispatch");
assert_eq!(response.status, 404);
let _ = fs::remove_dir_all(&root);
}
#[test]
fn private_server_files_are_not_available_over_http_or_rpc() {
let root = temporary_root("private-files");
fs::create_dir_all(root.join(".pray")).expect("private directory");
fs::write(root.join(".pray/auth.db"), b"private database bytes").expect("private file");
let auth = ServeAuth::http("127.0.0.1", false);
let http =
dispatch_http_request(&root, &auth, "GET", "/.pray/auth.db", &[]).expect("http response");
assert_eq!(http.status, 404);
assert!(!http.body.windows(8).any(|bytes| bytes == b"database"));
let rpc = handle_rpc(
&root,
&ServeAuth::stdio(),
&RpcRequest::new(
"private",
"artifact.get",
json!({ "path": ".pray/auth.db" }),
),
)
.expect("rpc response");
assert_eq!(rpc.status, 404);
assert!(!rpc.body.to_string().contains("database"));
let _ = fs::remove_dir_all(&root);
}
#[test]
fn distribution_files_remain_available() {
let root = temporary_root("public-files");
let auth = ServeAuth::http("127.0.0.1", false);
let response =
dispatch_http_request(&root, &auth, "GET", "/v1/index.json", &[]).expect("public response");
assert_eq!(response.status, 200);
let body: serde_json::Value = serde_json::from_slice(&response.body).expect("index json");
assert_eq!(body["spec"], "prayfile-distribution-1");
let _ = fs::remove_dir_all(&root);
}
#[cfg(unix)]
#[test]
fn static_distribution_symlink_is_not_available() {
use std::os::unix::fs::symlink;
let root = temporary_root("static-symlink");
let outside = temporary_root("static-symlink-outside");
let secret = outside.join("secret.praypkg");
fs::write(&secret, b"private artifact target").expect("outside file");
let artifact = root.join("v1/artifacts/sample/base/1.0.0/base.praypkg");
fs::create_dir_all(artifact.parent().expect("artifact parent")).expect("artifact directory");
symlink(&secret, &artifact).expect("artifact symlink");
let response = dispatch_http_request(
&root,
&ServeAuth::http("127.0.0.1", false),
"GET",
"/v1/artifacts/sample/base/1.0.0/base.praypkg",
&[],
)
.expect("symlink response");
assert_eq!(response.status, 404);
assert!(!response.body.windows(7).any(|bytes| bytes == b"private"));
let _ = fs::remove_dir_all(&root);
let _ = fs::remove_dir_all(&outside);
}
#[test]
fn apply_byte_range_returns_partial_content() {
let (status, body, range) =
super::apply_byte_range(200, b"abcdefgh".to_vec(), Some("bytes=2-4")).expect("range");
assert_eq!(status, 206);
assert_eq!(body, b"cde");
assert_eq!(range.as_deref(), Some("bytes 2-4/8"));
}
#[test]
fn apply_byte_range_accepts_a_range_from_the_first_byte() {
let (status, body, range) =
super::apply_byte_range(200, b"abcdefgh".to_vec(), Some("bytes=0-2")).expect("range");
assert_eq!(status, 206);
assert_eq!(body, b"abc");
assert_eq!(range.as_deref(), Some("bytes 0-2/8"));
}
#[test]
fn apply_byte_range_rejects_unsatisfiable_range() {
let (status, body, range) =
super::apply_byte_range(200, b"ab".to_vec(), Some("bytes=5-9")).expect("range");
assert_eq!(status, 416);
assert!(body.is_empty());
assert_eq!(range.as_deref(), Some("bytes */2"));
}