elif-http 0.8.8

HTTP server core for the elif.rs LLM-friendly web framework
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
//! Request abstraction for handling HTTP requests
//!
//! Provides rich request parsing and data extraction capabilities.

use super::ElifMethod;
use crate::errors::{HttpError, HttpResult};
use crate::response::ElifHeaderMap;
use axum::{body::Bytes, http::Uri};
use serde::de::DeserializeOwned;
use std::any::{Any, TypeId};
use std::collections::HashMap;

/// Request abstraction that wraps Axum's request types
/// with additional parsing and extraction capabilities
#[derive(Debug)]
pub struct ElifRequest {
    pub method: ElifMethod,
    pub uri: Uri,
    pub headers: ElifHeaderMap,
    pub path_params: HashMap<String, String>,
    pub query_params: HashMap<String, String>,
    pub extensions: HashMap<TypeId, Box<dyn Any + Send + Sync>>,
    body_bytes: Option<Bytes>,
}

impl ElifRequest {
    /// Create new ElifRequest from Axum components
    pub fn new(method: ElifMethod, uri: Uri, headers: ElifHeaderMap) -> Self {
        Self {
            method,
            uri,
            headers,
            path_params: HashMap::new(),
            query_params: HashMap::new(),
            extensions: HashMap::new(),
            body_bytes: None,
        }
    }

    /// Extract ElifRequest from request components
    pub fn extract_elif_request(
        method: ElifMethod,
        uri: Uri,
        headers: ElifHeaderMap,
        body: Option<Bytes>,
    ) -> ElifRequest {
        let mut request = ElifRequest::new(method, uri, headers);
        if let Some(body) = body {
            request = request.with_body(body);
        }
        request
    }

    /// Set path parameters extracted from route
    pub fn with_path_params(mut self, params: HashMap<String, String>) -> Self {
        self.path_params = params;
        self
    }

    /// Set query parameters
    pub fn with_query_params(mut self, params: HashMap<String, String>) -> Self {
        self.query_params = params;
        self
    }

    /// Set request body bytes (consuming)
    pub fn with_body(mut self, body: Bytes) -> Self {
        self.body_bytes = Some(body);
        self
    }

    /// Set request body bytes (borrowing - for middleware use)
    pub fn set_body(&mut self, body: Bytes) {
        self.body_bytes = Some(body);
    }

    /// Add header to request (for middleware use)
    pub fn add_header<K, V>(&mut self, key: K, value: V) -> HttpResult<()>
    where
        K: AsRef<str>,
        V: AsRef<str>,
    {
        use crate::response::{ElifHeaderName, ElifHeaderValue};

        let header_name = ElifHeaderName::from_str(key.as_ref())
            .map_err(|e| HttpError::bad_request(format!("Invalid header name: {}", e)))?;
        let header_value = ElifHeaderValue::from_str(value.as_ref())
            .map_err(|e| HttpError::bad_request(format!("Invalid header value: {}", e)))?;

        self.headers.insert(header_name, header_value);
        Ok(())
    }

