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
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
//! # ETag Middleware
//!
//! Provides HTTP ETag header generation and conditional request handling.
//! Supports both strong and weak ETags for caching optimization.

use crate::middleware::v2::{Middleware, Next, NextFuture};
use crate::request::{ElifMethod, ElifRequest};
use crate::response::{ElifHeaderValue, ElifResponse};
use axum::http::{HeaderMap, HeaderName, HeaderValue};
use std::collections::hash_map::DefaultHasher;
use std::hash::{Hash, Hasher};

/// ETag type
#[derive(Debug, Clone, PartialEq)]
pub enum ETagType {
    /// Strong ETag (exact match required)
    Strong(String),
    /// Weak ETag (semantic equivalence)
    Weak(String),
}

impl ETagType {
    /// Parse ETag from header value
    pub fn from_header_value(value: &str) -> Option<Self> {
        let value = value.trim();
        if value.starts_with("W/") {
            // Weak ETag: W/"value"
            if value.len() > 3 && value.starts_with("W/\"") && value.ends_with('"') {
                let etag_value = &value[3..value.len() - 1];
                Some(Self::Weak(etag_value.to_string()))
            } else {
                None
            }
        } else if value.starts_with('"') && value.ends_with('"') {
            // Strong ETag: "value"
            let etag_value = &value[1..value.len() - 1];
            Some(Self::Strong(etag_value.to_string()))
        } else {
            None
        }
    }

    /// Format ETag for response header
    pub fn to_header_value(&self) -> String {
        match self {
            Self::Strong(value) => format!("\"{}\"", value),
            Self::Weak(value) => format!("W/\"{}\"", value),
        }
    }

    /// Get the ETag value (without quotes or weak prefix)
    pub fn value(&self) -> &str {
        match self {
            Self::Strong(value) | Self::Weak(value) => value,
        }
    }

    /// Check if this ETag matches another for conditional requests
    /// For If-None-Match, both strong and weak comparison allowed
    pub fn matches_for_if_none_match(&self, other: &Self) -> bool {
        self.value() == other.value()
    }

    /// Check if this ETag matches another for If-Match (strong comparison only)
    pub fn matches_for_if_match(&self, other: &Self) -> bool {
        match (self, other) {
            (Self::Strong(a), Self::Strong(b)) => a == b,
            _ => false, // Weak ETags don't match for If-Match
        }
    }
}

/// ETag generation strategy
#[derive(Debug, Clone)]
pub enum ETagStrategy {
    /// Generate ETag from response body hash
    BodyHash,
    /// Generate weak ETag from response body hash
    WeakBodyHash,
    /// Use custom function to generate ETag
    Custom(fn(&[u8], &HeaderMap) -> Option<ETagType>),
}

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

/// Configuration for ETag middleware
#[derive(Debug, Clone)]
pub struct ETagConfig {
    /// Strategy for generating ETags
    pub strategy: ETagStrategy,
    /// Minimum response size to generate ETags for
    pub min_size: usize,
    /// Maximum response size to generate ETags for  
    pub max_size: usize,
    /// Content types to generate ETags for
    pub content_types: Vec<String>,
}

impl Default for ETagConfig {
    fn default() -> Self {
        Self {
            strategy: ETagStrategy::default(),
            min_size: 0,
            max_size: 10 * 1024 * 1024, // 10MB
            content_types: vec![
                "text/html".to_string(),
                "text/css".to_string(),
                "text/javascript".to_string(),
                "text/plain".to_string(),
                "application/json".to_string(),
                "application/javascript".to_string(),
                "application/xml".to_string(),
                "text/xml".to_string(),
                "image/svg+xml".to_string(),
            ],
        }
    }
}

/// Middleware for generating ETags and handling conditional requests
#[derive(Debug)]
pub struct ETagMiddleware {
    config: ETagConfig,
}

impl ETagMiddleware {
    /// Create new ETag middleware with default configuration
    pub fn new() -> Self {
        Self {
            config: ETagConfig::default(),
        }
    }

    /// Create ETag middleware with custom configuration
    pub fn with_config(config: ETagConfig) -> Self {
        Self { config }
    }

