fetchttp 1.0.1

`fetch` Web API Implementation In Rust!
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
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
//! HTTP request types and builders following the WHATWG Fetch specification.
//!
//! This module provides the [`Request`] type and related configuration types for
//! creating and managing HTTP requests. It follows the WHATWG Fetch specification
//! closely to provide a familiar API for web developers.
//!
//! # Request Creation
//!
//! Requests are created using the [`Request::new()`] constructor, which takes a URL
//! and optional [`RequestInit`] configuration. The configuration allows setting
//! HTTP method, headers, body, and various fetch options.
//!
//! # Examples
//!
//! ## Simple GET Request
//!
//! ```rust
//! use fetchttp::Request;
//!
//! let request = Request::new("https://api.example.com/users", None).unwrap();
//! assert_eq!(request.method(), "GET");
//! assert_eq!(request.url(), "https://api.example.com/users");
//! ```
//!
//! ## POST Request with JSON Body
//!
//! ```rust
//! use fetchttp::{Request, RequestInit, ReadableStream};
//! use serde_json::json;
//!
//! let data = json!({"name": "John", "email": "john@example.com"});
//!
//! let mut init = RequestInit::new();
//! init.method = Some("POST".to_string());
//! init.body = Some(ReadableStream::from_json(&data));
//!
//! let request = Request::new("https://api.example.com/users", Some(init)).unwrap();
//! assert_eq!(request.method(), "POST");
//! assert!(request.body().is_some());
//! ```
//!
//! ## Request with Custom Headers
//!
//! ```rust
//! use fetchttp::{Request, RequestInit, Headers};
//!
//! let mut headers = Headers::new();
//! headers.set("Authorization", "Bearer token123").unwrap();
//! headers.set("X-API-Version", "v1").unwrap();
//!
//! let mut init = RequestInit::new();
//! init.headers = Some(headers);
//!
//! let request = Request::new("https://api.example.com/protected", Some(init)).unwrap();
//! assert!(request.headers().has("authorization").unwrap());
//! ```

use crate::error::{FetchError, Result, TypeError};
use crate::{AbortSignal, Headers, ReadableStream};
use url::Url;

/// CORS mode for requests.
///
/// This enum specifies how cross-origin requests should be handled, following
/// the WHATWG Fetch specification.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequestMode {
    /// Only allow same-origin requests
    SameOrigin,
    /// Use CORS for cross-origin requests (default)
    Cors,
    /// Allow cross-origin requests without CORS
    NoCors,
    /// Navigation requests (for HTML documents)
    Navigate,
}

impl Default for RequestMode {
    fn default() -> Self {
        Self::Cors
    }
}

/// Credentials mode for requests.
///
/// This enum controls whether credentials (cookies, authorization headers, etc.)
/// are included in requests.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequestCredentials {
    /// Never include credentials
    Omit,
    /// Include credentials for same-origin requests only (default)
    SameOrigin,
    /// Always include credentials
    Include,
}

impl Default for RequestCredentials {
    fn default() -> Self {
        Self::SameOrigin
    }
}

/// Cache mode for requests.
///
/// This enum controls how the request interacts with the HTTP cache.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequestCache {
    /// Use default cache behavior
    Default,
    /// Don't use cache, don't store response
    NoStore,
    /// Bypass cache, always fetch from network
    Reload,
    /// Bypass cache but store response
    NoCache,
    /// Use cache if possible, don't validate
    ForceCache,
    /// Only use cache, fail if not cached
    OnlyIfCached,
}

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

/// Redirect mode for requests.
///
/// This enum controls how redirects are handled.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RequestRedirect {
    /// Follow redirects automatically (default)
    Follow,
    /// Fail on redirects
    Error,
    /// Return redirect response without following
    Manual,
}

impl Default for RequestRedirect {
    fn default() -> Self {
        Self::Follow
    }
}

