Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions src/body/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -733,6 +733,55 @@ mod tests {
assert!(rx.data().await.is_none());
}

use pin_project_lite::pin_project;

pin_project! {
#[derive(Debug)]
struct Inner {
#[pin]
body: Body,
}
}
Comment on lines +738 to +744
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Body type is Unpin so it doesn't make sense to pin it.


use crate::common::buf::BufList;
use bytes::Bytes;

impl Inner {
fn new(body: Body) -> Self {
Self { body }
}

async fn collect(self) -> std::result::Result<BufList<Bytes>, crate::Error> {
let mut output = BufList::new();
let body = self.body;
futures_util::pin_mut!(body);
while let Some(buf) = body.data().await {
output.push(buf?);
}
Ok(output)
}
}

use bytes::Buf;

#[tokio::test]
async fn read_from_channel_body() {
let (mut sender, body) = Body::channel();
let byte_stream = Inner::new(body);
tokio::spawn(async move {
sender.send_data(Bytes::from("data 1")).await.unwrap();
sender.send_data(Bytes::from("data 2")).await.unwrap();
sender.send_data(Bytes::from("data 3")).await.unwrap();
});

let mut aggregated_bytes: BufList<Bytes> = byte_stream.collect().await.expect("no errors");

assert_eq!(
aggregated_bytes.copy_to_bytes(aggregated_bytes.remaining()),
Bytes::from("data 1data 2data 3")
);
}

#[test]
fn channel_ready() {
let (mut tx, _rx) = Body::new_channel(DecodedLength::CHUNKED, /*wanter = */ false);
Expand Down