Add test from ferristseng repo

This commit is contained in:
asonix 2023-07-15 21:37:31 -05:00
parent 3a5e51c872
commit 100285a13a
2 changed files with 62 additions and 4 deletions

View file

@ -227,11 +227,20 @@ where
Poll::Ready(Ok(()))
}
} else if buf.remaining() > final_boundary_len(&self.boundary) {
write_final_boundary(&self.boundary, buf);
self.closed = true;
Poll::Ready(Ok(()))
write_final_boundary(&self.boundary, buf);
if buf.remaining() > crlf_len() {
write_crlf(buf);
Poll::Ready(Ok(()))
} else {
self.write_clrf_to_pending();
self.poll_read(cx, buf)
}
} else {
self.write_final_boundary_to_pending();
self.write_clrf_to_pending();
self.closed = true;
self.poll_read(cx, buf)
}

View file

@ -230,7 +230,7 @@ impl<'a> From<Part<SendRead<'a>>> for Part<UnsendRead<'a>> {
#[cfg(test)]
mod tests {
use std::{future::poll_fn, pin::Pin};
use std::{future::poll_fn, io::Cursor, pin::Pin};
struct Streamer<S>(S);
@ -269,7 +269,56 @@ mod tests {
let out = String::from_utf8(out).expect("Valid string");
assert_eq!(out, "--hello\r\ncontent-type: text/plain\r\ncontent-disposition: form-data; name=\"first_name\"\r\n\r\nJohn\r\n--hello\r\ncontent-type: text/plain\r\ncontent-disposition: form-data; name=\"last_name\"\r\n\r\nDoe\r\n--hello--")
assert_eq!(out, "--hello\r\ncontent-type: text/plain\r\ncontent-disposition: form-data; name=\"first_name\"\r\n\r\nJohn\r\n--hello\r\ncontent-type: text/plain\r\ncontent-disposition: form-data; name=\"last_name\"\r\n\r\nDoe\r\n--hello--\r\n")
}
#[tokio::test]
async fn test_form_body_stream() {
let body = super::Body::builder()
.boundary(String::from("hello"))
.append(super::Part::new_str(String::from("name1"), "value1"))
.append(super::Part::new_str(String::from("name2"), "value2"))
.append(super::Part::new(
String::from("input"),
Cursor::new("Hello World!"),
))
.build();
let mut out = Vec::new();
let mut streamer = Streamer(body);
while let Some(res) = streamer.next().await {
out.extend(res.expect("read success"));
}
let out = String::from_utf8(out).expect("Valid string");
assert_eq!(
out,
[
"--hello\r\n",
"content-type: text/plain\r\n",
"content-disposition: form-data; name=\"name1\"\r\n",
"\r\n",
"value1\r\n",
"--hello\r\n",
"content-type: text/plain\r\n",
"content-disposition: form-data; name=\"name2\"\r\n",
"\r\n",
"value2\r\n",
"--hello\r\n",
"content-type: application/octet-stream\r\n",
"content-disposition: form-data; name=\"input\"\r\n",
"\r\n",
"Hello World!\r\n",
"--hello--\r\n",
]
.into_iter()
.map(|s| s.chars())
.flatten()
.collect::<String>()
);
}
#[test]