ugi 0.2.1

Runtime-agnostic Rust request client with HTTP/1.1, HTTP/2, HTTP/3, H2C, WebSocket, SSE, and gRPC support
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
use std::path::PathBuf;

#[cfg(any(feature = "emulation", feature = "btls-backend"))]
use crate::browser_emulation::BoringTlsFingerprint;
use crate::error::{Error, ErrorKind, Result};
use crate::request::ProtocolPolicy;
use sha2::{Digest, Sha256};

/// Source of trusted root certificates for TLS verification.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum RootStore {
    /// Use the operating system's native certificate store.
    System,
    /// Use the WebPKI / Mozilla root certificates bundled with `webpki-roots`.
    WebPki,
    /// Load roots from a PEM file at the given path.
    PemFile(PathBuf),
    /// Load roots from an in-memory PEM string.
    Pem(String),
}

/// TLS configuration for a [`Client`](crate::Client) or individual request.
///
/// Constructed via the builder methods on this type and passed to
/// [`ClientBuilder::tls_config`](crate::ClientBuilder::tls_config) or
/// [`RequestBuilder::tls_config`](crate::RequestBuilder::tls_config).
#[derive(Clone, Debug, Default)]
pub struct TlsConfig {
    pub(crate) accept_invalid_certs: bool,
    pub(crate) backend: Option<TlsBackend>,
    pub(crate) alpn_protocols: Option<Vec<String>>,
    pub(crate) cipher_suites: Option<Vec<String>>,
    pub(crate) min_tls_version: Option<String>,
    pub(crate) max_tls_version: Option<String>,
    #[cfg(any(feature = "emulation", feature = "btls-backend"))]
    pub(crate) boring_tls_fingerprint: Option<BoringTlsFingerprint>,
    pub(crate) root_store: Option<RootStore>,
    pub(crate) pinned_certificates: Vec<PinnedCertificate>,
}

#[derive(Clone, Debug, Eq, PartialEq)]
pub struct PinnedCertificate {
    pub(crate) domain: String,
    pub(crate) fingerprint: Vec<u8>,
}

impl TlsConfig {
    pub fn danger_accept_invalid_certs(mut self, enabled: bool) -> Self {
        self.accept_invalid_certs = enabled;
        self
    }

    pub fn backend(mut self, backend: TlsBackend) -> Self {
        self.backend = Some(backend);
        self
    }

    pub fn alpn_protocols<I, S>(mut self, protocols: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        self.alpn_protocols = Some(
            protocols
                .into_iter()
                .map(|protocol| protocol.as_ref().to_owned())
                .collect(),
        );
        self
    }

    pub fn cipher_suites<I, S>(mut self, suites: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        self.cipher_suites = Some(
            suites
                .into_iter()
                .map(|suite| suite.as_ref().to_owned())
                .collect(),
        );
        self
    }

    pub fn min_tls_version(mut self, version: impl AsRef<str>) -> Self {
        self.min_tls_version = Some(version.as_ref().to_owned());
        self
    }

    pub fn max_tls_version(mut self, version: impl AsRef<str>) -> Self {
        self.max_tls_version = Some(version.as_ref().to_owned());
        self
    }

    #[cfg(feature = "emulation")]
    pub(crate) fn with_boring_tls_fingerprint(mut self, fingerprint: BoringTlsFingerprint) -> Self {
        self.boring_tls_fingerprint = Some(fingerprint);
        self
    }

    #[cfg(any(feature = "emulation", feature = "btls-backend"))]
    pub(crate) fn boring_tls_fingerprint(&self) -> Option<&BoringTlsFingerprint> {
        self.boring_tls_fingerprint.as_ref()
    }

    pub fn disable_alpn(mut self) -> Self {
        self.alpn_protocols = Some(Vec::new());
        self
    }

    pub fn root_store(mut self, root_store: RootStore) -> Self {
        self.root_store = Some(root_store);
        self
    }

    pub fn pin_certificate(
        mut self,
        domain: impl AsRef<str>,
        fingerprint: impl AsRef<str>,
    ) -> Result<Self> {
        self.pinned_certificates
            .push(PinnedCertificate::new(domain, fingerprint)?);
        Ok(self)
    }

    #[cfg(feature = "emulation")]
    pub(crate) fn ensure_emulation_backend(mut self) -> Result<Self> {
        #[cfg(feature = "btls-backend")]
        {
            match self.backend {
                Some(TlsBackend::Boring) => Ok(self),
                Some(_) => Err(Error::new(
                    ErrorKind::Transport,
                    "browser emulation requires the BoringSSL backend",
                )),
                None => {
                    self.backend = Some(TlsBackend::Boring);
                    Ok(self)
                }
            }
        }

        #[cfg(not(feature = "btls-backend"))]
        {
            let _ = &mut self;
            Err(Error::new(
                ErrorKind::Transport,
                "browser emulation requires the btls-backend feature",
            ))
        }
    }

