wasm: don't send request body as plain uint8 array (#1358)
Co-authored-by: Niklas Wolber <niklas.wolber+git@octopost.eu>
This commit is contained in:
		| @@ -2,7 +2,7 @@ | |||||||
| use super::multipart::Form; | use super::multipart::Form; | ||||||
| /// dox | /// dox | ||||||
| use bytes::Bytes; | use bytes::Bytes; | ||||||
| use js_sys::{Array, Uint8Array}; | use js_sys::{Uint8Array}; | ||||||
| use std::fmt; | use std::fmt; | ||||||
| use wasm_bindgen::JsValue; | use wasm_bindgen::JsValue; | ||||||
|  |  | ||||||
| @@ -40,8 +40,7 @@ impl Body { | |||||||
|             Inner::Bytes(body_bytes) => { |             Inner::Bytes(body_bytes) => { | ||||||
|                 let body_bytes: &[u8] = body_bytes.as_ref(); |                 let body_bytes: &[u8] = body_bytes.as_ref(); | ||||||
|                 let body_uint8_array: Uint8Array = body_bytes.into(); |                 let body_uint8_array: Uint8Array = body_bytes.into(); | ||||||
|                 let body_array = Array::from(&body_uint8_array); |                 let js_value: &JsValue = body_uint8_array.as_ref(); | ||||||
|                 let js_value: &JsValue = body_array.as_ref(); |  | ||||||
|                 Ok(js_value.to_owned()) |                 Ok(js_value.to_owned()) | ||||||
|             } |             } | ||||||
|             #[cfg(feature = "multipart")] |             #[cfg(feature = "multipart")] | ||||||
| @@ -131,8 +130,8 @@ impl fmt::Debug for Body { | |||||||
|  |  | ||||||
| #[cfg(test)] | #[cfg(test)] | ||||||
| mod tests { | mod tests { | ||||||
|     use js_sys::{Array, Uint8Array}; |     // use js_sys::{Array, Uint8Array}; | ||||||
|     use wasm_bindgen::{prelude::*, JsValue}; |     use wasm_bindgen::{prelude::*}; | ||||||
|     use wasm_bindgen_test::*; |     use wasm_bindgen_test::*; | ||||||
|  |  | ||||||
|     wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); |     wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser); | ||||||
| @@ -157,20 +156,17 @@ mod tests { | |||||||
|     async fn test_body_js() { |     async fn test_body_js() { | ||||||
|         use crate::Body; |         use crate::Body; | ||||||
|  |  | ||||||
|         let body = Body::from("TEST"); |         let body_value = "TEST"; | ||||||
|  |         let body = Body::from(body_value); | ||||||
|          |          | ||||||
|         let arr = [84u8, 69, 83, 84]; |         let mut init = web_sys::RequestInit::new(); | ||||||
|         let u8_arr: Uint8Array = arr.as_ref().into(); |         init.method("POST"); | ||||||
|         let expected = JsValue::from(Array::from(&u8_arr)); |         init.body(Some(body.to_js_value().expect("could not convert body to JsValue").as_ref())); | ||||||
|  |  | ||||||
|         // ty JS -.- |         let js_req = web_sys::Request::new_with_str_and_init("", &init).expect("could not create JS request"); | ||||||
|         assert_eq!( |         let text_promise = js_req.text().expect("could not get text promise"); | ||||||
|             format!("{:#?}", expected), |         let text = crate::wasm::promise::<JsValue>(text_promise).await.expect("could not get request body as text"); | ||||||
|             format!( |  | ||||||
|                 "{:#?}", |         assert_eq!(text.as_string().expect("text is not a string"), body_value); | ||||||
|                 body.to_js_value() |  | ||||||
|                     .expect("could not convert body to JsValue") |  | ||||||
|             ) |  | ||||||
|         ); |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user