    /// Set ETag generation strategy
    pub fn strategy(mut self, strategy: ETagStrategy) -> Self {
        self.config.strategy = strategy;
        self
    }

    /// Set minimum size for ETag generation
    pub fn min_size(mut self, min_size: usize) -> Self {
        self.config.min_size = min_size;
        self
    }

    /// Set maximum size for ETag generation
    pub fn max_size(mut self, max_size: usize) -> Self {
        self.config.max_size = max_size;
        self
    }

    /// Add content type for ETag generation
    pub fn content_type(mut self, content_type: impl Into<String>) -> Self {
        self.config.content_types.push(content_type.into());
        self
    }

    /// Use weak ETags (faster generation, semantic equivalence)
    pub fn weak(mut self) -> Self {
        self.config.strategy = ETagStrategy::WeakBodyHash;
        self
    }

    /// Check if response should have ETag generated
    fn should_generate_etag(&self, headers: &HeaderMap, body_size: usize) -> bool {
        // Check size limits
        if body_size < self.config.min_size || body_size > self.config.max_size {
            return false;
        }

        // Don't generate ETag if already present
        if headers.contains_key("etag") {
            return false;
        }

        // Check content type
        if let Some(content_type) = headers.get("content-type") {
            if let Ok(content_type_str) = content_type.to_str() {
                let content_type_lower = content_type_str.to_lowercase();
                return self
                    .config
                    .content_types
                    .iter()
                    .any(|ct| content_type_lower.starts_with(&ct.to_lowercase()));
            }
        }

        // Generate ETag for responses without content-type header
        true
    }

    /// Generate ETag for response body
    fn generate_etag(&self, body: &[u8], headers: &HeaderMap) -> Option<ETagType> {
        match &self.config.strategy {
            ETagStrategy::BodyHash => {
                let mut hasher = DefaultHasher::new();
                body.hash(&mut hasher);
                // Hash relevant headers (content-type, etc.)
                for (name, value) in headers.iter() {
                    name.as_str().hash(&mut hasher);
                    if let Ok(value_str) = value.to_str() {
                        value_str.hash(&mut hasher);
                    }
                }
                let hash = hasher.finish();
                Some(ETagType::Strong(format!("{:x}", hash)))
            }
            ETagStrategy::WeakBodyHash => {
                let mut hasher = DefaultHasher::new();
                body.hash(&mut hasher);
                let hash = hasher.finish();
                Some(ETagType::Weak(format!("{:x}", hash)))
            }
            ETagStrategy::Custom(func) => func(body, headers),
        }
    }

    /// Parse If-None-Match header
    fn parse_if_none_match(&self, header_value: &str) -> Vec<ETagType> {
        let mut etags = Vec::new();

        // Handle "*" case
        if header_value.trim() == "*" {
            return etags; // Return empty vec, will be handled specially
        }

        // Parse comma-separated ETags
        for etag_str in header_value.split(',') {
            if let Some(etag) = ETagType::from_header_value(etag_str) {
                etags.push(etag);
            }
        }

        etags
    }

    /// Parse If-Match header
    fn parse_if_match(&self, header_value: &str) -> Vec<ETagType> {
        let mut etags = Vec::new();

        // Handle "*" case
        if header_value.trim() == "*" {
            return etags; // Return empty vec, will be handled specially
        }

        // Parse comma-separated ETags
        for etag_str in header_value.split(',') {
            if let Some(etag) = ETagType::from_header_value(etag_str) {
                etags.push(etag);
            }
        }

        etags
    }

    /// Check If-None-Match condition
    fn check_if_none_match(&self, request_etags: &[ETagType], response_etag: &ETagType) -> bool {
        if request_etags.is_empty() {
            return true; // No condition to check
        }

        // If any ETag matches, condition fails (return 304)
        !request_etags
            .iter()
            .any(|req_etag| response_etag.matches_for_if_none_match(req_etag))
    }

    /// Check If-Match condition
    fn check_if_match(&self, request_etags: &[ETagType], response_etag: &ETagType) -> bool {
        if request_etags.is_empty() {
            return true; // No condition to check
        }

        // If any ETag matches with strong comparison, condition passes
        request_etags
            .iter()
            .any(|req_etag| response_etag.matches_for_if_match(req_etag))
    }

