brooks_lib/cdni/cors/
spec.rs1use crate::macros::IntoCdniMetadata;
21use brooks_macros::CdniMetadata;
22
23use regex::Regex;
24use serde::{Deserialize, Serialize};
25use serde_json::Value;
26
27use std::fmt::Debug;
28
29#[derive(Debug, Clone, Serialize, Deserialize)]
30pub struct AccessControlAllowOriginPattern {
31 pub pattern: String,
32
33 #[serde(skip)]
34 pub aug: Option<Regex>,
35}
36
37#[derive(Debug, Clone, Serialize, Deserialize)]
38pub struct AccessControlAllowOrigin<A: Debug + Clone + Default> {
39 #[serde(rename = "allow-list")]
40 pub allow_origin: Vec<AccessControlAllowOriginPattern>,
41 #[serde(rename = "wildcard-return")]
42 pub wildcard_return: bool,
43
44 #[serde(skip)]
45 pub aug: A,
46}
47
48#[derive(Debug, Clone, Serialize, Deserialize)]
49pub struct CrossoriginPolicy<A: Debug + Clone + Default> {
50 #[serde(rename = "allow-origin")]
51 pub allow_origin: AccessControlAllowOrigin<A>,
52 #[serde(rename = "preflight-only")]
53 #[serde(skip_serializing_if = "Option::is_none")]
54 pub preflight_only: Option<bool>,
55
56 #[serde(skip)]
57 pub aug: A,
58}
59
60#[derive(Debug, Clone, Serialize, Deserialize, CdniMetadata)]
61pub struct TypedCrossoriginPolicy<A: Debug + Clone + Default> {
62 #[serde(rename = "generic-metadata-type")]
63 pub tpe: String,
64 #[serde(rename = "generic-metadata-value")]
65 pub value: CrossoriginPolicy<A>,
66}
67
68#[cfg(test)]
69mod test_parse_cors_metadata {
70 use std::path::Path;
71
72 use crate::{cdni::cors::spec::TypedCrossoriginPolicy, tests::read_test_file};
73
74 #[test]
75 fn test_verify_draft_figure1() {
76 let json = read_test_file(Path::new("./src/cdni/tests/cors/figure1.json"));
77 let cop_metadata = serde_json::from_str::<TypedCrossoriginPolicy<()>>(&json)
78 .expect("Could not parse JSON test file");
79 assert_eq!(
80 cop_metadata.tpe,
81 TypedCrossoriginPolicy::<()>::typed_cdni_metadata_name()
82 );
83 }
84}