    /// Add path parameter (for middleware use)
    pub fn add_path_param<K, V>(&mut self, key: K, value: V)
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.path_params.insert(key.into(), value.into());
    }

    /// Add query parameter (for middleware use)
    pub fn add_query_param<K, V>(&mut self, key: K, value: V)
    where
        K: Into<String>,
        V: Into<String>,
    {
        self.query_params.insert(key.into(), value.into());
    }

    /// Get path parameter by name
    pub fn path_param(&self, name: &str) -> Option<&String> {
        self.path_params.get(name)
    }

    /// Get path parameter by name, parsed to specific type
    pub fn path_param_parsed<T>(&self, name: &str) -> HttpResult<T>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Display,
    {
        let param = self
            .path_param(name)
            .ok_or_else(|| HttpError::bad_request(format!("Missing path parameter: {}", name)))?;

        param
            .parse::<T>()
            .map_err(|e| HttpError::bad_request(format!("Invalid path parameter {}: {}", name, e)))
    }

    /// Get query parameter by name
    pub fn query_param(&self, name: &str) -> Option<&String> {
        self.query_params.get(name)
    }

    /// Get query parameter by name, parsed to specific type
    pub fn query_param_parsed<T>(&self, name: &str) -> HttpResult<Option<T>>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Display,
    {
        if let Some(param) = self.query_param(name) {
            let parsed = param.parse::<T>().map_err(|e| {
                HttpError::bad_request(format!("Invalid query parameter {}: {}", name, e))
            })?;
            Ok(Some(parsed))
        } else {
            Ok(None)
        }
    }

    /// Get required query parameter by name, parsed to specific type
    pub fn query_param_required<T>(&self, name: &str) -> HttpResult<T>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Display,
    {
        self.query_param_parsed(name)?.ok_or_else(|| {
            HttpError::bad_request(format!("Missing required query parameter: {}", name))
        })
    }

    /// Get header value by name
    pub fn header(&self, name: &str) -> Option<&crate::response::ElifHeaderValue> {
        self.headers.get_str(name)
    }

    /// Get header value as string
    pub fn header_string(&self, name: &str) -> HttpResult<Option<String>> {
        if let Some(value) = self.header(name) {
            let str_value = value.to_str().map_err(|_| {
                HttpError::bad_request(format!("Invalid header value for {}", name))
            })?;
            Ok(Some(str_value.to_string()))
        } else {
            Ok(None)
        }
    }

    /// Get Content-Type header
    pub fn content_type(&self) -> HttpResult<Option<String>> {
        self.header_string("content-type")
    }

    /// Check if request has JSON content type
    pub fn is_json(&self) -> bool {
        if let Ok(Some(content_type)) = self.content_type() {
            content_type.contains("application/json")
        } else {
            false
        }
    }

    /// Get request body as bytes
    pub fn body_bytes(&self) -> Option<&Bytes> {
        self.body_bytes.as_ref()
    }

    /// Parse JSON body to specified type
    pub fn json<T: DeserializeOwned>(&self) -> HttpResult<T> {
        let bytes = self
            .body_bytes()
            .ok_or_else(|| HttpError::bad_request("No request body".to_string()))?;

        serde_json::from_slice(bytes)
            .map_err(|e| HttpError::bad_request(format!("Invalid JSON body: {}", e)))
    }

    /// Parse JSON body to specified type (async version for consistency)
    pub async fn json_async<T: DeserializeOwned>(&self) -> HttpResult<T> {
        self.json()
    }

    /// Parse form data body to specified type
    pub fn form<T: DeserializeOwned>(&self) -> HttpResult<T> {
        let bytes = self
            .body_bytes()
            .ok_or_else(|| HttpError::bad_request("No request body".to_string()))?;

        let body_str = std::str::from_utf8(bytes)
            .map_err(|_| HttpError::bad_request("Invalid UTF-8 in form body".to_string()))?;

        serde_urlencoded::from_str(body_str)
            .map_err(|e| HttpError::bad_request(format!("Invalid form data: {}", e)))
    }

    /// Parse query parameters to specified type
    pub fn query<T: DeserializeOwned>(&self) -> HttpResult<T> {
        let query_str = self.query_string().unwrap_or("");
        serde_urlencoded::from_str::<T>(query_str)
            .map_err(|e| HttpError::bad_request(format!("Invalid query parameters: {}", e)))
    }

    /// Parse path parameters to specified type
    pub fn path_params<T: DeserializeOwned>(&self) -> HttpResult<T> {
        let json_value = serde_json::to_value(&self.path_params)
            .map_err(|e| HttpError::internal(format!("Failed to serialize path params: {}", e)))?;

        serde_json::from_value::<T>(json_value)
            .map_err(|e| HttpError::bad_request(format!("Invalid path parameters: {}", e)))
    }

    /// Get a query parameter as a specific type
    pub fn query_param_as<T>(&self, name: &str) -> HttpResult<Option<T>>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Display,
    {
        match self.query_param(name) {
            Some(param) => {
                let parsed = param.parse::<T>().map_err(|e| {
                    HttpError::bad_request(format!(
                        "Invalid {} query parameter '{}': {}",
                        name, param, e
                    ))
                })?;
                Ok(Some(parsed))
            }
            None => Ok(None),
        }
    }

    /// Get User-Agent header
    pub fn user_agent(&self) -> Option<String> {
        self.header_string("user-agent").unwrap_or(None)
    }

    /// Get Authorization header
    pub fn authorization(&self) -> Option<String> {
        self.header_string("authorization").unwrap_or(None)
    }

    /// Extract Bearer token from Authorization header
    pub fn bearer_token(&self) -> Option<String> {
        if let Some(auth) = self.authorization() {
            if auth.starts_with("Bearer ") {
                Some(auth[7..].to_string())
            } else {
                None
            }
        } else {
            None
        }
    }

    /// Get request IP address from headers or connection
    pub fn client_ip(&self) -> Option<String> {
        // Try common forwarded headers first
        if let Ok(Some(forwarded)) = self.header_string("x-forwarded-for") {
            // Take first IP if multiple
            if let Some(ip) = forwarded.split(',').next() {
                return Some(ip.trim().to_string());
            }
        }

        if let Ok(Some(real_ip)) = self.header_string("x-real-ip") {
            return Some(real_ip);
        }

        // Could extend with connection info if available
        None
    }

    /// Check if request is HTTPS
    pub fn is_secure(&self) -> bool {
        self.uri
            .scheme()
            .map(|s| s == &axum::http::uri::Scheme::HTTPS)
            .unwrap_or(false)
    }

    /// Get request host
    pub fn host(&self) -> Option<&str> {
        self.uri.host()
    }

    /// Get request path
    pub fn path(&self) -> &str {
        self.uri.path()
    }

    /// Get query string
    pub fn query_string(&self) -> Option<&str> {
        self.uri.query()
    }

    /// Convert ElifRequest to Axum Request for backward compatibility
    pub(crate) fn into_axum_request(self) -> axum::extract::Request {
        use axum::body::Body;

        let body = match self.body_bytes {
            Some(bytes) => Body::from(bytes),
            None => Body::empty(),
        };

        let mut builder = axum::extract::Request::builder()
            .method(self.method.to_axum())
            .uri(self.uri);

        // Add headers one by one
        for (key, value) in self.headers.iter() {
            builder = builder.header(key.to_axum(), value.to_axum());
        }

        builder
            .body(body)
            .expect("Failed to construct Axum request")
    }

    /// Convert Axum Request to ElifRequest for backward compatibility
    pub(crate) async fn from_axum_request(request: axum::extract::Request) -> Self {
        let (parts, body) = request.into_parts();

        // Extract body bytes
        let body_bytes = (axum::body::to_bytes(body, usize::MAX).await).ok();

        Self::extract_elif_request(
            ElifMethod::from_axum(parts.method),
            parts.uri,
            ElifHeaderMap::from_axum(parts.headers),
            body_bytes,
        )
    }

    /// Get a reference to the extensions map for reading middleware-added data
    pub fn extensions(&self) -> &HashMap<TypeId, Box<dyn Any + Send + Sync>> {
        &self.extensions
    }

    /// Get a mutable reference to the extensions map for adding middleware data
    pub fn extensions_mut(&mut self) -> &mut HashMap<TypeId, Box<dyn Any + Send + Sync>> {
        &mut self.extensions
    }

    /// Insert typed data into request extensions (helper for middleware)
    pub fn insert_extension<T: Send + Sync + 'static>(&mut self, data: T) {
        self.extensions.insert(TypeId::of::<T>(), Box::new(data));
    }

    /// Get typed data from request extensions (helper for middleware)
    pub fn get_extension<T: Send + Sync + 'static>(&self) -> Option<&T> {
        self.extensions
            .get(&TypeId::of::<T>())
            .and_then(|any| any.downcast_ref::<T>())
    }
}