    pub(crate) fn effective_alpn_protocols(&self, protocol_policy: ProtocolPolicy) -> Vec<String> {
        if let Some(protocols) = &self.alpn_protocols {
            return protocols.clone();
        }

        match protocol_policy {
            ProtocolPolicy::Http3Only => vec!["h3".to_owned()],
            _ => vec!["http/1.1".to_owned()],
        }
    }

    pub(crate) fn validate_http1_alpn(
        &self,
        protocol_policy: ProtocolPolicy,
    ) -> std::result::Result<Vec<String>, &'static str> {
        let protocols = self.effective_alpn_protocols(protocol_policy);
        if protocols.is_empty() || protocols.iter().all(|protocol| protocol == "http/1.1") {
            return Ok(protocols);
        }

        Err("custom ALPN protocols are not supported by the current HTTP/1 TLS transport")
    }

    #[cfg(feature = "h2")]
    pub(crate) fn validate_h2_alpn(&self) -> std::result::Result<Vec<String>, &'static str> {
        let protocols = self
            .alpn_protocols
            .clone()
            .unwrap_or_else(|| vec!["h2".to_owned()]);
        if protocols.iter().any(|protocol| protocol == "h2") {
            return Ok(protocols);
        }

        Err("HTTP/2 TLS transport requires ALPN to include h2")
    }

    #[cfg(feature = "h3")]
    pub(crate) fn validate_h3_alpn(&self) -> std::result::Result<Vec<String>, &'static str> {
        let protocols = self
            .alpn_protocols
            .clone()
            .unwrap_or_else(|| vec!["h3".to_owned()]);
        if protocols.iter().any(|protocol| protocol == "h3") {
            return Ok(protocols);
        }

        Err("HTTP/3 transport requires ALPN to include h3")
    }
}

#[cfg(feature = "rustls")]
fn cipher_from_str(name: &str) -> Option<rustls::SupportedCipherSuite> {
    let name = name.trim();
    let direct = rustls::ALL_CIPHER_SUITES
        .iter()
        .find(|suite| suite.suite().as_str().map_or(false, |n| n == name))
        .cloned();
    if direct.is_some() {
        return direct;
    }

    // rustls uses enum variant names like `TLS13_AES_128_GCM_SHA256`.
    // Many callers (and fingerprint datasets) use the IANA TLS 1.3 names
    // without the `TLS13_` prefix, e.g. `TLS_AES_128_GCM_SHA256`.
    if name.starts_with("TLS_") && !name.starts_with("TLS13_") && !name.contains("_WITH_") {
        let alt = format!("TLS13_{}", &name["TLS_".len()..]);
        return rustls::ALL_CIPHER_SUITES
            .iter()
            .find(|suite| suite.suite().as_str().map_or(false, |n| n == alt))
            .cloned();
    }

    None
}

#[cfg(feature = "rustls")]
fn version_gte(ver: &rustls::SupportedProtocolVersion, target: &str) -> bool {
    let target = target.trim();
    match (ver.version, target) {
        (rustls::ProtocolVersion::TLSv1_3, _) => true,
        (rustls::ProtocolVersion::TLSv1_2, "1.2") => true,
        (rustls::ProtocolVersion::TLSv1_2, "1.1") => true,
        (rustls::ProtocolVersion::TLSv1_2, "1.0") => true,
        _ => false,
    }
}

#[cfg(feature = "rustls")]
fn version_lte(ver: &rustls::SupportedProtocolVersion, target: &str) -> bool {
    let target = target.trim();
    match (ver.version, target) {
        (rustls::ProtocolVersion::TLSv1_2, "1.2") => true,
        (rustls::ProtocolVersion::TLSv1_3, "1.2") => false,
        (rustls::ProtocolVersion::TLSv1_3, "1.3") => true,
        (rustls::ProtocolVersion::TLSv1_2, "1.3") => true,
        _ => false,
    }
}

impl RootStore {
    pub fn system() -> Self {
        Self::System
    }

    pub fn webpki() -> Self {
        Self::WebPki
    }

    pub fn pem_file(path: impl Into<PathBuf>) -> Self {
        Self::PemFile(path.into())
    }

    pub fn pem(pem: impl Into<String>) -> Self {
        Self::Pem(pem.into())
    }

