actix-web-lab 0.29.5

In-progress extractors and middleware for Actix Web
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
//! JSON extractor that uses deser 0.10 and retains error context.

use std::{
    marker::PhantomData,
    pin::Pin,
    task::{Context, Poll, ready},
};

use ::deser_0_10::{ErrorCategory, de::DeserializeOwned};
use actix_web::{
    FromRequest, HttpMessage, HttpRequest, ResponseError, dev::Payload, http::StatusCode,
};
use derive_more::{Display, Error};
use deser_path_0_10::{Path, PathLayer};

use crate::{
    bytes::{BytesBody, BytesPayloadError},
    json::DEFAULT_JSON_LIMIT,
};

/// JSON extractor with const-generic payload size limit and error paths (using [`deser`]).
///
/// `DeserJson` is used to extract typed data from JSON request payloads with deser.
/// Requires the `deser-0_10` feature.
///
/// # Extractor
/// To extract typed data from a request body, the inner type `T` must implement the
/// [`DeserializeOwned`] trait.
///
/// Use the `LIMIT` const generic parameter to control the payload size limit. The default limit
/// that is exported ([`DEFAULT_JSON_LIMIT`]) is 2MiB.
///
/// Accepts JSON content types, including `application/*+json`. Does not read
/// [`actix_web::web::JsonConfig`] or decompress request bodies.
///
/// ```
/// use actix_web::{error, post, HttpResponse, Responder, ResponseError};
/// use actix_web_lab::deser_0_10::{DeserJson, DeserJsonPayloadError, DEFAULT_JSON_LIMIT};
/// use deser_0_10::Deserialize;
/// use serde::Serialize;
/// use serde_json::json;
///
/// #[derive(Deserialize, Serialize)]
/// #[deser(crate = deser_0_10)]
/// struct Info {
///     username: String,
/// }
///
/// /// Deserialize `Info` from the request body.
/// #[post("/")]
/// async fn index(info: DeserJson<Info>) -> String {
///     format!("Welcome {}!", info.username)
/// }
///
/// const LIMIT_32_MB: usize = 33_554_432;
///
/// /// Deserialize a payload with a higher 32MiB limit.
/// #[post("/big-payload")]
/// async fn big_payload(info: DeserJson<Info, LIMIT_32_MB>) -> String {
///     format!("Welcome {}!", info.username)
/// }
///
/// /// Capture an error that occurred while deserializing the body.
/// #[post("/normal-payload")]
/// async fn normal_payload(
///     res: Result<DeserJson<Info>, DeserJsonPayloadError>,
/// ) -> actix_web::Result<impl Responder> {
///     let item = res.map_err(|err| {
///         eprintln!("failed to deserialize JSON: {err}");
///         let path = match &err {
///             DeserJsonPayloadError::Deserialize { source } => Some(source.path().to_string()),
///             _ => None,
///         };
///
///         let res = HttpResponse::build(err.status_code()).json(json!({
///             "error": "invalid_json",
///             "detail": err.to_string(),
///             "path": path,
///         }));
///         error::InternalError::from_response(err, res)
///     })?;
///
///     Ok(HttpResponse::Ok().json(item.0))
/// }
/// ```
///
/// Deserialization errors expose their field and array index path through
/// [`DeserJsonDeserializeError::path`]. The path is empty when no path context is available.
/// [`DeserJsonDeserializeError::source`] retains the original [`::deser_0_10::Error`], including its
/// kind, category, message, source, location, and typed attachments. A [`deser_path_0_10::PathLayer`]
/// also attaches the path to the original error. The path and location appear in the error
/// message.
///
/// Syntax errors, incomplete JSON, and deser limit errors return HTTP 400. Data errors,
/// including missing fields, type conversion failures, and validation errors, return HTTP 422.
/// Unsupported types, API or configuration errors, and deserialization I/O errors return
/// HTTP 500. The status code uses [`::deser_0_10::Error::category`]. Unknown deserialization error
/// categories return HTTP 500. Exceeding the payload byte limit returns HTTP 413.
///
/// [`deser`]: deser_0_10
/// [`DeserializeOwned`]: ::deser_0_10::de::DeserializeOwned
#[derive(Debug, Display)]
pub struct DeserJson<T, const LIMIT: usize = DEFAULT_JSON_LIMIT>(pub T);

