self_update 1.3.1

Self updates for standalone executables
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
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
#![cfg(feature = "ureq")]

use std::time::Duration;

use ureq::tls::TlsProvider;
use ureq::{Agent, Body, http::Response};

use super::{HeaderMap, HttpClient, HttpResponse};
use crate::errors::{status_to_error, status_to_error_with_headers};
use crate::{Error, Result};

/// The certificate set a crate-built ureq agent trusts. `Vec<Certificate<'static>>` so the roots
/// outlive the per-call agent built from them.
#[cfg(any(not(feature = "reqwest"), test))]
type UreqRootCerts = std::sync::Arc<Vec<ureq::tls::Certificate<'static>>>;

/// How a [`UreqClient`] obtains the agent for each request.
enum UreqInner {
    /// Build a fresh per-call agent honoring the per-request timeout, the TLS feature, and proxy-env.
    Default,
    /// A user-injected agent (via `From<ureq::Agent>` / the `ureq_agent` setter) that owns its own
    /// timeout/TLS/proxy config, so the per-request timeout is *not* applied to it.
    Injected(Agent),
    /// Build a fresh per-call agent (like [`Default`](UreqInner::Default), so it still honors the
    /// per-request timeout) that trusts these custom root certificates and/or routes through this
    /// programmatic proxy. A configured proxy *replaces* proxy-env for this agent (ureq has a
    /// single proxy slot); with no proxy configured, proxy-env still applies.
    #[cfg(any(not(feature = "reqwest"), test))]
    Configured {
        certs: Option<UreqRootCerts>,
        proxy: Option<ureq::Proxy>,
    },
}

/// Sync [`HttpClient`] backed by a `ureq::Agent`.
pub struct UreqClient(UreqInner);

impl Default for UreqClient {
    fn default() -> Self {
        Self(UreqInner::Default)
    }
}

impl From<Agent> for UreqClient {
    fn from(agent: Agent) -> Self {
        Self(UreqInner::Injected(agent))
    }
}

/// The trust store a crate-built ureq agent starts from, before any caller-supplied certificates
/// replace it (CORP-2-4).
///
/// ureq's own default is [`RootCerts::WebPki`](ureq::tls::RootCerts::WebPki) -- Mozilla's bundled
/// roots, which ignore the machine's trust store entirely. That is the safer default for a
/// self-updater (it cannot be widened by anything installed on the host), so it stays the default
/// here. The `native-certs` feature opts into
/// [`PlatformVerifier`](ureq::tls::RootCerts::PlatformVerifier) instead, which is what a caller
/// behind a TLS-intercepting corporate proxy needs: the company CA lives in the OS store, not in
/// Mozilla's list.
///
/// Note the rustls lane *panics* on `PlatformVerifier` without ureq's `platform-verifier` feature
/// (`ureq::tls::rustls`), which is exactly what `native-certs = ["ureq?/platform-verifier"]`
/// guarantees -- so the two must stay wired together.
fn default_root_certs() -> ureq::tls::RootCerts {
    #[cfg(feature = "native-certs")]
    {
        ureq::tls::RootCerts::PlatformVerifier
    }
    #[cfg(not(feature = "native-certs"))]
    {
        ureq::tls::RootCerts::WebPki
    }
}

/// Build a per-call ureq agent honoring the per-request `timeout`, the TLS feature, and proxy-env.
/// `root_certs`, when `Some`, replaces the default trust store (see [`default_root_certs`]) with
/// the supplied certificates. `proxy`, when `Some`, replaces the env-var proxy for this agent
/// (ureq's config has a single proxy slot, so a programmatic proxy and `HTTP(S)_PROXY` cannot both
/// apply -- CORP-3-9); when `None`, `ureq::Proxy::try_from_env()` keeps the env behavior.
fn build_call_agent(
    timeout: Option<Duration>,
    #[cfg(any(not(feature = "reqwest"), test))] root_certs: Option<UreqRootCerts>,
    #[cfg(any(not(feature = "reqwest"), test))] proxy: Option<ureq::Proxy>,
) -> Agent {
    use ureq::tls::TlsConfig;
    // When both TLS features are enabled, rustls wins (it is the crate default); otherwise fall
    // back to native-tls (also the case when no TLS feature is set).
    #[cfg(feature = "rustls")]
    let provider = TlsProvider::Rustls;
    #[cfg(not(feature = "rustls"))]
    let provider = TlsProvider::NativeTls;

    #[cfg(any(not(feature = "reqwest"), test))]
    let mut tls = TlsConfig::builder()
        .provider(provider)
        .root_certs(default_root_certs());
    #[cfg(all(feature = "reqwest", not(test)))]
    let tls = TlsConfig::builder()
        .provider(provider)
        .root_certs(default_root_certs());
    #[cfg(any(not(feature = "reqwest"), test))]
    if let Some(certs) = root_certs {
        tls = tls.root_certs(ureq::tls::RootCerts::Specific(certs));
    }
    // A programmatic proxy wins over the environment; with none configured, honor
    // HTTP(S)_PROXY / NO_PROXY env vars (reqwest does this automatically).
    #[cfg(any(not(feature = "reqwest"), test))]
    let proxy = proxy.or_else(ureq::Proxy::try_from_env);
    #[cfg(all(feature = "reqwest", not(test)))]
    let proxy = ureq::Proxy::try_from_env();
    let config = Agent::config_builder()
        .tls_config(tls.build())
        .timeout_global(timeout)
        .proxy(proxy)
        // Disable ureq's built-in status-error so we reach our own is_success() check, which has
        // the response headers in hand and maps the status through `status_to_error_with_headers`
        // (NotFound / Unauthorized / RateLimited / HttpStatus).
        .http_status_as_error(false)
        .build();
    Agent::new_with_config(config)
}

