polyoxide-core 0.48.0

Core utilities and shared types for the polyoxide venue clients: the HTTP send loop and its hooks, window quotas, capacity buckets and holds, errors and macros
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
use std::sync::Arc;
use std::time::Duration;

use http::Method;
use polyoxide_rt::Runtime;
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
use url::Url;

use http::header::RETRY_AFTER;

use crate::error::ApiError;
use crate::hooks::{
    DefaultRetryPolicy, DynRetryPolicy, DynThrottle, NoThrottle, RequestParts, RetryPolicy,
    Throttle,
};
use crate::rate_limit::{RateLimiter, RetryConfig};
use crate::transport::{default_transport, HttpTransport};

/// Extract the `Retry-After` header value as a string, if present and valid UTF-8.
pub fn retry_after_header(headers: &http::HeaderMap) -> Option<String> {
    headers.get(RETRY_AFTER)?.to_str().ok().map(String::from)
}

/// Default request timeout in milliseconds
pub const DEFAULT_TIMEOUT_MS: u64 = 30_000;
/// Default connection pool size per host
pub const DEFAULT_POOL_SIZE: usize = 10;

/// Shared HTTP client with base URL, throttle, retry policy and retry config.
///
/// This is the common structure used by all API clients to hold the
/// transport every attempt is sent on, the base URL, and the hooks its send
/// loop, [`send`](Self::send), runs on every request.
#[derive(Clone)]
pub struct HttpClient {
    /// The transport every attempt is sent on, shared with every
    /// [`with_base_url`](Self::with_base_url) sibling.
    pub(crate) transport: Arc<dyn HttpTransport>,
    /// How long an attempt may take unless its request sets its own.
    pub(crate) timeout: Duration,
    /// Base URL for API requests
    pub base_url: Url,
    pub(crate) throttle: Arc<DynThrottle<'static>>,
    pub(crate) policy: Arc<DynRetryPolicy<'static>>,
    pub(crate) retry_config: RetryConfig,
    pub(crate) runtime: Arc<dyn Runtime>,
    concurrency_limiter: Option<Arc<Semaphore>>,
}

/// Written by hand: the throttle and the policy are trait objects, which
/// print nothing useful.
impl std::fmt::Debug for HttpClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HttpClient")
            .field("transport", &self.transport)
            .field("base_url", &self.base_url)
            .field("retry_config", &self.retry_config)
            .field("runtime", &self.runtime)
            .field("concurrency_limiter", &self.concurrency_limiter)
            .finish_non_exhaustive()
    }
}

impl HttpClient {
    /// Clone this client, pointed at a different base URL.
    ///
    /// The transport (and so its connection pool), throttle
    /// (and so its hold), retry policy, retry config, runtime, and concurrency limiter
    /// are all shared with the original. Use this to reach a sibling API host
    /// that should share the same transport configuration and request budget —
    /// a venue whose APIs live on their own subdomains is consumed by one
    /// client, and a 429 on one holds them all.
    ///
    /// Sharing the concurrency limiter is deliberate: the limit exists to keep
    /// Cloudflare from seeing a burst from this process, and that is a
    /// per-process concern rather than a per-host one.
    ///
    /// # Path prefixes are ignored
    ///
    /// Request paths are absolute (`/user-pnl`), and resolving an absolute
    /// path against a base replaces the base's path entirely. So a prefix in
    /// `base_url` is **silently dropped**, with or without a trailing slash:
    ///
    /// ```text
    /// http://host          + /user-pnl -> http://host/user-pnl
    /// http://host/proxy    + /user-pnl -> http://host/user-pnl   (prefix gone)
    /// http://host/proxy/   + /user-pnl -> http://host/user-pnl   (prefix gone)
    /// ```
    ///
    /// Point this at a scheme, host, and port — not at a sub-path. Fronting
    /// the API with a path-prefixed reverse proxy is not supported.
    pub fn with_base_url(&self, base_url: &str) -> Result<Self, ApiError> {
        Ok(Self {
            base_url: Url::parse(base_url)?,
            ..self.clone()
        })
    }

    /// The runtime this client's waits, clock reads and spawns go through:
    /// the one its builder was given, else the target's default. Shared with
    /// every [`with_base_url`](Self::with_base_url) sibling.
    pub fn runtime(&self) -> &Arc<dyn Runtime> {
        &self.runtime
    }