impl<T, const LIMIT: usize> std::ops::Deref for DeserJson<T, LIMIT> {
    type Target = T;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T, const LIMIT: usize> std::ops::DerefMut for DeserJson<T, LIMIT> {
    fn deref_mut(&mut self) -> &mut Self::Target {
        &mut self.0
    }
}

impl<T, const LIMIT: usize> DeserJson<T, LIMIT> {
    /// Unwraps into inner `T` value.
    pub fn into_inner(self) -> T {
        self.0
    }
}

/// See [here](#extractor) for example of usage as an extractor.
impl<T: DeserializeOwned, const LIMIT: usize> FromRequest for DeserJson<T, LIMIT> {
    type Error = DeserJsonPayloadError;
    type Future = DeserJsonExtractFut<T, LIMIT>;

    fn from_request(req: &HttpRequest, payload: &mut Payload) -> Self::Future {
        let can_parse_json =
            req.mime_type().ok().flatten().is_some_and(|mime| {
                mime.subtype() == mime::JSON || mime.suffix() == Some(mime::JSON)
            });

        let body = if can_parse_json {
            Ok(BytesBody::new(req, payload))
        } else {
            Err(Some(DeserJsonPayloadError::ContentType))
        };

        DeserJsonExtractFut {
            body,
            _res: PhantomData,
        }
    }
}

/// Future that extracts a JSON payload with deser.
#[allow(missing_debug_implementations)]
pub struct DeserJsonExtractFut<T, const LIMIT: usize> {
    body: Result<BytesBody<LIMIT>, Option<DeserJsonPayloadError>>,
    _res: PhantomData<fn() -> T>,
}

impl<T: DeserializeOwned, const LIMIT: usize> Future for DeserJsonExtractFut<T, LIMIT> {
    type Output = Result<DeserJson<T, LIMIT>, DeserJsonPayloadError>;

    fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
        let this = self.get_mut();

        let body = match &mut this.body {
            Ok(body) => body,
            Err(err) => {
                return Poll::Ready(Err(err
                    .take()
                    .expect("Future should not be polled after completion")));
            }
        };

        let bytes = ready!(Pin::new(body).poll(cx)).map_err(|err| match err {
            BytesPayloadError::OverflowKnownLength { length, limit } => {
                DeserJsonPayloadError::Overflow {
                    limit,
                    length: Some(length),
                }
            }
            BytesPayloadError::Overflow { limit } => DeserJsonPayloadError::Overflow {
                limit,
                length: None,
            },
            BytesPayloadError::Payload(source) => DeserJsonPayloadError::Payload { source },
        })?;

        let json = deser_json_0_10::Deserializer::from_slice(&bytes)
            .deserialize_with(|driver| driver.push_layer(PathLayer::new()))
            .map(DeserJson)
            .map_err(|source| DeserJsonPayloadError::Deserialize {
                source: source.into(),
            });

        Poll::Ready(json)
    }
}

/// Errors that can occur while extracting JSON with deser.
#[derive(Debug, Display, Error)]
#[non_exhaustive]
pub enum DeserJsonPayloadError {
    /// The payload exceeds the configured size limit.
    #[display("JSON payload is larger than allowed (limit: {limit} bytes)")]
    Overflow {
        /// Configured payload size limit.
        limit: usize,

        /// The Content-Length, if sent.
        length: Option<usize>,
    },

    /// The request does not have a JSON content type.
    #[display("Content type error")]
    ContentType,

    /// JSON deserialization failed.
    #[display("JSON deserialization failed: {source}")]
    Deserialize {
        /// Deserialization error with its path and original deser error.
        source: DeserJsonDeserializeError,
    },

    /// Reading the request payload failed.
    #[display("Failed to read JSON payload: {source}")]
    Payload {
        /// Original payload error.
        source: actix_web::error::PayloadError,
    },
}

/// Deserialization errors that can occur while extracting a JSON payload with deser.
#[derive(Debug, Display, Error)]
#[display("{source}")]
pub struct DeserJsonDeserializeError {
    /// Path where the deserialization error occurred.
    path: Path,