/// Enhanced parameter extraction methods with better error handling and type safety
impl ElifRequest {
    /// Extract and parse a path parameter with proper error handling
    ///
    /// This is the preferred method for extracting path parameters as it provides
    /// better error messages and type safety compared to the legacy methods.
    pub fn path_param_typed<T>(&self, name: &str) -> Result<T, crate::request::pipeline::ParamError>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug + std::fmt::Display,
    {
        crate::request::pipeline::parameter_extraction::extract_path_param(self, name)
    }

    /// Extract and parse a path parameter as i32
    pub fn path_param_int(&self, name: &str) -> Result<i32, crate::request::pipeline::ParamError> {
        self.path_param_typed(name)
    }

    /// Extract and parse a path parameter as u32
    pub fn path_param_u32(&self, name: &str) -> Result<u32, crate::request::pipeline::ParamError> {
        self.path_param_typed(name)
    }

    /// Extract and parse a path parameter as i64
    pub fn path_param_i64(&self, name: &str) -> Result<i64, crate::request::pipeline::ParamError> {
        self.path_param_typed(name)
    }

    /// Extract and parse a path parameter as u64
    pub fn path_param_u64(&self, name: &str) -> Result<u64, crate::request::pipeline::ParamError> {
        self.path_param_typed(name)
    }

