tact-client 0.4.3

TACT (Trusted Application Content Transfer) HTTP client for Blizzard's NGDP
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
//! HTTP client for TACT protocol

use crate::{CdnEntry, Error, Region, Result, VersionEntry, response_types};
use reqwest::{Client, Response};
use std::time::Duration;
use tokio::time::sleep;
use tracing::{debug, trace, warn};

/// Default maximum retries (0 = no retries, maintains backward compatibility)
const DEFAULT_MAX_RETRIES: u32 = 0;

/// Default initial backoff in milliseconds
const DEFAULT_INITIAL_BACKOFF_MS: u64 = 100;

/// Default maximum backoff in milliseconds
const DEFAULT_MAX_BACKOFF_MS: u64 = 10_000;

/// Default backoff multiplier
const DEFAULT_BACKOFF_MULTIPLIER: f64 = 2.0;

/// Default jitter factor (0.0 to 1.0)
const DEFAULT_JITTER_FACTOR: f64 = 0.1;

/// TACT protocol version
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProtocolVersion {
    /// Version 1: HTTP-based protocol on port 1119
    V1,
    /// Version 2: HTTPS-based REST API
    V2,
}

/// HTTP client for TACT protocol
#[derive(Debug, Clone)]
pub struct HttpClient {
    client: Client,
    region: Region,
    version: ProtocolVersion,
    max_retries: u32,
    initial_backoff_ms: u64,
    max_backoff_ms: u64,
    backoff_multiplier: f64,
    jitter_factor: f64,
    user_agent: Option<String>,
}

impl HttpClient {
    /// Create a new HTTP client for the specified region and protocol version
    pub fn new(region: Region, version: ProtocolVersion) -> Result<Self> {
        let client = Client::builder().timeout(Duration::from_secs(30)).build()?;

        Ok(Self {
            client,
            region,
            version,
            max_retries: DEFAULT_MAX_RETRIES,
            initial_backoff_ms: DEFAULT_INITIAL_BACKOFF_MS,
            max_backoff_ms: DEFAULT_MAX_BACKOFF_MS,
            backoff_multiplier: DEFAULT_BACKOFF_MULTIPLIER,
            jitter_factor: DEFAULT_JITTER_FACTOR,
            user_agent: None,
        })
    }

    /// Create a new HTTP client using the global connection pool
    ///
    /// This provides better performance than `new()` by reusing connections across
    /// multiple HttpClient instances. Recommended for production use.
    pub fn with_shared_pool(region: Region, version: ProtocolVersion) -> Self {
        let client = crate::pool::get_global_pool().clone();

        Self {
            client,
            region,
            version,
            max_retries: DEFAULT_MAX_RETRIES,
            initial_backoff_ms: DEFAULT_INITIAL_BACKOFF_MS,
            max_backoff_ms: DEFAULT_MAX_BACKOFF_MS,
            backoff_multiplier: DEFAULT_BACKOFF_MULTIPLIER,
            jitter_factor: DEFAULT_JITTER_FACTOR,
            user_agent: None,
        }
    }

    /// Create a new HTTP client with custom reqwest client
    pub fn with_client(client: Client, region: Region, version: ProtocolVersion) -> Self {
        Self {
            client,
            region,
            version,
            max_retries: DEFAULT_MAX_RETRIES,
            initial_backoff_ms: DEFAULT_INITIAL_BACKOFF_MS,
            max_backoff_ms: DEFAULT_MAX_BACKOFF_MS,
            backoff_multiplier: DEFAULT_BACKOFF_MULTIPLIER,
            jitter_factor: DEFAULT_JITTER_FACTOR,
            user_agent: None,
        }
    }

    /// Set the maximum number of retries for failed requests
    ///
    /// Default is 0 (no retries) to maintain backward compatibility.
    /// Only network and connection errors are retried, not parsing errors.
    pub fn with_max_retries(mut self, max_retries: u32) -> Self {
        self.max_retries = max_retries;
        self
    }

    /// Set the initial backoff duration in milliseconds
    ///
    /// Default is 100ms. This is the base delay before the first retry.
    pub fn with_initial_backoff_ms(mut self, initial_backoff_ms: u64) -> Self {
        self.initial_backoff_ms = initial_backoff_ms;
        self
    }

    /// Set the maximum backoff duration in milliseconds
    ///
    /// Default is 10,000ms (10 seconds). Backoff will not exceed this value.
    pub fn with_max_backoff_ms(mut self, max_backoff_ms: u64) -> Self {
        self.max_backoff_ms = max_backoff_ms;
        self
    }