    /// Handle conditional requests and add ETag to response with extracted headers
    async fn process_response_with_headers(
        &self,
        response: ElifResponse,
        if_none_match: Option<ElifHeaderValue>,
        if_match: Option<ElifHeaderValue>,
        request_method: ElifMethod,
    ) -> ElifResponse {
        // Convert elif types to axum types for internal processing
        let axum_if_none_match = if_none_match.as_ref().map(|v| v.to_axum());
        let axum_if_match = if_match.as_ref().map(|v| v.to_axum());
        let axum_method = request_method.to_axum();

        let axum_response = response.into_axum_response();
        let (parts, body) = axum_response.into_parts();

        // Collect body bytes
        let body_bytes = match axum::body::to_bytes(body, usize::MAX).await {
            Ok(bytes) => bytes,
            Err(_) => {
                // If we can't read the body, return as-is
                let response =
                    axum::response::Response::from_parts(parts, axum::body::Body::empty());
                return ElifResponse::from_axum_response(response).await;
            }
        };

        // Check if we should generate ETag
        if !self.should_generate_etag(&parts.headers, body_bytes.len()) {
            let response =
                axum::response::Response::from_parts(parts, axum::body::Body::from(body_bytes));
            return ElifResponse::from_axum_response(response).await;
        }

        // Generate ETag
        let etag = match self.generate_etag(&body_bytes, &parts.headers) {
            Some(etag) => etag,
            None => {
                // ETag generation failed, return original response
                let response =
                    axum::response::Response::from_parts(parts, axum::body::Body::from(body_bytes));
                return ElifResponse::from_axum_response(response).await;
            }
        };

        // Check conditional request headers

        // Handle If-None-Match (typically used with GET/HEAD for caching)
        if let Some(if_none_match) = axum_if_none_match {
            if let Ok(if_none_match_str) = if_none_match.to_str() {
                let request_etags = self.parse_if_none_match(if_none_match_str);

                // Special case: "*" matches any ETag
                // RFC 7232: For GET/HEAD, return 304. For others, return 412 if resource exists.
                if if_none_match_str.trim() == "*" {
                    return if axum_method == axum::http::Method::GET
                        || axum_method == axum::http::Method::HEAD
                    {
                        // Return 304 Not Modified for GET/HEAD
                        ElifResponse::from_axum_response(
                            axum::response::Response::builder()
                                .status(axum::http::StatusCode::NOT_MODIFIED)
                                .header("etag", etag.to_header_value())
                                .body(axum::body::Body::empty())
                                .unwrap(),
                        )
                        .await
                    } else {
                        // Return 412 Precondition Failed for state-changing methods
                        ElifResponse::from_axum_response(
                            axum::response::Response::builder()
                                .status(axum::http::StatusCode::PRECONDITION_FAILED)
                                .header("etag", etag.to_header_value())
                                .body(axum::body::Body::from(
                                    serde_json::to_vec(&serde_json::json!({
                                        "error": {
                                            "code": "precondition_failed",
                                            "message": "If-None-Match: * failed - resource exists"
                                        }
                                    }))
                                    .unwrap_or_default(),
                                ))
                                .unwrap(),
                        )
                        .await
                    };
                }

                if !self.check_if_none_match(&request_etags, &etag) {
                    // ETag matches - behavior depends on request method
                    return if axum_method == axum::http::Method::GET
                        || axum_method == axum::http::Method::HEAD
                    {
                        // Return 304 Not Modified for GET/HEAD
                        ElifResponse::from_axum_response(
                            axum::response::Response::builder()
                                .status(axum::http::StatusCode::NOT_MODIFIED)
                                .header("etag", etag.to_header_value())
                                .body(axum::body::Body::empty())
                                .unwrap(),
                        )
                        .await
                    } else {
                        // Return 412 Precondition Failed for state-changing methods
                        ElifResponse::from_axum_response(
                            axum::response::Response::builder()
                                .status(axum::http::StatusCode::PRECONDITION_FAILED)
                                .header("etag", etag.to_header_value())
                                .body(axum::body::Body::from(
                                    serde_json::to_vec(&serde_json::json!({
                                        "error": {
                                            "code": "precondition_failed",
                                            "message": "If-None-Match precondition failed - resource unchanged"
                                        }
                                    })).unwrap_or_default()
                                ))
                                .unwrap()
                        ).await
                    };
                }
            }
        }

        // Handle If-Match (typically used with PUT/POST for conflict detection)
        if let Some(if_match) = axum_if_match {
            if let Ok(if_match_str) = if_match.to_str() {
                let request_etags = self.parse_if_match(if_match_str);

                // Special case: "*" matches if resource exists
                if if_match_str.trim() == "*" {
                    // Resource exists (we have a response), so condition passes
                } else if !self.check_if_match(&request_etags, &etag) {
                    // No ETag matches with strong comparison, return 412 Precondition Failed
                    return ElifResponse::from_axum_response(
                        axum::response::Response::builder()
                            .status(axum::http::StatusCode::PRECONDITION_FAILED)
                            .header("etag", etag.to_header_value())
                            .body(axum::body::Body::from(
                                serde_json::to_vec(&serde_json::json!({
                                    "error": {
                                        "code": "precondition_failed",
                                        "message": "Request ETag does not match current resource ETag"
                                    }
                                })).unwrap_or_default()
                            ))
                            .unwrap()
                    ).await;
                }
            }
        }

        // Add ETag header to successful response
        let mut new_parts = parts;
        new_parts.headers.insert(
            HeaderName::from_static("etag"),
            HeaderValue::from_str(&etag.to_header_value()).unwrap(),
        );

        // Add Cache-Control header if not present
        if !new_parts.headers.contains_key("cache-control") {
            new_parts.headers.insert(
                HeaderName::from_static("cache-control"),
                HeaderValue::from_static("private, max-age=0"),
            );
        }

        let response =
            axum::response::Response::from_parts(new_parts, axum::body::Body::from(body_bytes));

        ElifResponse::from_axum_response(response).await
    }
}

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