/// Configuration for creating requests.
///
/// `RequestInit` provides all the options that can be set when creating a new
/// [`Request`]. All fields are optional and will use defaults if not specified.
///
/// # Examples
///
/// ```rust
/// use fetchttp::{RequestInit, ReadableStream, Headers, RequestMode};
///
/// let mut init = RequestInit::new();
/// init.method = Some("PUT".to_string());
/// init.body = Some(ReadableStream::from_text("Hello, World!"));
/// init.mode = Some(RequestMode::Cors);
///
/// // Headers can be set using the Headers type
/// let mut headers = Headers::new();
/// headers.set("Content-Type", "text/plain").unwrap();
/// init.headers = Some(headers);
/// ```
#[derive(Debug, Clone, Default)]
pub struct RequestInit {
    /// HTTP method (GET, POST, PUT, etc.)
    pub method: Option<String>,
    /// Request headers
    pub headers: Option<Headers>,
    /// Request body
    pub body: Option<ReadableStream>,
    /// CORS mode
    pub mode: Option<RequestMode>,
    /// Credentials mode
    pub credentials: Option<RequestCredentials>,
    /// Cache mode
    pub cache: Option<RequestCache>,
    /// Redirect mode
    pub redirect: Option<RequestRedirect>,
    /// Referrer URL or policy
    pub referrer: Option<String>,
    /// Referrer policy
    pub referrer_policy: Option<String>,
    /// Subresource integrity metadata
    pub integrity: Option<String>,
    /// Keep connection alive after page unload
    pub keepalive: Option<bool>,
    /// Abort signal for cancellation
    pub signal: Option<AbortSignal>,
}

impl RequestInit {
    /// Create a new empty RequestInit.
    ///
    /// All fields will be `None` and will use defaults when creating a request.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::RequestInit;
    ///
    /// let init = RequestInit::new();
    /// assert!(init.method.is_none());
    /// assert!(init.headers.is_none());
    /// assert!(init.body.is_none());
    /// ```
    pub fn new() -> Self {
        Self::default()
    }
}

/// An HTTP request following the WHATWG Fetch specification.
///
/// `Request` represents an HTTP request with all its associated metadata including
/// URL, method, headers, body, and fetch options. Requests are immutable once
/// created, but can be cloned if the body hasn't been consumed.
///
/// # Body Consumption
///
/// Request bodies can only be consumed once. After calling any body consumption
/// method (`text()`, `json()`, `array_buffer()`, etc.), the body is marked as used
/// and cannot be consumed again.
///
/// # Examples
///
/// ```rust
/// use fetchttp::{Request, RequestInit};
///
/// // Simple GET request
/// let request = Request::new("https://api.example.com/data", None).unwrap();
/// println!("Method: {}", request.method());
/// println!("URL: {}", request.url());
///
/// // POST request with configuration
/// let mut init = RequestInit::new();
/// init.method = Some("POST".to_string());
///
/// let request = Request::new("https://api.example.com/submit", Some(init)).unwrap();
/// assert_eq!(request.method(), "POST");
/// ```
#[derive(Debug, Clone)]
pub struct Request {
    /// Parsed URL for the request
    url: Url,
    /// HTTP method (normalized to uppercase for standard methods)
    method: String,
    /// Request headers
    headers: Headers,
    /// Request body (optional)
    body: Option<ReadableStream>,
    /// CORS mode
    mode: RequestMode,
    /// Credentials mode
    credentials: RequestCredentials,
    /// Cache mode
    cache: RequestCache,
    /// Redirect mode
    redirect: RequestRedirect,
    /// Referrer information
    referrer: String,
    /// Referrer policy
    referrer_policy: String,
    /// Subresource integrity metadata
    integrity: String,
    /// Keep-alive flag
    keepalive: bool,
    /// Abort signal for cancellation
    signal: Option<AbortSignal>,
}