// `client_with_root_certs` only dispatches to the ureq builder when reqwest is NOT also enabled
// (reqwest wins, exactly like `default_client`), so this is dead in a both-features lib build. Gate
// it to the lanes that actually reach it (and to `test`, where the ureq cert test exercises it).
#[cfg(any(not(feature = "reqwest"), test))]
impl UreqClient {
    /// Build a UreqClient that trusts the supplied custom root CA certificates and/or routes
    /// through the supplied proxy.
    ///
    /// The certificates and the proxy URL are parsed and validated here (a malformed PEM
    /// certificate or an unparseable proxy URL returns `Err`); the agent itself is built per
    /// request in [`get`](HttpClient::get), so it still honors the per-request timeout.
    /// `RootCerts::Specific` replaces the default trust store, so only the supplied certificates
    /// are trusted (see the `add_root_certificate` docs), and a configured proxy replaces the
    /// env-var proxy (see `build_call_agent`).
    pub(crate) fn build_configured(
        config: crate::http_client::ClientConfig<'_>,
    ) -> std::result::Result<
        std::sync::Arc<dyn crate::http_client::HttpClient>,
        crate::http_client::ClientConfigError,
    > {
        use crate::http_client::ClientConfigError;
        let mut ureq_certs = Vec::with_capacity(config.certs.len());
        for cert in config.certs {
            let c = if cert.is_pem() {
                ureq::tls::Certificate::from_pem(cert.bytes()).map_err(|e| {
                    ClientConfigError::Other(format!("invalid PEM certificate: {e}").into())
                })?
            } else {
                // `from_der` is infallible in ureq; invalid DER bytes are surfaced at connection
                // time, not here (documented on `Certificate::from_der` / `add_root_certificate`).
                ureq::tls::Certificate::from_der(cert.bytes())
            };
            ureq_certs.push(c.to_owned());
        }
        let proxy = match config.proxy {
            // ureq parses the credentials out of the URL itself and sends them as
            // `Proxy-Authorization`. Note ureq resolves a scheme-less URL (`host:port`) to an HTTP
            // CONNECT proxy, and rejects a socks:// URL unless its `socks-proxy` feature is on --
            // this crate does not enable it (SOCKS is an explicit non-goal).
            Some(url) => Some(ureq::Proxy::new(url).map_err(|e| {
                ClientConfigError::Proxy(crate::errors::proxy_error_message(url, e).into())
            })?),
            None => None,
        };
        Ok(std::sync::Arc::new(UreqClient(UreqInner::Configured {
            certs: (!ureq_certs.is_empty()).then(|| std::sync::Arc::new(ureq_certs)),
            proxy,
        })))
    }
}

/// Whether the per-request `http_status_as_error(false)` override in [`get`](HttpClient::get) is
/// needed for this call. Only an injected agent whose OWN config still has
/// `http_status_as_error(true)` (ureq's default) needs it. A caller who already disabled it at the
/// agent level needs no override -- skipping it there avoids the request-level TLS-config-cache
/// cost documented at the call site, and it is the only lane that keeps that agent's own TLS cache
/// warm across requests. A crate-built (non-injected) agent never takes this path at all, since it
/// is always built with `http_status_as_error(false)` (see `build_call_agent`).
fn needs_status_override(is_injected: bool, agent: &Agent) -> bool {
    is_injected && agent.config().http_status_as_error()
}