    /// Extract and parse a path parameter as UUID
    pub fn path_param_uuid(
        &self,
        name: &str,
    ) -> Result<uuid::Uuid, crate::request::pipeline::ParamError> {
        self.path_param_typed(name)
    }

    /// Extract and parse a path parameter as String (validates non-empty)
    pub fn path_param_string(
        &self,
        name: &str,
    ) -> Result<String, crate::request::pipeline::ParamError> {
        let value: String = self.path_param_typed(name)?;
        if value.is_empty() {
            return Err(crate::request::pipeline::ParamError::ParseError {
                param: name.to_string(),
                value: value.clone(),
                error: "Parameter cannot be empty".to_string(),
            });
        }
        Ok(value)
    }

    /// Extract and parse an optional query parameter with proper error handling
    pub fn query_param_typed_new<T>(
        &self,
        name: &str,
    ) -> Result<Option<T>, crate::request::pipeline::ParamError>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug + std::fmt::Display,
    {
        crate::request::pipeline::parameter_extraction::extract_query_param(self, name)
    }

    /// Extract and parse a required query parameter with proper error handling  
    pub fn query_param_required_typed<T>(
        &self,
        name: &str,
    ) -> Result<T, crate::request::pipeline::ParamError>
    where
        T: std::str::FromStr,
        T::Err: std::fmt::Debug + std::fmt::Display,
    {
        crate::request::pipeline::parameter_extraction::extract_required_query_param(self, name)
    }

    /// Extract query parameter as optional i32
    pub fn query_param_int_new(
        &self,
        name: &str,
    ) -> Result<Option<i32>, crate::request::pipeline::ParamError> {
        self.query_param_typed_new(name)
    }

    /// Extract query parameter as required i32
    pub fn query_param_int_required(
        &self,
        name: &str,
    ) -> Result<i32, crate::request::pipeline::ParamError> {
        self.query_param_required_typed(name)
    }

    /// Extract query parameter as optional u32
    pub fn query_param_u32_new(
        &self,
        name: &str,
    ) -> Result<Option<u32>, crate::request::pipeline::ParamError> {
        self.query_param_typed_new(name)
    }

    /// Extract query parameter as required u32
    pub fn query_param_u32_required(
        &self,
        name: &str,
    ) -> Result<u32, crate::request::pipeline::ParamError> {
        self.query_param_required_typed(name)
    }

    /// Extract query parameter as optional bool
    pub fn query_param_bool_new(
        &self,
        name: &str,
    ) -> Result<Option<bool>, crate::request::pipeline::ParamError> {
        self.query_param_typed_new(name)
    }

    /// Extract query parameter as required bool
    pub fn query_param_bool_required(
        &self,
        name: &str,
    ) -> Result<bool, crate::request::pipeline::ParamError> {
        self.query_param_required_typed(name)
    }

    /// Extract query parameter as optional String
    pub fn query_param_string_new(
        &self,
        name: &str,
    ) -> Result<Option<String>, crate::request::pipeline::ParamError> {
        self.query_param_typed_new(name)
    }

    /// Extract query parameter as required String
    pub fn query_param_string_required(
        &self,
        name: &str,
    ) -> Result<String, crate::request::pipeline::ParamError> {
        self.query_param_required_typed(name)
    }

    /// Validate that path parameter exists and is not empty
    pub fn validate_path_param(
        &self,
        name: &str,
    ) -> Result<&String, crate::request::pipeline::ParamError> {
        let param = self
            .path_params
            .get(name)
            .ok_or_else(|| crate::request::pipeline::ParamError::Missing(name.to_string()))?;

        if param.is_empty() {
            return Err(crate::request::pipeline::ParamError::ParseError {
                param: name.to_string(),
                value: param.clone(),
                error: "Parameter cannot be empty".to_string(),
            });
        }

        Ok(param)
    }

