diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-07 19:33:14 +0000 |
commit | 36d22d82aa202bb199967e9512281e9a53db42c9 (patch) | |
tree | 105e8c98ddea1c1e4784a60a5a6410fa416be2de /third_party/rust/warp/tests/multipart.rs | |
parent | Initial commit. (diff) | |
download | firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.tar.xz firefox-esr-36d22d82aa202bb199967e9512281e9a53db42c9.zip |
Adding upstream version 115.7.0esr.upstream/115.7.0esr
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'third_party/rust/warp/tests/multipart.rs')
-rw-r--r-- | third_party/rust/warp/tests/multipart.rs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/third_party/rust/warp/tests/multipart.rs b/third_party/rust/warp/tests/multipart.rs new file mode 100644 index 0000000000..3172367bcb --- /dev/null +++ b/third_party/rust/warp/tests/multipart.rs @@ -0,0 +1,54 @@ +#![deny(warnings)] +use bytes::BufMut; +use futures_util::{TryFutureExt, TryStreamExt}; +use warp::{multipart, Filter}; + +#[tokio::test] +async fn form_fields() { + let _ = pretty_env_logger::try_init(); + + let route = multipart::form().and_then(|form: multipart::FormData| { + async { + // Collect the fields into (name, value): (String, Vec<u8>) + let part: Result<Vec<(String, Vec<u8>)>, warp::Rejection> = form + .and_then(|part| { + let name = part.name().to_string(); + let value = part.stream().try_fold(Vec::new(), |mut vec, data| { + vec.put(data); + async move { Ok(vec) } + }); + value.map_ok(move |vec| (name, vec)) + }) + .try_collect() + .await + .map_err(|e| { + panic!("multipart error: {:?}", e); + }); + part + } + }); + + let boundary = "--abcdef1234--"; + let body = format!( + "\ + --{0}\r\n\ + content-disposition: form-data; name=\"foo\"\r\n\r\n\ + bar\r\n\ + --{0}--\r\n\ + ", + boundary + ); + + let req = warp::test::request() + .method("POST") + .header("content-length", body.len()) + .header( + "content-type", + format!("multipart/form-data; boundary={}", boundary), + ) + .body(body); + + let vec = req.filter(&route).await.unwrap(); + assert_eq!(&vec[0].0, "foo"); + assert_eq!(&vec[0].1, b"bar"); +} |