mirror of
https://github.com/fafhrd91/actix-web
synced 2025-09-01 17:27:18 +02:00
fix: include content-length with bytes payload (#3695)
* fix: include content-length with bytes payload * chore: json unit-test patch * Update doc comment --------- Co-authored-by: Yuki Okushi <huyuumi.dev@gmail.com>
This commit is contained in:
@@ -2,6 +2,7 @@
|
|||||||
|
|
||||||
## Unreleased
|
## Unreleased
|
||||||
|
|
||||||
|
- Update `TestRequest::set_payload` to generate "Content-Length" header
|
||||||
- Malformed websocket frames are now gracefully rejected.
|
- Malformed websocket frames are now gracefully rejected.
|
||||||
|
|
||||||
## 3.11.0
|
## 3.11.0
|
||||||
|
@@ -11,7 +11,7 @@ use std::{
|
|||||||
|
|
||||||
use actix_codec::{AsyncRead, AsyncWrite, ReadBuf};
|
use actix_codec::{AsyncRead, AsyncWrite, ReadBuf};
|
||||||
use bytes::{Bytes, BytesMut};
|
use bytes::{Bytes, BytesMut};
|
||||||
use http::{Method, Uri, Version};
|
use http::{header, Method, Uri, Version};
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
header::{HeaderMap, TryIntoHeaderPair},
|
header::{HeaderMap, TryIntoHeaderPair},
|
||||||
@@ -98,9 +98,13 @@ impl TestRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Set request payload.
|
/// Set request payload.
|
||||||
|
///
|
||||||
|
/// This sets the `Content-Length` header with the size of `data`.
|
||||||
pub fn set_payload(&mut self, data: impl Into<Bytes>) -> &mut Self {
|
pub fn set_payload(&mut self, data: impl Into<Bytes>) -> &mut Self {
|
||||||
let mut payload = crate::h1::Payload::empty();
|
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());
|
parts(&mut self.0).payload = Some(payload.into());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
@@ -616,7 +616,7 @@ mod tests {
|
|||||||
}
|
}
|
||||||
));
|
));
|
||||||
|
|
||||||
let (req, mut pl) = TestRequest::default()
|
let (mut req, mut pl) = TestRequest::default()
|
||||||
.insert_header((
|
.insert_header((
|
||||||
header::CONTENT_TYPE,
|
header::CONTENT_TYPE,
|
||||||
header::HeaderValue::from_static("application/json"),
|
header::HeaderValue::from_static("application/json"),
|
||||||
@@ -624,6 +624,7 @@ mod tests {
|
|||||||
.set_payload(Bytes::from_static(&[0u8; 1000]))
|
.set_payload(Bytes::from_static(&[0u8; 1000]))
|
||||||
.to_http_parts();
|
.to_http_parts();
|
||||||
|
|
||||||
|
req.head_mut().headers_mut().remove(header::CONTENT_LENGTH);
|
||||||
let json = JsonBody::<MyObject>::new(&req, &mut pl, None, true)
|
let json = JsonBody::<MyObject>::new(&req, &mut pl, None, true)
|
||||||
.limit(100)
|
.limit(100)
|
||||||
.await;
|
.await;
|
||||||
|
Reference in New Issue
Block a user