    /// Original deserialization error.
    source: ::deser_0_10::Error,
}

impl DeserJsonDeserializeError {
    /// Returns the path at which the deserialization error occurred.
    ///
    /// The path is empty when no path context is available.
    pub fn path(&self) -> impl std::fmt::Display + '_ {
        &self.path
    }

    /// Returns the source error.
    pub fn source(&self) -> &::deser_0_10::Error {
        &self.source
    }
}

impl From<::deser_0_10::Error> for DeserJsonDeserializeError {
    fn from(source: ::deser_0_10::Error) -> Self {
        Self {
            path: source.attachment::<Path>().cloned().unwrap_or_default(),
            source,
        }
    }
}

impl ResponseError for DeserJsonPayloadError {
    fn status_code(&self) -> StatusCode {
        match self {
            Self::Overflow { .. } => StatusCode::PAYLOAD_TOO_LARGE,
            Self::ContentType => StatusCode::NOT_ACCEPTABLE,
            Self::Deserialize { source } => match source.source().category() {
                ErrorCategory::Syntax | ErrorCategory::Eof | ErrorCategory::Limit => {
                    StatusCode::BAD_REQUEST
                }
                ErrorCategory::Data => StatusCode::UNPROCESSABLE_ENTITY,
                ErrorCategory::Unsupported | ErrorCategory::Usage | ErrorCategory::Io => {
                    StatusCode::INTERNAL_SERVER_ERROR
                }
                _ => StatusCode::INTERNAL_SERVER_ERROR,
            },
            Self::Payload { source } => source.status_code(),
        }
    }
}

#[cfg(test)]
mod tests {
    use std::{error::Error as _, str};

    use ::deser_0_10::{Deserialize, ErrorCategory, ErrorKind};
    use actix_web::{App, error::PayloadError, http::header, test, web};
    use bytes::Bytes;
    use deser_path_0_10::{Path, PathSegment};
    use futures_util::{Stream, stream};

    use super::*;

    #[derive(Debug, Deserialize, PartialEq)]
    #[deser(crate = ::deser_0_10)]
    struct Server {
        host: String,
        port: u16,
    }

    #[derive(Debug, Deserialize, PartialEq)]
    #[deser(crate = ::deser_0_10)]
    struct Config {
        servers: Vec<Server>,
    }

    #[actix_web::test]
    async fn deserialization_error_path_is_empty_without_context() {
        let err = DeserJsonDeserializeError::from(::deser_0_10::Error::new(
            ErrorKind::Syntax,
            "invalid JSON",
        ));

        assert!(err.path().to_string().is_empty());
    }

    #[actix_web::test]
    async fn deserialization_error_categories_have_expected_responses() {
        for (kind, status) in [
            (ErrorKind::Syntax, StatusCode::BAD_REQUEST),
            (ErrorKind::EndOfFile, StatusCode::BAD_REQUEST),
            (ErrorKind::LimitExceeded, StatusCode::BAD_REQUEST),
            (ErrorKind::InvalidType, StatusCode::UNPROCESSABLE_ENTITY),
            (
                ErrorKind::UnsupportedType,
                StatusCode::INTERNAL_SERVER_ERROR,
            ),
            (ErrorKind::InvalidState, StatusCode::INTERNAL_SERVER_ERROR),
            (ErrorKind::Configuration, StatusCode::INTERNAL_SERVER_ERROR),
            (ErrorKind::Io, StatusCode::INTERNAL_SERVER_ERROR),
        ] {
            let err = DeserJsonPayloadError::Deserialize {
                source: ::deser_0_10::Error::new(kind, "deserialization failed").into(),
            };

            assert_eq!(err.error_response().status(), status, "{kind:?}");
        }
    }

