diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 823633753..9bbddd712 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -2,6 +2,7 @@ ## Unreleased +- Update `TestRequest::set_payload` to generate "Content-Length" header - Malformed websocket frames are now gracefully rejected. ## 3.11.0 diff --git a/actix-http/src/test.rs b/actix-http/src/test.rs index dfa9a86c9..5632a310c 100644 --- a/actix-http/src/test.rs +++ b/actix-http/src/test.rs @@ -11,7 +11,7 @@ use std::{ use actix_codec::{AsyncRead, AsyncWrite, ReadBuf}; use bytes::{Bytes, BytesMut}; -use http::{Method, Uri, Version}; +use http::{header, Method, Uri, Version}; use crate::{ header::{HeaderMap, TryIntoHeaderPair}, @@ -98,9 +98,13 @@ impl TestRequest { } /// Set request payload. + /// + /// This sets the `Content-Length` header with the size of `data`. pub fn set_payload(&mut self, data: impl Into) -> &mut Self { let mut payload = crate::h1::Payload::empty(); - payload.unread_data(data.into()); + let bytes = data.into(); + self.insert_header((header::CONTENT_LENGTH, bytes.len())); + payload.unread_data(bytes); parts(&mut self.0).payload = Some(payload.into()); self } diff --git a/actix-web/src/types/json.rs b/actix-web/src/types/json.rs index 22ed624c3..58464f360 100644 --- a/actix-web/src/types/json.rs +++ b/actix-web/src/types/json.rs @@ -616,7 +616,7 @@ mod tests { } )); - let (req, mut pl) = TestRequest::default() + let (mut req, mut pl) = TestRequest::default() .insert_header(( header::CONTENT_TYPE, header::HeaderValue::from_static("application/json"), @@ -624,6 +624,7 @@ mod tests { .set_payload(Bytes::from_static(&[0u8; 1000])) .to_http_parts(); + req.head_mut().headers_mut().remove(header::CONTENT_LENGTH); let json = JsonBody::::new(&req, &mut pl, None, true) .limit(100) .await;