    /// Acquire a concurrency permit, if a limiter is configured.
    ///
    /// The returned permit **must** be held until the HTTP response has been
    /// received. Dropping the permit releases the concurrency slot.
    /// Returns `None` when no concurrency limit is set.
    ///
    /// [`send`](Self::send) takes one for each attempt. Transitional for any
    /// other caller: no crate calls it since Stories 3.4 to 3.6 moved the
    /// hand-written loops onto [`send`](Self::send), but tests observe the
    /// permit through it. It is removed from the public API once they can
    /// observe it another way, and `docs/s1-removals.md` names [`send`](Self::send).
    pub async fn acquire_concurrency(&self) -> Option<OwnedSemaphorePermit> {
        let sem = self.concurrency_limiter.as_ref()?;
        Some(
            sem.clone()
                .acquire_owned()
                .await
                .expect("concurrency semaphore is never closed"),
        )
    }

    /// GET a URL and return the raw response body as bytes.
    ///
    /// Use this for endpoints that return non-JSON payloads (e.g. `application/zip`
    /// downloads). It runs on [`send`](Self::send), so it is throttled, gated,
    /// retried and held as every other request is.
    ///
    /// A non-2xx response is [`ApiError::Response`].
    ///
    /// # Errors
    ///
    /// Returns [`ApiError`] on URL-join failure, network errors, or non-2xx
    /// responses.
    pub async fn get_bytes(
        &self,
        path: &str,
        query: &[(String, String)],
    ) -> Result<Vec<u8>, ApiError> {
        let mut parts = RequestParts::new(Method::GET, path);
        parts.query = query.to_vec();
        let response = self.send(parts, &[], None).await?;

        // Only a policy that is `Done` with a failed response gets here.
        if !response.status().is_success() {
            return Err(ApiError::from_response(response).await);
        }

        let bytes = response.bytes().await?;
        Ok(bytes.to_vec())
    }
}

/// Builder for configuring HTTP clients.
///
/// Provides a consistent way to configure HTTP clients across all API crates
/// with sensible defaults.
///
/// # Example
///
/// ```
/// use polyoxide_core::HttpClientBuilder;
///
/// let client = HttpClientBuilder::new("https://api.example.com")
///     .timeout_ms(60_000)
///     .pool_size(20)
///     .build()
///     .unwrap();
/// ```
pub struct HttpClientBuilder {
    base_url: String,
    timeout_ms: u64,
    pool_size: usize,
    throttle: Option<Arc<DynThrottle<'static>>>,
    policy: Option<Arc<DynRetryPolicy<'static>>>,
    retry_config: RetryConfig,
    max_concurrent: Option<usize>,
    runtime: Option<Arc<dyn Runtime>>,
    gzip: Option<bool>,
    http_transport: Option<Arc<dyn HttpTransport>>,
}

impl HttpClientBuilder {
    /// Create a new HTTP client builder with the given base URL.
    pub fn new(base_url: impl Into<String>) -> Self {
        Self {
            base_url: base_url.into(),
            timeout_ms: DEFAULT_TIMEOUT_MS,
            pool_size: DEFAULT_POOL_SIZE,
            throttle: None,
            policy: None,
            retry_config: RetryConfig::default(),
            max_concurrent: None,
            runtime: None,
            gzip: None,
            http_transport: None,
        }
    }

    /// Set request timeout in milliseconds.
    ///
    /// Default: 30,000ms (30 seconds)
    pub fn timeout_ms(mut self, timeout: u64) -> Self {
        self.timeout_ms = timeout;
        self
    }

    /// Set connection pool size per host.
    ///
    /// Default: 10 connections
    pub fn pool_size(mut self, size: usize) -> Self {
        self.pool_size = size;
        self
    }

    /// Set a rate limiter for this client: [`with_throttle`](Self::with_throttle)
    /// with a [`RateLimiter`].
    pub fn with_rate_limiter(self, limiter: RateLimiter) -> Self {
        self.with_throttle(limiter)
    }

