mod support;
use std::sync::Arc;

use hpx::{Client, cookie::Jar};
use http::header::COOKIE;
use support::server;

#[tokio::test]
async fn cookie_response_accessor() {
    let server = server::http(move |_req| async move {
        http::Response::builder()
            .header("Set-Cookie", "key=val")
            .header(
                "Set-Cookie",
                "expires=1; Expires=Wed, 21 Oct 2015 07:28:00 GMT",
            )
            .header("Set-Cookie", "path=1; Path=/the-path")
            .header("Set-Cookie", "maxage=1; Max-Age=100")
            .header("Set-Cookie", "domain=1; Domain=mydomain")
            .header("Set-Cookie", "secure=1; Secure")
            .header("Set-Cookie", "httponly=1; HttpOnly")
            .header("Set-Cookie", "samesitelax=1; SameSite=Lax")
            .header("Set-Cookie", "samesitestrict=1; SameSite=Strict")
            .body(Default::default())
            .unwrap()
    });

    let url = format!("http://{}/", server.addr());
    let res = hpx::get(&url).send().await.unwrap();

    let cookies = res.cookies().collect::<Vec<_>>();

    // key=val
    assert_eq!(cookies[0].name(), "key");
    assert_eq!(cookies[0].value(), "val");

    // expires
    assert_eq!(cookies[1].name(), "expires");
    assert_eq!(
        cookies[1].expires().unwrap(),
        std::time::SystemTime::UNIX_EPOCH + std::time::Duration::from_secs(1_445_412_480)
    );

    // path
    assert_eq!(cookies[2].name(), "path");
    assert_eq!(cookies[2].path().unwrap(), "/the-path");

    // max-age
    assert_eq!(cookies[3].name(), "maxage");
    assert_eq!(
        cookies[3].max_age().unwrap(),
        std::time::Duration::from_secs(100)
    );

    // domain
    assert_eq!(cookies[4].name(), "domain");
    assert_eq!(cookies[4].domain().unwrap(), "mydomain");

    // secure
    assert_eq!(cookies[5].name(), "secure");
    assert!(cookies[5].secure());

    // httponly
    assert_eq!(cookies[6].name(), "httponly");
    assert!(cookies[6].http_only());

    // samesitelax
    assert_eq!(cookies[7].name(), "samesitelax");
    assert!(cookies[7].same_site_lax());

    // samesitestrict
    assert_eq!(cookies[8].name(), "samesitestrict");
    assert!(cookies[8].same_site_strict());
}

#[tokio::test]
async fn cookie_store_simple() {
    let server = server::http(move |req| async move {
        if req.uri() == "/2" {
            assert_eq!(req.headers()["cookie"], "key=val");
        }
        http::Response::builder()
            .header("Set-Cookie", "key=val; HttpOnly")
            .body(Default::default())
            .unwrap()
    });

    let client = Client::builder().cookie_store(true).build().unwrap();

    let url = format!("http://{}/", server.addr());
    client.get(&url).send().await.unwrap();

    let url = format!("http://{}/2", server.addr());
    client.get(&url).send().await.unwrap();
}

#[tokio::test]
async fn cookie_store_overwrite_existing() {
    let server = server::http(move |req| async move {
        if req.uri() == "/" {
            http::Response::builder()
                .header("Set-Cookie", "key=val")
                .body(Default::default())
                .unwrap()
        } else if req.uri() == "/2" {
            assert_eq!(req.headers()["cookie"], "key=val");
            http::Response::builder()
                .header("Set-Cookie", "key=val2")
                .body(Default::default())
                .unwrap()
        } else {
            assert_eq!(req.uri(), "/3");
            assert_eq!(req.headers()["cookie"], "key=val2");
            http::Response::default()
        }
    });

    let client = Client::builder().cookie_store(true).build().unwrap();

    let url = format!("http://{}/", server.addr());
    client.get(&url).send().await.unwrap();

    let url = format!("http://{}/2", server.addr());
    client.get(&url).send().await.unwrap();

    let url = format!("http://{}/3", server.addr());
    client.get(&url).send().await.unwrap();
}

#[tokio::test]
async fn cookie_store_max_age() {
    let server = server::http(move |req| async move {
        assert_eq!(req.headers().get("cookie"), None);
        http::Response::builder()
            .header("Set-Cookie", "key=val; Max-Age=0")
            .body(Default::default())
            .unwrap()
    });

    let client = Client::builder().cookie_store(true).build().unwrap();
    let url = format!("http://{}/", server.addr());
    client.get(&url).send().await.unwrap();
    client.get(&url).send().await.unwrap();
}

