1- #[ cfg( feature = "multipart" ) ]
2- use super :: multipart:: Form ;
3- /// dox
41use bytes:: Bytes ;
52use std:: { borrow:: Cow , fmt} ;
63
7- /// The body of a `Request`.
8- ///
9- /// In most cases, this is not needed directly, as the
10- /// [`RequestBuilder.body`][builder] method uses `Into<Body>`, which allows
11- /// passing many things (like a string or vector of bytes).
12- ///
13- /// [builder]: ./struct.RequestBuilder.html#method.body
4+ /// The body of a [`super::Request`].
145pub struct Body {
156 inner : Inner ,
167}
178
189enum Inner {
1910 Single ( Single ) ,
20- /// MultipartForm holds a multipart/form-data body.
21- #[ cfg( feature = "multipart" ) ]
22- MultipartForm ( Form ) ,
2311}
2412
2513#[ derive( Clone ) ]
@@ -52,46 +40,13 @@ impl Body {
5240 pub fn as_bytes ( & self ) -> Option < & [ u8 ] > {
5341 match & self . inner {
5442 Inner :: Single ( single) => Some ( single. as_bytes ( ) ) ,
55- #[ cfg( feature = "multipart" ) ]
56- Inner :: MultipartForm ( _) => None ,
57- }
58- }
59-
60- #[ cfg( feature = "multipart" ) ]
61- pub ( crate ) fn as_single ( & self ) -> Option < & Single > {
62- match & self . inner {
63- Inner :: Single ( single) => Some ( single) ,
64- Inner :: MultipartForm ( _) => None ,
65- }
66- }
67-
68- #[ inline]
69- #[ cfg( feature = "multipart" ) ]
70- pub ( crate ) fn from_form ( f : Form ) -> Body {
71- Self {
72- inner : Inner :: MultipartForm ( f) ,
73- }
74- }
75-
76- /// into_part turns a regular body into the body of a multipart/form-data part.
77- #[ cfg( feature = "multipart" ) ]
78- pub ( crate ) fn into_part ( self ) -> Body {
79- match self . inner {
80- Inner :: Single ( single) => Self {
81- inner : Inner :: Single ( single) ,
82- } ,
83- Inner :: MultipartForm ( form) => Self {
84- inner : Inner :: MultipartForm ( form) ,
85- } ,
8643 }
8744 }
8845
8946 #[ allow( unused) ]
9047 pub ( crate ) fn is_empty ( & self ) -> bool {
9148 match & self . inner {
9249 Inner :: Single ( single) => single. is_empty ( ) ,
93- #[ cfg( feature = "multipart" ) ]
94- Inner :: MultipartForm ( form) => form. is_empty ( ) ,
9550 }
9651 }
9752
@@ -100,8 +55,6 @@ impl Body {
10055 Inner :: Single ( single) => Some ( Self {
10156 inner : Inner :: Single ( single. clone ( ) ) ,
10257 } ) ,
103- #[ cfg( feature = "multipart" ) ]
104- Inner :: MultipartForm ( _) => None ,
10558 }
10659 }
10760}
@@ -156,128 +109,3 @@ impl fmt::Debug for Body {
156109 f. debug_struct ( "Body" ) . finish ( )
157110 }
158111}
159-
160- #[ cfg( test) ]
161- mod tests {
162- // use crate::Body;
163- // use js_sys::Uint8Array;
164- // use wasm_bindgen::prelude::*;
165- // use wasm_bindgen_test::*;
166-
167- // wasm_bindgen_test::wasm_bindgen_test_configure!(run_in_browser);
168-
169- // #[wasm_bindgen]
170- // extern "C" {
171- // // Use `js_namespace` here to bind `console.log(..)` instead of just
172- // // `log(..)`
173- // #[wasm_bindgen(js_namespace = console)]
174- // fn log(s: String);
175- // }
176-
177- // #[wasm_bindgen_test]
178- // async fn test_body() {
179- // let body = Body::from("TEST");
180- // assert_eq!([84, 69, 83, 84], body.as_bytes().unwrap());
181- // }
182-
183- // #[wasm_bindgen_test]
184- // async fn test_body_js_static_str() {
185- // let body_value = "TEST";
186- // let body = Body::from(body_value);
187-
188- // let mut init = web_sys::RequestInit::new();
189- // init.method("POST");
190- // init.body(Some(
191- // body.to_js_value()
192- // .expect("could not convert body to JsValue")
193- // .as_ref(),
194- // ));
195-
196- // let js_req = web_sys::Request::new_with_str_and_init("", &init)
197- // .expect("could not create JS request");
198- // let text_promise = js_req.text().expect("could not get text promise");
199- // let text = crate::wasm::promise::<JsValue>(text_promise)
200- // .await
201- // .expect("could not get request body as text");
202-
203- // assert_eq!(text.as_string().expect("text is not a string"), body_value);
204- // }
205- // #[wasm_bindgen_test]
206- // async fn test_body_js_string() {
207- // let body_value = "TEST".to_string();
208- // let body = Body::from(body_value.clone());
209-
210- // let mut init = web_sys::RequestInit::new();
211- // init.method("POST");
212- // init.body(Some(
213- // body.to_js_value()
214- // .expect("could not convert body to JsValue")
215- // .as_ref(),
216- // ));
217-
218- // let js_req = web_sys::Request::new_with_str_and_init("", &init)
219- // .expect("could not create JS request");
220- // let text_promise = js_req.text().expect("could not get text promise");
221- // let text = crate::wasm::promise::<JsValue>(text_promise)
222- // .await
223- // .expect("could not get request body as text");
224-
225- // assert_eq!(text.as_string().expect("text is not a string"), body_value);
226- // }
227-
228- // #[wasm_bindgen_test]
229- // async fn test_body_js_static_u8_slice() {
230- // let body_value: &'static [u8] = b"\x00\x42";
231- // let body = Body::from(body_value);
232-
233- // let mut init = web_sys::RequestInit::new();
234- // init.method("POST");
235- // init.body(Some(
236- // body.to_js_value()
237- // .expect("could not convert body to JsValue")
238- // .as_ref(),
239- // ));
240-
241- // let js_req = web_sys::Request::new_with_str_and_init("", &init)
242- // .expect("could not create JS request");
243-
244- // let array_buffer_promise = js_req
245- // .array_buffer()
246- // .expect("could not get array_buffer promise");
247- // let array_buffer = crate::wasm::promise::<JsValue>(array_buffer_promise)
248- // .await
249- // .expect("could not get request body as array buffer");
250-
251- // let v = Uint8Array::new(&array_buffer).to_vec();
252-
253- // assert_eq!(v, body_value);
254- // }
255-
256- // #[wasm_bindgen_test]
257- // async fn test_body_js_vec_u8() {
258- // let body_value = vec![0u8, 42];
259- // let body = Body::from(body_value.clone());
260-
261- // let mut init = web_sys::RequestInit::new();
262- // init.method("POST");
263- // init.body(Some(
264- // body.to_js_value()
265- // .expect("could not convert body to JsValue")
266- // .as_ref(),
267- // ));
268-
269- // let js_req = web_sys::Request::new_with_str_and_init("", &init)
270- // .expect("could not create JS request");
271-
272- // let array_buffer_promise = js_req
273- // .array_buffer()
274- // .expect("could not get array_buffer promise");
275- // let array_buffer = crate::wasm::promise::<JsValue>(array_buffer_promise)
276- // .await
277- // .expect("could not get request body as array buffer");
278-
279- // let v = Uint8Array::new(&array_buffer).to_vec();
280-
281- // assert_eq!(v, body_value);
282- // }
283- }
0 commit comments