    /// Set the throttle every request on this client goes through, shared
    /// with every [`with_base_url`](HttpClient::with_base_url) sibling.
    ///
    /// Default: [`NoThrottle`], which charges and holds
    /// nothing.
    pub fn with_throttle(mut self, throttle: impl Throttle + 'static) -> Self {
        self.throttle = Some(DynThrottle::new_arc(throttle));
        self
    }

    /// Set the policy that decides what follows each response.
    ///
    /// Default: [`DefaultRetryPolicy`], which
    /// retries a 429 and nothing else.
    pub fn with_retry_policy(mut self, policy: impl RetryPolicy + 'static) -> Self {
        self.policy = Some(DynRetryPolicy::new_arc(policy));
        self
    }

    /// Set the retry schedule: how many retries, and the backoff between them.
    pub fn with_retry_config(mut self, config: RetryConfig) -> Self {
        self.retry_config = config;
        self
    }

    /// Set the maximum number of concurrent in-flight HTTP requests.
    ///
    /// Prevents Cloudflare 1015 rate-limit errors caused by request bursts
    /// when many callers share the same client concurrently.
    pub fn with_max_concurrent(mut self, max: usize) -> Self {
        self.max_concurrent = Some(max);
        self
    }

    /// Set the runtime this client's waits, clock reads and spawns go
    /// through, shared with every [`with_base_url`](HttpClient::with_base_url)
    /// sibling.
    ///
    /// Default: [`polyoxide_rt::default_runtime`]. A throttle installed with
    /// [`with_throttle`](Self::with_throttle) keeps time on the runtime it was
    /// built on: build it on this one, as the venue crates do.
    pub fn runtime(mut self, runtime: Arc<dyn Runtime>) -> Self {
        self.runtime = Some(runtime);
        self
    }

    /// Ask for gzip-compressed responses and decode them, or pin that off.
    ///
    /// Unset by default, which leaves reqwest's own default. This crate enables
    /// reqwest's `gzip` feature, so an unset client asks for gzip and decodes
    /// the body transparently, as every client did for a consumer whose
    /// workspace enabled the feature before this setting existed. 0.37.0 set
    /// it off unless asked, which took compression away from those consumers.
    pub fn gzip(mut self, enabled: bool) -> Self {
        self.gzip = Some(enabled);
        self
    }

    /// Send every attempt on `transport` instead of the target's default,
    /// shared with every [`with_base_url`](HttpClient::with_base_url) sibling.
    ///
    /// The builder's timeout still travels with each request, as its
    /// `HttpRequest::timeout`, but [`pool_size`](Self::pool_size) and
    /// [`gzip`](Self::gzip) feed only the default `ReqwestTransport`: a
    /// transport you inject owns its own connections and compression.
    pub fn http_transport(mut self, transport: Arc<dyn HttpTransport>) -> Self {
        self.http_transport = Some(transport);
        self
    }

    /// Build the HTTP client.
    pub fn build(self) -> Result<HttpClient, ApiError> {
        let timeout = Duration::from_millis(self.timeout_ms);
        let transport = match self.http_transport {
            Some(transport) => transport,
            None => default_transport(timeout, self.pool_size, self.gzip)?,
        };

        let base_url = Url::parse(&self.base_url)?;

        Ok(HttpClient {
            transport,
            timeout,
            base_url,
            throttle: self
                .throttle
                .unwrap_or_else(|| DynThrottle::new_arc(NoThrottle)),
            policy: self
                .policy
                .unwrap_or_else(|| DynRetryPolicy::new_arc(DefaultRetryPolicy)),
            retry_config: self.retry_config,
            runtime: self.runtime.unwrap_or_else(polyoxide_rt::default_runtime),
            concurrency_limiter: self.max_concurrent.map(|n| Arc::new(Semaphore::new(n))),
        })
    }
}