impl HttpClient for UreqClient {
    fn get(
        &self,
        url: &str,
        headers: &HeaderMap,
        timeout: Option<Duration>,
    ) -> Result<Box<dyn HttpResponse>> {
        // An injected agent owns its own timeout/TLS/proxy config, so the per-request `timeout` is
        // only applied to the crate-built (Default / Certs) agents.
        let built_agent;
        let (agent, is_injected): (&Agent, bool) = match &self.0 {
            UreqInner::Injected(agent) => (agent, true),
            UreqInner::Default => {
                built_agent = build_call_agent(
                    timeout,
                    #[cfg(any(not(feature = "reqwest"), test))]
                    None,
                    #[cfg(any(not(feature = "reqwest"), test))]
                    None,
                );
                (&built_agent, false)
            }
            #[cfg(any(not(feature = "reqwest"), test))]
            UreqInner::Configured { certs, proxy } => {
                built_agent = build_call_agent(timeout, certs.clone(), proxy.clone());
                (&built_agent, false)
            }
        };
        let mut req = agent.get(url);
        // Skip the override entirely when the injected agent's own config already disables
        // ureq's status-error (see the cost this avoids, described below) -- a free win for that
        // class of callers, and it makes this a genuinely distinct code path from the one below.
        if needs_status_override(is_injected, agent) {
            // An injected agent that still has ureq's default `http_status_as_error(true)` would
            // turn a non-2xx into a headerless `ureq::Error::StatusCode`. ureq 3 supports overriding
            // that per request, so apply the override here: the response then reaches the
            // header-aware check at the bottom of `get` and every client lane classifies a non-2xx
            // identically. Nothing about the *values* of the injected agent's own timeout/TLS/proxy
            // config is touched by this -- but the override is not free. Verified against the locked
            // ureq 3.3.0 (`Cargo.lock`): `RequestBuilder::config()` inserts a `RequestLevelConfig`
            // extension into the request, which `run()` (`run.rs:37-41`) reads back as
            // `ConnectionDetails::request_level = true` for that connection. Both TLS backends'
            // connectors (`tls/rustls.rs:97-114`, `tls/native_tls.rs:86-103`) refuse to *populate*
            // the agent's `OnceLock` TLS-config cache on a request-level connection -- they may only
            // reuse a value an agent-level connection already cached. An agent injected solely for
            // this crate never makes an agent-level connection, so this branch rebuilds the
            // `ClientConfig` / `TlsConnector` (root store included) on every new HTTPS connection.
            // ureq 3 exposes no way to derive a modified `Config` from an injected agent other than
            // this per-request override, so the cost is unavoidable for a caller who wants
            // `http_status_as_error(true)` from an injected agent; the `&&` above is the only lane
            // that avoids it (and, as a side effect, lets that agent's own TLS cache stay warm).
            req = req.config().http_status_as_error(false).build();
        }

        for (key, value) in headers.iter() {
            req = req.header(key, value);
        }

        let res = match req.call() {
            Ok(r) => r,
            Err(ureq::Error::StatusCode(code)) if is_injected => {
                // Deliberately-kept dead code, in both lanes above. When the override in `get` runs
                // (agent had `http_status_as_error(true)`), it forces the *effective* config for this
                // connection to `false` (`run.rs:128-129` only raises `StatusCode` when
                // `config.http_status_as_error()` is true), so this arm is not expected to fire; if a
                // future ureq ignores the request-level override, map the bare code rather than
                // surfacing it as an opaque transport error. When the override is *skipped* (agent
                // already had `http_status_as_error(false)`), `run()` falls back to the agent-level
                // config (`run.rs:37-41`), which is that same `false` -- so this arm stays unreachable
                // there too, for the same reason, not because the skip reopened it. `StatusCode`
                // carries no headers, so this path cannot see a spent quota on a 403 (it stays
                // `Unauthorized`) and a 429 classifies as `RateLimited` carrying no wait -- the
                // status alone is the rate-limit signal (RFC 6585).
                return Err(status_to_error(code, url));
            }
            Err(e) => return Err(Error::Transport(Box::new(e))),
        };

        if !res.status().is_success() {
            return Err(status_to_error_with_headers(
                res.status().as_u16(),
                url,
                res.headers(),
            ));
        }

        Ok(Box::new(res))
    }
}

impl HttpResponse for Response<Body> {
    fn headers(&self) -> &HeaderMap<http::HeaderValue> {
        Response::headers(self)
    }

