Skip to content

Commit

Permalink
test(web-security): add tests for trusted-types CSP directive witho…
Browse files Browse the repository at this point in the history
…ut values
  • Loading branch information
azasypkin committed Oct 13, 2023
1 parent 2db6c0a commit 9e7cae4
Showing 1 changed file with 25 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub enum ContentSecurityPolicyDirective {
// 1 extension directive
#[serde(deserialize_with = "deserialize_directive_without_value")]
UpgradeInsecureRequests,
// 2 experimental directives
// 2 trusted types directives
RequireTrustedTypesFor([ContentSecurityPolicyRequireTrustedTypesForDirectiveValue; 1]),
TrustedTypes(HashSet<ContentSecurityPolicyTrustedTypesDirectiveValue>),
// 2 reporting directives
Expand Down Expand Up @@ -382,6 +382,13 @@ mod tests {
}
"###);

assert_json_snapshot!(ContentSecurityPolicyDirective::TrustedTypes(HashSet::new()), @r###"
{
"n": "trusted-types",
"v": []
}
"###);

assert_json_snapshot!(ContentSecurityPolicyDirective::ReportTo(["https://google.com".to_string()]), @r###"
{
"n": "report-to",
Expand Down Expand Up @@ -435,6 +442,13 @@ mod tests {
@r###""trusted-types 'allow-duplicates' my-another-policy my-policy""###
);

assert_debug_snapshot!(
String::try_from(
ContentSecurityPolicyDirective::TrustedTypes(HashSet::new())
)?,
@r###""trusted-types""###
);

Ok(())
}

Expand Down Expand Up @@ -675,6 +689,16 @@ mod tests {
ContentSecurityPolicyDirective::UpgradeInsecureRequests
);

let directive = serde_json::from_value::<Directive>(json!({
"name": "trusted-types",
"value": []
}))?;

assert_eq!(
ContentSecurityPolicyDirective::try_from(&directive)?,
ContentSecurityPolicyDirective::TrustedTypes(HashSet::new())
);

Ok(())
}

Expand Down

0 comments on commit 9e7cae4

Please sign in to comment.