text
stringlengths
12
786k
module Suite = struct exception TestFailure of string type result = | Error of exn | Fail of string | Pass type outcome = { label : string ; result : result ; time_s : float ; } let string_of_result = function | Error e -> Printf . sprintf " Error : % s " ( Printexc . to_string e ) ...
module Zipper = struct type crumb = { left : Suite . t list ; label : string ; right : Suite . t list ; } type t = { crumbs : crumb list ; location : Suite . t ; } let of_suite suite = { crumbs = [ ] ; location = suite ; } let to_suite { location ; _ } = location l...
module Utils = struct type output = { log : string list ; outcomes : Suite . outcome list ; } type raw_summary = { total : int ; errors : int ; failures : int ; passes : int ; passed : bool } type summary = { report : string ; passed : bool ; } let run suite callback = let lo...
module Runner = struct let paint color string = match color with | ` Red -> " \ 027 [ 31m " ^ string ^ " \ 027 [ 0m " | ` Green -> " \ 027 [ 32m " ^ string ^ " \ 027 [ 0m " | ` Yellow -> " \ 027 [ 33m " ^ string ^ " \ 027 [ 0m " | _ -> string ...
module type S = sig type t type data val create : filename : string -> logger : Logger . t -> t Deferred . t val store : t -> data -> unit Deferred . t end
module type Storage_intf = sig type data type location val store : location -> data -> unit Deferred . t end
module Make ( Data : sig type t end ) end type t = { filename : string ; reader : Data . t Linear_pipe . Reader . t ; writer : Data . t Linear_pipe . Writer . t } let write_to_storage { filename ; _ } request data = let % bind ( ) = Store . store filename data in Request . put reque...
let can_parse_uri_safe _ ( ) = let open Csrf . Crypto in let with_secret = Sihl . Configuration . read_secret ( ) |> Secret . make in let value = Nocrypto . Rng . generate token_length in let enc = Encrypted_token . from_struct ~ with_secret value in let parsed = enc |> Encrypted_token . ...
let crypto_undo_helper encrypt decrypt = let open Csrf . Crypto in let with_secret = Sihl . Configuration . read_secret ( ) |> Secret . make in let value = Nocrypto . Rng . generate token_length in let dec = encrypt ~ with_secret value |> decrypt ~ with_secret in let open Alcotest in check bool ...
let decrypt_random_undoes_encrypt_random _ ( ) = let open Csrf . Crypto in crypto_undo_helper Encrypted_token . from_struct_random Decrypted_token . from_encrypted_random ; ;
let decrypt_undoes_encrypt _ ( ) = let open Csrf . Crypto in crypto_undo_helper Encrypted_token . from_struct Decrypted_token . from_encrypted ; ;
let csrf_simulation _ ( ) = let open Csrf . Crypto in let with_secret = Sihl . Configuration . read_secret ( ) |> Secret . make in let value = Nocrypto . Rng . generate token_length in let enc = Encrypted_token . from_struct ~ with_secret value in let req = Decrypted_token . from_encrypt...
let apply_middlewares handler ? not_allowed_handler = handler |> Rock . Middleware . apply ( Sihl . Web . Middleware . csrf ? not_allowed_handler ~ key : csrf_name ( ) ) ; ;
let default_response ? status ( ) = Lwt . return @@ Response . of_plain_text ? status " Pizzas " ; ;
let get_csrf req = Csrf . find req |> Option . get
let uri_decrypt token = let open Csrf . Crypto in let with_secret = Sihl . Configuration . read_secret ( ) |> Secret . make in token |> Encrypted_token . of_uri_safe_string |> Option . get |> Decrypted_token . from_encrypted_random ~ with_secret ; ;
let get_request_without_token_succeeds _ ( ) = let req = Request . get route in let handler _ = default_response ( ) in let wrapped_handler = apply_middlewares handler in let % lwt response = wrapped_handler req in let status = Response . status response |> Opium . Status . to_code in let open...
let get_request_yields_token _ ( ) = let req = Request . get route in let token = ref " " in let handler req = token := get_csrf req ; default_response ( ) in let wrapped_handler = apply_middlewares handler in let % lwt response = wrapped_handler req in let value = Sihl . Test . Session . ...
let two_get_requests_yield_correct_tokens _ ( ) = let req = Request . get route in let token1 = ref " " in let token2 = ref " " in let handler tkn req = tkn := get_csrf req ; default_response ( ) in let wrapped_handler1 = apply_middlewares @@ handler token1 in let % lwt resp1 = wrapped_ha...
let post_request_yields_token _ ( ) = let post_req = Request . post route in let token = ref " " in let not_allowed_handler req = token := get_csrf req ; default_response ~ status ` : Forbidden ( ) in let handler _ = default_response ( ) in let wrapped_handler = apply_middlewares handle...
let two_post_requests_yield_different_token _ ( ) = let req = Request . post route in let token1 = ref " " in let token2 = ref " " in let not_allowed_handler tkn req = tkn := get_csrf req ; default_response ( ) in let handler _ = default_response ( ) in let wrapped_handler1 = apply_mid...
let post_request_both_invalid_tokens_fails _ ( ) = let requests = CCList . map ( fun body -> Request . of_urlencoded ~ body route ` POST ) [ [ ] ; [ csrf_name , [ " garbage " ] ] ] in let add_cookie = [ CCFun . id ; Sihl . Test . Session . set_value_req [ csrf_n...
let cookie_invalid_helper reqs = let req = Request . get route in let token = ref " " in let allowed = ref 0 in let handler1 req = token := get_csrf req ; default_response ( ) in let handler2 _ = allowed := ! allowed + 1 ; default_response ( ) in let wrapped_handler = apply_middlewares h...
let post_request_cookie_invalid_token_fails _ ( ) = let reqs ( token , _ ) = CCList . map ( fun add_cookie -> Request . of_urlencoded ~ body [ : csrf_name , [ token ] ] route ` POST |> add_cookie ) [ CCFun . id ; Sihl . Test . Session . set_value_req [ csrf_name , "...
let post_request_request_invalid_token_fails _ ( ) = let reqs ( _ , cookie ) = CCList . map ( fun body -> Request . of_urlencoded ~ body route ` POST |> Request . add_cookie cookie . Cookie . value ) [ [ ] ; [ csrf_name , [ " garbage " ] ] ] in cookie_invalid_hel...
let post_request_with_nonmatching_token_fails _ ( ) = let open Csrf . Crypto in let req = Request . get route in let token = ref " " in let handler req = token := get_csrf req ; default_response ( ) in let wrapped_handler = apply_middlewares handler in let % lwt response = wrapped_handler req...
let post_request_with_nonmatching_cookie_fails _ ( ) = let open Csrf . Crypto in let req = Request . get route in let token = ref " " in let handler req = token := get_csrf req ; default_response ( ) in let wrapped_handler = apply_middlewares handler in let % lwt _ = wrapped_handler req in l...
let post_request_with_valid_token_succeeds _ ( ) = let req = Request . get route in let token = ref " " in let handler req = token := get_csrf req ; default_response ( ) in let wrapped_handler = apply_middlewares handler in let % lwt response = wrapped_handler req in let cookie = response |> ...
let two_post_requests_succeed _ ( ) = let req = Request . get route in let token1 = ref " " in let token2 = ref " " in let token3 = ref " " in let handler tkn req = tkn := get_csrf req ; default_response ( ) in let wrapped_handler = apply_middlewares @@ handler token1 in let % lwt res...
let stale_duplicated_token_post_request_succeed _ ( ) = let req = Request . get route in let token = ref " " in let handler req = token := get_csrf req ; default_response ( ) in let wrapped_handler = apply_middlewares handler in let % lwt response = wrapped_handler req in let old_token = ! to...
let suite = [ ( " csrf crypto " , [ test_case " uri safe encoded string can be decoded " ` Quick can_parse_uri_safe ; test_case " decryption accounting for random undoes random encryption " ` Quick decrypt_random_undoes_encrypt_random ; test_case " decryption undoes encryption " ` Qui...
let ( ) = Unix . putenv " CHECK_CSRF " " true " ; Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " csrf " suite ) ; ;
let log_src = Logs . Src . create " sihl . middleware . error "
module Logs = ( val Logs . src_log log_src : Logs . LOG )
let page request_id = Format . asprintf { | < head > < meta charset " = utf - 8 " > < meta name " = viewport " content " = width = device - width , initial - scale = 1 , shrink - to - fit = no " > < title > Internal Server Error </ title > < style > % s </ style > </ h...
let site_error_handler req = let request_id = Web_id . find req |> Option . value ~ default " " :- in let site = page request_id in Opium . Response . of_plain_text site |> Opium . Response . set_content_type " text / html ; charset = utf - 8 " |> Lwt . return ; ;
let json_error_handler req = let request_id = Web_id . find req |> Option . value ~ default " " :- in let msg = Format . sprintf " Something went wrong , our administrators have been notified . " in let body = Format . sprintf { " { " | errors " : [ " % s " ] , " requ...
let exn_to_string exn req = let msg = Printexc . to_string exn and stack = Printexc . get_backtrace ( ) in let request_id = Web_id . find req |> Option . value ~ default " " :- in let req_str = Format . asprintf " % a " Opium . Request . pp_hum req in Format . asprintf " Request i...
let create_error_email ( sender , recipient ) error = Contract_email . create ~ sender ~ recipient ~ subject " : Exception caught " error ; ;
let middleware ? email_config ( ? reporter = fun _ _ -> Lwt . return ( ) ) ? error_handler ( ) = let filter handler req = Lwt . catch ( fun ( ) -> handler req ) ( fun exn -> let error = exn_to_string exn req in Logs . err ( fun m -> m " % s " error ) ; let _ = match...
let assert_delete_cookie cookie = Alcotest . ( check ( pair string string ) " flash is empty " ( " _flash " , " " ) cookie . Opium . Cookie . value ) ; match cookie . Opium . Cookie . expires with | ` Max_age value -> if Int64 . equal value Int64 . zero then ( ) e...
let not_touching_flash_without_set_cookie_doesnt_set_cookie _ ( ) = let req = Opium . Request . get " " / in let % lwt res = Rock . Middleware . apply ( Sihl . Web . Middleware . flash ( ) ) ( fun _ -> Lwt . return @@ Opium . Response . of_plain_text " " ) req in let...
let not_touching_flash_removes_cookie _ ( ) = let req = Opium . Request . get " " / |> Opium . Request . add_cookie ( " _flash " , { { " | alert " : null , " notice " : null , " custom " [ ] } } ) :| in let % lwt res = Rock . Middleware . apply ( S...
let flash_is_cleared_after_request _ ( ) = let req = Opium . Request . get " " / in let % lwt res = Rock . Middleware . apply ( Sihl . Web . Middleware . flash ( ) ) ( fun _ -> let res = Opium . Response . of_plain_text " " |> Sihl . Web . Flash . set_alert " fo...
let set_and_read_flash_message _ ( ) = let req = Opium . Request . get " " / in let % lwt res = Rock . Middleware . apply ( Sihl . Web . Middleware . flash ( ) ) ( fun req -> let alert = Sihl . Web . Flash . find_alert req in let notice = Sihl . Web . Flash . find_no...
let suite = [ ( " flash " , [ test_case " not touching flash without set cookie doesn ' t change cookie " ` Quick not_touching_flash_without_set_cookie_doesnt_set_cookie ; test_case " not touching flash removes cookie " ` Quick not_touching_flash_removes_cookie ; test_case " flash is cl...
let ( ) = Logs . set_level ( Sihl . Log . get_log_level ( ) ) ; Logs . set_reporter ( Sihl . Log . cli_reporter ( ) ) ; Lwt_main . run ( Alcotest_lwt . run " flash " suite ) ; ;
let log_src = Logs . Src . create " sihl . middleware . htmx "
module Logs = ( val Logs . src_log log_src : Logs . LOG )
let is_htmx req = Opium . Request . header " HX - Request " req |> Option . is_some
let current_url req = Opium . Request . header " HX - Current - URL " req
let prompt req = Opium . Request . header " HX - Prompt " req
let set_prompt prompt req = Opium . Request . add_header_or_replace ( " HX - Prompt " , prompt ) req ; ;
let target req = Opium . Request . header " HX - Target " req
let set_target target req = Opium . Request . add_header_or_replace ( " HX - Target " , target ) req ; ;
let trigger_name req = Opium . Request . header " HX - Trigger - Name " req
let set_trigger_name trigger_name req = Opium . Request . add_header_or_replace ( " HX - Trigger - Name " , trigger_name ) req ; ;
let trigger_req req = Opium . Request . header " HX - Trigger " req
let set_trigger_req trigger req = Opium . Request . add_header_or_replace ( " HX - Trigger " , trigger ) req ; ;
let set_push push resp = Opium . Response . add_header_or_replace ( " HX - Push " , push ) resp ; ;
let set_redirect redirect resp = Opium . Response . add_header_or_replace ( " HX - Redirect " , redirect ) resp ; ;
let set_refresh refresh resp = Opium . Response . add_header_or_replace ( " HX - Refresh " , refresh ) resp ; ;
let set_trigger trigger resp = Opium . Response . add_header_or_replace ( " HX - Trigger " , trigger ) resp ; ;
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 . Respon...
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 := fal...
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 " " :/ ...
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_middlew...
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...
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 |...
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 ...
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 #...
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...