    fn body(self: Box<Self>) -> Box<dyn std::io::Read> {
        Box::new((*self).into_body().into_reader())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use std::io::{Read as _, Write as _};
    use std::net::TcpListener;

    /// Serve a single HTTP response (the given status line + a short body) over a fresh loopback
    /// listener, then close. Returns the base URL (`http://127.0.0.1:<port>/`). No external network.
    /// Thin wrapper over [`stub_with_headers`] with no extra headers, so there is one stub server.
    fn stub(status: &'static str) -> String {
        stub_with_headers(status, "")
    }

    /// Default-agent path (`is_injected == false`): the per-call agent is built with
    /// `http_status_as_error(false)`, so a non-2xx response is returned to our own `is_success()`
    /// check at the bottom of `get`, which routes it through `status_to_error_with_headers`.
    fn get_default(status: &'static str) -> Error {
        let client = UreqClient::default();
        let base = stub(status);
        client
            .get(&base, &HeaderMap::new(), None)
            .err()
            .expect("non-2xx must be an Err")
    }

    /// Injected-agent path (`is_injected == true`): a user-supplied default `ureq::Agent` has
    /// `http_status_as_error == true` (the ureq default), but `get` overrides that *per request*, so
    /// a non-2xx is returned as a response and routed through the same header-aware
    /// `status_to_error_with_headers` check as the crate-built agents.
    fn get_injected(status: &'static str) -> Error {
        let agent = ureq::Agent::new_with_config(ureq::Agent::config_builder().build());
        let client = UreqClient::from(agent);
        let base = stub(status);
        client
            .get(&base, &HeaderMap::new(), None)
            .err()
            .expect("non-2xx must be an Err")
    }

    /// Injected agent built with `http_status_as_error(false)` (the OTHER injected case): the user
    /// disabled ureq's status-error at the agent level too, so the per-request override in `get` is
    /// a no-op and `call()` returns `Ok(res)` on a non-2xx. Control reaches the same bottom-of-`get`
    /// `!res.status().is_success()` check, routing the status through `status_to_error_with_headers`.
    fn get_injected_no_status_error(status: &'static str) -> Error {
        let agent = ureq::Agent::new_with_config(
            ureq::Agent::config_builder()
                .http_status_as_error(false)
                .build(),
        );
        let client = UreqClient::from(agent);
        let base = stub(status);
        client
            .get(&base, &HeaderMap::new(), None)
            .err()
            .expect("non-2xx must be an Err")
    }

    /// Like [`stub`], but injects `extra_headers` (a pre-formatted `Name: value\r\n` block), so a
    /// response can carry the rate-limit headers a spent quota comes with. Takes a plain `&str`
    /// (copied into the serving thread) so a header block can carry a value derived at runtime, e.g.
    /// a reset epoch relative to *now*.
    fn stub_with_headers(status: &'static str, extra_headers: &str) -> String {
        let extra_headers = extra_headers.to_string();
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let base = format!("http://{}/", listener.local_addr().unwrap());
        std::thread::spawn(move || {
            if let Ok((mut stream, _)) = listener.accept() {
                let mut buf = [0u8; 4096];
                let _ = stream.read(&mut buf);
                let body = "err";
                let out = format!(
                    "HTTP/1.1 {}\r\nContent-Type: text/plain\r\n{}Content-Length: {}\r\nConnection: close\r\n\r\n{}",
                    status,
                    extra_headers,
                    body.len(),
                    body
                );
                let _ = stream.write_all(out.as_bytes());
                let _ = stream.flush();
            }
        });
        base
    }

    /// The `x-ratelimit-reset` / `RateLimit-Reset` header value for a window resetting
    /// `offset_secs` from *now*, as the unix timestamp those headers carry.
    ///
    /// Derived rather than hardcoded: a fixed epoch silently ages into the past, and the classifier
    /// keeps a past instant (it just renders no wait), so tests pinned to one stop exercising the
    /// future-window path without ever failing. `offset_secs` must stay under the 24h ceiling
    /// (`MAX_RATE_LIMIT_WAIT`), past which a reset instant is rejected as `None`.
    fn reset_epoch(offset_secs: u64) -> String {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .expect("clock is after the unix epoch")
            .as_secs();
        (now + offset_secs).to_string()
    }

    /// The header block for a spent primary quota (`remaining: 0`) whose window resets in
    /// [`RESET_WINDOW`] seconds, in the `x-ratelimit-*` (github/gitea/gitee) spelling.
    fn spent_quota_headers() -> String {
        format!(
            "x-ratelimit-remaining: 0\r\nx-ratelimit-reset: {}\r\n",
            reset_epoch(RESET_WINDOW)
        )
    }

    /// The reset window every derived-epoch test serves: comfortably inside the 24h ceiling, and
    /// long enough that the remaining wait cannot round down to zero on a loaded CI box.
    const RESET_WINDOW: u64 = 600;

    /// Assert that `err` is a `RateLimited` whose `reset_at` resolves to a *positive* wait bounded by
    /// the window that was served -- the assertion `reset_at: Some(_)` cannot make, since a reset
    /// instant in the past is kept as `Some` and yields no wait at all.
    fn assert_wait_within_window(err: &Error, context: &str) {
        let wait = err
            .rate_limit_delay()
            .unwrap_or_else(|| panic!("{context}: a future reset instant must yield a wait"));
        assert!(
            wait > Duration::from_secs(RESET_WINDOW / 2)
                && wait <= Duration::from_secs(RESET_WINDOW),
            "{context}: the wait must be derived from the served future window (~{RESET_WINDOW}s), \
             got {wait:?}"
        );
    }

    #[test]
    fn default_agent_maps_a_spent_quota_403_to_rate_limited() {
        // The default per-call agent reaches the bottom-of-`get` check with the response in hand, so
        // it must classify a 403 carrying `x-ratelimit-remaining: 0` as RateLimited rather than
        // Unauthorized -- matching the reqwest lane. The reset epoch is derived from now and the
        // wait asserted positive, so a future window is really exercised (a hardcoded epoch ages into
        // the past, where `reset_at: Some(_)` still holds but carries no wait).
        let client = UreqClient::default();
        let base = stub_with_headers("403 Forbidden", &spent_quota_headers());
        let err = client
            .get(&base, &HeaderMap::new(), None)
            .err()
            .expect("non-2xx must be an Err");
        assert!(
            matches!(
                err,
                Error::RateLimited {
                    status: 403,
                    reset_at: Some(_),
                    ..
                }
            ),
            "default-agent 403 with a spent quota must map to RateLimited, got {:?}",
            err
        );
        assert_wait_within_window(&err, "ureq default agent, 403 spent quota");
    }

    #[test]
    fn injected_agent_sees_rate_limit_headers() {
        // An injected agent keeps ureq's default `http_status_as_error(true)`, which used to make a
        // non-2xx surface as a headerless `ureq::Error::StatusCode` and left a spent-quota 403
        // classified as `Unauthorized` -- disagreeing with both reqwest lanes. `get` now overrides
        // that per request, so the injected lane must see the quota headers and classify the same
        // `RateLimited` (with the reset instant) the default agent does.
        let agent = ureq::Agent::new_with_config(ureq::Agent::config_builder().build());
        let client = UreqClient::from(agent);
        let base = stub_with_headers("403 Forbidden", &spent_quota_headers());
        let err = client
            .get(&base, &HeaderMap::new(), None)
            .err()
            .expect("non-2xx must be an Err");
        assert!(
            matches!(
                err,
                Error::RateLimited {
                    status: 403,
                    reset_at: Some(_),
                    ..
                }
            ),
            "an injected agent must reach the header-aware check and classify a spent quota, got {:?}",
            err
        );
        assert_wait_within_window(&err, "ureq injected agent, 403 spent quota");
        assert_eq!(err.http_status(), Some(403));
    }

    #[test]
    fn injected_agent_with_status_error_disabled_sees_rate_limit_headers() {
        // The OTHER injected case: the user already built the agent with
        // `http_status_as_error(false)`, so `get` skips the per-request override entirely (see
        // `needs_status_override`) rather than applying a redundant no-op one. This lane must still
        // classify the spent quota identically -- pinned separately from the default-injected agent
        // above so a failure names which injected path broke.
        let agent = ureq::Agent::new_with_config(
            ureq::Agent::config_builder()
                .http_status_as_error(false)
                .build(),
        );
        let client = UreqClient::from(agent);
        let base = stub_with_headers("403 Forbidden", "x-ratelimit-remaining: 0\r\n");
        let err = client
            .get(&base, &HeaderMap::new(), None)
            .err()
            .expect("non-2xx must be an Err");
        assert!(
            matches!(err, Error::RateLimited { status: 403, .. }),
            "an injected agent that defers status handling must reach the header-aware check, got {:?}",
            err
        );
    }

    #[test]
    fn needs_status_override_skips_agents_that_already_disabled_status_error() {
        // Pins the decision `get` makes before ever touching the network (A8): an injected agent
        // whose own config still has ureq's default `http_status_as_error(true)` must be overridden
        // per request (this is the only lane that pays the request-level TLS-config-cache cost
        // documented at the call site in `get`); an injected agent that already disabled it must be
        // skipped rather than overridden again (the free win: no request-level marking at all, so
        // that agent's own TLS-config cache stays eligible to warm up). Without the `&&` on the
        // agent's own setting, the first two assertions below would both see `true` and this test
        // would fail on the second one.
        let default_status_error_agent =
            ureq::Agent::new_with_config(ureq::Agent::config_builder().build());
        assert!(
            needs_status_override(true, &default_status_error_agent),
            "an injected agent that still has ureq's default http_status_as_error(true) must be \
             overridden"
        );

        let status_error_disabled_agent = ureq::Agent::new_with_config(
            ureq::Agent::config_builder()
                .http_status_as_error(false)
                .build(),
        );
        assert!(
            !needs_status_override(true, &status_error_disabled_agent),
            "an injected agent that already disabled http_status_as_error must be skipped, not \
             overridden again"
        );

        // is_injected gates the whole branch independently of the agent's own setting: a crate-built
        // (non-injected) agent never takes this path, even if (hypothetically) its config still had
        // http_status_as_error(true).
        assert!(
            !needs_status_override(false, &default_status_error_agent),
            "a non-injected agent must never take the injected-only override path"
        );
    }

    #[test]
    fn get_maps_a_429_with_gitlab_header_spelling_to_rate_limited() {
        // The 429 status branch and gitlab's un-prefixed `RateLimit-Remaining` spelling, end to end
        // through the real client: the header names travel over the wire and through ureq's own
        // `HeaderMap`, so a typo in either the status branch or the header lookup fails here. The
        // reset epoch is derived from now and the resulting wait asserted, so the gitlab spelling is
        // proven to feed a *usable* future window rather than a stale instant.
        let client = UreqClient::default();
        let base = stub_with_headers(
            "429 Too Many Requests",
            &format!(
                "RateLimit-Remaining: 0\r\nRateLimit-Reset: {}\r\n",
                reset_epoch(RESET_WINDOW)
            ),
        );
        let err = client
            .get(&base, &HeaderMap::new(), None)
            .err()
            .expect("non-2xx must be an Err");
        assert!(
            matches!(
                err,
                Error::RateLimited {
                    status: 429,
                    reset_at: Some(_),
                    ..
                }
            ),
            "a 429 with gitlab's un-prefixed spent-quota header must map to RateLimited, got {:?}",
            err
        );
        assert_wait_within_window(&err, "ureq default agent, 429 gitlab spelling");
        assert_eq!(err.http_status(), Some(429));
    }

    #[test]
    fn get_parses_retry_after_from_the_response_headers() {
        // `Retry-After` is read off the live response headers by name; every other test builds the
        // signals struct directly, so this is the only coverage that the `retry-after` lookup finds
        // a real served header. Assert the parsed delta-seconds `Duration`, not just the variant.
        let client = UreqClient::default();
        let base = stub_with_headers(
            "429 Too Many Requests",
            "RateLimit-Remaining: 0\r\nRetry-After: 120\r\n",
        );
        let err = client
            .get(&base, &HeaderMap::new(), None)
            .err()
            .expect("non-2xx must be an Err");
        let Error::RateLimited { retry_after, .. } = err else {
            panic!("a 429 with a spent quota must be RateLimited, got {err:?}");
        };
        assert_eq!(
            retry_after,
            Some(Duration::from_secs(120)),
            "the served `Retry-After: 120` must be parsed into a 120s Duration"
        );
    }

    /// A [`ClientConfig`](crate::http_client::ClientConfig) over the given certs with no proxy.
    fn cert_config(certs: &[crate::tls::Certificate]) -> crate::http_client::ClientConfig<'_> {
        crate::http_client::ClientConfig { certs, proxy: None }
    }

