text
stringlengths
0
601k
let set_trigger_after_settle trigger_after_settle resp = Opium . Response . add_header_or_replace ( " HX - Trigger - After - Settle " , trigger_after_settle ) resp ; ;
let set_trigger_after_swap trigger_after_swap resp = Opium . Response . add_header_or_replace ( " HX - Trigger - After - Swap " , trigger_after_swap ) resp ; ;
let combine_routers_matches_first_route _ ( ) = let was_called1 = ref false in let was_called2 = ref false in let handler1 _ = was_called1 := true ; Lwt . return ( Opium . Response . of_plain_text " ello 1 " ) in let handler2 _ = was_called2 := true ; Lwt . return ( Opium . Response . of_plain_text " ello 2 " ) in let ...
let combine_routers_calls_middlewares _ ( ) = let root_middleware_was_called = ref false in let sub_middleware_was_called = ref false in let index_was_called = ref false in let foo_was_called = ref false in let bar_was_called = ref false in let reset_assert_state ( ) = root_middleware_was_called := false ; sub_middlewa...
let global_middleware_before_router _ ( ) = let filter _ _ = Lwt . return @@ Opium . Response . of_plain_text " all good " ! in let middleware = Rock . Middleware . create ~ name " : test " ~ filter in let router = Sihl . Web . choose ~ middlewares [ ] : ~ scope " " :/ [ ] in let service = Sihl . Web . Http . register ...
let suite = [ ( " http " , [ test_case " combine routers matches first route " ` Quick combine_routers_matches_first_route ; test_case " combine routers calls middlewares " ` Quick combine_routers_calls_middlewares ; test_case " global middleware before router " ` Quick global_middleware_before_router ] ) ] ; ;
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " http " suite ) ; ;
let generate_random_id _ ( ) = let middleware = Sihl . Web . Middleware . id ( ) in let req = Opium . Request . get " / foo / bar " in let id_first = ref " " in let handler req = let id = Sihl . Web . Id . find req |> Option . get in id_first := id ; Alcotest . ( check bool " non empty string " true ( String . length i...
let use_provided_id _ ( ) = let middleware = Sihl . Web . Middleware . id ( ) in let req = Opium . Request . get " / foo / bar " |> Opium . Request . add_header ( " X - Request - ID " , " randomid123 " ) in let handler req = let id = Sihl . Web . Id . find req |> Option . get in Alcotest . ( check string " is provided ...
let suite = [ ( " id " , [ test_case " generate random id " ` Quick generate_random_id ; test_case " use provided id " ` Quick use_provided_id ] ) ] ; ;
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " id " suite ) ; ;
type t = < href : string [ @ bs . get ] [ @ bs . set ] ; protocol : string [ @ bs . get ] [ @ bs . set ] ; host : string [ @ bs . get ] [ @ bs . set ] ; hostname : string [ @ bs . get ] [ @ bs . set ] ; port : string [ @ bs . get ] [ @ bs . set ] ; pathname : string [ @ bs . get ] [ @ bs . set ] ; search : string [ @ b...
let getHref location = location ## href
let setHref location value = location ## href #= value
let getProtocol location = location ## protocol
let setProtocol location value = location ## protocol #= value
let getHost location = location ## host
let setHost location value = location ## host #= value
let getHostname location = location ## hostname
let setHostname location value = location ## hostname #= value
let getPort location = location ## port
let setPort location value = location ## port #= value
let getPathname location = location ## pathname
let setPathname location value = location ## pathname #= value
let getSearch location = location ## search
let setSearch location value = location ## search #= value
let getHash location = location ## hash
let setHash location value = location ## hash #= value
let getUsername location = location ## username
let setUsername location value = location ## username #= value
let getPassword location = location ## password
let setPassword location value = location ## password #= value
let getOrigin location = location ## origin
type location = { href : string ; protocol : string ; host : string ; hostname : string ; port : string ; pathname : string ; search : string ; hash : string ; username : string ; password : string ; origin : string }
let asRecord location = { href = location ## href ; protocol = location ## protocol ; host = location ## host ; hostname = location ## hostname ; port = location ## port ; pathname = location ## pathname ; search = location ## search ; hash = location ## hash ; username = location ## username ; password = location ## p...
type style = < setProperty : Web_json . t Js . undefined [ @ bs . get ] ; setProperty__ : string -> string Js . null -> string Js . null -> unit [ @ bs . meth ] ; > Js . t
type t = < style : style [ @ bs . get ] ; value : string Js . undefined [ @ bs . set ] [ @ bs . get ] ; checked : bool Js . undefined [ @ bs . set ] [ @ bs . get ] ; childNodes : t Js . Array . t [ @ bs . get ] ; firstChild : t Js . Null . t [ @ bs . get ] ; appendChild : t -> t [ @ bs . meth ] ; removeChild : t -> t [...
type event = t Web_event . t
type event_cb = t Web_event . cb
let style n = n ## style
let getStyle n key = getStyle n ## style key
let setStyle n key value = setStyle n ## style key value
let setStyleProperty n ( ? priority = false ) key value = let style = n ## style in match Js . Undefined . toOption style ## setProperty with | None -> setStyle n key value | Some _valid -> style ## setProperty__ key value ( if priority then ( Js . Null . return " important " ) else Js . Null . empty )
let childNodes n = n ## childNodes
let firstChild n = n ## firstChild
let appendChild n child = n ## appendChild child
let removeChild n child = n ## removeChild child
let insertBefore n child refNode = n ## insertBefore child refNode
let remove n child = n ## remove child
let setAttributeNS n namespace key value = n ## setAttributeNS namespace key value
let setAttribute n key value = n ## setAttribute key value
let setAttributeNsOptional n namespace key value = match namespace with | " " -> n ## setAttribute key value | ns -> n ## setAttributeNS ns key value
let removeAttributeNS n namespace key = n ## removeAttributeNS namespace key
let removeAttribute n key = n ## removeAttribute key
let removeAttributeNsOptional n namespace key = match namespace with | " " -> n ## removeAttribute key | ns -> n ## removeAttributeNS ns key
let addEventListener n typ listener options = n ## addEventListener typ listener options
let removeEventListener n typ listener options = n ## removeEventListener typ listener options
let focus n = n ## focus ( )
let set_nodeValue n text = n ## nodeValue #= text
let get_nodeValue n = n ## nodeValue
let remove_polyfill : unit -> unit = fun ( ) -> [ % bs . raw { | // remove polyfill ( function ( ) { if ( ( ' ! remove ' in Element . prototype ) ) { Element . prototype . remove = function ( ) { if ( this . parentNode ) { this . parentNode . removeChild ( this ) ; } } ; } ; } ( ) ) } ] |
let singularize str = Option . value ~ default : str ( CCString . chop_suffix ~ suf " : s " str ) ; ;
module Form = struct type t = ( string * string option * string option ) list [ @@ deriving yojson , show ] let set ( ? key = " _form " ) ( errors : Conformist . error list ) ( urlencoded : ( string * string list ) list ) resp = let t = List . map ( fun ( k , v ) -> errors |> List . find_opt ( fun ( field , _ , _ ) -> ...
module type SERVICE = sig type t val find : string -> t option Lwt . t val search : ? filter : string -> ? sort [ : ` Desc | ` Asc ] -> ? limit : int -> ? offset : int -> unit -> ( t list * int ) Lwt . t val insert : t -> ( t , string ) Result . t Lwt . t val update : string -> t -> ( t , string ) result Lwt . t val de...
module Query = struct type sort = [ ` Desc | ` Asc ] type t = { filter : string option ; limit : int option ; offset : int option ; sort : sort option } let default_limit = 50 let sort_of_string ( str : string ) : sort option = match str with | " asc " -> Some ` Asc | " desc " -> Some ` Desc | _ -> None ; ; let string_...
module type VIEW = sig type t val skip_index_fetch : bool val index : Rock . Request . t -> string -> t list * int -> Query . t -> [ > Html_types . html ] Tyxml . Html . elt Lwt . t val new ' : Rock . Request . t -> string -> Form . t -> [ > Html_types . html ] Tyxml . Html . elt Lwt . t val show : Rock . Request . t -...
module type CONTROLLER = sig type t val index : string -> Rock . Request . t -> Rock . Response . t Lwt . t val new ' : ? key : string -> string -> Rock . Request . t -> Rock . Response . t Lwt . t val create : string -> ( ' a , ' b , t ) Conformist . t -> Rock . Request . t -> Rock . Response . t Lwt . t val show : st...
module MakeController ( Service : SERVICE ) ( View : VIEW with type t = Service . t ) = struct exception Exception of string type t = Service . t let fetch_csrf name req = match Web_csrf . find req with | None -> Logs . err ( fun m -> m " CSRF middleware not installed for resource ' % s ' " name ) ; failwith " CSRF mid...
type action = [ ` Index | ` Create | ` New | ` Edit | ` Show | ` Update | ` Destroy ]
let router_of_action ( type a ) ( module Controller : CONTROLLER with type t = a ) name schema ( action : action ) = match action with | ` Index -> Web . get ( Format . sprintf " /% s " name ) ( Controller . index name ) | ` Create -> Web . post ( Format . sprintf " /% s " name ) ( Controller . create name schema ) | `...
let routers_of_actions ( type a ) name schema ( module Controller : CONTROLLER with type t = a ) ( actions : action list ) = List . map ( router_of_action ( module Controller ) name schema ) actions ; ;
let resource_of_controller ( type a ) ? only name schema ( module Controller : CONTROLLER with type t = a ) = match only with | None -> routers_of_actions name schema ( module Controller ) [ ` Index ; ` Create ; ` New ; ` Edit ; ` Show ; ` Update ; ` Destroy ] | Some actions -> routers_of_actions name schema ( module C...
let resource_of_service ( type a ) ? only name schema ~ view ( : module View : VIEW with type t = a ) ( module Service : SERVICE with type t = a ) = let module Controller = MakeController ( Service ) ( View ) in resource_of_controller ? only name schema ( module Controller ) ; ;
let no_cookie_set_without_session _ ( ) = let req = Opium . Request . get " " |> Opium . Request . add_cookie ( " _session " , " { } " ) in let handler _ = Opium . Response . of_plain_text " " |> Lwt . return in let % lwt response = handler req in Alcotest . ( check int " no cookies set " 0 ( List . length ( Opium . Re...
let unsigned_session_cookie _ ( ) = let req = Opium . Request . get " " |> Opium . Request . add_cookie ( " _session " , { { " | foo " " : bar " } } ) | in let handler req = let value = Sihl . Web . Session . find " foo " req in Alcotest . ( check ( option string ) " no session " None value ) ; Opium . Response . of_pl...
let invalid_session_cookie_signature _ ( ) = let req = Opium . Request . get " " |> Opium . Request . add_cookie ( " _session " , { { " | foo " " : bar " } . aE75kXj9sbZp6tP7oJLhrp9c /+ w } ) =| in let handler req = let value = Sihl . Web . Session . find " foo " req in Alcotest . ( check ( option string ) " no session...
let invalid_session_cookie_value _ ( ) = let req = Opium . Request . get " " |> Opium . Request . add_cookie ( " _session " , " foobar . jE75kXj9sbZp6tP7oJLhrp9c /+ w " ) = in let handler req = let value = Sihl . Web . Session . find " foo " req in Alcotest . ( check ( option string ) " no session " None value ) ; Opiu...
let cookie_set _ ( ) = let req = Opium . Request . get " " in let handler _ = let resp = Opium . Response . of_plain_text " " in Lwt . return @@ Sihl . Web . Session . set [ " foo " , " bar " ] resp in let % lwt response = handler req in let cookie = Opium . Response . cookies response |> List . hd in let cookie_value ...
let session_persisted_across_requests _ ( ) = let req = Opium . Request . get " " in let handler _ = let resp = Opium . Response . of_plain_text " " in Lwt . return @@ Sihl . Web . Session . set [ " foo " , " bar " ] resp in let % lwt response = handler req in let cookies = Opium . Response . cookies response in Alcote...
let fetch_all_kv_pairs _ ( ) = let open Sihl . Web in let session = [ " foo " , " bar " ; " baz " , " qux " ; " foo " , " quux " ] in let req = Request . get " " |> Sihl . Test . Session . set_value_req session in let handler req = let all = req |> Session . get_all |> Option . get in Alcotest . ( check ( list ( pair s...
let update_value _ ( ) = let open Sihl . Web in let target1 = " baz " , " qux " in let target2 = " waldo " , " grault " in let con = " able " in let session = [ " foo " , " bar " ; target1 ; " foo " , " quux " ] in let req = Request . get " " in let handler _ = Response . of_plain_text " " |> Session . set session |> S...
let delete_value _ ( ) = let open Sihl . Web in let target1 = " baz " , " qux " in let target2 = " waldo " , " grault " in let session = [ " foo " , " bar " ; target1 ; " foo " , " quux " ] in let req = Request . get " " in let handler _ = Response . of_plain_text " " |> Session . set session |> Session . update_or_set...
let set_value _ ( ) = let open Sihl . Web in let target1 = " baz " , " qux " in let target2 = " waldo " in let updated = " grault " in let session = [ " foo " , " bar " ; target1 ; " foo " , " quux " ] in let req = Request . get " " in let handler _ = Response . of_plain_text " " |> Session . set session |> Session . s...
let suite = [ ( " session " , [ test_case " no cookie set without session " ` Quick no_cookie_set_without_session ; test_case " unsigned session cookie " ` Quick unsigned_session_cookie ; test_case " invalid session cookie signature " ` Quick invalid_session_cookie_signature ; test_case " invalid session cookie value "...
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " session " suite ) ; ;
let remove_trailing_slash _ ( ) = let middleware = Sihl . Web . Middleware . trailing_slash ( ) in let req = Opium . Request . get " / foo / bar " / in let handler req = Alcotest . ( check string " without trailing slash " " / foo / bar " req . Opium . Request . target ) ; Lwt . return @@ Opium . Response . of_plain_te...
let remove_trailing_slash_on_root _ ( ) = let middleware = Sihl . Web . Middleware . trailing_slash ( ) in let req = Opium . Request . get " " / in let handler req = Alcotest . ( check string " does not remove trailing slash " " " / req . Opium . Request . target ) ; Lwt . return @@ Opium . Response . of_plain_text " "...
let remove_trailing_slash_on_root_with_prefix _ ( ) = Unix . putenv " PREFIX_PATH " " path " ; let middleware = Sihl . Web . Middleware . trailing_slash ( ) in let req = Opium . Request . get " / path " //// in let handler req = Alcotest . ( check string " does not remove trailing slash " " / path " req . Opium . Reque...
let suite = [ ( " trailing slash " , [ test_case " remove trailing slash " ` Quick remove_trailing_slash ; test_case " remove trailing slash on root " ` Quick remove_trailing_slash_on_root ; test_case " remove trailing slash on root with prefix " ` Quick remove_trailing_slash_on_root_with_prefix ] ) ] ; ;
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " trailing slash " suite ) ; ;
type t = < history : History . t Js . Undefined . t [ @ bs . get ] ; location : Web_location . t [ @ bs . get ] ; clearTimeout : timeoutHandlerID -> unit [ @ bs . meth ] ; requestAnimationFrame : ( float -> unit ) -> int [ @ bs . meth ] ; cancelAnimationFrame : int -> unit [ @ bs . meth ] ; setInterval : ( unit -> unit...
let history ( ) = window ## history
let localStorage ( ) = window ## localStorage
let location ( ) = window ## location
let requestAnimationFrame callback = window ## requestAnimationFrame callback
let cancelAnimationFrame id = window ## cancelAnimationFrame id
let clearTimeout id = window ## clearTimeout id
let setInterval cb msTime = window ## setInterval cb msTime
let setTimeout cb msTime = window ## setTimeout cb msTime
let addEventListener typ listener options = window ## addEventListener typ listener options