    pub(crate) fn pem_bytes(&self) -> Result<Option<Vec<u8>>> {
        match self {
            Self::PemFile(path) => std::fs::read(path).map(Some).map_err(|err| {
                Error::with_source(
                    ErrorKind::Transport,
                    format!("failed to read PEM root store from {}", path.display()),
                    err,
                )
            }),
            Self::Pem(pem) => Ok(Some(pem.as_bytes().to_vec())),
            Self::System | Self::WebPki => Ok(None),
        }
    }
}

impl PinnedCertificate {
    pub fn new(domain: impl AsRef<str>, fingerprint: impl AsRef<str>) -> Result<Self> {
        let domain = domain.as_ref().trim().to_ascii_lowercase();
        if domain.is_empty() {
            return Err(Error::new(
                ErrorKind::InvalidUrl,
                "pinned certificate domain cannot be empty",
            ));
        }
        let fingerprint = parse_fingerprint(fingerprint.as_ref())?;
        Ok(Self {
            domain,
            fingerprint,
        })
    }
}

pub(crate) fn verify_pinned_certificate(
    pins: &[PinnedCertificate],
    host: &str,
    cert_der: &[u8],
) -> Result<()> {
    let host = host.to_ascii_lowercase();
    let Some(pin) = pins.iter().find(|pin| pin.domain == host) else {
        return Ok(());
    };
    let actual = Sha256::digest(cert_der);
    if actual.as_slice() == pin.fingerprint.as_slice() {
        return Ok(());
    }
    Err(Error::new(
        ErrorKind::Transport,
        format!("pinned certificate fingerprint mismatch for {host}"),
    ))
}

fn parse_fingerprint(input: &str) -> Result<Vec<u8>> {
    let normalized = input
        .trim()
        .strip_prefix("sha256/")
        .unwrap_or(input.trim())
        .chars()
        .filter(|ch| !matches!(ch, ':' | ' ' | '-'))
        .collect::<String>();
    if normalized.len() != 64 || !normalized.chars().all(|ch| ch.is_ascii_hexdigit()) {
        return Err(Error::new(
            ErrorKind::Transport,
            "certificate fingerprint must be 32-byte SHA-256 hex",
        ));
    }

    let mut bytes = Vec::with_capacity(32);
    let chars = normalized.as_bytes();
    for i in (0..chars.len()).step_by(2) {
        let hi = decode_hex(chars[i])?;
        let lo = decode_hex(chars[i + 1])?;
        bytes.push((hi << 4) | lo);
    }
    Ok(bytes)
}

fn decode_hex(byte: u8) -> Result<u8> {
    match byte {
        b'0'..=b'9' => Ok(byte - b'0'),
        b'a'..=b'f' => Ok(byte - b'a' + 10),
        b'A'..=b'F' => Ok(byte - b'A' + 10),
        _ => Err(Error::new(
            ErrorKind::Transport,
            "certificate fingerprint must be valid hex",
        )),
    }
}

/// Selects the TLS implementation to use.
///
/// Only variants for features that are enabled at compile time are available.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum TlsBackend {
    #[cfg(feature = "rustls")]
    Rustls,
    #[cfg(feature = "native-tls")]
    Native,
    #[cfg(feature = "btls-backend")]
    Boring,
}

#[cfg(feature = "rustls")]
mod rustls_backend {
    use std::io::BufReader;
    use std::sync::Arc;
    use std::time::SystemTime;

    use rustls::client::{
        HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier, WebPkiVerifier,
    };
    use rustls::{
        Certificate, ClientConfig, DigitallySignedStruct, Error, RootCertStore, ServerName,
        SignatureScheme,
    };

    use super::{RootStore, TlsConfig, verify_pinned_certificate};

    impl TlsConfig {
        pub(crate) fn build_client_config(
            &self,
            protocol_policy: crate::request::ProtocolPolicy,
        ) -> std::result::Result<Arc<ClientConfig>, &'static str> {
            self.build_client_config_with_alpn(self.validate_http1_alpn(protocol_policy)?)
        }

