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 route1 = Sihl . Web . get " / some / path " handler1 in let route2 = Sihl . Web . get " " /** handler2 in let router = Sihl . Web . choose ~ scope " :/ scope " [ route1 ; route2 ] in let service = Sihl . Web . Http . register router in let % lwt ( ) = Sihl . Container . Service . start service in let % lwt _ = Cohttp_lwt_unix . Client . get ( Uri . of_string " http :// localhost : 3000 / scope / some / path " ) in Alcotest . ( check bool " was called " true ! was_called1 ) ; Alcotest . ( check bool " was not called " false ! was_called2 ) ; Sihl . Container . Service . stop service ; ; |
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_middleware_was_called := false ; index_was_called := false ; foo_was_called := false ; bar_was_called := false in reset_assert_state ( ) ; let middleware_root = Rock . Middleware . create ~ name " : root " ~ filter ( : fun hander req -> root_middleware_was_called := true ; hander req ) in let middleware_sub = Rock . Middleware . create ~ name " : sub " ~ filter ( : fun hander req -> sub_middleware_was_called := true ; hander req ) in let middleware_foo = Rock . Middleware . create ~ name " : foo " ~ filter ( : fun _ _ -> Lwt . return @@ Sihl . Web . Response . of_plain_text " foo middleware " ) in let middleware_bar = Rock . Middleware . create ~ name " : bar " ~ filter ( : fun _ _ -> Lwt . return @@ Sihl . Web . Response . of_plain_text " bar middleware " ) in let router = Sihl . Web . ( choose ~ middlewares [ : middleware_root ] ~ scope " :/ root " [ choose ~ middlewares [ : middleware_sub ] ~ scope " :/ sub " [ get " " / ( fun _ -> index_was_called := true ; Lwt . return ( Opium . Response . of_plain_text " " ) ) / ; get ~ middlewares [ : middleware_foo ] " / foo " ( fun _ -> foo_was_called := true ; Lwt . return ( Opium . Response . of_plain_text " / foo " ) ) ; get " / fooz " ( fun _ -> Lwt . return ( Opium . Response . of_plain_text " / fooz " ) ) ] ; get " / bar " ~ middlewares [ : middleware_bar ] ( fun _ -> bar_was_called := true ; Lwt . return ( Opium . Response . of_plain_text " / bar " ) ) ] ) in let service = Sihl . Web . Http . register router in let status_of_resp resp = resp |> Lwt . map fst |> Lwt . map Cohttp . Response . status |> Lwt . map Cohttp . Code . code_of_status in let % lwt ( ) = Sihl . Container . Service . start service in reset_assert_state ( ) ; let % lwt status = Cohttp_lwt_unix . Client . get ( Uri . of_string " http :// localhost : 3000 / root " ) |> status_of_resp in Alcotest . ( check int " / root not found " 404 status ) ; Alcotest . ( check bool " / root middleware not called " false ! root_middleware_was_called ) ; reset_assert_state ( ) ; let % lwt status = Cohttp_lwt_unix . Client . get ( Uri . of_string " http :// localhost : 3000 / root " ) / |> status_of_resp in Alcotest . ( check int " / root / not found " 404 status ) ; Alcotest . ( check bool " / root middleware not called " false ! root_middleware_was_called ) ; reset_assert_state ( ) ; let % lwt status = Cohttp_lwt_unix . Client . get ( Uri . of_string " http :// localhost : 3000 / root / sub " ) |> status_of_resp in Alcotest . ( check int " / root / sub not found " 404 status ) ; reset_assert_state ( ) ; let % lwt status = Cohttp_lwt_unix . Client . get ( Uri . of_string " http :// localhost : 3000 / root / sub " ) / |> status_of_resp in Alcotest . ( check int " / root / sub / found " 200 status ) ; Alcotest . ( check bool " root middleware called " true ! root_middleware_was_called ) ; Alcotest . ( check bool " sub middleware called " true ! sub_middleware_was_called ) ; reset_assert_state ( ) ; let % lwt body = Cohttp_lwt_unix . Client . get ( Uri . of_string " http :// localhost : 3000 / root / sub / foo " ) |> Lwt . map snd in let % lwt body = Cohttp_lwt . Body . to_string body in Alcotest . ( check string " foo middleware called " " foo middleware " body ) ; Alcotest . ( check bool " root middleware called " true ! root_middleware_was_called ) ; Alcotest . ( check bool " sub middleware called " true ! sub_middleware_was_called ) ; reset_assert_state ( ) ; let % lwt body = Cohttp_lwt_unix . Client . get ( Uri . of_string " http :// localhost : 3000 / root / bar " ) |> Lwt . map snd in let % lwt body = Cohttp_lwt . Body . to_string body in Alcotest . ( check string " bar middleware called " " bar middleware " body ) ; Alcotest . ( check bool " root middleware called " true ! root_middleware_was_called ) ; Alcotest . ( check bool " sub middleware not called " false ! sub_middleware_was_called ) ; Sihl . Container . Service . stop service ; ; |
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 ~ middlewares [ : middleware ] router in let % lwt ( ) = Sihl . Container . Service . start service in let % lwt resp , body = Cohttp_lwt_unix . Client . get ( Uri . of_string " http :// localhost : 3000 / non / existing " ) in let status = resp |> Cohttp . Response . status |> Cohttp . Code . code_of_status in let % lwt body = Cohttp_lwt . Body . to_string body in Alcotest . ( check int " matched without route " 200 status ) ; Alcotest . ( check string " responds " " all good " ! body ) ; Sihl . Container . Service . stop service ; ; |
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 id > 0 ) ) ; Lwt . return @@ Opium . Response . of_plain_text " " in let % lwt _ = Rock . Middleware . apply middleware handler req in let id_second = ref " " in let handler req = let id = Sihl . Web . Id . find req |> Option . get in id_second := id ; Alcotest . ( check bool " non empty string " true ( String . length id > 0 ) ) ; Lwt . return @@ Opium . Response . of_plain_text " " in let % lwt _ = Rock . Middleware . apply middleware handler req in Alcotest . ( check bool " different ids " false ( String . equal ! id_first ! id_second ) ) ; Lwt . return ( ) ; ; |
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 id " " randomid123 " id ) ; Lwt . return @@ Opium . Response . of_plain_text " " in let % lwt _ = Rock . Middleware . apply middleware handler req in Lwt . return ( ) ; ; |
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 [ @ bs . get ] [ @ bs . set ] ; hash : string [ @ bs . get ] [ @ bs . set ] ; username : string [ @ bs . get ] [ @ bs . set ] ; password : string [ @ bs . get ] [ @ bs . set ] ; origin : string [ @ bs . get ] ; |
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 ## password ; origin = location ## origin } |
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 [ @ bs . meth ] ; insertBefore : t -> t -> t [ @ bs . meth ] ; remove : unit -> unit [ @ bs . meth ] ; setAttributeNS : string -> string -> string -> unit [ @ bs . meth ] ; setAttribute : string -> string -> unit [ @ bs . meth ] ; removeAttributeNS : string -> string -> unit [ @ bs . meth ] ; removeAttribute : string -> unit [ @ bs . meth ] ; addEventListener : string -> t Web_event . cb -> Web_event . options -> unit [ @ bs . meth ] ; removeEventListener : string -> t Web_event . cb -> Web_event . options -> unit [ @ bs . meth ] ; focus : unit -> unit [ @ bs . meth ] ; nodeValue : string [ @ bs . set ] [ @ bs . get { null } ] ; |
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 , _ , _ ) -> String . equal field k ) |> Option . map ( fun ( field , input , value ) -> field , CCList . head_opt input , Some value ) |> Option . value ~ default ( : k , CCList . head_opt v , None ) ) urlencoded in let json = t |> to_yojson |> Yojson . Safe . to_string in Web_flash . set [ key , json ] resp ; ; let find_form ( ? key = " _form " ) req = match Web_flash . find key req with | None -> [ ] | Some json -> let yojson = try Some ( Yojson . Safe . from_string json ) with | _ -> None in ( match yojson with | Some yojson -> ( match of_yojson yojson with | Error _ -> [ ] | Ok form -> form ) | None -> [ ] ) ; ; let find ( k : string ) ( form : t ) : string option * string option = form |> List . find_opt ( fun ( k ' , _ , _ ) -> String . equal k k ' ) |> Option . map ( fun ( _ , value , error ) -> value , error ) |> Option . value ~ default ( : None , None ) ; ; end |
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 delete : t -> ( unit , string ) result Lwt . t end |
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_of_sort = function | ` Desc -> " desc " | ` Asc -> " asc " ; ; let of_request req = let ( let * ) = Option . bind in let filter = let * filter = Opium . Request . query " filter " req in if String . equal " " filter then None else Some filter in let limit = let * limit = Opium . Request . query " limit " req in let * limit = if String . equal " " limit then None else Some limit in int_of_string_opt limit in let offset = let * offset = Opium . Request . query " offset " req in let * offset = if String . equal " " offset then None else Some offset in int_of_string_opt offset in let sort = let * sort = Opium . Request . query " sort " req in let * sort = if String . equal " " sort then None else Some sort in sort_of_string sort in { filter ; limit ; offset ; sort } ; ; let to_query_string ( query : t ) : string = Uri . empty |> ( fun uri -> match query . filter with | Some filter -> Uri . add_query_param uri ( " filter " , [ filter ] ) | None -> uri ) |> ( fun uri -> match query . limit with | Some limit -> Uri . add_query_param uri ( " limit " , [ string_of_int limit ] ) | None -> uri ) |> ( fun uri -> match query . offset with | Some offset -> Uri . add_query_param uri ( " offset " , [ string_of_int offset ] ) | None -> uri ) |> ( fun uri -> match query . sort with | Some sort -> Uri . add_query_param uri ( " sort " , [ string_of_sort sort ] ) | None -> uri ) |> Uri . to_string ; ; let next_page ( query : t ) ( total : int ) : t option = let limit = Option . value ~ default : default_limit query . limit in let offset = Option . value ~ default : 0 query . offset in if limit + offset <= total then Some { query with offset = Some ( limit + offset ) } else None ; ; let previous_page ( query : t ) : t option = let limit = Option . value ~ default : default_limit query . limit in let offset = Option . value ~ default : 0 query . offset in if offset - limit >= 0 then Some { query with offset = Some ( offset - limit ) } else None ; ; let last_page ( query : t ) ( total : int ) : t option = let limit = Option . value ~ default : default_limit query . limit in let offset = Option . value ~ default : 0 query . offset in if offset < total - limit then Some { query with offset = Some ( total - 1 ) } else None ; ; let first_page ( query : t ) : t option = let offset = Option . value ~ default : 0 query . offset in if offset > 0 then Some { query with offset = Some 0 } else None ; ; end |
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 -> t -> [ > Html_types . html ] Tyxml . Html . elt Lwt . t val edit : Rock . Request . t -> string -> Form . t -> t -> [ > Html_types . html ] Tyxml . Html . elt Lwt . t end |
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 : string -> Rock . Request . t -> Rock . Response . t Lwt . t val edit : ? key : string -> string -> Rock . Request . t -> Rock . Response . t Lwt . t val update : string -> ( ' a , ' b , t ) Conformist . t -> Rock . Request . t -> Rock . Response . t Lwt . t val delete ' : string -> Rock . Request . t -> Rock . Response . t Lwt . t end |
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 middleware not installed " | Some token -> token ; ; let index name req = let open Query in let csrf = fetch_csrf name req in let ( { filter ; limit ; offset ; sort } as query ) = of_request req in let % lwt result = if View . skip_index_fetch then Lwt . return ( [ ] , 0 ) else Service . search ? filter ? limit ? offset ? sort ( ) in let % lwt html = View . index req csrf result query in Lwt . return @@ Opium . Response . of_html html ; ; let new ' ? key name req = let csrf = fetch_csrf name req in let form = Form . find_form ? key req in let % lwt html = View . new ' req csrf form in Lwt . return @@ Opium . Response . of_html html ; ; let create name schema req = let % lwt urlencoded = Opium . Request . to_urlencoded req in let thing = Conformist . decode_and_validate schema urlencoded in match thing with | Ok thing -> let % lwt thing = Service . insert thing in ( match thing with | Ok _ -> Opium . Response . redirect_to ( Format . sprintf " /% s " name ) |> Web_flash . set_notice ( Format . sprintf " Successfully added % s " ( singularize name ) ) |> Lwt . return | Error msg -> Opium . Response . redirect_to ( Format . sprintf " /% s / new " name ) |> Form . set [ ] urlencoded |> Web_flash . set_alert msg |> Lwt . return ) | Error errors -> Opium . Response . redirect_to ( Format . sprintf " /% s / new " name ) |> Web_flash . set_alert " Some of the input is invalid " |> Form . set errors urlencoded |> Lwt . return ; ; let show name req = let id = Opium . Router . param req " id " in let % lwt thing = Service . find id in match thing with | Some thing -> let % lwt html = View . show req thing in Lwt . return @@ Opium . Response . of_html html | None -> Opium . Response . redirect_to ( Format . sprintf " /% s " name ) |> Web_flash . set_alert ( Format . sprintf " % s with id ' % s ' not found " ( singularize ( capitalize name ) ) id ) |> Lwt . return ; ; let edit ? key name req = let id = Opium . Router . param req " id " in let % lwt thing = Service . find id in match thing with | Some thing -> let csrf = fetch_csrf name req in let form = Form . find_form ? key req in let % lwt html = View . edit req csrf form thing in Lwt . return @@ Opium . Response . of_html html | None -> Opium . Response . redirect_to ( Format . sprintf " /% s " name ) |> Web_flash . set_alert ( Format . sprintf " % s with id ' % s ' not found " ( singularize ( capitalize name ) ) id ) |> Lwt . return ; ; let update name schema req = let % lwt urlencoded = Opium . Request . to_urlencoded req in let thing = Conformist . decode_and_validate schema urlencoded in let id = Opium . Router . param req " id " in match thing with | Ok thing -> let % lwt updated = Service . update id thing in ( match updated with | Ok _ -> Opium . Response . redirect_to ( Format . sprintf " /% s /% s " name id ) |> Web_flash . set_notice ( Format . sprintf " Successfully updated % s " ( singularize name ) ) |> Lwt . return | Error msg -> Opium . Response . redirect_to ( Format . sprintf " /% s /% s / edit " name id ) |> Web_flash . set_alert msg |> Form . set [ ] urlencoded |> Lwt . return ) | Error errors -> Opium . Response . redirect_to ( Format . sprintf " /% s /% s / edit " name id ) |> Web_flash . set_alert " Some of the input is invalid " |> Form . set errors urlencoded |> Lwt . return ; ; let delete ' name req = let id = Opium . Router . param req " id " in let query = Query . of_request req in let target_uri = Format . sprintf " /% s % s " name ( Query . to_query_string query ) in let % lwt thing = Service . find id in match thing with | None -> Opium . Response . redirect_to target_uri |> Web_flash . set_alert ( Format . sprintf " % s with id ' % s ' not found " ( singularize ( capitalize name ) ) id ) |> Lwt . return | Some thing -> let % lwt result = Service . delete thing in ( match result with | Ok ( ) -> Opium . Response . redirect_to target_uri |> Web_flash . set_notice ( Format . sprintf " Successfully deleted % s ' % s ' " ( singularize name ) id ) |> Lwt . return | Error msg -> Opium . Response . redirect_to target_uri |> Web_flash . set_notice ( Format . sprintf " Failed to delete % s : ' % s ' " ( singularize name ) msg ) |> Lwt . return ) ; ; end |
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 ) | ` New -> Web . get ( Format . sprintf " /% s / new " name ) ( Controller . new ' name ) | ` Edit -> Web . get ( Format . sprintf " /% s /: id / edit " name ) ( Controller . edit name ) | ` Show -> Web . get ( Format . sprintf " /% s /: id " name ) ( Controller . show name ) | ` Update -> Web . put ( Format . sprintf " /% s /: id " name ) ( Controller . update name schema ) | ` Destroy -> Web . delete ( Format . sprintf " /% s /: id " name ) ( Controller . delete ' name ) ; ; |
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 Controller ) actions ; ; |
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 . Response . cookies response ) ) ) ; Lwt . return ( ) ; ; |
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_plain_text " " |> Lwt . return in let % lwt _ = handler req in Lwt . return ( ) ; ; |
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 " None value ) ; Opium . Response . of_plain_text " " |> Lwt . return in let % lwt _ = handler req in Lwt . return ( ) ; ; |
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 ) ; Opium . Response . of_plain_text " " |> Lwt . return in let % lwt _ = handler req in Lwt . return ( ) ; ; |
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 = cookie . Opium . Cookie . value in Alcotest . ( check ( pair string string ) " persists session values " ( " _session " , { { " | foo " " : bar " } . jE75kXj9sbZp6tP7oJLhrp9c /+ w } ) =| cookie_value ) ; Lwt . return ( ) ; ; |
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 Alcotest . ( check int " responds with exactly one cookie " 1 ( List . length cookies ) ) ; let cookie = Opium . Response . cookie " _session " response |> Option . get in let cookie_value = cookie . Opium . Cookie . value in Alcotest . ( check ( pair string string ) " persists session values " ( " _session " , { { " | foo " " : bar " } . jE75kXj9sbZp6tP7oJLhrp9c /+ w } ) =| cookie_value ) ; let req = Opium . Request . get " " |> Opium . Request . add_cookie cookie . Opium . Cookie . value in let handler req = let session_value = Sihl . Web . Session . find " foo " req in Alcotest . ( check ( option string ) " has session value " ( Some " bar " ) session_value ) ; let resp = Opium . Response . of_plain_text " " |> Sihl . Web . Session . set [ " fooz " , " other " ] in Lwt . return resp in let % lwt response = handler req in let cookies = Opium . Response . cookies response in Alcotest . ( check int " responds with exactly one cookie " 1 ( List . length cookies ) ) ; let cookie = Opium . Response . cookie " _session " response |> Option . get in let cookie_value = cookie . Opium . Cookie . value in Alcotest . ( check ( pair string string ) " persists session values " ( " _session " , { { " | fooz " " : other " } . VRJU0 / vmwzPLrDU0zulQ7MojZUU } ) =| cookie_value ) ; let req = Opium . Request . get " " |> Opium . Request . add_cookie cookie . Opium . Cookie . value in let handler req = Alcotest . ( check ( option string ) " has deleted session value " None ( Sihl . Web . Session . find " foo " req ) ) ; Alcotest . ( check ( option string ) " has set session value " ( Some " other " ) ( Sihl . Web . Session . find " fooz " req ) ) ; Lwt . return @@ Opium . Response . of_plain_text " " in let % lwt _ = handler req in Lwt . return ( ) ; ; |
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 string string ) ) " session unchanged , duplicates removed " ( CCList . tail_opt session |> Option . get ) all ) ; Response . of_plain_text " " |> Lwt . return in let % lwt _ = handler req in Lwt . return ( ) ; ; |
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 |> Session . update_or_set_value ~ key ( : fst target1 ) ( function | None -> Alcotest . fail " value should be found " | Some v -> Some ( con ^ v ) ) req |> Session . update_or_set_value ~ key ( : fst target2 ) ( function | None -> Some ( snd target2 ) | Some _ -> Alcotest . fail " value should not be found " ) req |> Lwt . return in let % lwt response = handler req in Alcotest . ( check ( list @@ pair string string ) " values updated in session " [ CCPair . map_snd ( ( ^ ) con ) target1 ; CCList . last_opt session |> Option . get ; target2 ] ( Sihl . Test . Session . get_all_resp response |> Option . get ) ) ; Lwt . return ( ) ; ; |
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_value ~ key ( : fst target1 ) ( function | None -> Alcotest . fail " value should be found " | Some _ -> None ) req |> Session . update_or_set_value ~ key ( : fst target2 ) ( function | None -> None | Some _ -> Alcotest . fail " value should not be found " ) req |> Lwt . return in let % lwt response = handler req in Alcotest . ( check ( list @@ pair string string ) " session remains same " [ CCList . last_opt session |> Option . get ] ( Sihl . Test . Session . get_all_resp response |> Option . get ) ) ; Lwt . return ( ) ; ; |
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 . set_value ~ key ( : fst target1 ) updated req |> Session . set_value ~ key : target2 updated req |> Lwt . return in let % lwt response = handler req in Alcotest . ( check ( list @@ pair string string ) " values set in session " [ CCPair . map_snd ( CCFun . const updated ) target1 ; CCList . last_opt session |> Option . get ; target2 , updated ] ( Sihl . Test . Session . get_all_resp response |> Option . get ) ) ; Lwt . return ( ) ; ; |
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 " ` Quick invalid_session_cookie_value ; test_case " cookie set " ` Quick cookie_set ; test_case " session persisted across requests " ` Quick session_persisted_across_requests ; test_case " fetch all values in session " ` Quick fetch_all_kv_pairs ; test_case " update existing and non - existing value in session " ` Quick update_value ; test_case " delete existing and non - existing value in session " ` Quick delete_value ; test_case " set value in session " ` Quick set_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_text " " in let % lwt _ = Rock . Middleware . apply middleware handler req 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_text " " in let % lwt _ = Rock . Middleware . apply middleware handler req in let req = Opium . Request . get " / foo / bar /? some = query & other = query " in let handler req = Alcotest . ( check string " without trailing slash " " / foo / bar ? some = query & other = query " req . Opium . Request . target ) ; Lwt . return @@ Opium . Response . of_plain_text " " in let % lwt _ = Rock . Middleware . apply middleware handler req in Lwt . return ( ) ; ; |
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 " " / in let % lwt _ = Rock . Middleware . apply middleware handler req in Lwt . return ( ) ; ; |
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 . Request . target ) ; Lwt . return @@ Opium . Response . of_plain_text " " / in let % lwt _ = Rock . Middleware . apply middleware handler req in Lwt . return ( ) ; ; |
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 ) -> float -> timeoutHandlerID [ @ bs . meth ] ; setTimeout : ( unit -> unit ) -> float -> timeoutHandlerID [ @ bs . meth ] ; addEventListener : string -> Web_node . t Web_event . cb -> Web_event . options -> unit [ @ bs . meth ] ; removeEventListener : string -> Web_node . t Web_event . cb -> Web_event . options -> unit [ @ bs . meth ] ; localStorage : LocalStorage . t Js . Undefined . t [ @ bs . get ] ; |
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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.