143 lines
5.6 KiB
Rust
143 lines
5.6 KiB
Rust
use super::*;
|
|
|
|
const DEFAULT_HTTP_BODY_BYTES: usize = 2 * 1024 * 1024;
|
|
const MAX_STORED_PLAN_BYTES: usize = 16 * 1024 * 1024;
|
|
const PATH_COUNT: usize = 49;
|
|
const PATH_LEN: usize = 3100;
|
|
const METHODS: [&str; 7] = ["get", "post", "put", "patch", "delete", "head", "options"];
|
|
const OPERATION_COUNT: usize = PATH_COUNT * METHODS.len();
|
|
|
|
// Complete descriptions and schemas avoid multiplying quality findings for
|
|
// long paths. The test must pass the stored-plan cap before exercising HTTP.
|
|
fn long_path_document_and_keys() -> (String, Vec<String>) {
|
|
let mut paths = serde_json::Map::new();
|
|
let mut keys = Vec::new();
|
|
for index in 0..PATH_COUNT {
|
|
let path = format!("/{index:04}{}", "a".repeat(PATH_LEN - 5));
|
|
let mut item = serde_json::Map::new();
|
|
item.insert(
|
|
"parameters".to_owned(),
|
|
serde_json::json!([{
|
|
"name": "filter", "in": "query", "description": "Record filter",
|
|
"schema": { "type": "string" }
|
|
}]),
|
|
);
|
|
for (method_index, method) in METHODS.iter().enumerate() {
|
|
keys.push(format!("{} {path}", method.to_uppercase()));
|
|
item.insert(
|
|
(*method).to_owned(),
|
|
serde_json::json!({
|
|
"operationId": format!("read_record_{index}_{method_index}"),
|
|
"summary": "Read record",
|
|
"description": "Returns a matching record for the requested filter.",
|
|
"responses": { "200": {
|
|
"description": "Matched record",
|
|
"content": { "application/json": { "schema": {
|
|
"type": "object", "properties": { "result": { "type": "string" } }
|
|
} } }
|
|
} }
|
|
}),
|
|
);
|
|
}
|
|
paths.insert(path, Value::Object(item));
|
|
}
|
|
(
|
|
serde_json::json!({
|
|
"openapi": "3.0.3", "info": { "title": "Long path keys", "version": "1" },
|
|
"servers": [{ "url": "https://api.example.test" }], "paths": paths
|
|
})
|
|
.to_string(),
|
|
keys,
|
|
)
|
|
}
|
|
|
|
#[tokio::test(flavor = "multi_thread")]
|
|
#[serial]
|
|
async fn plan_http_body_limit_accepts_valid_large_confirmation() {
|
|
let (document, keys) = long_path_document_and_keys();
|
|
assert!(document.len() <= MAX_ARTIFACT_BYTES);
|
|
assert_eq!(keys.len(), OPERATION_COUNT);
|
|
assert!(keys.iter().all(|key| key.len() <= 4096));
|
|
println!(
|
|
"B1_FIXTURE document_bytes={} operations={}",
|
|
document.len(),
|
|
keys.len()
|
|
);
|
|
|
|
let registry = test_registry().await;
|
|
let app = build_test_app(
|
|
registry.clone(),
|
|
test_storage_root("openapi_plan_body_limit"),
|
|
);
|
|
let server = spawn_admin_api(app).await;
|
|
let client = authorized_client(&server).await;
|
|
let response = client
|
|
.post(format!("{server}/imports/openapi/preview"))
|
|
.multipart(Form::new().part(
|
|
"file",
|
|
file_part(document.as_bytes(), "openapi.json", "application/json"),
|
|
))
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
let status = response.status();
|
|
let preview: Value = response.json().await.unwrap();
|
|
assert_eq!(status, reqwest::StatusCode::OK, "{preview}");
|
|
let job_id = preview["job_id"].as_str().unwrap();
|
|
|
|
// The smaller control proves actual persistence. Explicit create decisions
|
|
// add no semantic change, but repeat the keys in a larger HTTP request.
|
|
for explicit_decisions in [false, true] {
|
|
let decisions: Vec<Value> = if explicit_decisions {
|
|
keys.iter()
|
|
.map(|key| {
|
|
serde_json::json!({
|
|
"operation_key": key, "action": "create"
|
|
})
|
|
})
|
|
.collect()
|
|
} else {
|
|
Vec::new()
|
|
};
|
|
let payload = serde_json::json!({
|
|
"selection_mode": "full", "selected_operation_keys": keys, "decisions": decisions
|
|
});
|
|
let request_bytes = serde_json::to_vec(&payload).unwrap().len();
|
|
assert_eq!(request_bytes > DEFAULT_HTTP_BODY_BYTES, explicit_decisions);
|
|
let response = client
|
|
.post(format!("{server}/imports/openapi/{job_id}/plans"))
|
|
.json(&payload)
|
|
.send()
|
|
.await
|
|
.unwrap();
|
|
let status = response.status();
|
|
let body = response.text().await.unwrap();
|
|
println!(
|
|
"B1_HTTP explicit_decisions={explicit_decisions} request_bytes={request_bytes} status={status}"
|
|
);
|
|
assert_eq!(
|
|
status,
|
|
reqwest::StatusCode::OK,
|
|
"valid plan rejected: {}",
|
|
body.chars().take(2000).collect::<String>()
|
|
);
|
|
let plan: Value = serde_json::from_str(&body).unwrap();
|
|
assert_eq!(plan["counts"]["selected"], OPERATION_COUNT);
|
|
assert_eq!(plan["counts"]["actionable"], OPERATION_COUNT);
|
|
assert_eq!(plan["counts"]["blocked"], 0);
|
|
assert_eq!(plan["counts"]["excluded"], 0);
|
|
let plan_id: crank_registry::ImportPlanId = plan["plan_reference"].as_str().unwrap().into();
|
|
let stored = registry
|
|
.get_import_plan(&WorkspaceId::new("ws_default"), &plan_id)
|
|
.await
|
|
.unwrap()
|
|
.unwrap();
|
|
let stored_bytes = serde_json::to_vec(&stored.payload).unwrap().len();
|
|
println!(
|
|
"B1_PERSISTED explicit_decisions={explicit_decisions} stored_bytes={stored_bytes}"
|
|
);
|
|
assert!(stored_bytes <= MAX_STORED_PLAN_BYTES);
|
|
assert_eq!(stored.payload["counts"]["selected"], OPERATION_COUNT);
|
|
}
|
|
}
|