        #[cfg(feature = "h2")]
        pub(crate) fn build_h2_client_config(
            &self,
        ) -> std::result::Result<Arc<ClientConfig>, &'static str> {
            self.build_client_config_with_alpn(self.validate_h2_alpn()?)
        }

        fn build_client_config_with_alpn(
            &self,
            alpn_protocols: Vec<String>,
        ) -> std::result::Result<Arc<ClientConfig>, &'static str> {
            let root_store = build_root_store(self)?;

            // TLS versions
            let mut versions: Vec<&'static rustls::SupportedProtocolVersion> =
                rustls::ALL_VERSIONS.iter().copied().collect();
            if let Some(min) = &self.min_tls_version {
                versions.retain(|v| super::version_gte(v, min));
            }
            if let Some(max) = &self.max_tls_version {
                versions.retain(|v| super::version_lte(v, max));
            }
            if versions.is_empty() {
                return Err("no TLS versions remain after filtering");
            }

            // Cipher suites
            let mut cipher_suites: Vec<rustls::SupportedCipherSuite> =
                if let Some(suites) = &self.cipher_suites {
                    suites
                        .iter()
                        .filter_map(|n| super::cipher_from_str(n))
                        .collect()
                } else {
                    rustls::ALL_CIPHER_SUITES.iter().cloned().collect()
                };
            cipher_suites.retain(|s| versions.contains(&s.version()));
            if cipher_suites.is_empty() {
                return Err("no TLS cipher suites remain after filtering");
            }

            let mut config = rustls::ClientConfig::builder()
                .with_cipher_suites(&cipher_suites)
                .with_kx_groups(&rustls::ALL_KX_GROUPS)
                .with_protocol_versions(&versions)
                .map_err(|_| "invalid tls versions")?
                .with_root_certificates(root_store.clone())
                .with_no_client_auth();

            if self.accept_invalid_certs || !self.pinned_certificates.is_empty() {
                let verifier = build_certificate_verifier(self, root_store)?;
                config.dangerous().set_certificate_verifier(verifier);
            }

            config.alpn_protocols = alpn_protocols
                .into_iter()
                .map(|protocol| protocol.into_bytes())
                .collect();

            Ok(Arc::new(config))
        }
    }

    fn build_root_store(config: &TlsConfig) -> std::result::Result<RootCertStore, &'static str> {
        match config.root_store.as_ref().unwrap_or(&RootStore::WebPki) {
            RootStore::WebPki => {
                let mut root_store = RootCertStore::empty();
                root_store.add_trust_anchors(webpki_roots::TLS_SERVER_ROOTS.0.iter().map(|ta| {
                    rustls::OwnedTrustAnchor::from_subject_spki_name_constraints(
                        ta.subject,
                        ta.spki,
                        ta.name_constraints,
                    )
                }));
                Ok(root_store)
            }
            RootStore::System => {
                let mut root_store = RootCertStore::empty();
                let certs = rustls_native_certs::load_native_certs();
                for err in certs.errors {
                    let _ = err;
                }
                let (added, _) = root_store.add_parsable_certificates(&certs.certs);
                if added == 0 {
                    return Err("failed to load any system root certificates");
                }
                Ok(root_store)
            }
            RootStore::PemFile(_) | RootStore::Pem(_) => {
                let pem = config
                    .root_store
                    .as_ref()
                    .and_then(|store| store.pem_bytes().ok())
                    .flatten()
                    .ok_or("failed to load PEM root store")?;
                let mut root_store = RootCertStore::empty();
                let mut reader = BufReader::new(pem.as_slice());
                let certs = rustls_pemfile::certs(&mut reader)
                    .map_err(|_| "failed to parse PEM root certificates")?;
                let (added, _) = root_store.add_parsable_certificates(&certs);
                if added == 0 {
                    return Err("failed to add any PEM root certificates");
                }
                Ok(root_store)
            }
        }
    }

    fn build_certificate_verifier(
        config: &TlsConfig,
        root_store: RootCertStore,
    ) -> std::result::Result<Arc<dyn ServerCertVerifier>, &'static str> {
        let inner = if config.accept_invalid_certs {
            None
        } else {
            Some(WebPkiVerifier::new(root_store, None))
        };
        Ok(Arc::new(PinnedVerifier {
            inner,
            pins: config.pinned_certificates.clone(),
        }))
    }

    struct PinnedVerifier {
        inner: Option<WebPkiVerifier>,
        pins: Vec<super::PinnedCertificate>,
    }

    impl std::fmt::Debug for PinnedVerifier {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("PinnedVerifier")
                .field("has_inner", &self.inner.is_some())
                .field("pins", &self.pins)
                .finish()
        }
    }

    impl ServerCertVerifier for PinnedVerifier {
        fn verify_server_cert(
            &self,
            end_entity: &Certificate,
            intermediates: &[Certificate],
            server_name: &ServerName,
            scts: &mut dyn Iterator<Item = &[u8]>,
            ocsp_response: &[u8],
            now: SystemTime,
        ) -> std::result::Result<ServerCertVerified, Error> {
            if let Some(inner) = &self.inner {
                inner.verify_server_cert(
                    end_entity,
                    intermediates,
                    server_name,
                    scts,
                    ocsp_response,
                    now,
                )?;
            }
            if let ServerName::DnsName(name) = server_name {
                verify_pinned_certificate(&self.pins, name.as_ref(), end_entity.0.as_ref())
                    .map_err(|err| Error::General(err.to_string()))?;
            }
            Ok(ServerCertVerified::assertion())
        }

        fn verify_tls12_signature(
            &self,
            message: &[u8],
            cert: &Certificate,
            dss: &DigitallySignedStruct,
        ) -> std::result::Result<HandshakeSignatureValid, Error> {
            if let Some(inner) = &self.inner {
                return inner.verify_tls12_signature(message, cert, dss);
            }
            Ok(HandshakeSignatureValid::assertion())
        }

        fn verify_tls13_signature(
            &self,
            message: &[u8],
            cert: &Certificate,
            dss: &DigitallySignedStruct,
        ) -> std::result::Result<HandshakeSignatureValid, Error> {
            if let Some(inner) = &self.inner {
                return inner.verify_tls13_signature(message, cert, dss);
            }
            Ok(HandshakeSignatureValid::assertion())
        }

        fn supported_verify_schemes(&self) -> Vec<SignatureScheme> {
            if let Some(inner) = &self.inner {
                return inner.supported_verify_schemes();
            }
            vec![
                SignatureScheme::ECDSA_NISTP256_SHA256,
                SignatureScheme::RSA_PSS_SHA256,
                SignatureScheme::RSA_PKCS1_SHA256,
                SignatureScheme::ED25519,
            ]
        }
    }
}