#[tokio::test]
async fn cookie_store_expires() {
    let server = server::http(move |req| async move {
        assert_eq!(req.headers().get("cookie"), None);
        http::Response::builder()
            .header(
                "Set-Cookie",
                "key=val; Expires=Wed, 21 Oct 2015 07:28:00 GMT",
            )
            .body(Default::default())
            .unwrap()
    });

    let client = Client::builder().cookie_store(true).build().unwrap();

    let url = format!("http://{}/", server.addr());
    client.get(&url).send().await.unwrap();
    client.get(&url).send().await.unwrap();
}

#[tokio::test]
async fn cookie_store_path() {
    let server = server::http(move |req| async move {
        if req.uri() == "/" {
            assert_eq!(req.headers().get("cookie"), None);
            http::Response::builder()
                .header("Set-Cookie", "key=val; Path=/subpath")
                .body(Default::default())
                .unwrap()
        } else {
            assert_eq!(req.uri(), "/subpath");
            assert_eq!(req.headers()["cookie"], "key=val");
            http::Response::default()
        }
    });

    let client = Client::builder().cookie_store(true).build().unwrap();

    let url = format!("http://{}/", server.addr());
    client.get(&url).send().await.unwrap();
    client.get(&url).send().await.unwrap();

    let url = format!("http://{}/subpath", server.addr());
    client.get(&url).send().await.unwrap();
}

#[tokio::test]
async fn cookie_store_stores_response_cookie_with_manual_cookie() {
    let server = server::http(|req| async move {
        if req.uri() == "/1" {
            assert_eq!(req.headers()["cookie"], "key=val");
        }

        if req.uri() == "/2" {
            assert_eq!(req.headers()["cookie"], "key=val");
        }

        http::Response::builder()
            .header("Set-Cookie", "key=val; HttpOnly")
            .body(Default::default())
            .unwrap()
    });

    let client = Client::builder().cookie_store(true).build().unwrap();

    let set_url = format!("http://{}/1", server.addr());
    let _ = client
        .get(&set_url)
        .header("cookie", "key=val")
        .send()
        .await
        .unwrap();

    let check_url = format!("http://{}/2", server.addr());
    let _ = client.get(&check_url).send().await.unwrap();
}

#[tokio::test]
async fn cookie_request_level_compression() {
    let server = server::http(|req| async move {
        match req.uri().path() {
            "/set" => http::Response::builder()
                .header("Set-Cookie", "cookie1=value1")
                .header("Set-Cookie", "cookie2=value2")
                .header("Set-Cookie", "cookie3=value3")
                .body(Default::default())
                .unwrap(),
            "/default" | "/compressed" => {
                let cookies = req
                    .headers()
                    .get(COOKIE)
                    .and_then(|v| v.to_str().ok())
                    .unwrap();
                assert!(
                    cookies.contains("cookie1=value1")
                        && cookies.contains("cookie2=value2")
                        && cookies.contains("cookie3=value3")
                );
                assert!(cookies.contains("; "));
                http::Response::default()
            }
            "/uncompressed" => {
                let cookies: Vec<_> = req
                    .headers()
                    .get_all(COOKIE)
                    .iter()
                    .map(|v| v.to_str().unwrap())
                    .collect();

                assert_eq!(cookies.len(), 3);

                assert!(cookies.contains(&"cookie1=value1"));
                assert!(cookies.contains(&"cookie2=value2"));
                assert!(cookies.contains(&"cookie3=value3"));
                http::Response::default()
            }
            _ => unreachable!(),
        }
    });

    let base_url = format!("http://{}", server.addr());

    // Create a jar with compression enabled (default)
    let jar = Arc::new(Jar::default());

    // Create a client with this jar
    let client = Client::builder()
        .cookie_provider(jar.clone())
        .build()
        .unwrap();

    // Set cookies
    client
        .get(format!("{}/set", base_url))
        .send()
        .await
        .unwrap();

    // Request with default behavior (compressed)
    client
        .get(format!("{}/default", base_url))
        .send()
        .await
        .unwrap();

    // Request with compressed cookies
    client
        .get(format!("{}/compressed", base_url))
        .cookie_provider(jar.compressed())
        .send()
        .await
        .unwrap();

    // Request with uncompressed cookies
    client
        .get(format!("{}/uncompressed", base_url))
        .cookie_provider(jar.uncompressed())
        .send()
        .await
        .unwrap();
}
