1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-22 09:31:10 +00:00

test(multipart): replace SlowStream helper

This commit is contained in:
Rob Ede 2024-07-06 23:38:37 +01:00
parent 5c9e6e7c1d
commit 6ae131ce29
No known key found for this signature in database
GPG key ID: 97C636207D3EF933
2 changed files with 15 additions and 44 deletions

View file

@ -66,6 +66,7 @@ assert_matches = "1"
awc = "3" awc = "3"
env_logger = "0.11" env_logger = "0.11"
futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] } futures-util = { version = "0.3.17", default-features = false, features = ["alloc"] }
futures-test = "0.3"
multer = "3" multer = "3"
tokio = { version = "1.24.2", features = ["sync"] } tokio = { version = "1.24.2", features = ["sync"] }
tokio-stream = "0.1" tokio-stream = "0.1"

View file

@ -482,7 +482,8 @@ mod tests {
FromRequest, FromRequest,
}; };
use assert_matches::assert_matches; use assert_matches::assert_matches;
use futures_util::{future::lazy, StreamExt as _}; use futures_test::stream::StreamTestExt as _;
use futures_util::{future::lazy, stream, StreamExt as _};
use tokio::sync::mpsc; use tokio::sync::mpsc;
use tokio_stream::wrappers::UnboundedReceiverStream; use tokio_stream::wrappers::UnboundedReceiverStream;
@ -545,45 +546,6 @@ mod tests {
) )
} }
// Stream that returns from a Bytes, one char at a time and Pending every other poll()
struct SlowStream {
bytes: Bytes,
pos: usize,
ready: bool,
}
impl SlowStream {
fn new(bytes: Bytes) -> SlowStream {
SlowStream {
bytes,
pos: 0,
ready: false,
}
}
}
impl Stream for SlowStream {
type Item = Result<Bytes, PayloadError>;
fn poll_next(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Option<Self::Item>> {
let this = self.get_mut();
if !this.ready {
this.ready = true;
cx.waker().wake_by_ref();
return Poll::Pending;
}
if this.pos == this.bytes.len() {
return Poll::Ready(None);
}
let res = Poll::Ready(Some(Ok(this.bytes.slice(this.pos..(this.pos + 1)))));
this.pos += 1;
this.ready = false;
res
}
}
fn create_simple_request_with_header() -> (Bytes, HeaderMap) { fn create_simple_request_with_header() -> (Bytes, HeaderMap) {
let (body, headers) = crate::test::create_form_data_payload_and_headers_with_boundary( let (body, headers) = crate::test::create_form_data_payload_and_headers_with_boundary(
BOUNDARY, BOUNDARY,
@ -721,7 +683,9 @@ mod tests {
#[actix_rt::test] #[actix_rt::test]
async fn test_stream() { async fn test_stream() {
let (bytes, headers) = create_double_request_with_header(); let (bytes, headers) = create_double_request_with_header();
let payload = SlowStream::new(bytes); let payload = stream::iter(bytes)
.map(|byte| Ok(Bytes::copy_from_slice(&[byte])))
.interleave_pending();
let mut multipart = Multipart::new(&headers, payload); let mut multipart = Multipart::new(&headers, payload);
match multipart.next().await.unwrap() { match multipart.next().await.unwrap() {
@ -899,7 +863,9 @@ mod tests {
"multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", "multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"",
), ),
); );
let payload = SlowStream::new(bytes); let payload = stream::iter(bytes)
.map(|byte| Ok(Bytes::copy_from_slice(&[byte])))
.interleave_pending();
let mut multipart = Multipart::new(&headers, payload); let mut multipart = Multipart::new(&headers, payload);
let res = multipart.next().await.unwrap(); let res = multipart.next().await.unwrap();
@ -929,7 +895,9 @@ mod tests {
"multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", "multipart/mixed; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"",
), ),
); );
let payload = SlowStream::new(bytes); let payload = stream::iter(bytes)
.map(|byte| Ok(Bytes::copy_from_slice(&[byte])))
.interleave_pending();
let mut multipart = Multipart::new(&headers, payload); let mut multipart = Multipart::new(&headers, payload);
let res = multipart.next().await.unwrap(); let res = multipart.next().await.unwrap();
@ -955,7 +923,9 @@ mod tests {
"multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"", "multipart/form-data; boundary=\"abbc761f78ff4d7cb7573b5a23f96ef0\"",
), ),
); );
let payload = SlowStream::new(bytes); let payload = stream::iter(bytes)
.map(|byte| Ok(Bytes::copy_from_slice(&[byte])))
.interleave_pending();
let mut multipart = Multipart::new(&headers, payload); let mut multipart = Multipart::new(&headers, payload);
let res = multipart.next().await.unwrap(); let res = multipart.next().await.unwrap();