    #[test]
    fn build_configured_rejects_non_pem() {
        // ureq's `tls::Certificate::from_pem` validates the PEM framing eagerly and errors when the
        // bytes contain no PEM certificate, so `build_configured` must surface a config-time `Err`
        // (the parse is deferred to here from the infallible `Certificate::from_pem` constructor)
        // rather than panicking or building an agent over garbage.
        let certs = [crate::tls::Certificate::from_pem(
            b"not a pem certificate".to_vec(),
        )];
        let res = UreqClient::build_configured(cert_config(&certs));
        assert!(
            matches!(res, Err(crate::http_client::ClientConfigError::Other(_))),
            "bytes with no PEM certificate must be rejected at build time as a cert failure"
        );
    }

    #[test]
    fn build_configured_accepts_garbage_der_deferring_validation() {
        // ureq's `tls::Certificate::from_der` is infallible, so invalid DER bytes are accepted
        // here and only surface at connection time. This is intentional and documented (on
        // `add_root_certificate` and in the reference specs); the reqwest client rejects the same
        // bytes at build time (`build_configured_rejects_garbage_der` in reqwest.rs). This test
        // pins the asymmetry: if ureq ever gains eager DER validation, this fails and the
        // deferred-validation caveat in the docs should be removed.
        let certs = [crate::tls::Certificate::from_der(b"not der".to_vec())];
        let res = UreqClient::build_configured(cert_config(&certs));
        assert!(
            res.is_ok(),
            "ureq DER validation is deferred to connection time; build must accept the bytes"
        );
    }