impl Request {
    /// Create a new HTTP request.
    ///
    /// This constructor validates the URL and request configuration, applies
    /// defaults for unspecified options, and performs validation according to
    /// the WHATWG Fetch specification.
    ///
    /// # Arguments
    ///
    /// * `input` - The URL to request (must be a valid absolute URL)
    /// * `init` - Optional request configuration
    ///
    /// # Returns
    ///
    /// A new `Request` instance, or an error if validation fails.
    ///
    /// # Errors
    ///
    /// * [`TypeError`] - If the URL is invalid, method is invalid, or GET/HEAD requests have a body
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit, ReadableStream};
    ///
    /// // Simple GET request
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert_eq!(request.method(), "GET");
    ///
    /// // POST request with body
    /// let mut init = RequestInit::new();
    /// init.method = Some("POST".to_string());
    /// init.body = Some(ReadableStream::from_text("data"));
    ///
    /// let request = Request::new("https://example.com/api", Some(init)).unwrap();
    /// assert_eq!(request.method(), "POST");
    ///
    /// // Invalid URL will fail
    /// assert!(Request::new("not-a-url", None).is_err());
    ///
    /// // GET with body will fail
    /// let mut invalid_init = RequestInit::new();
    /// invalid_init.method = Some("GET".to_string());
    /// invalid_init.body = Some(ReadableStream::from_text("body"));
    /// assert!(Request::new("https://example.com", Some(invalid_init)).is_err());
    /// ```
    pub fn new(input: &str, init: Option<RequestInit>) -> Result<Self> {
        // Parse and validate URL
        let url = Url::parse(input)?;
        let init = init.unwrap_or_default();

        // Validate and normalize method
        let method = init.method.unwrap_or_else(|| "GET".to_string());
        let method = Self::normalize_method(&method)?;

        // Validate method-body combinations
        if matches!(method.as_str(), "GET" | "HEAD") && init.body.is_some() {
            return Err(FetchError::Type(TypeError::new(
                "Request with GET/HEAD method cannot have body",
            )));
        }

        // Initialize headers
        let mut headers = init.headers.unwrap_or_default();

        // Auto-set Content-Type for bodies that have a default type
        if let Some(ref body) = init.body {
            if let (Ok(None), Some(content_type)) =
                (headers.get("content-type"), body.get_content_type())
            {
                headers.set("content-type", content_type)?;
            }
        }

        Ok(Self {
            url,
            method,
            headers,
            body: init.body,
            mode: init.mode.unwrap_or_default(),
            credentials: init.credentials.unwrap_or_default(),
            cache: init.cache.unwrap_or_default(),
            redirect: init.redirect.unwrap_or_default(),
            referrer: init.referrer.unwrap_or_else(|| "about:client".to_string()),
            referrer_policy: init.referrer_policy.unwrap_or_default(),
            integrity: init.integrity.unwrap_or_default(),
            keepalive: init.keepalive.unwrap_or(false),
            signal: init.signal,
        })
    }

    /// Get the request URL.
    ///
    /// # Returns
    ///
    /// The request URL as a string.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::Request;
    ///
    /// let request = Request::new("https://api.example.com/users?page=1", None).unwrap();
    /// assert_eq!(request.url(), "https://api.example.com/users?page=1");
    /// ```
    pub fn url(&self) -> &str {
        self.url.as_str()
    }

    /// Get the request method.
    ///
    /// # Returns
    ///
    /// The HTTP method as a string (uppercase for standard methods).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit};
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert_eq!(request.method(), "GET");
    ///
    /// let mut init = RequestInit::new();
    /// init.method = Some("post".to_string()); // Will be normalized to uppercase
    /// let request = Request::new("https://example.com", Some(init)).unwrap();
    /// assert_eq!(request.method(), "POST");
    /// ```
    pub fn method(&self) -> &str {
        &self.method
    }

    /// Get the request headers.
    ///
    /// # Returns
    ///
    /// A reference to the request headers.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit, Headers};
    ///
    /// let mut headers = Headers::new();
    /// headers.set("User-Agent", "MyApp/1.0").unwrap();
    ///
    /// let mut init = RequestInit::new();
    /// init.headers = Some(headers);
    ///
    /// let request = Request::new("https://example.com", Some(init)).unwrap();
    /// assert!(request.headers().has("user-agent").unwrap());
    /// ```
    pub fn headers(&self) -> &Headers {
        &self.headers
    }

    /// Get the request body.
    ///
    /// # Returns
    ///
    /// An optional reference to the request body.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit, ReadableStream};
    ///
    /// // Request without body
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert!(request.body().is_none());
    ///
    /// // Request with body
    /// let mut init = RequestInit::new();
    /// init.method = Some("POST".to_string());
    /// init.body = Some(ReadableStream::from_text("data"));
    ///
    /// let request = Request::new("https://example.com", Some(init)).unwrap();
    /// assert!(request.body().is_some());
    /// ```
    pub fn body(&self) -> Option<&ReadableStream> {
        self.body.as_ref()
    }

    /// Check if the request body has been used.
    ///
    /// # Returns
    ///
    /// `true` if the body has been consumed, `false` otherwise.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit, ReadableStream};
    ///
    /// let mut init = RequestInit::new();
    /// init.method = Some("POST".to_string());
    /// init.body = Some(ReadableStream::from_text("data"));
    ///
    /// let request = Request::new("https://example.com", Some(init)).unwrap();
    /// assert!(!request.body_used());
    ///
    /// // After consuming the body, it would be marked as used
    /// // (body consumption moves the request, so we can't show this in the example)
    /// ```
    pub fn body_used(&self) -> bool {
        self.body.as_ref().is_some_and(|b| b.is_used())
    }

    /// Get the request mode.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestMode};
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert_eq!(request.mode(), RequestMode::Cors);
    /// ```
    pub fn mode(&self) -> RequestMode {
        self.mode
    }

    /// Get the credentials mode.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestCredentials};
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert_eq!(request.credentials(), RequestCredentials::SameOrigin);
    /// ```
    pub fn credentials(&self) -> RequestCredentials {
        self.credentials
    }

    /// Get the cache mode.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestCache};
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert_eq!(request.cache(), RequestCache::Default);
    /// ```
    pub fn cache(&self) -> RequestCache {
        self.cache
    }

    /// Get the redirect mode.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestRedirect};
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert_eq!(request.redirect(), RequestRedirect::Follow);
    /// ```
    pub fn redirect(&self) -> RequestRedirect {
        self.redirect
    }

    /// Get the referrer information.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::Request;
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert_eq!(request.referrer(), "about:client");
    /// ```
    pub fn referrer(&self) -> &str {
        &self.referrer
    }

    /// Get the referrer policy.
    pub fn referrer_policy(&self) -> &str {
        &self.referrer_policy
    }

    /// Get the integrity metadata.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::Request;
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert_eq!(request.integrity(), "");
    /// ```
    pub fn integrity(&self) -> &str {
        &self.integrity
    }

    /// Get the keepalive flag.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::Request;
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// assert!(!request.keepalive());
    /// ```
    pub fn keepalive(&self) -> bool {
        self.keepalive
    }

    /// Get the abort signal.
    ///
    /// # Returns
    ///
    /// An optional reference to the abort signal.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit, AbortController};
    ///
    /// let controller = AbortController::new();
    /// let mut init = RequestInit::new();
    /// init.signal = Some(controller.signal().clone());
    ///
    /// let request = Request::new("https://example.com", Some(init)).unwrap();
    /// assert!(request.signal().is_some());
    /// ```
    pub fn signal(&self) -> Option<&AbortSignal> {
        self.signal.as_ref()
    }

    /// Clone the request (WHATWG Fetch API method).
    ///
    /// This method follows the WHATWG Fetch specification for cloning requests.
    /// It will fail if the request body has already been used.
    ///
    /// # Returns
    ///
    /// A cloned request, or an error if the body has been used.
    ///
    /// # Errors
    ///
    /// * [`TypeError`] - If the request body has already been consumed
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::Request;
    ///
    /// let request = Request::new("https://example.com", None).unwrap();
    /// let cloned = request.clone_request().unwrap();
    ///
    /// assert_eq!(request.url(), cloned.url());
    /// assert_eq!(request.method(), cloned.method());
    /// ```
    pub fn clone_request(&self) -> Result<Self> {
        if self.body_used() {
            return Err(FetchError::Type(TypeError::new(
                "Cannot clone a request with a used body",
            )));
        }
        Ok(Clone::clone(self))
    }

    /// Consume the request and return the body as bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit, ReadableStream};
    ///
    /// # tokio_test::block_on(async {
    /// let mut init = RequestInit::new();
    /// init.method = Some("POST".to_string());
    /// init.body = Some(ReadableStream::from_text("Hello, World!"));
    ///
    /// let request = Request::new("https://example.com", Some(init)).unwrap();
    /// let bytes = request.array_buffer().await.unwrap();
    /// assert_eq!(bytes, b"Hello, World!");
    /// # });
    /// ```
    pub async fn array_buffer(self) -> Result<bytes::Bytes> {
        match self.body {
            Some(body) => body.array_buffer().await,
            None => Ok(bytes::Bytes::new()),
        }
    }

    /// Consume the request and return the body as a blob (bytes).
    pub async fn blob(self) -> Result<bytes::Bytes> {
        self.array_buffer().await
    }

    /// Consume the request and return the body as form data.
    pub async fn form_data(self) -> Result<String> {
        match self.body {
            Some(body) => body.form_data().await,
            None => Ok(String::new()),
        }
    }

    /// Consume the request and parse the body as JSON.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit, ReadableStream};
    /// use serde_json::json;
    ///
    /// # tokio_test::block_on(async {
    /// let data = json!({"name": "John", "age": 30});
    ///
    /// let mut init = RequestInit::new();
    /// init.method = Some("POST".to_string());
    /// init.body = Some(ReadableStream::from_json(&data));
    ///
    /// let request = Request::new("https://example.com", Some(init)).unwrap();
    /// let parsed: serde_json::Value = request.json().await.unwrap();
    /// assert_eq!(parsed["name"], "John");
    /// # });
    /// ```
    pub async fn json<T: serde::de::DeserializeOwned>(self) -> Result<T> {
        match self.body {
            Some(body) => body.json().await,
            None => Err(FetchError::Type(TypeError::new(
                "Unexpected end of JSON input",
            ))),
        }
    }

    /// Consume the request and return the body as text.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use fetchttp::{Request, RequestInit, ReadableStream};
    ///
    /// # tokio_test::block_on(async {
    /// let mut init = RequestInit::new();
    /// init.method = Some("POST".to_string());
    /// init.body = Some(ReadableStream::from_text("Hello, World!"));
    ///
    /// let request = Request::new("https://example.com", Some(init)).unwrap();
    /// let text = request.text().await.unwrap();
    /// assert_eq!(text, "Hello, World!");
    /// # });
    /// ```
    pub async fn text(self) -> Result<String> {
        match self.body {
            Some(body) => body.text().await,
            None => Ok(String::new()),
        }
    }

    /// Normalize and validate an HTTP method.
    ///
    /// Standard methods (GET, POST, etc.) are normalized to uppercase.
    /// Custom methods are preserved as-is but validated for valid characters.
    fn normalize_method(method: &str) -> Result<String> {
        if method.is_empty() {
            return Err(FetchError::Type(TypeError::new("Invalid method")));
        }

        // Validate method characters (HTTP token)
        for byte in method.bytes() {
            if !matches!(byte, b'!' | b'#'..=b'\'' | b'*' | b'+' | b'-' | b'.' | b'0'..=b'9' | b'A'..=b'Z' | b'^'..=b'z' | b'|' | b'~')
            {
                return Err(FetchError::Type(TypeError::new("Invalid method")));
            }
        }

        // Normalize standard methods to uppercase
        let upper = method.to_ascii_uppercase();
        match upper.as_str() {
            "GET" | "POST" | "PUT" | "DELETE" | "HEAD" | "OPTIONS" | "PATCH" => Ok(upper),
            _ => Ok(method.to_string()), // Preserve case for custom methods
        }
    }

    /// Get the internal URL object for use by the client.
    pub(crate) fn get_url(&self) -> &Url {
        &self.url
    }

    /// Take the body from the request for consumption.
    ///
    /// This method is used internally by the HTTP client to consume the request body.
    pub(crate) fn take_body(&mut self) -> Option<ReadableStream> {
        self.body.take()
    }
}

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

    #[test]
    fn test_request_creation() {
        let request = Request::new("https://example.com", None).unwrap();
        assert_eq!(request.url(), "https://example.com/");
        assert_eq!(request.method(), "GET");
        assert_eq!(request.mode(), RequestMode::Cors);
        assert_eq!(request.credentials(), RequestCredentials::SameOrigin);
        assert_eq!(request.cache(), RequestCache::Default);
        assert_eq!(request.redirect(), RequestRedirect::Follow);
        assert!(!request.keepalive());
        assert!(!request.body_used());
    }

    #[test]
    fn test_request_with_init() {
        let mut headers = Headers::new();
        headers.set("x-test", "value").unwrap();

        let mut init = RequestInit::new();
        init.method = Some("POST".to_string());
        init.headers = Some(headers);
        init.body = Some(ReadableStream::from_text("test body"));
        init.mode = Some(RequestMode::SameOrigin);
        init.credentials = Some(RequestCredentials::Include);

        let request = Request::new("https://example.com", Some(init)).unwrap();
        assert_eq!(request.method(), "POST");
        assert_eq!(request.mode(), RequestMode::SameOrigin);
        assert_eq!(request.credentials(), RequestCredentials::Include);
        assert!(request.headers().has("x-test").unwrap());
        assert!(request.body().is_some());
    }

    #[test]
    fn test_request_method_validation() {
        // Valid methods
        assert!(Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("GET".to_string());
                init
            })
        )
        .is_ok());

        assert!(Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("POST".to_string());
                init
            })
        )
        .is_ok());

        assert!(Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("CUSTOM".to_string());
                init
            })
        )
        .is_ok());

        // Invalid method (empty)
        assert!(Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("".to_string());
                init
            })
        )
        .is_err());

        // GET with body should fail
        assert!(Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("GET".to_string());
                init.body = Some(ReadableStream::from_text("body"));
                init
            })
        )
        .is_err());
    }

    #[test]
    fn test_request_url_validation() {
        // Valid URLs
        assert!(Request::new("https://example.com", None).is_ok());
        assert!(Request::new("http://localhost:8080/path", None).is_ok());

        // Invalid URLs
        assert!(Request::new("not-a-url", None).is_err());
        assert!(Request::new("", None).is_err());
    }

    #[test]
    fn test_request_defaults() {
        let init = RequestInit::new();
        assert!(init.method.is_none());
        assert!(init.headers.is_none());
        assert!(init.body.is_none());
        assert!(init.mode.is_none());
        assert!(init.credentials.is_none());
        assert!(init.cache.is_none());
        assert!(init.redirect.is_none());
        assert!(init.referrer.is_none());
        assert!(init.referrer_policy.is_none());
        assert!(init.integrity.is_none());
        assert!(init.keepalive.is_none());
        assert!(init.signal.is_none());
    }

    #[test]
    fn test_request_enum_defaults() {
        assert_eq!(RequestMode::default(), RequestMode::Cors);
        assert_eq!(
            RequestCredentials::default(),
            RequestCredentials::SameOrigin
        );
        assert_eq!(RequestCache::default(), RequestCache::Default);
        assert_eq!(RequestRedirect::default(), RequestRedirect::Follow);
    }

    #[tokio::test]
    async fn test_request_body_methods() {
        let request = Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("POST".to_string());
                init.body = Some(ReadableStream::from_text("test body"));
                init
            }),
        )
        .unwrap();

        let text = request.text().await.unwrap();
        assert_eq!(text, "test body");
    }

    #[tokio::test]
    async fn test_request_json_body() {
        let data = serde_json::json!({"key": "value"});
        let request = Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("POST".to_string());
                init.body = Some(ReadableStream::from_json(&data));
                init
            }),
        )
        .unwrap();

        let parsed: serde_json::Value = request.json().await.unwrap();
        assert_eq!(parsed["key"], "value");
    }

    #[test]
    fn test_method_normalization() {
        let request = Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("get".to_string());
                init
            }),
        )
        .unwrap();
        assert_eq!(request.method(), "GET");

        let request = Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("custom".to_string());
                init
            }),
        )
        .unwrap();
        assert_eq!(request.method(), "custom");
    }

    #[test]
    fn test_content_type_auto_set() {
        let request = Request::new(
            "https://example.com",
            Some({
                let mut init = RequestInit::new();
                init.method = Some("POST".to_string());
                init.body = Some(ReadableStream::from_json(&serde_json::json!({})));
                init
            }),
        )
        .unwrap();

        assert_eq!(
            request.headers().get("content-type").unwrap().unwrap(),
            "application/json"
        );
    }

    #[test]
    fn test_request_clone() {
        let request = Request::new("https://example.com", None).unwrap();
        let cloned = request.clone_request().unwrap();

        assert_eq!(request.url(), cloned.url());
        assert_eq!(request.method(), cloned.method());
    }
}