162 lines
4.6 KiB
Rust
162 lines
4.6 KiB
Rust
use crank_core::HttpMethod;
|
|
use crank_import::rest::{
|
|
NormalizationConfig, NormalizedSchemaKind, SourceDigest, normalize_verified_document,
|
|
};
|
|
|
|
fn normalize(document: &str) -> crank_import::rest::NormalizedIr {
|
|
normalize_verified_document(
|
|
document,
|
|
SourceDigest::parse("0".repeat(64)).unwrap(),
|
|
&NormalizationConfig::default(),
|
|
)
|
|
.unwrap()
|
|
}
|
|
|
|
#[test]
|
|
fn swagger2_form_data_uses_effective_consumes_and_builds_body_schema() {
|
|
let document = r#"
|
|
swagger: '2.0'
|
|
info: { title: Forms }
|
|
host: example.test
|
|
schemes: [https]
|
|
consumes: [application/json, multipart/form-data]
|
|
produces: [application/json]
|
|
paths:
|
|
/upload:
|
|
post:
|
|
operationId: upload
|
|
consumes: [application/x-www-form-urlencoded, multipart/form-data]
|
|
parameters:
|
|
- { name: title, in: formData, type: string, required: true }
|
|
- { name: count, in: formData, type: integer }
|
|
- { name: file, in: formData, type: file }
|
|
responses:
|
|
'200': { description: OK, schema: { type: object, properties: { id: { type: string } } } }
|
|
'204': { description: Empty }
|
|
"#;
|
|
|
|
let ir = normalize(document);
|
|
let operation = &ir.operations[0];
|
|
assert_eq!(operation.method, HttpMethod::Post);
|
|
assert_eq!(
|
|
operation
|
|
.request_body_variants
|
|
.iter()
|
|
.map(|variant| variant.content_type.as_str())
|
|
.collect::<Vec<_>>(),
|
|
vec!["application/x-www-form-urlencoded", "multipart/form-data"]
|
|
);
|
|
assert!(
|
|
operation
|
|
.request_body_variants
|
|
.iter()
|
|
.all(|variant| variant.required)
|
|
);
|
|
let Some(NormalizedSchemaKind::Object { properties, .. }) = operation
|
|
.request_body_variants
|
|
.first()
|
|
.and_then(|variant| variant.schema.as_ref())
|
|
.map(|schema| &schema.kind)
|
|
else {
|
|
panic!("formData must become an object request schema");
|
|
};
|
|
assert!(properties.contains_key("title"));
|
|
assert!(properties.contains_key("count"));
|
|
assert!(properties.contains_key("file"));
|
|
assert!(
|
|
operation
|
|
.findings
|
|
.iter()
|
|
.all(|finding| finding.code != "unsupported_consumes")
|
|
);
|
|
assert_eq!(
|
|
operation
|
|
.response_variants
|
|
.iter()
|
|
.map(|variant| (variant.status_code, variant.content_type.as_deref()))
|
|
.collect::<Vec<_>>(),
|
|
vec![(200, Some("application/json")), (204, None)]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn swagger2_operation_produces_overrides_global_and_preserves_json_variants() {
|
|
let document = r#"
|
|
swagger: '2.0'
|
|
info: { title: Responses }
|
|
host: example.test
|
|
schemes: [https]
|
|
produces: [application/json]
|
|
paths:
|
|
/items:
|
|
get:
|
|
operationId: listItems
|
|
produces: [application/problem+json, application/xml]
|
|
responses:
|
|
'200': { description: OK, schema: { type: array, items: { type: string } } }
|
|
"#;
|
|
|
|
let ir = normalize(document);
|
|
let operation = &ir.operations[0];
|
|
assert_eq!(
|
|
operation
|
|
.response_variants
|
|
.iter()
|
|
.map(|variant| variant.content_type.as_deref())
|
|
.collect::<Vec<_>>(),
|
|
vec![Some("application/problem+json")]
|
|
);
|
|
assert!(
|
|
operation
|
|
.findings
|
|
.iter()
|
|
.any(|finding| finding.code == "unsupported_produces")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn swagger2_imports_head_options_and_reports_path_blockers() {
|
|
let document = r#"
|
|
swagger: '2.0'
|
|
info: { title: Methods }
|
|
host: example.test
|
|
schemes: [https]
|
|
paths:
|
|
/health:
|
|
head:
|
|
operationId: healthHead
|
|
responses: { '204': { description: Empty } }
|
|
options:
|
|
operationId: healthOptions
|
|
responses: { '204': { description: Empty } }
|
|
/broken/{id}:
|
|
get:
|
|
parameters:
|
|
- { name: other, in: path, required: true, type: string }
|
|
responses: { '200': { description: OK } }
|
|
/malformed: invalid
|
|
"#;
|
|
|
|
let ir = normalize(document);
|
|
assert!(
|
|
ir.operations
|
|
.iter()
|
|
.any(|operation| operation.method == HttpMethod::Head)
|
|
);
|
|
assert!(
|
|
ir.operations
|
|
.iter()
|
|
.any(|operation| operation.method == HttpMethod::Options)
|
|
);
|
|
assert!(ir.operations.iter().any(|operation| {
|
|
operation.findings.iter().any(|finding| {
|
|
finding.code == "path_parameter_placeholder_missing"
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
})
|
|
}));
|
|
assert!(ir.findings.iter().any(|finding| {
|
|
finding.code == "invalid_path_item"
|
|
&& finding.severity == crank_import::rest::ImportFindingSeverity::Error
|
|
}));
|
|
}
|