    // spec: CORP-3-9
    #[test]
    fn build_configured_reports_an_unparseable_proxy_as_a_proxy_failure() {
        // The proxy URL is parsed at build time (like the certificates), and the failure must be
        // attributed to the proxy so it surfaces as `Error::InvalidProxy` rather than as a
        // certificate error the caller never configured. The password must not survive into the
        // message.
        let res = UreqClient::build_configured(crate::http_client::ClientConfig {
            certs: &[],
            proxy: Some("http://corpuser:hunter2@ not a proxy url"),
        });
        let Err(crate::http_client::ClientConfigError::Proxy(e)) = res else {
            panic!("an unparseable proxy URL must be a ClientConfigError::Proxy");
        };
        let msg = e.to_string();
        assert!(
            !msg.contains("hunter2") && msg.contains("REDACTED"),
            "the proxy password must be redacted from the error, got: {msg}"
        );
    }

    // spec: CORP-3-9, CORP-3-10
    #[test]
    fn a_configured_proxy_replaces_the_env_proxy_on_the_per_call_agent() {
        // ureq's config has a single proxy slot, so the programmatic proxy and `HTTP(S)_PROXY`
        // cannot both apply: a configured proxy must win, and with none configured the env lookup
        // must still run (`Proxy::try_from_env`), which is the pre-CORP-3 behavior this must not
        // regress. Asserted on the agent's own config rather than by driving traffic, so the test
        // needs no environment mutation (which would race with other tests in the same process).
        let proxy = ureq::Proxy::new("http://corpuser:hunter2@proxy.corp:8080").unwrap();
        let agent = build_call_agent(None, None, Some(proxy));
        let configured = agent
            .config()
            .proxy()
            .expect("the proxy must be configured");
        assert_eq!(configured.host(), "proxy.corp");
        assert_eq!(configured.port(), 8080);
        assert_eq!(
            configured.username(),
            Some("corpuser"),
            "the credentials embedded in the URL must be parsed out for Proxy-Authorization"
        );
        assert_eq!(configured.password(), Some("hunter2"));
        // And the certificates ride along on the same agent (CORP-3-5): a proxy must not discard
        // the configured trust store.
        let certs = std::sync::Arc::new(vec![
            ureq::tls::Certificate::from_der(b"deferred-validation").to_owned(),
        ]);
        let both = build_call_agent(
            None,
            Some(certs),
            Some(ureq::Proxy::new("http://proxy.corp:8080").unwrap()),
        );
        assert!(
            matches!(
                both.config().tls_config().root_certs(),
                ureq::tls::RootCerts::Specific(_)
            ),
            "a configured proxy must not drop the custom root certificates"
        );
        assert!(both.config().proxy().is_some());
    }

