index : static-web-server.git

ascending towards madness

author Jose Quintana <joseluisquintana20@gmail.com> 2022-03-13 22:32:03.0 +00:00:00
committer Jose Quintana <joseluisquintana20@gmail.com> 2022-03-13 22:32:03.0 +00:00:00
commit
7b6fc0b8999a0092f9fe62bc16d69189a2c69776 [patch]
tree
734d1ab7bf02219f02e65b40a569a44319c3ad5a
parent
d57ee2f9dcff62e5687adeaf05567b42ddd87840
download
7b6fc0b8999a0092f9fe62bc16d69189a2c69776.tar.gz

test: `cache-control` test cases



Diff

 src/control_headers.rs | 71 ++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 68 insertions(+), 3 deletions(-)

diff --git a/src/control_headers.rs b/src/control_headers.rs
index aecc904..2611e31 100644
--- a/src/control_headers.rs
+++ b/src/control_headers.rs
@@ -5,6 +5,12 @@
use headers::{CacheControl, HeaderMapExt};
use hyper::{Body, Response};

// Cache-Control `max-age` variants
const MAX_AGE_ONE_HOUR: u64 = 60 * 60;
const MAX_AGE_ONE_DAY: u64 = 60 * 60 * 24_u64;
const MAX_AGE_ONE_YEAR: u64 = 60 * 60 * 24 * 365;

// `Cache-Control` list of extensions
const CACHE_EXT_ONE_HOUR: [&str; 4] = ["atom", "json", "rss", "xml"];
const CACHE_EXT_ONE_YEAR: [&str; 32] = [
    "avif", "bmp", "bz2", "css", "doc", "gif", "gz", "htc", "ico", "jpeg", "jpg", "js", "jxl",
@@ -15,18 +21,18 @@ const CACHE_EXT_ONE_YEAR: [&str; 32] = [
/// It appends a `Cache-Control` header to a response if that one is part of a set of file types.
pub fn append_headers(uri: &str, resp: &mut Response<Body>) {
    // Default max-age value in seconds (one day)
    let mut max_age = 60 * 60 * 24_u64;
    let mut max_age = MAX_AGE_ONE_DAY;

    if CACHE_EXT_ONE_HOUR
        .iter()
        .any(|x| uri.ends_with(&[".", *x].concat()))
    {
        max_age = 60 * 60;
        max_age = MAX_AGE_ONE_HOUR;
    } else if CACHE_EXT_ONE_YEAR
        .iter()
        .any(|x| uri.ends_with(&[".", *x].concat()))
    {
        max_age = 60 * 60 * 24 * 365;
        max_age = MAX_AGE_ONE_YEAR;
    }

    let cache_control = CacheControl::new()
@@ -39,3 +45,62 @@ pub fn append_headers(uri: &str, resp: &mut Response<Body>) {
fn duration_from_secs(secs: u64) -> std::time::Duration {
    std::time::Duration::from_secs(std::cmp::min(secs, u32::MAX as u64))
}

#[cfg(test)]
mod tests {
    use hyper::{Body, Response, StatusCode};

    use super::{
        append_headers, CACHE_EXT_ONE_HOUR, CACHE_EXT_ONE_YEAR, MAX_AGE_ONE_DAY, MAX_AGE_ONE_HOUR,
        MAX_AGE_ONE_YEAR,
    };

    #[tokio::test]
    async fn headers_one_hour() {
        let mut resp = Response::new(Body::empty());
        *resp.status_mut() = StatusCode::OK;

        for ext in CACHE_EXT_ONE_HOUR.iter() {
            append_headers(&["/some.", ext].concat(), &mut resp);

            let cache_control = resp.headers().get(http::header::CACHE_CONTROL).unwrap();
            assert_eq!(resp.status(), StatusCode::OK);
            assert_eq!(
                cache_control.to_str().unwrap(),
                format!("public, max-age={}", MAX_AGE_ONE_HOUR)
            );
        }
    }

    #[tokio::test]
    async fn headers_one_day_default() {
        let mut resp = Response::new(Body::empty());
        *resp.status_mut() = StatusCode::OK;

        append_headers("/", &mut resp);

        let cache_control = resp.headers().get(http::header::CACHE_CONTROL).unwrap();
        assert_eq!(resp.status(), StatusCode::OK);
        assert_eq!(
            cache_control.to_str().unwrap(),
            format!("public, max-age={}", MAX_AGE_ONE_DAY)
        );
    }

    #[tokio::test]
    async fn headers_one_year() {
        let mut resp = Response::new(Body::empty());
        *resp.status_mut() = StatusCode::OK;

        for ext in CACHE_EXT_ONE_YEAR.iter() {
            append_headers(&["/some.", ext].concat(), &mut resp);

            let cache_control = resp.headers().get(http::header::CACHE_CONTROL).unwrap();
            assert_eq!(resp.status(), StatusCode::OK);
            assert_eq!(
                cache_control.to_str().unwrap(),
                format!("public, max-age={}", MAX_AGE_ONE_YEAR)
            );
        }
    }
}