    /// Set the backoff multiplier
    ///
    /// Default is 2.0. The backoff duration is multiplied by this value after each retry.
    pub fn with_backoff_multiplier(mut self, backoff_multiplier: f64) -> Self {
        self.backoff_multiplier = backoff_multiplier;
        self
    }

    /// Set the jitter factor (0.0 to 1.0)
    ///
    /// Default is 0.1 (10% jitter). Adds randomness to prevent thundering herd.
    pub fn with_jitter_factor(mut self, jitter_factor: f64) -> Self {
        self.jitter_factor = jitter_factor.clamp(0.0, 1.0);
        self
    }

    /// Set a custom user agent string
    ///
    /// If not set, reqwest's default user agent will be used.
    pub fn with_user_agent(mut self, user_agent: impl Into<String>) -> Self {
        self.user_agent = Some(user_agent.into());
        self
    }

    /// Get the base URL for the current configuration
    pub fn base_url(&self) -> String {
        match self.version {
            ProtocolVersion::V1 => {
                format!("http://{}.patch.battle.net:1119", self.region)
            }
            ProtocolVersion::V2 => {
                format!("https://{}.version.battle.net", self.region)
            }
        }
    }

    /// Get the current region
    pub fn region(&self) -> Region {
        self.region
    }

    /// Get the current protocol version
    pub fn version(&self) -> ProtocolVersion {
        self.version
    }

    /// Set the region
    pub fn set_region(&mut self, region: Region) {
        self.region = region;
    }