    // spec: CORP-2-4
    #[test]
    fn default_trust_store_follows_the_native_certs_feature() {
        // The whole point of `native-certs`: a crate-built agent must consult the OS trust store
        // rather than Mozilla's bundled roots, because a corporate MITM proxy's CA is only ever in
        // the former. Without the feature the WebPki default must be preserved exactly -- widening
        // the trust store of a self-updater is not something a minor release gets to do silently.
        let agent = build_call_agent(None, None, None);
        let roots = agent.config().tls_config().root_certs();
        #[cfg(feature = "native-certs")]
        assert!(
            matches!(roots, ureq::tls::RootCerts::PlatformVerifier),
            "with `native-certs` the per-call agent must use the OS trust store, got {roots:?}"
        );
        #[cfg(not(feature = "native-certs"))]
        assert!(
            matches!(roots, ureq::tls::RootCerts::WebPki),
            "without `native-certs` the per-call agent must keep ureq's WebPki roots, got {roots:?}"
        );
    }

    // spec: CORP-1-14, CORP-1-15, CORP-2-4
    #[test]
    fn caller_supplied_certs_replace_the_default_trust_store() {
        // CORP-1-15's documented limitation, pinned against the CORP-2 change: adding the default
        // `root_certs` call must not turn `Specific` into a merge, and must not let the default
        // win over an explicit cert. `Specific` still fully replaces whatever the default was.
        let certs = std::sync::Arc::new(vec![
            ureq::tls::Certificate::from_der(b"der-bytes").to_owned(),
        ]);
        let agent = build_call_agent(None, Some(certs), None);
        let roots = agent.config().tls_config().root_certs();
        assert!(
            matches!(roots, ureq::tls::RootCerts::Specific(_)),
            "caller-supplied roots must replace the default trust store, got {roots:?}"
        );
    }