#[cfg(test)]
mod tests {
    use super::TlsConfig;
    use crate::ProtocolPolicy;

    #[test]
    fn tls_config_defaults_to_http1_alpn() {
        let protocols = TlsConfig::default().effective_alpn_protocols(ProtocolPolicy::Auto);
        assert_eq!(protocols, vec!["http/1.1".to_owned()]);
    }

    #[test]
    fn tls_config_can_disable_alpn() {
        let protocols = TlsConfig::default()
            .disable_alpn()
            .effective_alpn_protocols(ProtocolPolicy::Auto);
        assert!(protocols.is_empty());
    }

    #[test]
    fn tls_config_rejects_unsupported_http1_alpn_override() {
        let err = TlsConfig::default()
            .alpn_protocols(["h2"])
            .validate_http1_alpn(ProtocolPolicy::Auto)
            .unwrap_err();
        assert_eq!(
            err,
            "custom ALPN protocols are not supported by the current HTTP/1 TLS transport"
        );
    }

    #[cfg(feature = "h2")]
    #[test]
    fn tls_config_allows_h2_alpn_for_h2_transport() {
        let protocols = TlsConfig::default()
            .alpn_protocols(["h2", "http/1.1"])
            .validate_h2_alpn()
            .unwrap();
        assert_eq!(protocols, vec!["h2".to_owned(), "http/1.1".to_owned()]);
    }

    #[cfg(feature = "rustls")]
    #[test]
    fn tls_config_accepts_iana_tls13_cipher_suite_names() {
        // This is an IANA TLS 1.3 name. rustls uses `TLS13_...` enum variant names,
        // but we accept the IANA form for fingerprint datasets.
        let config = TlsConfig::default()
            .cipher_suites(["TLS_AES_128_GCM_SHA256"])
            .min_tls_version("1.3")
            .max_tls_version("1.3");

        let client_config = config
            .build_client_config(ProtocolPolicy::Http1Only)
            .expect("iana cipher suite name should be accepted");
        assert_eq!(client_config.alpn_protocols, vec![b"http/1.1".to_vec()]);
    }

    #[cfg(feature = "rustls")]
    #[test]
    fn tls_config_rejects_unknown_cipher_suite_names() {
        let config = TlsConfig::default()
            .cipher_suites(["TLS_NOT_A_REAL_CIPHER_SUITE"])
            .min_tls_version("1.3")
            .max_tls_version("1.3");

        let err = config
            .build_client_config(ProtocolPolicy::Http1Only)
            .unwrap_err();
        assert!(err.contains("cipher suites"));
    }

    #[cfg(feature = "rustls")]
    #[test]
    fn tls_config_rejects_invalid_tls_version_range() {
        let config = TlsConfig::default()
            .min_tls_version("1.3")
            .max_tls_version("1.2");
        let err = config
            .build_client_config(ProtocolPolicy::Http1Only)
            .unwrap_err();
        assert!(err.contains("TLS versions"));
    }
}