    /// Calculate backoff duration with exponential backoff and jitter
    #[allow(
        clippy::cast_precision_loss,
        clippy::cast_possible_wrap,
        clippy::cast_possible_truncation,
        clippy::cast_sign_loss
    )]
    fn calculate_backoff(&self, attempt: u32) -> Duration {
        let base_backoff =
            self.initial_backoff_ms as f64 * self.backoff_multiplier.powi(attempt as i32);
        let capped_backoff = base_backoff.min(self.max_backoff_ms as f64);

        // Add jitter
        let jitter_range = capped_backoff * self.jitter_factor;
        let jitter = rand::random::<f64>() * 2.0 * jitter_range - jitter_range;
        let final_backoff = (capped_backoff + jitter).max(0.0) as u64;

        Duration::from_millis(final_backoff)
    }

    /// Execute an HTTP request with retry logic and optional headers
    async fn execute_with_retry_internal(
        &self,
        url: &str,
        headers: Option<&[(&str, &str)]>,
    ) -> Result<Response> {
        let mut last_error = None;

        for attempt in 0..=self.max_retries {
            if attempt > 0 {
                let backoff = self.calculate_backoff(attempt - 1);
                debug!("Retry attempt {} after {:?} backoff", attempt, backoff);
                sleep(backoff).await;
            }

            debug!("HTTP request to {} (attempt {})", url, attempt + 1);

            let mut request = self.client.get(url);
            if let Some(ref user_agent) = self.user_agent {
                request = request.header("User-Agent", user_agent);
            }

            // Add custom headers if provided
            if let Some(headers) = headers {
                for &(key, value) in headers {
                    request = request.header(key, value);
                }
            }

            match request.send().await {
                Ok(response) => {
                    trace!("Response status: {}", response.status());

                    // Check if we should retry based on status code
                    let status = response.status();
                    if (status.is_server_error()
                        || status == reqwest::StatusCode::TOO_MANY_REQUESTS)
                        && attempt < self.max_retries
                    {
                        warn!(
                            "Request returned {} (attempt {}): will retry",
                            status,
                            attempt + 1
                        );
                        last_error = Some(Error::InvalidResponse);
                        continue;
                    }

                    return Ok(response);
                }
                Err(e) => {
                    // Check if error is retryable
                    let is_retryable = e.is_connect() || e.is_timeout() || e.is_request();

                    if is_retryable && attempt < self.max_retries {
                        warn!(
                            "Request failed (attempt {}): {}, will retry",
                            attempt + 1,
                            e
                        );
                        last_error = Some(Error::Http(e));
                    } else {
                        // Non-retryable error or final attempt
                        debug!(
                            "Request failed (attempt {}): {}, not retrying",
                            attempt + 1,
                            e
                        );
                        return Err(Error::Http(e));
                    }
                }
            }
        }

        // This should only be reached if all retries failed
        Err(last_error.unwrap_or(Error::InvalidResponse))
    }

    /// Execute an HTTP request with retry logic
    async fn execute_with_retry(&self, url: &str) -> Result<Response> {
        self.execute_with_retry_internal(url, None).await
    }

    /// Execute an HTTP request with additional headers and retry logic
    async fn execute_with_retry_and_headers(
        &self,
        url: &str,
        headers: &[(&str, &str)],
    ) -> Result<Response> {
        self.execute_with_retry_internal(url, Some(headers)).await
    }

    /// Get versions manifest for a product (V1 protocol)
    pub async fn get_versions(&self, product: &str) -> Result<Response> {
        if self.version != ProtocolVersion::V1 {
            return Err(Error::InvalidProtocolVersion);
        }

        let url = format!("{}/{}/versions", self.base_url(), product);
        self.execute_with_retry(&url).await
    }

    /// Get CDN configuration for a product (V1 protocol)
    pub async fn get_cdns(&self, product: &str) -> Result<Response> {
        if self.version != ProtocolVersion::V1 {
            return Err(Error::InvalidProtocolVersion);
        }

        let url = format!("{}/{}/cdns", self.base_url(), product);
        self.execute_with_retry(&url).await
    }

    /// Get BGDL manifest for a product (V1 protocol)
    pub async fn get_bgdl(&self, product: &str) -> Result<Response> {
        if self.version != ProtocolVersion::V1 {
            return Err(Error::InvalidProtocolVersion);
        }

        let url = format!("{}/{}/bgdl", self.base_url(), product);
        self.execute_with_retry(&url).await
    }

    /// Get product summary (V2 protocol)
    pub async fn get_summary(&self) -> Result<Response> {
        if self.version != ProtocolVersion::V2 {
            return Err(Error::InvalidProtocolVersion);
        }

        let url = self.base_url();
        self.execute_with_retry(&url).await
    }

    /// Get product details (V2 protocol)
    pub async fn get_product(&self, product: &str) -> Result<Response> {
        if self.version != ProtocolVersion::V2 {
            return Err(Error::InvalidProtocolVersion);
        }

        let url = format!("{}/v2/products/{}", self.base_url(), product);
        self.execute_with_retry(&url).await
    }

    /// Get product versions using modern HTTP endpoint (V2 protocol)
    /// Uses the primary endpoint: <https://us.version.battle.net/wow/versions>
    pub async fn get_product_versions_http(&self, product: &str) -> Result<Response> {
        if self.version != ProtocolVersion::V2 {
            return Err(Error::InvalidProtocolVersion);
        }

        let url = format!("{}/{}/versions", self.base_url(), product);
        debug!("Fetching product versions from HTTP endpoint: {}", url);
        self.execute_with_retry(&url).await
    }

    /// Get CDN information using modern HTTP endpoint (V2 protocol)
    /// Uses the primary endpoint: <https://us.version.battle.net/wow/cdns>
    pub async fn get_product_cdns_http(&self, product: &str) -> Result<Response> {
        if self.version != ProtocolVersion::V2 {
            return Err(Error::InvalidProtocolVersion);
        }

        let url = format!("{}/{}/cdns", self.base_url(), product);
        debug!("Fetching CDN configuration from HTTP endpoint: {}", url);
        self.execute_with_retry(&url).await
    }

    /// Make a raw GET request to a path
    pub async fn get(&self, path: &str) -> Result<Response> {
        let url = if path.starts_with('/') {
            format!("{}{}", self.base_url(), path)
        } else {
            format!("{}/{}", self.base_url(), path)
        };

        self.execute_with_retry(&url).await
    }

    /// Download a file from CDN
    pub async fn download_file(&self, cdn_host: &str, path: &str, hash: &str) -> Result<Response> {
        let url = format!(
            "http://{}/{}/{}/{}/{}",
            cdn_host,
            path,
            &hash[0..2],
            &hash[2..4],
            hash
        );

        // Use execute_with_retry for CDN downloads as well
        let response = self.execute_with_retry(&url).await?;

        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(Error::file_not_found(hash));
        }

        Ok(response)
    }

    /// Download a file from CDN with HTTP range request for partial content
    ///
    /// # Arguments
    /// * `cdn_host` - CDN hostname
    /// * `path` - Path prefix for the CDN
    /// * `hash` - File hash
    /// * `range` - Byte range to download (e.g., (0, Some(1023)) for first 1024 bytes)
    ///
    /// # Returns
    /// Returns a response with the requested byte range. The response will have status 206
    /// (Partial Content) if the range is supported, or status 200 (OK) with full content
    /// if range requests are not supported.
    pub async fn download_file_range(
        &self,
        cdn_host: &str,
        path: &str,
        hash: &str,
        range: (u64, Option<u64>),
    ) -> Result<Response> {
        let url = format!(
            "http://{}/{}/{}/{}/{}",
            cdn_host,
            path,
            &hash[0..2],
            &hash[2..4],
            hash
        );

        // Build Range header value
        let range_header = match range {
            (start, Some(end)) => format!("bytes={start}-{end}"),
            (start, None) => format!("bytes={start}-"),
        };

        debug!("Range request: {} Range: {}", url, range_header);

        let response = self
            .execute_with_retry_and_headers(&url, &[("Range", &range_header)])
            .await?;

        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(Error::file_not_found(hash));
        }

        // Check if server supports range requests
        match response.status() {
            reqwest::StatusCode::PARTIAL_CONTENT => {
                trace!("Server returned partial content (206)");
            }
            reqwest::StatusCode::OK => {
                warn!("Server returned full content (200) - range requests not supported");
            }
            status => {
                warn!(
                    "Unexpected status code for range request: {} (expected 206 or 200)",
                    status
                );
                // Still return the response - let the caller handle unexpected status codes
            }
        }

        Ok(response)
    }

    /// Download multiple ranges from a file in a single request
    ///
    /// # Arguments
    /// * `cdn_host` - CDN hostname
    /// * `path` - Path prefix for the CDN
    /// * `hash` - File hash
    /// * `ranges` - Multiple byte ranges to download
    ///
    /// # Note
    /// Multi-range requests return multipart/byteranges content type that needs
    /// special parsing. Use with caution - not all CDN servers support this.
    pub async fn download_file_multirange(
        &self,
        cdn_host: &str,
        path: &str,
        hash: &str,
        ranges: &[(u64, Option<u64>)],
    ) -> Result<Response> {
        let url = format!(
            "http://{}/{}/{}/{}/{}",
            cdn_host,
            path,
            &hash[0..2],
            &hash[2..4],
            hash
        );

        // Build multi-range header value
        let mut range_specs = Vec::new();
        for &(start, end) in ranges {
            match end {
                Some(end) => range_specs.push(format!("{start}-{end}")),
                None => range_specs.push(format!("{start}-")),
            }
        }
        let range_header = format!("bytes={}", range_specs.join(", "));

        debug!("Multi-range request: {} Range: {}", url, range_header);

        let response = self
            .execute_with_retry_and_headers(&url, &[("Range", &range_header)])
            .await?;

        if response.status() == reqwest::StatusCode::NOT_FOUND {
            return Err(Error::file_not_found(hash));
        }

        Ok(response)
    }

    /// Get parsed versions manifest for a product
    pub async fn get_versions_parsed(&self, product: &str) -> Result<Vec<VersionEntry>> {
        let response = self.get_versions(product).await?;
        let text = response.text().await?;
        response_types::parse_versions(&text)
    }

    /// Get parsed CDN manifest for a product
    pub async fn get_cdns_parsed(&self, product: &str) -> Result<Vec<CdnEntry>> {
        let response = self.get_cdns(product).await?;
        let text = response.text().await?;
        response_types::parse_cdns(&text)
    }

    /// Get parsed product versions using modern HTTP endpoint
    pub async fn get_product_versions_http_parsed(
        &self,
        product: &str,
    ) -> Result<Vec<VersionEntry>> {
        let response = self.get_product_versions_http(product).await?;
        let text = response.text().await?;
        response_types::parse_versions(&text)
    }

    /// Get parsed CDN configuration using modern HTTP endpoint
    pub async fn get_product_cdns_http_parsed(&self, product: &str) -> Result<Vec<CdnEntry>> {
        let response = self.get_product_cdns_http(product).await?;
        let text = response.text().await?;
        response_types::parse_cdns(&text)
    }

    /// Get parsed BGDL manifest for a product
    pub async fn get_bgdl_parsed(&self, product: &str) -> Result<Vec<response_types::BgdlEntry>> {
        let response = self.get_bgdl(product).await?;
        let text = response.text().await?;
        response_types::parse_bgdl(&text)
    }
}

