Skip to content

Commit 86b6a75

Browse files
committed
Finish up tests
1 parent 1f76692 commit 86b6a75

File tree

3 files changed

+104
-74
lines changed

3 files changed

+104
-74
lines changed

src/security/csp.rs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -103,18 +103,29 @@ pub struct ReportToEndpoint {
103103
/// # Examples
104104
///
105105
/// ```
106-
/// let mut policy = http_types::security::new()
107-
/// .default_src(http_types::security::Source::SameOrigin)
106+
/// use http_types::{headers, security, Response, StatusCode};
107+
///
108+
/// let mut policy = security::ContentSecurityPolicy::new();
109+
/// policy
110+
/// .default_src(security::Source::SameOrigin)
108111
/// .default_src("areweasyncyet.rs")
109-
/// .script_src(http_types::security::Source::SameOrigin)
110-
/// .object_src(http_types::security::Source::None)
111-
/// .base_uri(http_types::security::Source::None)
112+
/// .script_src(security::Source::SameOrigin)
113+
/// .script_src(security::Source::UnsafeInline)
114+
/// .object_src(security::Source::None)
115+
/// .base_uri(security::Source::None)
112116
/// .upgrade_insecure_requests();
113117
///
114-
/// let mut headers = http::Headers::new();
115-
/// policy.apply(&mut headers);
118+
/// let mut res = Response::new(StatusCode::Ok);
119+
/// res.set_body("Hello, Chashu!");
120+
///
121+
/// security::default(&mut res);
122+
/// policy.apply(&mut res);
116123
///
117-
/// assert_eq!(headers["content-security-policy"], "base-uri 'none'; default-src 'self' areweasyncyet.rs; object-src 'none'; script-src 'self'; upgrade-insecure-requests");
124+
/// let name =
125+
/// headers::HeaderName::from_ascii("content-security-policy".to_owned().into_bytes()).unwrap();
126+
/// let headers = res.header(&name).unwrap();
127+
/// let header = headers.iter().next().unwrap();
128+
/// assert_eq!(header, "base-uri 'none'; default-src 'self' areweasyncyet.rs; object-src 'none'; script-src 'self' 'unsafe-inline'; upgrade-insecure-requests");
118129
/// ```
119130
120131
#[derive(Debug)]

src/security/mod.rs

Lines changed: 78 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,31 @@
11
//! HTTP Security Headers.
22
//!
3-
//! Adapted from [helmetjs](https://helmetjs.github.io/).
4-
//!
5-
//! ## Example
6-
//! ```
7-
//! let mut headers = http::Headers::new();
8-
//! http_types::security::default(&mut headers);
9-
//! assert_eq!(headers["X-Content-Type-Options"], "nosniff");
10-
//! assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
11-
//! ```
3+
// //! ## Example
4+
// //!
5+
// //! ```
6+
// //! use http_types::Response;
7+
// //!
8+
// //! let mut res = Response::new(StatusCode::Ok);
9+
// //! http_types::security::default(&mut res);
10+
// //! assert_eq!(res["X-Content-Type-Options"], "nosniff");
11+
// //! assert_eq!(res["X-XSS-Protection"], "1; mode=block");
12+
// //! ```
1213
use crate::headers::{HeaderName, Headers};
1314
pub use csp::{ContentSecurityPolicy, ReportTo, ReportToEndpoint, Source};
1415

1516
mod csp;
1617

1718
/// Apply a set of default protections.
1819
///
19-
/// ## Examples
20-
/// ```
21-
/// let mut headers = http::Headers::new();
22-
/// http_types::security::default(&mut headers);
23-
/// assert_eq!(headers["X-Content-Type-Options"], "nosniff");
24-
/// assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
25-
/// ```
20+
// /// ## Examples
21+
// /// ```
22+
// /// use http_types::Response;
23+
// ///
24+
// /// let mut res = Response::new(StatusCode::Ok);
25+
// /// http_types::security::default(&mut headers);
26+
// /// assert_eq!(headers["X-Content-Type-Options"], "nosniff");
27+
// /// assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
28+
// /// ```
2629
pub fn default(mut headers: impl AsMut<Headers>) {
2730
dns_prefetch_control(&mut headers);
2831
nosniff(&mut headers);
@@ -36,12 +39,14 @@ pub fn default(mut headers: impl AsMut<Headers>) {
3639
///
3740
/// [read more](https://helmetjs.github.io/docs/dns-prefetch-control/)
3841
///
39-
/// ## Examples
40-
/// ```
41-
/// let mut headers = http::Headers::new();
42-
/// http_types::security::dns_prefetch_control(&mut headers);
43-
/// assert_eq!(headers["X-DNS-Prefetch-Control"], "on");
44-
/// ```
42+
// /// ## Examples
43+
// /// ```
44+
// /// use http_types::Response;
45+
// ///
46+
// /// let mut res = Response::new(StatusCode::Ok);
47+
// /// http_types::security::dns_prefetch_control(&mut headers);
48+
// /// assert_eq!(headers["X-DNS-Prefetch-Control"], "on");
49+
// /// ```
4550
#[inline]
4651
pub fn dns_prefetch_control(mut headers: impl AsMut<Headers>) {
4752
headers
@@ -63,12 +68,14 @@ pub enum FrameOptions {
6368
///
6469
/// [read more](https://helmetjs.github.io/docs/frameguard/)
6570
///
66-
/// ## Examples
67-
/// ```
68-
/// let mut headers = http::Headers::new();
69-
/// http_types::security::frameguard(&mut headers, None);
70-
/// assert_eq!(headers["X-Frame-Options"], "sameorigin");
71-
/// ```
71+
// /// ## Examples
72+
// /// ```
73+
// /// use http_types::Response;
74+
// ///
75+
// /// let mut res = Response::new(StatusCode::Ok);
76+
// /// http_types::security::frameguard(&mut headers, None);
77+
// /// assert_eq!(headers["X-Frame-Options"], "sameorigin");
78+
// /// ```
7279
#[inline]
7380
pub fn frameguard(mut headers: impl AsMut<Headers>, guard: Option<FrameOptions>) {
7481
let kind = match guard {
@@ -83,13 +90,15 @@ pub fn frameguard(mut headers: impl AsMut<Headers>, guard: Option<FrameOptions>)
8390
///
8491
/// [read more](https://helmetjs.github.io/docs/hide-powered-by/)
8592
///
86-
/// ## Examples
87-
/// ```
88-
/// let mut headers = http::Headers::new();
89-
/// headers.as_mut().insert("X-Powered-By", "Tide/Rust".parse().unwrap());
90-
/// http_types::security::hide_powered_by(&mut headers);
91-
/// assert_eq!(headers.get("X-Powered-By"), None);
92-
/// ```
93+
// /// ## Examples
94+
// /// ```
95+
// /// use http_types::Response;
96+
// ///
97+
// /// let mut res = Response::new(StatusCode::Ok);
98+
// /// headers.as_mut().insert("X-Powered-By", "Tide/Rust".parse().unwrap());
99+
// /// http_types::security::hide_powered_by(&mut headers);
100+
// /// assert_eq!(headers.get("X-Powered-By"), None);
101+
// /// ```
93102
#[inline]
94103
pub fn hide_powered_by(mut headers: impl AsMut<Headers>) {
95104
headers
@@ -104,12 +113,14 @@ pub fn hide_powered_by(mut headers: impl AsMut<Headers>) {
104113
///
105114
/// [read more](https://helmetjs.github.io/docs/hsts/)
106115
///
107-
/// ## Examples
108-
/// ```
109-
/// let mut headers = http::Headers::new();
110-
/// http_types::security::hsts(&mut headers);
111-
/// assert_eq!(headers["Strict-Transport-Security"], "max-age=5184000");
112-
/// ```
116+
// /// ## Examples
117+
// /// ```
118+
// /// use http_types::Response;
119+
// ///
120+
// /// let mut res = Response::new(StatusCode::Ok);
121+
// /// http_types::security::hsts(&mut headers);
122+
// /// assert_eq!(headers["Strict-Transport-Security"], "max-age=5184000");
123+
// /// ```
113124
#[inline]
114125
pub fn hsts(mut headers: impl AsMut<Headers>) {
115126
headers
@@ -123,12 +134,14 @@ pub fn hsts(mut headers: impl AsMut<Headers>) {
123134
///
124135
/// [read more](https://helmetjs.github.io/docs/dont-sniff-mimetype/)
125136
///
126-
/// ## Examples
127-
/// ```
128-
/// let mut headers = http::Headers::new();
129-
/// http_types::security::nosniff(&mut headers);
130-
/// assert_eq!(headers["X-Content-Type-Options"], "nosniff");
131-
/// ```
137+
// /// ## Examples
138+
// /// ```
139+
// /// use http_types::Response;
140+
// ///
141+
// /// let mut res = Response::new(StatusCode::Ok);
142+
// /// http_types::security::nosniff(&mut headers);
143+
// /// assert_eq!(headers["X-Content-Type-Options"], "nosniff");
144+
// /// ```
132145
#[inline]
133146
pub fn nosniff(mut headers: impl AsMut<Headers>) {
134147
headers
@@ -141,12 +154,14 @@ pub fn nosniff(mut headers: impl AsMut<Headers>) {
141154
///
142155
/// [read more](https://helmetjs.github.io/docs/xss-filter/)
143156
///
144-
/// ## Examples
145-
/// ```
146-
/// let mut headers = http::Headers::new();
147-
/// http_types::security::xss_filter(&mut headers);
148-
/// assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
149-
/// ```
157+
// /// ## Examples
158+
// /// ```
159+
// /// use http_types::Response;
160+
// ///
161+
// /// let mut res = Response::new(StatusCode::Ok);
162+
// /// http_types::security::xss_filter(&mut headers);
163+
// /// assert_eq!(headers["X-XSS-Protection"], "1; mode=block");
164+
// /// ```
150165
#[inline]
151166
pub fn xss_filter(mut headers: impl AsMut<Headers>) {
152167
headers
@@ -183,14 +198,16 @@ pub enum ReferrerOptions {
183198
/// [Mozilla Developer Network](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Referrer-Policy)
184199
///
185200
///
186-
/// ## Examples
187-
/// ```
188-
/// let mut headers = http::Headers::new();
189-
/// http_types::security::referrer_policy(&mut headers, Some(http_types::security::ReferrerOptions::UnsafeUrl));
190-
/// http_types::security::referrer_policy(&mut headers, None);
191-
/// let mut referrerValues: Vec<&str> = headers.get_all("Referrer-Policy").iter().map(|x| x.to_str().unwrap()).collect();
192-
/// assert_eq!(referrerValues.sort(), vec!("unsafe-url", "no-referrer").sort());
193-
/// ```
201+
// /// ## Examples
202+
// /// ```
203+
// /// use http_types::Response;
204+
// ///
205+
// /// let mut res = Response::new(StatusCode::Ok);
206+
// /// http_types::security::referrer_policy(&mut headers, Some(http_types::security::ReferrerOptions::UnsafeUrl));
207+
// /// http_types::security::referrer_policy(&mut headers, None);
208+
// /// let mut referrerValues: Vec<&str> = headers.get_all("Referrer-Policy").iter().map(|x| x.to_str().unwrap()).collect();
209+
// /// assert_eq!(referrerValues.sort(), vec!("unsafe-url", "no-referrer").sort());
210+
// /// ```
194211
#[inline]
195212
pub fn referrer_policy(mut headers: impl AsMut<Headers>, referrer: Option<ReferrerOptions>) {
196213
let policy = match referrer {

tests/security.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use http_types::{headers, security, Response, StatusCode};
1+
use http_types::{headers::HeaderName, security, Response, StatusCode};
22

33
#[test]
44
fn security_test() {
@@ -18,9 +18,11 @@ fn security_test() {
1818
security::default(&mut res);
1919
policy.apply(&mut res);
2020

21-
let name =
22-
headers::HeaderName::from_ascii("content-security-policy".to_owned().into_bytes()).unwrap();
23-
let headers = res.header(&name).unwrap();
24-
let header = headers.iter().next().unwrap();
21+
let header = res
22+
.header(&HeaderName::from_ascii("content-security-policy".to_owned().into_bytes()).unwrap())
23+
.unwrap()
24+
.iter()
25+
.next()
26+
.unwrap();
2527
assert_eq!(header, "base-uri 'none'; default-src 'self' areweasyncyet.rs; object-src 'none'; script-src 'self' 'unsafe-inline'; upgrade-insecure-requests");
2628
}

0 commit comments

Comments
 (0)