File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -119,6 +119,7 @@ let run ~env t =
119119 ; name = test.OSnap_Config.Types. name
120120 ; actions = test.OSnap_Config.Types. actions
121121 ; ignore_regions = test.OSnap_Config.Types. ignore
122+ ; additional_headers = test.OSnap_Config.Types. additional_headers
122123 ; threshold = test.OSnap_Config.Types. threshold
123124 ; retry = test.OSnap_Config.Types. retry
124125 ; warnings = []
Original file line number Diff line number Diff line change @@ -498,3 +498,27 @@ let clear_cookies target =
498498 in
499499 Result. ok ()
500500;;
501+
502+ let set_headers ~headers target =
503+ match headers with
504+ | None -> Result. ok ()
505+ | Some headers ->
506+ let ( let *? ) = Result. bind in
507+ let open Commands.Network in
508+ let sessionId = target.sessionId in
509+ let *? _ =
510+ let open SetExtraHTTPHeaders in
511+ let response =
512+ Request. make ~session Id ~params: (Params. make ~headers () ) |> OSnap_Websocket. send
513+ in
514+ let response = response |> Response. parse in
515+ let error =
516+ response.Response. error
517+ |> Option. map (fun (error : Response.error ) ->
518+ `OSnap_CDP_Protocol_Error error.message)
519+ |> Option. value ~default: (`OSnap_CDP_Protocol_Error " " )
520+ in
521+ Option. to_result response.Response. result ~none: error
522+ in
523+ Result. ok ()
524+ ;;
Original file line number Diff line number Diff line change @@ -109,3 +109,8 @@ val screenshot
109109val clear_cookies
110110 : OSnap_Browser_Target. target
111111 -> (unit , [> `OSnap_CDP_Protocol_Error of string ]) Result. t
112+
113+ val set_headers
114+ : headers:(string * string ) list option
115+ -> OSnap_Browser_Target. target
116+ -> (unit , [> `OSnap_CDP_Protocol_Error of string ]) Result. t
Original file line number Diff line number Diff line change @@ -45,6 +45,18 @@ let enable_events t =
4545 in
4646 Option. to_result response.Response. result ~none: error
4747 in
48+ let *? _ =
49+ let open Network.Enable in
50+ let params = Params. make () in
51+ let response = Request. make ~session Id ~params |> Websocket. send |> Response. parse in
52+ let error =
53+ response.Response. error
54+ |> Option. map (fun (error : Response.error ) ->
55+ `OSnap_CDP_Protocol_Error error.message)
56+ |> Option. value ~default: (`OSnap_CDP_Protocol_Error " " )
57+ in
58+ Option. to_result response.Response. result ~none: error
59+ in
4860 let *? _ =
4961 let open Page.SetLifecycleEventsEnabled in
5062 let response =
Original file line number Diff line number Diff line change @@ -126,6 +126,9 @@ module YAML = struct
126126 |> Result. join
127127 |> Result. map (Option. value ~default: 0xFF0000FFl )
128128 in
129+ let * additional_headers =
130+ yaml |> OSnap_Config_Utils.YAML. parse_http_headers ~path " additionalHttpHeaders"
131+ in
129132 let duplicates =
130133 default_sizes
131134 |> List. filter (fun (s : OSnap_Config_Types.size ) -> Option. is_some s.name)
@@ -150,6 +153,7 @@ module YAML = struct
150153 ; snapshot_directory
151154 ; diff_pixel_color
152155 ; parallelism
156+ ; additional_headers
153157 }
154158 ;;
155159end
Original file line number Diff line number Diff line change @@ -100,8 +100,24 @@ module YAML = struct
100100 ~parser: (parse_ignore ~path )
101101 |> Result. map (Option. value ~default: [] )
102102 in
103+ let * additional_headers =
104+ test
105+ |> OSnap_Config_Utils.YAML. parse_http_headers ~path " additionalHttpHeaders"
106+ |> Result. map (Option. fold ~some: Option. some ~none: global_config.additional_headers)
107+ in
103108 let * () = Common. collect_duplicates sizes in
104- Result. ok { only; skip; threshold; retry; name; url; sizes; actions; ignore }
109+ Result. ok
110+ { only
111+ ; skip
112+ ; threshold
113+ ; retry
114+ ; name
115+ ; url
116+ ; sizes
117+ ; actions
118+ ; ignore
119+ ; additional_headers
120+ }
105121 ;;
106122
107123 let parse global_config path =
Original file line number Diff line number Diff line change @@ -25,6 +25,8 @@ type ignoreType =
2525 | Selector of string * size_restriction
2626 | SelectorAll of string * size_restriction
2727
28+ type additional_headers = (string * string ) list option
29+
2830type test =
2931 { only : bool
3032 ; skip : bool
@@ -35,6 +37,7 @@ type test =
3537 ; sizes : size list
3638 ; actions : action list
3739 ; ignore : ignoreType list
40+ ; additional_headers : additional_headers
3841 }
3942
4043type global =
@@ -50,4 +53,5 @@ type global =
5053 ; snapshot_directory : string
5154 ; diff_pixel_color : int32
5255 ; parallelism : int option
56+ ; additional_headers : additional_headers
5357 }
Original file line number Diff line number Diff line change @@ -265,6 +265,32 @@ module YAML = struct
265265 OSnap_Config_Types. { name; width; height } |> Result. ok
266266 ;;
267267
268+ let parse_http_headers ~path key obj =
269+ obj
270+ |> Yaml.Util. find key
271+ |> Result. map
272+ (Option. map (fun headers ->
273+ match headers with
274+ | `Null -> Result. ok []
275+ | headers ->
276+ let keys = Yaml.Util. keys headers in
277+ let values = Yaml.Util. values headers in
278+ (match keys, values with
279+ | _ , Error _ | Error _ , _ ->
280+ Result. error
281+ (`Msg
282+ " HTTP Headers are in an invalid format. Expected to see an object." )
283+ | Ok keys , Ok values ->
284+ let * values =
285+ OSnap_Utils.List. map_until_exception Yaml.Util. to_string values
286+ in
287+ Result. ok (List. combine keys values))))
288+ |> Result. map to_result_option
289+ |> Result. join
290+ |> Result. map_error (function `Msg message ->
291+ `OSnap_Config_Parse_Error (message, path))
292+ ;;
293+
268294 let parse_size ~path ~default_sizes size =
269295 match size with
270296 | `String name ->
Original file line number Diff line number Diff line change @@ -171,6 +171,7 @@ let run ~env (global_config : Config.Types.global) target test =
171171 let base_snapshot = Eio.Path. (dirs.base / filename) in
172172 let updated_snapshot = Eio.Path. (dirs.updated / filename) in
173173 let diff_image = Eio.Path. (dirs.diff / diff_filename) in
174+ let *? () = target |> Browser.Actions. set_headers ~headers: test.additional_headers in
174175 let *? () = target |> Browser.Actions. clear_cookies in
175176 let *? () =
176177 target
Original file line number Diff line number Diff line change 66 ; height : int
77 ; actions : OSnap_Config.Types .action list
88 ; ignore_regions : OSnap_Config.Types .ignoreType list
9+ ; additional_headers : OSnap_Config.Types .additional_headers
910 ; threshold : int
1011 ; retry : int
1112 ; exists : bool
You can’t perform that action at this time.
0 commit comments