impl Default for HttpClient {
    fn default() -> Self {
        Self::new(Region::US, ProtocolVersion::V2).expect("Failed to create default HTTP client")
    }
}

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

    #[test]
    fn test_base_url_v1() {
        let client = HttpClient::new(Region::US, ProtocolVersion::V1).unwrap();
        assert_eq!(client.base_url(), "http://us.patch.battle.net:1119");

        let client = HttpClient::new(Region::EU, ProtocolVersion::V1).unwrap();
        assert_eq!(client.base_url(), "http://eu.patch.battle.net:1119");
    }

    #[test]
    fn test_base_url_v2() {
        let client = HttpClient::new(Region::US, ProtocolVersion::V2).unwrap();
        assert_eq!(client.base_url(), "https://us.version.battle.net");

        let client = HttpClient::new(Region::EU, ProtocolVersion::V2).unwrap();
        assert_eq!(client.base_url(), "https://eu.version.battle.net");
    }

    #[test]
    fn test_region_setting() {
        let mut client = HttpClient::new(Region::US, ProtocolVersion::V1).unwrap();
        assert_eq!(client.region(), Region::US);

        client.set_region(Region::EU);
        assert_eq!(client.region(), Region::EU);
        assert_eq!(client.base_url(), "http://eu.patch.battle.net:1119");
    }

    #[test]
    fn test_retry_configuration() {
        let client = HttpClient::new(Region::US, ProtocolVersion::V1)
            .unwrap()
            .with_max_retries(3)
            .with_initial_backoff_ms(200)
            .with_max_backoff_ms(5000)
            .with_backoff_multiplier(1.5)
            .with_jitter_factor(0.2);

        assert_eq!(client.max_retries, 3);
        assert_eq!(client.initial_backoff_ms, 200);
        assert_eq!(client.max_backoff_ms, 5000);
        assert_eq!(client.backoff_multiplier, 1.5);
        assert_eq!(client.jitter_factor, 0.2);
    }

    #[test]
    fn test_jitter_factor_clamping() {
        let client1 = HttpClient::new(Region::US, ProtocolVersion::V1)
            .unwrap()
            .with_jitter_factor(1.5);
        assert_eq!(client1.jitter_factor, 1.0); // Should be clamped to 1.0

        let client2 = HttpClient::new(Region::US, ProtocolVersion::V1)
            .unwrap()
            .with_jitter_factor(-0.5);
        assert_eq!(client2.jitter_factor, 0.0); // Should be clamped to 0.0
    }

    #[test]
    fn test_backoff_calculation() {
        let client = HttpClient::new(Region::US, ProtocolVersion::V1)
            .unwrap()
            .with_initial_backoff_ms(100)
            .with_max_backoff_ms(1000)
            .with_backoff_multiplier(2.0)
            .with_jitter_factor(0.0); // No jitter for predictable test

        // Test exponential backoff
        let backoff0 = client.calculate_backoff(0);
        assert_eq!(backoff0.as_millis(), 100); // 100ms * 2^0 = 100ms

        let backoff1 = client.calculate_backoff(1);
        assert_eq!(backoff1.as_millis(), 200); // 100ms * 2^1 = 200ms

        let backoff2 = client.calculate_backoff(2);
        assert_eq!(backoff2.as_millis(), 400); // 100ms * 2^2 = 400ms

        // Test max backoff capping
        let backoff5 = client.calculate_backoff(5);
        assert_eq!(backoff5.as_millis(), 1000); // Would be 3200ms but capped at 1000ms
    }

    #[test]
    fn test_default_retry_configuration() {
        let client = HttpClient::new(Region::US, ProtocolVersion::V1).unwrap();
        assert_eq!(client.max_retries, 0); // Default should be 0 for backward compatibility
    }

    #[test]
    fn test_user_agent_configuration() {
        let client = HttpClient::new(Region::US, ProtocolVersion::V1)
            .unwrap()
            .with_user_agent("MyCustomAgent/1.0");

        assert_eq!(client.user_agent, Some("MyCustomAgent/1.0".to_string()));
    }

    #[test]
    fn test_user_agent_default_none() {
        let client = HttpClient::new(Region::US, ProtocolVersion::V1).unwrap();
        assert!(client.user_agent.is_none());
    }

    // Range request tests
    #[test]
    fn test_range_request_header_formatting() {
        // Test range header formatting
        let range1 = (0, Some(1023));
        let header1 = match range1 {
            (start, Some(end)) => format!("bytes={start}-{end}"),
            (start, None) => format!("bytes={start}-"),
        };
        assert_eq!(header1, "bytes=0-1023");

        let range2 = (1024, None::<u64>);
        let header2 = match range2 {
            (start, Some(end)) => format!("bytes={start}-{end}"),
            (start, None) => format!("bytes={start}-"),
        };
        assert_eq!(header2, "bytes=1024-");
    }

    #[test]
    fn test_multirange_header_building() {
        let ranges = [(0, Some(31)), (64, Some(95)), (128, None)];
        let mut range_specs = Vec::new();

        for &(start, end) in &ranges {
            match end {
                Some(end) => range_specs.push(format!("{start}-{end}")),
                None => range_specs.push(format!("{start}-")),
            }
        }

        let range_header = format!("bytes={}", range_specs.join(", "));
        assert_eq!(range_header, "bytes=0-31, 64-95, 128-");
    }
}