impl Default for HttpClientBuilder {
    fn default() -> Self {
        Self::new(String::new())
    }
}

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

    #[test]
    fn test_should_retry_uses_retry_after_header() {
        let config = RetryConfig::default();
        let d = config.retry_delay(0, Some("2"));
        assert_eq!(d, Duration::from_millis(2000));
    }

    #[test]
    fn test_should_retry_retry_after_fractional_seconds() {
        let config = RetryConfig::default();
        // 1.5s, not the 0.5s this once used: a server-supplied delay is only
        // honoured when it exceeds the client's own backoff, and attempt 0's
        // jitter range is [375, 625]ms — straddling it made the assertion
        // depend on the roll. The point here is that fractions parse.
        let d = config.retry_delay(0, Some("1.5"));
        assert_eq!(d, Duration::from_millis(1500));
    }

    #[test]
    fn retry_after_below_our_own_backoff_does_not_shorten_the_wait() {
        let config = RetryConfig::default();
        // Cloudflare answers a tripped rate limit with 429 + `error code: 1015`
        // and a Retry-After that floors to zero. Taking it verbatim collapsed
        // the sleep to nothing: the observed failure was three "retry after 0ms"
        // attempts inside 65ms, which deepens a 1015 ban rather than waiting it
        // out. A server asking us to wait *longer* is honoured; one asking us to
        // wait less than our own policy is not.
        for header in ["0", "0.0", "-1", "-30", "0.0001"] {
            let d = config.retry_delay(0, Some(header));
            assert!(
                d >= Duration::from_millis(375),
                "Retry-After: {header:?} produced a {d:?} sleep; the floor is the \
                 client's own attempt-0 backoff, >=375ms after jitter"
            );
        }
    }

    #[test]
    fn retry_after_zero_still_backs_off_exponentially_across_attempts() {
        let config = RetryConfig::default();
        // Flooring at a flat minimum would still let a 1015 ban be hammered at a
        // fixed cadence. The floor has to be the *attempt's* backoff, so a
        // degenerate header still yields 500ms, 1s, 2s.
        for (attempt, min_ms) in [(0u32, 375u64), (1, 750), (2, 1_500)] {
            let d = config.retry_delay(attempt, Some("0"));
            assert!(
                d >= Duration::from_millis(min_ms),
                "attempt {attempt} with Retry-After: 0 slept {d:?}, expected >={min_ms}ms"
            );
        }
    }

    #[test]
    fn test_should_retry_retry_after_clamped_to_max_backoff() {
        let config = RetryConfig::default();
        // Default max_backoff_ms = 10_000; header says 60s
        let d = config.retry_delay(0, Some("60"));
        assert_eq!(d, Duration::from_millis(10_000));
    }

    #[test]
    fn test_should_retry_retry_after_invalid_falls_back() {
        let config = RetryConfig::default();
        // Non-numeric Retry-After (HTTP-date format) falls back to computed backoff
        let d = config.retry_delay(0, Some("Wed, 21 Oct 2025 07:28:00 GMT"));
        // Should be in the jitter range for attempt 0: [375, 625]ms
        let ms = d.as_millis() as u64;
        assert!(
            (375..=625).contains(&ms),
            "expected fallback backoff in [375, 625], got {ms}"
        );
    }

    // ── Builder wiring ───────────────────────────────────────────

    /// Notes when it signs: just after the throttle let the attempt go.
    #[derive(Default)]
    struct SignedAt(std::sync::Mutex<Option<std::time::Instant>>);

    impl crate::hooks::Authenticator for SignedAt {
        async fn sign(&self, _parts: &mut RequestParts, _attempt: u32) -> Result<(), ApiError> {
            *self.0.lock().unwrap() = Some(std::time::Instant::now());
            Ok(())
        }
    }

    /// How long `client` takes to let a `POST /order` through to its signing,
    /// against a mock server.
    async fn time_to_sign(builder: HttpClientBuilder, server: &mockito::ServerGuard) -> Duration {
        let client = builder.build().unwrap();
        let client = client.with_base_url(&server.url()).unwrap();
        let signed = SignedAt::default();
        let start = std::time::Instant::now();
        client
            .send(
                RequestParts::new(Method::POST, "/order"),
                &[],
                Some(crate::hooks::DynAuthenticator::from_ref(&signed)),
            )
            .await
            .unwrap();
        let at = signed.0.lock().unwrap().expect("the request was signed");
        at - start
    }

    #[tokio::test]
    async fn test_builder_without_rate_limiter() {
        let mut server = mockito::Server::new_async().await;
        let _order = server.mock("POST", "/order").create_async().await;
        let builder = HttpClientBuilder::new("https://example.com");
        let elapsed = time_to_sign(builder, &server).await;
        assert!(elapsed < Duration::from_millis(10));
    }

    // ── Concurrency limiter ─────────────────────────────────────

    #[tokio::test]
    async fn test_acquire_concurrency_none_when_not_configured() {
        let client = HttpClientBuilder::new("https://example.com")
            .build()
            .unwrap();
        assert!(client.acquire_concurrency().await.is_none());
    }

    #[tokio::test]
    async fn test_acquire_concurrency_returns_permit() {
        let client = HttpClientBuilder::new("https://example.com")
            .with_max_concurrent(2)
            .build()
            .unwrap();
        let permit = client.acquire_concurrency().await;
        assert!(permit.is_some());
    }

    #[tokio::test]
    async fn test_concurrency_shared_across_clones() {
        let client = HttpClientBuilder::new("https://example.com")
            .with_max_concurrent(1)
            .build()
            .unwrap();
        let clone = client.clone();

        // Hold the only permit from the original
        let _permit = client.acquire_concurrency().await.unwrap();

        // Clone should block because concurrency=1 and permit is held
        let result =
            tokio::time::timeout(Duration::from_millis(50), clone.acquire_concurrency()).await;
        assert!(result.is_err(), "clone should block when permit is held");
    }

    #[tokio::test]
    async fn test_concurrency_limits_parallel_tasks() {
        let client = HttpClientBuilder::new("https://example.com")
            .with_max_concurrent(2)
            .build()
            .unwrap();

        let start = std::time::Instant::now();
        let mut handles = Vec::new();
        for _ in 0..4 {
            let c = client.clone();
            handles.push(tokio::spawn(async move {
                let _permit = c.acquire_concurrency().await;
                tokio::time::sleep(Duration::from_millis(50)).await;
            }));
        }
        for h in handles {
            h.await.unwrap();
        }
        // 4 tasks, concurrency 2, 50ms each => ~100ms minimum
        assert!(
            start.elapsed() >= Duration::from_millis(90),
            "expected ~100ms, got {:?}",
            start.elapsed()
        );
    }

    #[tokio::test]
    async fn test_builder_with_max_concurrent() {
        let client = HttpClientBuilder::new("https://example.com")
            .with_max_concurrent(5)
            .build()
            .unwrap();
        // Should be able to acquire 5 permits
        let mut permits = Vec::new();
        for _ in 0..5 {
            permits.push(client.acquire_concurrency().await);
        }
        assert!(permits.iter().all(|p| p.is_some()));

        // 6th should block
        let result =
            tokio::time::timeout(Duration::from_millis(50), client.acquire_concurrency()).await;
        assert!(result.is_err());
    }

    // ── get_bytes() ──────────────────────────────────────────────

    #[tokio::test]
    async fn test_get_bytes_returns_body_verbatim() {
        let mut server = mockito::Server::new_async().await;
        // Intentionally non-UTF-8 bytes to prove we're not assuming text.
        let body: Vec<u8> = vec![0x50, 0x4B, 0x03, 0x04, 0x00, 0xFF, 0xFE, 0x42];
        let mock = server
            .mock("GET", "/v1/accounting/snapshot")
            .match_query(mockito::Matcher::UrlEncoded("user".into(), "0xabc".into()))
            .with_status(200)
            .with_header("content-type", "application/zip")
            .with_body(body.clone())
            .create_async()
            .await;

        let client = HttpClientBuilder::new(server.url()).build().unwrap();
        let out = client
            .get_bytes(
                "/v1/accounting/snapshot",
                &[("user".to_string(), "0xabc".to_string())],
            )
            .await
            .unwrap();
        assert_eq!(out, body);
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn test_get_bytes_maps_non_2xx_to_api_error() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/does-not-exist")
            .with_status(404)
            .with_header("content-type", "application/json")
            .with_body(r#"{"error": "not found"}"#)
            .create_async()
            .await;

        let client = HttpClientBuilder::new(server.url()).build().unwrap();
        let err = client.get_bytes("/does-not-exist", &[]).await.unwrap_err();
        match err {
            ApiError::Response(response) => {
                assert_eq!(response.status, StatusCode::NOT_FOUND);
                assert_eq!(response.message, "not found");
            }
            other => panic!("expected ApiError::Response, got {other:?}"),
        }
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn test_get_bytes_no_query_params() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/raw")
            .with_status(200)
            .with_body(&b"hello"[..])
            .create_async()
            .await;

        let client = HttpClientBuilder::new(server.url()).build().unwrap();
        let out = client.get_bytes("/raw", &[]).await.unwrap();
        assert_eq!(out, b"hello");
        mock.assert_async().await;
    }

    // ── gzip ─────────────────────────────────────────────────────

    #[tokio::test]
    async fn gzip_follows_reqwest_unless_set() {
        // Unset, the client leaves reqwest's default, which asks for gzip with
        // the feature on. 0.37.0 forced it off, so a consumer whose workspace
        // had the feature on lost compression on every polyoxide client.
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/plain")
            .match_header("accept-encoding", mockito::Matcher::Regex("gzip".into()))
            .with_body("ok")
            .create_async()
            .await;

        let client = HttpClientBuilder::new(server.url()).build().unwrap();
        assert_eq!(client.get_bytes("/plain", &[]).await.unwrap(), b"ok");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn gzip_false_asks_for_no_compression() {
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/plain")
            .match_header("accept-encoding", mockito::Matcher::Missing)
            .with_body("ok")
            .create_async()
            .await;

        let client = HttpClientBuilder::new(server.url())
            .gzip(false)
            .build()
            .unwrap();
        assert_eq!(client.get_bytes("/plain", &[]).await.unwrap(), b"ok");
        mock.assert_async().await;
    }

    #[tokio::test]
    async fn gzip_asks_for_and_decodes_a_compressed_body() {
        // `{"serverTime":1}` compressed by python3's `gzip.compress(.., mtime=0)`.
        const GZIPPED: &[u8] = &[
            0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xab, 0x56, 0x2a, 0x4e,
            0x2d, 0x2a, 0x4b, 0x2d, 0x0a, 0xc9, 0xcc, 0x4d, 0x55, 0xb2, 0x32, 0xac, 0x05, 0x00,
            0xe2, 0x1d, 0x3e, 0x1a, 0x10, 0x00, 0x00, 0x00,
        ];
        let mut server = mockito::Server::new_async().await;
        let mock = server
            .mock("GET", "/time")
            .match_header("accept-encoding", mockito::Matcher::Regex("gzip".into()))
            .with_header("content-encoding", "gzip")
            .with_body(GZIPPED)
            .create_async()
            .await;

        let client = HttpClientBuilder::new(server.url())
            .gzip(true)
            .build()
            .unwrap();
        let body = client.get_bytes("/time", &[]).await.unwrap();
        assert_eq!(body, br#"{"serverTime":1}"#);
        mock.assert_async().await;
    }

    #[test]
    fn with_base_url_retargets_and_shares_transport() {
        let client = HttpClientBuilder::new("https://api.example.com")
            .with_max_concurrent(4)
            .build()
            .unwrap();
        let sibling = client.with_base_url("https://sibling.example.com").unwrap();

        assert_eq!(sibling.base_url.host_str(), Some("sibling.example.com"));
        // The original is untouched — this returns a clone, not a mutation.
        assert_eq!(client.base_url.host_str(), Some("api.example.com"));
        // Both must draw on the same concurrency budget, since the limit exists
        // to stop this process bursting rather than to pace any one host.
        assert!(sibling.concurrency_limiter.is_some());
    }

    #[test]
    fn with_base_url_rejects_a_malformed_url() {
        let client = HttpClientBuilder::new("https://api.example.com")
            .build()
            .unwrap();
        assert!(client.with_base_url("not-a-url").is_err());
    }

    #[test]
    fn base_url_path_prefixes_are_dropped() {
        // Documented footgun, pinned so it cannot change silently: request
        // paths are absolute, so they replace the base path entirely. If this
        // test ever fails, the doc comment on with_base_url needs updating too.
        let client = HttpClientBuilder::new("http://localhost:8080/proxy")
            .build()
            .unwrap();
        assert_eq!(
            client.base_url.join("/user-pnl").unwrap().as_str(),
            "http://localhost:8080/user-pnl",
            "a path prefix in the base URL is not preserved"
        );

        let with_slash = client
            .with_base_url("http://localhost:8080/proxy/")
            .unwrap();
        assert_eq!(
            with_slash.base_url.join("/user-pnl").unwrap().as_str(),
            "http://localhost:8080/user-pnl",
            "a trailing slash does not preserve the prefix either"
        );
    }
}