Skip to content

Commit 730ba54

Browse files
committed
feat: add ability to define headers sent with each request
1 parent f9a44c0 commit 730ba54

12 files changed

Lines changed: 126 additions & 1 deletion

lib/OSnap.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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 = []

lib/OSnap_Browser/OSnap_Browser_Actions.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff 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 ~sessionId ~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+
;;

lib/OSnap_Browser/OSnap_Browser_Actions.mli

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,8 @@ val screenshot
109109
val 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

lib/OSnap_Browser/OSnap_Browser_Target.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff 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 ~sessionId ~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 =

lib/OSnap_Config/OSnap_Config_Global.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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
;;
155159
end

lib/OSnap_Config/OSnap_Config_Test.ml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff 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 =

lib/OSnap_Config/OSnap_Config_Types.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff 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+
2830
type 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

4043
type 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
}

lib/OSnap_Config/OSnap_Config_Utils.ml

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff 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 ->

lib/OSnap_Test/OSnap_Test.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff 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

lib/OSnap_Test/OSnap_Test_Types.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ type t =
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

0 commit comments

Comments
 (0)