    #[actix_web::test]
    async fn extracts_json_content_types() {
        for content_type in [
            "application/json",
            "application/json; charset=utf-8",
            "application/vnd.api+json",
        ] {
            let (req, mut payload) = test::TestRequest::default()
                .insert_header((header::CONTENT_TYPE, content_type))
                .set_payload(r#"{"host":"localhost","port":8080}"#)
                .to_http_parts();

            let mut json = DeserJson::<Server>::from_request(&req, &mut payload)
                .await
                .unwrap();

            assert_eq!(json.host, "localhost");
            json.port = 80;
            assert_eq!(
                json.into_inner(),
                Server {
                    host: "localhost".into(),
                    port: 80
                }
            );
        }
    }

    #[actix_web::test]
    async fn retains_nested_error_path_and_location() {
        let body = "{\n  \"servers\": [{\"host\": \"a\", \"port\": 80}, {\"host\": \"b\", \"port\": \"bad\"}]\n}";
        let (req, mut payload) = test::TestRequest::default()
            .insert_header(header::ContentType::json())
            .set_payload(body)
            .to_http_parts();

        let err = DeserJson::<Config>::from_request(&req, &mut payload)
            .await
            .unwrap_err();

        assert_eq!(err.status_code(), StatusCode::UNPROCESSABLE_ENTITY);

        let DeserJsonPayloadError::Deserialize { source } = &err else {
            panic!("unexpected error: {err}");
        };

        assert_eq!(source.path().to_string(), "servers[1].port");

        let source = source.source();
        let path = source.attachment::<Path>().unwrap();
        assert_eq!(path.to_string(), "servers[1].port");
        assert_eq!(
            path.segments(),
            &[
                PathSegment::Key("servers".into()),
                PathSegment::Index(1),
                PathSegment::Key("port".into()),
            ]
        );
        assert_eq!(source.kind(), ErrorKind::InvalidType);
        assert_eq!(source.category(), ErrorCategory::Data);
        assert_eq!(source.line(), Some(2));
        assert_eq!(source.offset(), Some(body.find("\"bad\"").unwrap()));
        assert!(source.column().is_some());
        assert!(source.message().contains("expected u16"));
        assert!(err.to_string().contains("path: servers[1].port"));
        assert_eq!(err.source().unwrap().to_string(), source.to_string());
    }

    #[actix_web::test]
    async fn retains_missing_field_error() {
        let (req, mut payload) = test::TestRequest::default()
            .insert_header(header::ContentType::json())
            .set_payload(r#"{"servers":[{"host":"a"}]}"#)
            .to_http_parts();

        let err = DeserJson::<Config>::from_request(&req, &mut payload)
            .await
            .unwrap_err();

        assert_eq!(err.status_code(), StatusCode::UNPROCESSABLE_ENTITY);

        let DeserJsonPayloadError::Deserialize { source } = err else {
            panic!("unexpected error: {err}");
        };

        assert_eq!(source.path().to_string(), "servers[0]");

        let source = source.source();
        assert_eq!(source.kind(), ErrorKind::MissingField);
        assert_eq!(source.category(), ErrorCategory::Data);
        assert_eq!(
            source.attachment::<Path>().unwrap().to_string(),
            "servers[0]"
        );
        assert!(source.message().contains("port"));
    }

    #[actix_web::test]
    async fn rejects_out_of_range_numbers() {
        for body in ["65536", "-1"] {
            let (req, mut payload) = test::TestRequest::default()
                .insert_header(header::ContentType::json())
                .set_payload(body)
                .to_http_parts();

            let err = DeserJson::<u16>::from_request(&req, &mut payload)
                .await
                .unwrap_err();

            assert_eq!(err.status_code(), StatusCode::UNPROCESSABLE_ENTITY);

            let DeserJsonPayloadError::Deserialize { source } = err else {
                panic!("unexpected error: {err}");
            };

            assert_eq!(source.source().kind(), ErrorKind::OutOfRange);
            assert_eq!(source.source().category(), ErrorCategory::Data);
        }
    }

    #[actix_web::test]
    async fn rejects_invalid_values() {
        let (req, mut payload) = test::TestRequest::default()
            .insert_header(header::ContentType::json())
            .set_payload(r#""invalid address""#)
            .to_http_parts();

        let err = DeserJson::<std::net::IpAddr>::from_request(&req, &mut payload)
            .await
            .unwrap_err();

        assert_eq!(err.status_code(), StatusCode::UNPROCESSABLE_ENTITY);

        let DeserJsonPayloadError::Deserialize { source } = err else {
            panic!("unexpected error: {err}");
        };

        assert_eq!(source.source().kind(), ErrorKind::InvalidValue);
        assert_eq!(source.source().category(), ErrorCategory::Data);
    }

    #[actix_web::test]
    async fn retains_custom_error_source_and_attachments() {
        #[derive(Debug)]
        struct ParsedPort;

        #[derive(Debug)]
        struct ValidationCode(&'static str);

        impl ::deser_0_10::ErrorAttachment for ValidationCode {}

        impl<'de> Deserialize<'de> for ParsedPort {
            fn deserialize_atom(
                slot: &mut ::deser_0_10::de::Slot<Self>,
                atom: ::deser_0_10::Atom<'_>,
                state: &mut ::deser_0_10::State,
            ) -> Result<(), ::deser_0_10::Error> {
                let mut text = None::<String>;
                String::deserialize_atom(::deser_0_10::de::Slot::wrap(&mut text), atom, state)?;

                text.unwrap().parse::<u16>().map_err(|source| {
                    let mut err = ::deser_0_10::Error::new(ErrorKind::Custom, "invalid port");
                    err.set_source(source);
                    err.set_attachment(ValidationCode("invalid_port"));
                    err
                })?;

                slot.set(Self);
                Ok(())
            }
        }

        let (req, mut payload) = test::TestRequest::default()
            .insert_header(header::ContentType::json())
            .set_payload(r#"["bad"]"#)
            .to_http_parts();

        let err = DeserJson::<Vec<ParsedPort>>::from_request(&req, &mut payload)
            .await
            .unwrap_err();

        assert_eq!(err.status_code(), StatusCode::UNPROCESSABLE_ENTITY);

        let source = err
            .source()
            .unwrap()
            .downcast_ref::<DeserJsonDeserializeError>()
            .unwrap();

        assert_eq!(source.path().to_string(), "[0]");
        assert!(
            std::error::Error::source(source)
                .unwrap()
                .is::<::deser_0_10::Error>()
        );

        let source = source.source();
        assert_eq!(source.kind(), ErrorKind::Custom);
        assert_eq!(source.category(), ErrorCategory::Data);
        assert_eq!(source.message(), "invalid port");
        assert!(source.source().unwrap().is::<std::num::ParseIntError>());
        assert_eq!(
            source.attachment::<ValidationCode>().unwrap().0,
            "invalid_port"
        );
        assert_eq!(source.attachment::<Path>().unwrap().to_string(), "[0]");
        assert_eq!(source.offset(), Some(1));
        assert_eq!(source.line(), Some(1));
        assert_eq!(source.column(), Some(2));
    }

    #[actix_web::test]
    async fn rejects_invalid_json_and_trailing_values() {
        for (body, category) in [
            (&b""[..], ErrorCategory::Eof),
            (b"{", ErrorCategory::Eof),
            (br#"{"host": "a" "port": 80}"#, ErrorCategory::Syntax),
            (br#"{"host": "a", "port": 80,}"#, ErrorCategory::Syntax),
            (br#"{"host": "a", "port": 80} {}"#, ErrorCategory::Syntax),
            (b"{\"host\": \"\xff\", \"port\": 80}", ErrorCategory::Syntax),
        ] {
            let (req, mut payload) = test::TestRequest::default()
                .insert_header(header::ContentType::json())
                .set_payload(Bytes::from_static(body))
                .to_http_parts();

            let err = DeserJson::<Server>::from_request(&req, &mut payload)
                .await
                .unwrap_err();

            assert_eq!(err.status_code(), StatusCode::BAD_REQUEST);
            let DeserJsonPayloadError::Deserialize { source } = err else {
                panic!("unexpected error: {err}");
            };

            let source = source.source();
            assert_eq!(source.category(), category);
            assert!(source.line().is_some());
            assert!(source.column().is_some());
        }
    }

    #[actix_web::test]
    async fn rejects_non_json_content_types_without_consuming_payload() {
        for content_type in [None, Some("text/plain"), Some("invalid")] {
            let mut request = test::TestRequest::default().set_payload("42");
            if let Some(content_type) = content_type {
                request = request.insert_header((header::CONTENT_TYPE, content_type));
            }
            let (req, mut payload) = request.to_http_parts();

            let err = DeserJson::<u32>::from_request(&req, &mut payload)
                .await
                .unwrap_err();

            assert!(matches!(err, DeserJsonPayloadError::ContentType));
            assert_eq!(err.status_code(), StatusCode::NOT_ACCEPTABLE);
            assert_eq!(BytesBody::<2>::new(&req, &mut payload).await.unwrap(), "42");
        }
    }

    #[actix_web::test]
    async fn enforces_declared_limit() {
        let (req, mut payload) = test::TestRequest::default()
            .insert_header(header::ContentType::json())
            .insert_header((header::CONTENT_LENGTH, 3))
            .to_http_parts();

        let err = DeserJson::<u32, 2>::from_request(&req, &mut payload)
            .await
            .unwrap_err();

        assert!(matches!(
            err,
            DeserJsonPayloadError::Overflow {
                limit: 2,
                length: Some(3)
            }
        ));
        assert_eq!(err.status_code(), StatusCode::PAYLOAD_TOO_LARGE);
    }

    #[actix_web::test]
    async fn enforces_streamed_limit() {
        let req = test::TestRequest::default()
            .insert_header(header::ContentType::json())
            .to_http_request();
        let chunks = stream::iter([Ok(Bytes::from_static(b"4")), Ok(Bytes::from_static(b"2"))]);
        let mut payload = Payload::from(
            Box::pin(chunks) as Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>>>>
        );

        let err = DeserJson::<u32, 1>::from_request(&req, &mut payload)
            .await
            .unwrap_err();

        assert!(matches!(
            err,
            DeserJsonPayloadError::Overflow {
                limit: 1,
                length: None
            }
        ));
        assert_eq!(err.status_code(), StatusCode::PAYLOAD_TOO_LARGE);
    }

    #[actix_web::test]
    async fn accepts_streamed_payload_at_limit() {
        let req = test::TestRequest::default()
            .insert_header(header::ContentType::json())
            .to_http_request();
        let chunks = stream::iter([Ok(Bytes::from_static(b"4")), Ok(Bytes::from_static(b"2"))]);
        let mut payload = Payload::from(
            Box::pin(chunks) as Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>>>>
        );

        let json = DeserJson::<u32, 2>::from_request(&req, &mut payload)
            .await
            .unwrap();

        assert_eq!(json.into_inner(), 42);
    }

    #[actix_web::test]
    async fn retains_payload_error() {
        let req = test::TestRequest::default()
            .insert_header(header::ContentType::json())
            .to_http_request();
        let chunks = stream::iter([Err(PayloadError::Incomplete(None))]);
        let mut payload = Payload::from(
            Box::pin(chunks) as Pin<Box<dyn Stream<Item = Result<Bytes, PayloadError>>>>
        );

        let err = DeserJson::<u32>::from_request(&req, &mut payload)
            .await
            .unwrap_err();

        assert!(matches!(
            err,
            DeserJsonPayloadError::Payload {
                source: PayloadError::Incomplete(None)
            }
        ));
        assert_eq!(
            err.status_code(),
            PayloadError::Incomplete(None).status_code()
        );
        assert!(err.source().unwrap().is::<PayloadError>());
    }

    #[actix_web::test]
    async fn handler_response_includes_error_context() {
        let app = test::init_service(App::new().route(
            "/",
            web::post().to(|json: DeserJson<Config>| async move { json.servers.len().to_string() }),
        ))
        .await;
        let request = test::TestRequest::post()
            .insert_header(header::ContentType::json())
            .set_payload(r#"{"servers":[{"host":"a","port":"bad"}]}"#)
            .to_request();

        let response = test::call_service(&app, request).await;
        assert_eq!(response.status(), StatusCode::UNPROCESSABLE_ENTITY);
        let body = test::read_body(response).await;
        let body = str::from_utf8(&body).unwrap();
        assert!(body.contains("path: servers[0].port"));
        assert!(body.contains("line 1 column"));
    }
}