···1515 { config; plugs = Pipeline.empty; router = None; ws_handler = None }
16161717let plug t p = { t with plugs = Pipeline.plug t.plugs p }
1818+let with_plug p t = plug t p
1919+2020+let plug_pipeline t pipeline =
2121+ { t with plugs = Pipeline.compose t.plugs pipeline }
2222+2323+let with_pipeline pipeline t = plug_pipeline t pipeline
2424+let ( @> ) t p = plug t p
2525+let ( @>> ) t pipeline = plug_pipeline t pipeline
1826let router t r = { t with router = Some r }
1927let websocket t handler = { t with ws_handler = Some handler }
2028let ws_handler t = t.ws_handler
+87
test/test_plug.ml
···14431443 ]
14441444end
1445144514461446+module Test_endpoint_pipeline = struct
14471447+ let make_request target : Hcs.Server.request =
14481448+ {
14491449+ meth = `GET;
14501450+ target;
14511451+ version = Hcs.Server.HTTP_1_1;
14521452+ headers = [];
14531453+ body = "";
14541454+ }
14551455+14561456+ let request_id_plug = Hcs.Plug.Request_id.create ()
14571457+14581458+ let test_with_plug_pipe () =
14591459+ let endpoint =
14601460+ Hcs.Endpoint.create Hcs.Endpoint.default_config |> fun endpoint ->
14611461+ Hcs.Endpoint.with_plug request_id_plug endpoint
14621462+ in
14631463+ let handler = Hcs.Endpoint.to_handler endpoint in
14641464+ let resp = handler (make_request "/") in
14651465+ let has_request_id =
14661466+ List.exists
14671467+ (fun (name, _value) -> String.lowercase_ascii name = "x-request-id")
14681468+ resp.Hcs.Response.headers
14691469+ in
14701470+ Alcotest.check Alcotest.bool "request id header" true has_request_id
14711471+14721472+ let test_endpoint_operator_plug () =
14731473+ let endpoint =
14741474+ Hcs.Endpoint.create Hcs.Endpoint.default_config |> fun endpoint ->
14751475+ Hcs.Endpoint.(endpoint @> request_id_plug)
14761476+ in
14771477+14781478+ let handler = Hcs.Endpoint.to_handler endpoint in
14791479+ let resp = handler (make_request "/") in
14801480+ let has_request_id =
14811481+ List.exists
14821482+ (fun (name, _value) -> String.lowercase_ascii name = "x-request-id")
14831483+ resp.Hcs.Response.headers
14841484+ in
14851485+ Alcotest.check Alcotest.bool "request id header" true has_request_id
14861486+14871487+ let test_with_pipeline_pipe () =
14881488+ let endpoint_pipeline =
14891489+ Hcs.Pipeline.create [ Hcs.Plug.Compress.create (); request_id_plug ]
14901490+ in
14911491+ let endpoint =
14921492+ Hcs.Endpoint.create Hcs.Endpoint.default_config |> fun endpoint ->
14931493+ Hcs.Endpoint.with_pipeline endpoint_pipeline endpoint
14941494+ in
14951495+ let handler = Hcs.Endpoint.to_handler endpoint in
14961496+ let resp = handler (make_request "/") in
14971497+ let has_request_id =
14981498+ List.exists
14991499+ (fun (name, _value) -> String.lowercase_ascii name = "x-request-id")
15001500+ resp.Hcs.Response.headers
15011501+ in
15021502+ Alcotest.check Alcotest.bool "request id header" true has_request_id
15031503+15041504+ let test_endpoint_operator_pipeline () =
15051505+ let endpoint_pipeline = Hcs.Pipeline.create [ request_id_plug ] in
15061506+ let endpoint =
15071507+ Hcs.Endpoint.create Hcs.Endpoint.default_config |> fun endpoint ->
15081508+ Hcs.Endpoint.(endpoint @>> endpoint_pipeline)
15091509+ in
15101510+15111511+ let handler = Hcs.Endpoint.to_handler endpoint in
15121512+ let resp = handler (make_request "/") in
15131513+ let has_request_id =
15141514+ List.exists
15151515+ (fun (name, _value) -> String.lowercase_ascii name = "x-request-id")
15161516+ resp.Hcs.Response.headers
15171517+ in
15181518+ Alcotest.check Alcotest.bool "request id header" true has_request_id
15191519+15201520+ let tests =
15211521+ [
15221522+ Alcotest.test_case "with_plug uses pipeline" `Quick test_with_plug_pipe;
15231523+ Alcotest.test_case "endpoint @> adds plug" `Quick
15241524+ test_endpoint_operator_plug;
15251525+ Alcotest.test_case "with_pipeline attaches pipeline" `Quick
15261526+ test_with_pipeline_pipe;
15271527+ Alcotest.test_case "endpoint @>> adds pipeline" `Quick
15281528+ test_endpoint_operator_pipeline;
15291529+ ]
15301530+end
15311531+14461532module Test_static = struct
14471533 open Hcs.Plug.Static
14481534···15521638 ("Csrf", Test_csrf.tests);
15531639 ("Basic_auth", Test_basic_auth.tests);
15541640 ("Retry", Test_retry.tests env);
16411641+ ("Endpoint_pipeline", Test_endpoint_pipeline.tests);
15551642 ("Static", Test_static.tests);
15561643 ]