@@ -66,7 +66,11 @@ impl RequestInit {
6666impl From < & RequestInit > for web_sys:: RequestInit {
6767 fn from ( req : & RequestInit ) -> Self {
6868 let inner = web_sys:: RequestInit :: new ( ) ;
69- inner. set_headers ( req. headers . as_ref ( ) ) ;
69+
70+ if !req. headers . is_empty ( ) {
71+ inner. set_headers ( req. headers . as_ref ( ) ) ;
72+ }
73+
7074 inner. set_method ( req. method . as_ref ( ) ) ;
7175 inner. set_redirect ( req. redirect . into ( ) ) ;
7276 if let Some ( cache) = req. cache {
@@ -77,16 +81,18 @@ impl From<&RequestInit> for web_sys::RequestInit {
7781 }
7882
7983 // set the Cloudflare-specific `cf` property on FFI RequestInit
80- let r = :: js_sys:: Reflect :: set (
81- inner. as_ref ( ) ,
82- & JsValue :: from ( "cf" ) ,
83- & JsValue :: from ( & req. cf ) ,
84- ) ;
85- debug_assert ! (
86- r. is_ok( ) ,
87- "setting properties should never fail on our dictionary objects"
88- ) ;
89- let _ = r;
84+ if !req. cf . is_default ( ) {
85+ let r = :: js_sys:: Reflect :: set (
86+ inner. as_ref ( ) ,
87+ & JsValue :: from ( "cf" ) ,
88+ & JsValue :: from ( & req. cf ) ,
89+ ) ;
90+ debug_assert ! (
91+ r. is_ok( ) ,
92+ "setting properties should never fail on our dictionary objects"
93+ ) ;
94+ let _ = r;
95+ }
9096
9197 inner
9298 }
@@ -278,6 +284,21 @@ impl CfProperties {
278284 pub fn new ( ) -> Self {
279285 Default :: default ( )
280286 }
287+
288+ pub fn is_default ( & self ) -> bool {
289+ let de = CfProperties :: default ( ) ;
290+ self . apps == de. apps
291+ && self . cache_everything == de. cache_everything
292+ && self . cache_key == de. cache_key
293+ && self . cache_ttl == de. cache_ttl
294+ && self . cache_ttl_by_status == de. cache_ttl_by_status
295+ && self . minify == de. minify
296+ && self . mirage == de. mirage
297+ && self . image . is_none ( )
298+ && self . polish == de. polish
299+ && self . resolve_override == de. resolve_override
300+ && self . scrape_shield == de. scrape_shield
301+ }
281302}
282303
283304impl Default for CfProperties {
@@ -301,7 +322,7 @@ impl Default for CfProperties {
301322/// Configuration options for Cloudflare's minification features:
302323/// <https://www.cloudflare.com/website-optimization/>
303324#[ wasm_bindgen]
304- #[ derive( Clone , Copy , Debug , Default , Serialize ) ]
325+ #[ derive( Clone , Copy , Debug , Default , Serialize , PartialEq ) ]
305326#[ serde( rename_all = "kebab-case" ) ]
306327pub struct MinifyConfig {
307328 pub js : bool ,
@@ -311,7 +332,7 @@ pub struct MinifyConfig {
311332
312333/// Configuration options for Cloudflare's image optimization feature:
313334/// <https://blog.cloudflare.com/introducing-polish-automatic-image-optimizati/>
314- #[ derive( Clone , Copy , Debug , Default , Serialize ) ]
335+ #[ derive( Clone , Copy , Debug , Default , Serialize , PartialEq ) ]
315336#[ serde( rename_all = "kebab-case" ) ]
316337pub enum PolishConfig {
317338 #[ default]
@@ -568,3 +589,13 @@ impl From<CacheMode> for web_sys::RequestCache {
568589 }
569590 }
570591}
592+
593+ #[ test]
594+ fn request_init_no_invalid_options ( ) {
595+ let mut init = RequestInit :: new ( ) ;
596+ init. method = Method :: Post ;
597+
598+ let js_init: web_sys:: RequestInit = ( & init) . into ( ) ;
599+
600+ let _ = web_sys:: Request :: new_with_str_and_init ( "https://httpbin.org/post" , & js_init) . unwrap ( ) ;
601+ }
0 commit comments