    #[test]
    fn injected_agent_no_status_error_falls_through_to_is_success_check() {
        // 404 must still map to NotFound via the bottom-of-`get` is_success() path (the
        // defensive `StatusCode` arm never fires when http_status_as_error(false)).
        let err = get_injected_no_status_error("404 Not Found");
        assert!(
            matches!(err, Error::NotFound { .. }),
            "injected no-status-error 404 must map to Error::NotFound via is_success(), got {:?}",
            err
        );
        assert_eq!(err.http_status(), Some(404));

        // 500 maps to HttpStatus carrying its exact code through the same fall-through path.
        let err = get_injected_no_status_error("500 Internal Server Error");
        assert!(
            matches!(err, Error::HttpStatus { status: 500, .. }),
            "injected no-status-error 500 must map to Error::HttpStatus(500), got {:?}",
            err
        );
        assert_eq!(err.http_status(), Some(500));
    }

    #[test]
    fn injected_agent_maps_404_to_not_found() {
        let err = get_injected("404 Not Found");
        assert!(
            matches!(err, Error::NotFound { .. }),
            "injected-agent 404 must map to Error::NotFound, got {:?}",
            err
        );
        assert_eq!(err.http_status(), Some(404));
    }

    #[test]
    fn injected_agent_maps_401_and_403_to_unauthorized() {
        let err = get_injected("401 Unauthorized");
        assert!(
            matches!(err, Error::Unauthorized { status: 401, .. }),
            "injected-agent 401 must map to Error::Unauthorized(401), got {:?}",
            err
        );
        let err = get_injected("403 Forbidden");
        assert!(
            matches!(err, Error::Unauthorized { status: 403, .. }),
            "injected-agent 403 must map to Error::Unauthorized(403), got {:?}",
            err
        );
    }

    #[test]
    fn injected_agent_maps_500_and_400_to_http_status() {
        let err = get_injected("500 Internal Server Error");
        assert!(
            matches!(err, Error::HttpStatus { status: 500, .. }),
            "injected-agent 500 must map to Error::HttpStatus(500), got {:?}",
            err
        );
        let err = get_injected("400 Bad Request");
        assert!(
            matches!(err, Error::HttpStatus { status: 400, .. }),
            "injected-agent 400 must map to Error::HttpStatus(400), got {:?}",
            err
        );
    }

    #[test]
    fn default_agent_path_maps_statuses_identically_to_injected() {
        // The default per-call agent (`http_status_as_error(false)`) reaches the bottom-of-`get`
        // `is_success()` check and must produce the SAME structured variants as an injected agent
        // (which gets the same setting via the per-request override), so both ureq lanes agree.
        assert!(matches!(
            get_default("404 Not Found"),
            Error::NotFound { .. }
        ));
        assert!(matches!(
            get_default("401 Unauthorized"),
            Error::Unauthorized { status: 401, .. }
        ));
        assert!(matches!(
            get_default("403 Forbidden"),
            Error::Unauthorized { status: 403, .. }
        ));
        assert!(matches!(
            get_default("503 Service Unavailable"),
            Error::HttpStatus { status: 503, .. }
        ));
    }

    #[test]
    fn transport_failure_maps_to_transport_variant() {
        // A connection refused to a closed port (no listener) cannot complete the request, so the
        // catch-all `Err(e) => Error::Transport` arm fires (default agent) -- NOT a status variant.
        // Bind+drop to obtain a port nothing is listening on.
        let listener = TcpListener::bind("127.0.0.1:0").unwrap();
        let addr = listener.local_addr().unwrap();
        drop(listener);
        let url = format!("http://{}/", addr);
        let client = UreqClient::default();
        let err = client
            .get(&url, &HeaderMap::new(), None)
            .err()
            .expect("connection refused must be an Err");
        assert!(
            matches!(err, Error::Transport(_)),
            "a failed (uncompleted) request must map to Error::Transport, got {:?}",
            err
        );
        assert_eq!(err.http_status(), None, "Transport has no HTTP status code");
    }
}