#[cfg(all(feature = "json", any(feature = "tls", feature = "tls-native")))]
// test temporarily disabled because httpbin is down / we need to figure out
// how to eliminate the external dependency.
// #[test]
// fn agent_set_header() {
//     use serde::Deserialize;
//     use std::collections::HashMap;

//     #[derive(Deserialize, Debug)]
//     struct HttpBin {
//         headers: HashMap<String, String>,
//     }

//     let agent = ureq::Agent::new();
//     let resp = agent
//         .get("https://httpbin.org/get")
//         .set("header", "value")
//         .set("Connection", "close")
//         .call()
//         .unwrap();
//     assert_eq!(resp.status(), 200);
//     let json: HttpBin = resp.into_json().unwrap();
//     // println!("{:?}", json);
//     assert_eq!("value", json.headers.get("Header").unwrap());
// }
#[test]
#[cfg(any(feature = "tls", feature = "tls-native"))]
// From here https://badssl.com/download/
// Decrypt key with: openssl rsa -in ./badssl.com-client.pem
fn tls_client_certificate() {
    const BADSSL_CLIENT_CERT_PEM: &str = r#"Bag Attributes
    localKeyID: 41 C3 6C 33 C7 E3 36 DD EA 4A 1F C0 B7 23 B8 E6 9C DC D8 0F
subject=C = US, ST = California, L = San Francisco, O = BadSSL, CN = BadSSL Client Certificate

issuer=C = US, ST = California, L = San Francisco, O = BadSSL, CN = BadSSL Client Root Certificate Authority

-----BEGIN CERTIFICATE-----
MIIEqDCCApCgAwIBAgIUK5Ns4y2CzosB/ZoFlaxjZqoBTIIwDQYJKoZIhvcNAQEL
BQAwfjELMAkGA1UEBhMCVVMxEzARBgNVBAgMCkNhbGlmb3JuaWExFjAUBgNVBAcM
DVNhbiBGcmFuY2lzY28xDzANBgNVBAoMBkJhZFNTTDExMC8GA1UEAwwoQmFkU1NM
IENsaWVudCBSb290IENlcnRpZmljYXRlIEF1dGhvcml0eTAeFw0xOTExMjcwMDE5
NTdaFw0yMTExMjYwMDE5NTdaMG8xCzAJBgNVBAYTAlVTMRMwEQYDVQQIDApDYWxp
Zm9ybmlhMRYwFAYDVQQHDA1TYW4gRnJhbmNpc2NvMQ8wDQYDVQQKDAZCYWRTU0wx
IjAgBgNVBAMMGUJh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l4NW5Qryw18rDY+UivnaZkEdEsnlo3fc8MSQF78dDHCX
nwmHfOmBnBoSbLl+W5ByHkJRHOnX+8qKq9ePNFUMf/hZNYuma9BCFBUCgYEA0m2p
VDn12YdhFUUBIH91aD5cQIsBhkHFU4vqW4zBt6TsJpFciWbrBrTeRzeDou59aIsn
zGBrLMykOY+EwwRku9KTVM4U791Z/NFbH89GqyUaicb4or+BXw5rGF8DmzSsDo0f
ixJ9TVD5DmDi3c9ZQ7ljrtdSxPdA8kOoYPFsApkCgYEA08uZSPQAI6aoe/16UEK4
Rk9qhz47kHlNuVZ27ehoyOzlQ5Lxyy0HacmKaxkILOLPuUxljTQEWAv3DAIdVI7+
WMN41Fq0eVe9yIWXoNtGwUGFirsA77YVSm5RcN++3GQMZedUfUAl+juKFvJkRS4j
MTkXdGw+mDa3/wsjTGSa2mECgYABO6NCWxSVsbVf6oeXKSgG9FaWCjp4DuqZErjM
0IZSDSVVFIT2SSQXZffncuvSiJMziZ0yFV6LZKeRrsWYXu44K4Oxe4Oj5Cgi0xc1
mIFRf2YoaIIMchLP+8Wk3ummfyiC7VDB/9m8Gj1bWDX8FrrvKqbq31gcz1YSFVNn
PgLkAQKBgFzG8NdL8os55YcjBcOZMUs5QTKiQSyZM0Abab17k9JaqsU0jQtzeFsY
FTiwh2uh6l4gdO/dGC/P0Vrp7F05NnO7oE4T+ojDzVQMnFpCBeL7x08GfUQkphEG
m0Wqhhi8/24Sy934t5Txgkfoltg8ahkx934WjP6WWRnSAu+cf+vW
-----END RSA PRIVATE KEY-----
"#;

    use ureq::OrAnyStatus;

    let certs = rustls_pemfile::certs(&mut BADSSL_CLIENT_CERT_PEM.as_bytes())
        .collect::<Result<Vec<_>, _>>()
        .unwrap();
    let key = rustls_pemfile::private_key(&mut BADSSL_CLIENT_CERT_PEM.as_bytes())
        .unwrap()
        .unwrap();

    let root_store = rustls::RootCertStore {
        roots: webpki_roots::TLS_SERVER_ROOTS.iter().cloned().collect(),
    };

    let tls_config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_client_auth_cert(certs, key)
        .unwrap();

    let agent = ureq::builder()
        .tls_config(std::sync::Arc::new(tls_config))
        .build();

    let resp = agent.get("https://client.badssl.com/").call();

    // 26 Nov 2021, client.badssl.com responds with a 400:
    // In practice this doesn't matter since this test only tries to prove that
    // we can use a client certificate and the TLS negotiation does work.
    // However our test used to check for a 200, and thus fails.

    // < HTTP/1.1 400 Bad Request
    // < Server: nginx/1.10.3 (Ubuntu)
    // < Date: Fri, 26 Nov 2021 13:13:23 GMT
    // < Content-Type: text/html
    // < Content-Length: 240
    // < Connection: close
    // <
    // <html>
    // <head><title>400 The SSL certificate error</title></head>
    // <body bgcolor="white">
    // <center><h1>400 Bad Request</h1></center>
    // <center>The SSL certificate error</center>
    // <hr><center>nginx/1.10.3 (Ubuntu)</center>
    // </body>

    // We accept that 400 error, but .unwrap() here will fail if the TLS
    // negotiation didn't succeed, and that's what we're testing for.
    let resp = resp.or_any_status().unwrap();

    assert!(resp.into_string().unwrap().len() > 10);
}

// This tests that IPv6 addresses as host names work.
// This is a regression test for passing the host name to `rustls::ServerName::try_from(host_name)`
#[test]
#[cfg(any(feature = "tls", feature = "tls-native"))]
fn ipv6_addr_in_dns_name() {
    let root_store = rustls::RootCertStore {
        roots: webpki_roots::TLS_SERVER_ROOTS.to_vec(),
    };

    let tls_config = rustls::ClientConfig::builder()
        .with_root_certificates(root_store)
        .with_no_client_auth();

    let agent = ureq::builder()
        .tls_config(std::sync::Arc::new(tls_config))
        .build();

    let resp = agent.get("https://[2606:4700:4700::1111]/").call();

    assert!(
        !matches!(resp, Err(ureq::Error::Transport(ref t)) if t.kind() == ureq::ErrorKind::Dns)
    );
}
