Files
crank/crates/crank-import/tests/http_replay.rs
T

69 lines
2.8 KiB
Rust

use crank_import::rest::{
NormalizationConfig, NormalizedIr, SourceDigest, normalize_verified_document, preview_from_ir,
validate_normalized_ir,
};
use serde_json::json;
#[test]
fn serialized_http_ir_preserves_variants_mime_aliases_and_mapping_without_parser_state() {
let document = r#"
openapi: 3.0.3
info: {title: Replay}
servers: [{url: 'https://example.test/api/v2'}]
paths:
/upload:
post:
operationId: upload
parameters:
- {in: query, name: body, schema: {type: string}}
- {in: header, name: content_type, schema: {type: string}}
requestBody:
required: true
content:
application/json:
schema: {type: object, required: [name], properties: {name: {type: string}}}
multipart/form-data:
schema: {type: object, required: [file], properties: {file: {type: string, format: binary}}}
encoding: {file: {contentType: 'image/png, image/jpeg'}}
responses:
'200': {description: OK, content: {application/json: {schema: {type: string}}}}
'204': {description: Empty}
"#;
let ir = normalize_verified_document(
document,
SourceDigest::parse("0".repeat(64)).unwrap(),
&NormalizationConfig::default(),
)
.unwrap();
let restored: NormalizedIr = serde_json::from_slice(&serde_json::to_vec(&ir).unwrap()).unwrap();
validate_normalized_ir(&restored).unwrap();
let first = preview_from_ir(&ir);
let second = preview_from_ir(&restored);
assert_eq!(first, second);
assert_eq!(second.groups[0].operations.len(), 1);
let candidate = &second.groups[0].operations[0];
let draft = &candidate.draft;
let draft_value = serde_json::to_value(draft).unwrap();
let persisted: crank_import::rest::RestImportCandidate =
serde_json::from_value(draft_value.clone()).unwrap();
assert_eq!(serde_json::to_value(persisted).unwrap(), draft_value);
for arguments in [
json!({"query_body": "query", "header_content_type": "header", "content_type": "application/json", "body": {"name": "Ada"}}),
json!({"query_body": "query", "header_content_type": "header", "content_type": "multipart/form-data", "body": {"file": {"data_base64": "AA==", "content_type": "image/png"}}}),
] {
draft.input_schema.validate_shape(&arguments).unwrap();
let mapped = draft
.input_mapping
.apply(&json!({"mcp": arguments}))
.unwrap();
assert_eq!(mapped["request"]["query"]["body"], "query");
assert_eq!(mapped["request"]["headers"]["content_type"], "header");
}
assert_eq!(draft.target.base_url, "https://example.test/api/v2");
assert_eq!(draft.target.response_contract.len(), 2);
assert_eq!(
draft.target.multipart_field_mime_types["multipart/form-data"]["file"],
["image/png", "image/jpeg"]
);
}