impl Middleware for ETagMiddleware {
    fn handle(&self, request: ElifRequest, next: Next) -> NextFuture<'static> {
        let config = self.config.clone();

        Box::pin(async move {
            // Extract needed headers and method before moving request
            let if_none_match = request.header("if-none-match").cloned();
            let if_match = request.header("if-match").cloned();
            let method = request.method.clone();

            let response = next.run(request).await;

            // Process response to add ETag and handle conditional requests
            let middleware = ETagMiddleware { config };
            middleware
                .process_response_with_headers(response, if_none_match, if_match, method)
                .await
        })
    }

    fn name(&self) -> &'static str {
        "ETagMiddleware"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::request::ElifRequest;
    use crate::response::ElifResponse;

    #[test]
    fn test_etag_parsing() {
        // Test strong ETag
        let etag = ETagType::from_header_value("\"abc123\"").unwrap();
        assert_eq!(etag, ETagType::Strong("abc123".to_string()));
        assert_eq!(etag.to_header_value(), "\"abc123\"");

        // Test weak ETag
        let etag = ETagType::from_header_value("W/\"abc123\"").unwrap();
        assert_eq!(etag, ETagType::Weak("abc123".to_string()));
        assert_eq!(etag.to_header_value(), "W/\"abc123\"");

        // Test invalid ETag
        assert!(ETagType::from_header_value("invalid").is_none());
        assert!(ETagType::from_header_value("\"unclosed").is_none());
    }

    #[test]
    fn test_etag_matching() {
        let strong1 = ETagType::Strong("abc123".to_string());
        let strong2 = ETagType::Strong("abc123".to_string());
        let strong3 = ETagType::Strong("def456".to_string());
        let weak1 = ETagType::Weak("abc123".to_string());

        // If-None-Match allows both strong and weak comparison
        assert!(strong1.matches_for_if_none_match(&strong2));
        assert!(strong1.matches_for_if_none_match(&weak1));
        assert!(!strong1.matches_for_if_none_match(&strong3));

        // If-Match requires strong comparison
        assert!(strong1.matches_for_if_match(&strong2));
        assert!(!strong1.matches_for_if_match(&weak1));
        assert!(!strong1.matches_for_if_match(&strong3));
    }

    #[test]
    fn test_etag_config() {
        let config = ETagConfig::default();
        assert_eq!(config.min_size, 0);
        assert_eq!(config.max_size, 10 * 1024 * 1024);
        assert!(config
            .content_types
            .contains(&"application/json".to_string()));
    }

    #[test]
    fn test_should_generate_etag() {
        let middleware = ETagMiddleware::new();

        // Test with JSON content type
        let mut headers = HeaderMap::new();
        headers.insert("content-type", "application/json".parse().unwrap());
        assert!(middleware.should_generate_etag(&headers, 1024));

        // Test with existing ETag
        headers.insert("etag", "\"existing\"".parse().unwrap());
        assert!(!middleware.should_generate_etag(&headers, 1024));

        // Test with unsupported content type
        let mut headers = HeaderMap::new();
        headers.insert("content-type", "image/jpeg".parse().unwrap());
        assert!(!middleware.should_generate_etag(&headers, 1024));
    }

    #[tokio::test]
    async fn test_etag_generation() {
        let middleware = ETagMiddleware::new();

        let request = ElifRequest::new(
            crate::request::ElifMethod::GET,
            "/api/data".parse().unwrap(),
            crate::response::headers::ElifHeaderMap::new(),
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Hello, World!"
                }))
            })
        });

        let response = middleware.handle(request, next).await;

        assert_eq!(
            response.status_code(),
            crate::response::status::ElifStatusCode::OK
        );

        // Convert to check headers
        let axum_response = response.into_axum_response();
        let (parts, _) = axum_response.into_parts();
        assert!(parts.headers.contains_key("etag"));
    }

    #[tokio::test]
    async fn test_if_none_match_304() {
        let middleware = ETagMiddleware::new();

        // First request to get ETag
        let request = ElifRequest::new(
            crate::request::ElifMethod::GET,
            "/api/data".parse().unwrap(),
            crate::response::headers::ElifHeaderMap::new(),
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Hello, World!"
                }))
            })
        });

        let response = middleware.handle(request, next).await;
        let axum_response = response.into_axum_response();
        let (parts, _) = axum_response.into_parts();

        let etag_header = parts.headers.get("etag").unwrap();
        let etag_value = etag_header.to_str().unwrap();

        // Second request with If-None-Match
        let mut headers = crate::response::headers::ElifHeaderMap::new();
        let header_name =
            crate::response::headers::ElifHeaderName::from_str("if-none-match").unwrap();
        let header_value = crate::response::headers::ElifHeaderValue::from_str(etag_value).unwrap();
        headers.insert(header_name, header_value);
        let request = ElifRequest::new(
            crate::request::ElifMethod::GET,
            "/api/data".parse().unwrap(),
            headers,
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Hello, World!"
                }))
            })
        });

        let response = middleware.handle(request, next).await;
        assert_eq!(
            response.status_code(),
            crate::response::status::ElifStatusCode::NOT_MODIFIED
        );
    }

    #[tokio::test]
    async fn test_if_match_412() {
        let middleware = ETagMiddleware::new();

        let mut headers = crate::response::headers::ElifHeaderMap::new();
        let header_name = crate::response::headers::ElifHeaderName::from_str("if-match").unwrap();
        let header_value =
            crate::response::headers::ElifHeaderValue::from_str("\"non-matching-etag\"").unwrap();
        headers.insert(header_name, header_value);
        let request = ElifRequest::new(
            crate::request::ElifMethod::PUT,
            "/api/data".parse().unwrap(),
            headers,
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Updated!"
                }))
            })
        });

        let response = middleware.handle(request, next).await;
        assert_eq!(
            response.status_code(),
            crate::response::status::ElifStatusCode::PRECONDITION_FAILED
        );
    }

    #[tokio::test]
    async fn test_if_none_match_star_put_request() {
        let middleware = ETagMiddleware::new();

        let mut headers = crate::response::headers::ElifHeaderMap::new();
        let header_name =
            crate::response::headers::ElifHeaderName::from_str("if-none-match").unwrap();
        let header_value = crate::response::headers::ElifHeaderValue::from_str("*").unwrap();
        headers.insert(header_name, header_value);
        let request = ElifRequest::new(
            crate::request::ElifMethod::PUT, // State-changing method
            "/api/data".parse().unwrap(),
            headers,
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Created!"
                }))
            })
        });

        let response = middleware.handle(request, next).await;
        // Should return 412 for PUT with If-None-Match: * when resource exists
        assert_eq!(
            response.status_code(),
            crate::response::status::ElifStatusCode::PRECONDITION_FAILED
        );
    }

    #[tokio::test]
    async fn test_if_none_match_star_get_request() {
        let middleware = ETagMiddleware::new();

        let mut headers = crate::response::headers::ElifHeaderMap::new();
        let header_name =
            crate::response::headers::ElifHeaderName::from_str("if-none-match").unwrap();
        let header_value = crate::response::headers::ElifHeaderValue::from_str("*").unwrap();
        headers.insert(header_name, header_value);
        let request = ElifRequest::new(
            crate::request::ElifMethod::GET, // Safe method
            "/api/data".parse().unwrap(),
            headers,
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Data"
                }))
            })
        });

        let response = middleware.handle(request, next).await;
        // Should return 304 for GET with If-None-Match: *
        assert_eq!(
            response.status_code(),
            crate::response::status::ElifStatusCode::NOT_MODIFIED
        );
    }

    #[tokio::test]
    async fn test_if_none_match_etag_put_request() {
        let middleware = ETagMiddleware::new();

        // First request to get ETag
        let request = ElifRequest::new(
            crate::request::ElifMethod::GET,
            "/api/data".parse().unwrap(),
            crate::response::headers::ElifHeaderMap::new(),
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Data"
                }))
            })
        });

        let response = middleware.handle(request, next).await;
        let axum_response = response.into_axum_response();
        let (parts, _) = axum_response.into_parts();

        let etag_header = parts.headers.get("etag").unwrap();
        let etag_value = etag_header.to_str().unwrap();

        // Second request - PUT with matching ETag
        let mut headers = crate::response::headers::ElifHeaderMap::new();
        let header_name =
            crate::response::headers::ElifHeaderName::from_str("if-none-match").unwrap();
        let header_value = crate::response::headers::ElifHeaderValue::from_str(etag_value).unwrap();
        headers.insert(header_name, header_value);
        let request = ElifRequest::new(
            crate::request::ElifMethod::PUT,
            "/api/data".parse().unwrap(),
            headers,
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Data"
                }))
            })
        });

        let response = middleware.handle(request, next).await;
        // Should return 412 for PUT when ETag matches
        assert_eq!(
            response.status_code(),
            crate::response::status::ElifStatusCode::PRECONDITION_FAILED
        );
    }

    #[tokio::test]
    async fn test_weak_etag_strategy() {
        let middleware = ETagMiddleware::new().weak();

        let request = ElifRequest::new(
            crate::request::ElifMethod::GET,
            "/api/data".parse().unwrap(),
            crate::response::headers::ElifHeaderMap::new(),
        );

        let next = Next::new(|_req| {
            Box::pin(async move {
                ElifResponse::ok().json_value(serde_json::json!({
                    "message": "Hello, World!"
                }))
            })
        });

        let response = middleware.handle(request, next).await;
        let axum_response = response.into_axum_response();
        let (parts, _) = axum_response.into_parts();

        let etag_header = parts.headers.get("etag").unwrap();
        let etag_value = etag_header.to_str().unwrap();
        assert!(etag_value.starts_with("W/"));
    }

    #[test]
    fn test_etag_middleware_builder() {
        let middleware = ETagMiddleware::new()
            .min_size(1024)
            .max_size(5 * 1024 * 1024)
            .content_type("application/xml")
            .weak();

        assert_eq!(middleware.config.min_size, 1024);
        assert_eq!(middleware.config.max_size, 5 * 1024 * 1024);
        assert!(middleware
            .config
            .content_types
            .contains(&"application/xml".to_string()));
        assert!(matches!(
            middleware.config.strategy,
            ETagStrategy::WeakBodyHash
        ));
    }
}