    /// Check if request has a specific path parameter
    pub fn has_path_param(&self, name: &str) -> bool {
        self.path_params.contains_key(name)
    }

    /// Check if request has a specific query parameter
    pub fn has_query_param(&self, name: &str) -> bool {
        self.query_params.contains_key(name)
    }

    /// Get all path parameter names
    pub fn path_param_names(&self) -> Vec<&String> {
        self.path_params.keys().collect()
    }

    /// Get all query parameter names
    pub fn query_param_names(&self) -> Vec<&String> {
        self.query_params.keys().collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::response::ElifHeaderMap;
    use axum::http::Uri;
    use std::collections::HashMap;

    #[test]
    fn test_new_path_param_methods() {
        let mut params = HashMap::new();
        params.insert("id".to_string(), "123".to_string());
        params.insert("slug".to_string(), "test-post".to_string());

        let mut request = ElifRequest::new(
            ElifMethod::GET,
            "/users/123/posts/test-post".parse().unwrap(),
            ElifHeaderMap::new(),
        );
        request.path_params = params;

        // Test existing convenient methods
        assert_eq!(request.path_param("id"), Some(&"123".to_string()));
        assert_eq!(request.path_param("slug"), Some(&"test-post".to_string()));
        assert_eq!(request.path_param("nonexistent"), None);

        // Test typed path params (existing method)
        let id: u32 = request.path_param_parsed("id").unwrap();
        assert_eq!(id, 123);

        let slug: String = request.path_param_parsed("slug").unwrap();
        assert_eq!(slug, "test-post");

        // Test error on invalid type conversion
        assert!(request.path_param_parsed::<u32>("slug").is_err());
    }

    #[test]
    fn test_query_param_methods() {
        let mut query_params = HashMap::new();
        query_params.insert("page".to_string(), "2".to_string());
        query_params.insert("search".to_string(), "hello world".to_string());

        let mut request = ElifRequest::new(
            ElifMethod::GET,
            "/search?page=2&search=hello%20world".parse().unwrap(),
            ElifHeaderMap::new(),
        );
        request.query_params = query_params;

        // Test query param access
        assert_eq!(request.query_param("page"), Some(&"2".to_string()));
        assert_eq!(
            request.query_param("search"),
            Some(&"hello world".to_string())
        );
        assert_eq!(request.query_param("nonexistent"), None);

        // Test typed query params
        let page: Option<u32> = request.query_param_as("page").unwrap();
        assert_eq!(page, Some(2));

        let nonexistent: Option<u32> = request.query_param_as("nonexistent").unwrap();
        assert_eq!(nonexistent, None);

        // Test error on invalid type conversion
        assert!(request.query_param_as::<u32>("search").is_err());
    }

    #[test]
    fn test_header_method() {
        let mut headers = ElifHeaderMap::new();
        headers.insert(
            "Content-Type".parse().unwrap(),
            "application/json".parse().unwrap(),
        );
        headers.insert(
            "User-Agent".parse().unwrap(),
            "test-client/1.0".parse().unwrap(),
        );

        let request = ElifRequest::new(ElifMethod::POST, "/api/test".parse().unwrap(), headers);

        // Test header access
        assert_eq!(
            request.header_string("content-type").unwrap(),
            Some("application/json".to_string())
        );
        assert_eq!(
            request.header_string("user-agent").unwrap(),
            Some("test-client/1.0".to_string())
        );
        assert_eq!(request.header_string("nonexistent").unwrap(), None);
    }

    #[test]
    fn test_query_param_extraction() {
        let mut query_params = HashMap::new();
        query_params.insert("page".to_string(), "2".to_string());
        query_params.insert("per_page".to_string(), "25".to_string());
        query_params.insert("search".to_string(), "rust".to_string());

        let request = ElifRequest::new(
            ElifMethod::GET,
            "/posts?page=2&per_page=25&search=rust".parse().unwrap(),
            ElifHeaderMap::new(),
        )
        .with_query_params(query_params);

        assert_eq!(request.query_param("page"), Some(&"2".to_string()));
        let page: u32 = request.query_param_required("page").unwrap();
        assert_eq!(page, 2);

        let per_page: Option<u32> = request.query_param_parsed("per_page").unwrap();
        assert_eq!(per_page, Some(25));

        assert!(request.query_param_parsed::<u32>("search").is_err()); // Should fail parsing
    }

    #[test]
    fn test_json_detection() {
        let mut headers = ElifHeaderMap::new();
        let header_name = crate::response::ElifHeaderName::from_str("content-type").unwrap();
        let header_value = crate::response::ElifHeaderValue::from_str("application/json").unwrap();
        headers.insert(header_name, header_value);

        let request = ElifRequest::new(ElifMethod::POST, "/api/users".parse().unwrap(), headers);

        assert!(request.is_json());
    }

    #[test]
    fn test_bearer_token_extraction() {
        let mut headers = ElifHeaderMap::new();
        let header_name = crate::response::ElifHeaderName::from_str("authorization").unwrap();
        let header_value = crate::response::ElifHeaderValue::from_str("Bearer abc123xyz").unwrap();
        headers.insert(header_name, header_value);

        let request = ElifRequest::new(ElifMethod::GET, "/api/protected".parse().unwrap(), headers);

        let token = request.bearer_token().unwrap();
        assert_eq!(token, "abc123xyz");
    }

    #[test]
    fn test_extract_elif_request() {
        let method = ElifMethod::POST;
        let uri: Uri = "/test".parse().unwrap();
        let headers = ElifHeaderMap::new();
        let body = Some(Bytes::from("test body"));

        let request = ElifRequest::extract_elif_request(
            method.clone(),
            uri.clone(),
            headers.clone(),
            body.clone(),
        );

        assert_eq!(request.method, method);
        assert_eq!(request.uri, uri);
        assert_eq!(request.body_bytes(), body.as_ref());
    }

    #[test]
    fn test_borrowing_api_headers() {
        let mut request = ElifRequest::new(
            ElifMethod::GET,
            "/test".parse().unwrap(),
            ElifHeaderMap::new(),
        );

        // Test adding headers with borrowing API
        request.add_header("x-middleware", "processed").unwrap();
        request.add_header("x-custom", "value").unwrap();

        let middleware_header =
            crate::response::headers::ElifHeaderName::from_str("x-middleware").unwrap();
        let custom_header = crate::response::headers::ElifHeaderName::from_str("x-custom").unwrap();
        assert!(request.headers.contains_key(&middleware_header));
        assert!(request.headers.contains_key(&custom_header));
        assert_eq!(
            request
                .headers
                .get(&middleware_header)
                .unwrap()
                .to_str()
                .unwrap(),
            "processed"
        );
    }

    #[test]
    fn test_borrowing_api_params() {
        let mut request = ElifRequest::new(
            ElifMethod::GET,
            "/users/123?page=2".parse().unwrap(),
            ElifHeaderMap::new(),
        );

        // Test adding parameters with borrowing API
        request.add_path_param("id", "123");
        request.add_path_param("section", "profile");
        request.add_query_param("page", "2");
        request.add_query_param("limit", "10");

        assert_eq!(request.path_param("id"), Some(&"123".to_string()));
        assert_eq!(request.path_param("section"), Some(&"profile".to_string()));
        assert_eq!(request.query_param("page"), Some(&"2".to_string()));
        assert_eq!(request.query_param("limit"), Some(&"10".to_string()));
    }

    #[test]
    fn test_borrowing_api_body() {
        let mut request = ElifRequest::new(
            ElifMethod::POST,
            "/test".parse().unwrap(),
            ElifHeaderMap::new(),
        );

        let body_data = Bytes::from("test body content");
        request.set_body(body_data.clone());

        assert_eq!(request.body_bytes(), Some(&body_data));
    }

    #[test]
    fn test_borrowing_api_middleware_pattern() {
        let mut request = ElifRequest::new(
            ElifMethod::GET,
            "/api/users".parse().unwrap(),
            ElifHeaderMap::new(),
        );

        // Simulate middleware adding context data
        request.add_header("x-request-id", "req-123").unwrap();
        request.add_path_param("user_id", "456");
        request.add_query_param("enriched", "true");

        // Verify all modifications were applied
        let request_id_header =
            crate::response::headers::ElifHeaderName::from_str("x-request-id").unwrap();
        assert!(request.headers.contains_key(&request_id_header));
        assert_eq!(request.path_param("user_id"), Some(&"456".to_string()));
        assert_eq!(request.query_param("enriched"), Some(&"true".to_string()));
    }
}