From ec15b02a69d5bdcb6867d442a653bba4aa27a8a3 Mon Sep 17 00:00:00 2001 From: Zolisa Bleki Date: Sat, 30 Nov 2024 18:48:11 +0200 Subject: [PATCH] update test cases --- zarr-eio/test/test_eio.ml | 40 ++++++++++++++++++++++++++++--------- zarr-lwt/test/test_lwt.ml | 33 +++++++++++++++++++++++------- zarr-sync/test/test_sync.ml | 38 +++++++++++++++++++++++++++-------- 3 files changed, 87 insertions(+), 24 deletions(-) diff --git a/zarr-eio/test/test_eio.ml b/zarr-eio/test/test_eio.ml index 2e2e979..6f20032 100644 --- a/zarr-eio/test/test_eio.ml +++ b/zarr-eio/test/test_eio.ml @@ -152,16 +152,19 @@ let _ = test_storage (module FilesystemStore) s; HttpStore.with_open ~net:env#net (Uri.of_string "http://127.0.0.1:8080") (fun store -> let module S = Tiny_httpd in - let server = S.create ~max_connections:1000 ~addr:"127.0.0.1" ~port:8080 () in + let server = S.create ~max_connections:100 ~addr:"127.0.0.1" ~port:8080 () in let dir = tmp_dir in S.add_route_handler server ~meth:`HEAD S.Route.rest_of_path_urlencoded (fun path _ -> let fspath = Filename.concat dir path in match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath length) with | exception Sys_error e -> S.Response.make_raw ~code:404 e - | s -> + | l -> let headers = - [("Content-Length", Int64.to_string s) - ;("Content-Type", "application/octet-stream")] + [("Content-Length", Int64.to_string l) + ;("Content-Type", + if String.ends_with ~suffix:".json" path + then "application/json" + else "application/octet-stream")] in let r = S.Response.make_raw ~code:200 "" in S.Response.update_headers (List.append headers) r @@ -170,22 +173,41 @@ let _ = let fspath = Filename.concat dir path in match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath input_all) with | exception Sys_error _ -> S.Response.make_raw ~code:404 (Printf.sprintf "%s not found" path) - | s -> S.Response.make_raw ~code:200 s + | s -> + let headers = + [("Content-Length", Int.to_string (String.length s)) + ;("Content-Type", + if String.ends_with ~suffix:".json" path + then "application/json" + else "application/octet-stream")] + in + S.Response.make_raw ~headers ~code:200 s ); - S.add_route_handler server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req -> - let write oc = Out_channel.(output_string oc req.body; flush oc) in + S.add_route_handler_stream server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req -> + let write oc = + let max_size = 1024 * 10 * 1024 in + let req' = S.Request.limit_body_size ~bytes:(Bytes.create 4096) ~max_size req in + S.IO.Input.iter (Out_channel.output oc) req'.body; + Out_channel.flush oc + in let fspath = Filename.concat dir path in Zarr.Util.create_parent_dir fspath 0o700; let f = [Open_wronly; Open_trunc; Open_creat] in match Out_channel.(with_open_gen f 0o700 fspath write) with | exception Sys_error e -> S.Response.make_raw ~code:500 e - | () -> S.Response.make_raw ~code:201 req.body + | () -> + let opt = List.assoc_opt "content-type" req.headers in + let content_type = Option.fold ~none:"application/octet-stream" ~some:Fun.id opt in + let headers = [("content-type", content_type); ("Connection", "close")] in + S.Response.make_raw ~headers ~code:201 (Printf.sprintf "%s created" path) ); S.add_route_handler server ~meth:`DELETE S.Route.rest_of_path_urlencoded (fun path _ -> let fspath = Filename.concat dir path in match Sys.remove fspath with | exception Sys_error e -> S.Response.make_raw ~code:404 e - | () -> S.Response.make_raw ~code:200 (Printf.sprintf "%s deleted successfully" path) + | () -> + let headers = [("Connection", "close")] in + S.Response.make_raw ~headers ~code:200 (Printf.sprintf "%s deleted successfully" path) ); let _ = Thread.create S.run server in let gnode = Node.Group.root in diff --git a/zarr-lwt/test/test_lwt.ml b/zarr-lwt/test/test_lwt.ml index 7e696cf..c62a3ef 100644 --- a/zarr-lwt/test/test_lwt.ml +++ b/zarr-lwt/test/test_lwt.ml @@ -163,9 +163,9 @@ let _ = let fspath = Filename.concat dir path in match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath length) with | exception Sys_error e -> S.Response.make_raw ~code:404 e - | s -> + | l -> let headers = - [("Content-Length", Int64.to_string s) + [("Content-Length", Int64.to_string l) ;("Content-Type", if String.ends_with ~suffix:".json" path then "application/json" @@ -178,22 +178,41 @@ let _ = let fspath = Filename.concat dir path in match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath input_all) with | exception Sys_error _ -> S.Response.make_raw ~code:404 (Printf.sprintf "%s not found" path) - | s -> S.Response.make_raw ~code:200 s + | s -> + let headers = + [("Content-Length", Int.to_string (String.length s)) + ;("Content-Type", + if String.ends_with ~suffix:".json" path + then "application/json" + else "application/octet-stream")] + in + S.Response.make_raw ~headers ~code:200 s ); - S.add_route_handler server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req -> - let write oc = Out_channel.(output_string oc req.body; flush oc) in + S.add_route_handler_stream server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req -> + let write oc = + let max_size = 1024 * 10 * 1024 in + let req' = S.Request.limit_body_size ~bytes:(Bytes.create 4096) ~max_size req in + S.IO.Input.iter (Out_channel.output oc) req'.body; + Out_channel.flush oc + in let fspath = Filename.concat dir path in Zarr.Util.create_parent_dir fspath 0o700; let f = [Open_wronly; Open_trunc; Open_creat] in match Out_channel.(with_open_gen f 0o700 fspath write) with | exception Sys_error e -> S.Response.make_raw ~code:500 e - | () -> S.Response.make_raw ~code:201 req.body + | () -> + let opt = List.assoc_opt "content-type" req.headers in + let content_type = Option.fold ~none:"application/octet-stream" ~some:Fun.id opt in + let headers = [("content-type", content_type); ("Connection", "close")] in + S.Response.make_raw ~headers ~code:201 (Printf.sprintf "%s created" path) ); S.add_route_handler server ~meth:`DELETE S.Route.rest_of_path_urlencoded (fun path _ -> let fspath = Filename.concat dir path in match Sys.remove fspath with | exception Sys_error e -> S.Response.make_raw ~code:404 e - | () -> S.Response.make_raw ~code:200 (Printf.sprintf "%s deleted successfully" path) + | () -> + let headers = [("Connection", "close")] in + S.Response.make_raw ~headers ~code:200 (Printf.sprintf "%s deleted successfully" path) ); let _ = Thread.create S.run server in diff --git a/zarr-sync/test/test_sync.ml b/zarr-sync/test/test_sync.ml index 35823c8..d6544c9 100644 --- a/zarr-sync/test/test_sync.ml +++ b/zarr-sync/test/test_sync.ml @@ -218,10 +218,13 @@ let _ = let fspath = Filename.concat dir path in match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath length) with | exception Sys_error e -> S.Response.make_raw ~code:404 e - | s -> + | l -> let headers = - [("Content-Length", Int64.to_string s) - ;("Content-Type", "application/octet-stream")] + [("Content-Length", Int64.to_string l) + ;("Content-Type", + if String.ends_with ~suffix:".json" path + then "application/json" + else "application/octet-stream")] in let r = S.Response.make_raw ~code:200 "" in S.Response.update_headers (List.append headers) r @@ -230,22 +233,41 @@ let _ = let fspath = Filename.concat dir path in match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath input_all) with | exception Sys_error _ -> S.Response.make_raw ~code:404 (Printf.sprintf "%s not found" path) - | s -> S.Response.make_raw ~code:200 s + | s -> + let headers = + [("Content-Length", Int.to_string (String.length s)) + ;("Content-Type", + if String.ends_with ~suffix:".json" path + then "application/json" + else "application/octet-stream")] + in + S.Response.make_raw ~headers ~code:200 s ); - S.add_route_handler server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req -> - let write oc = Out_channel.(output_string oc req.body; flush oc) in + S.add_route_handler_stream server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req -> + let write oc = + let max_size = 1024 * 10 * 1024 in + let req' = S.Request.limit_body_size ~bytes:(Bytes.create 4096) ~max_size req in + S.IO.Input.iter (Out_channel.output oc) req'.body; + Out_channel.flush oc + in let fspath = Filename.concat dir path in Zarr.Util.create_parent_dir fspath 0o700; let f = [Open_wronly; Open_trunc; Open_creat] in match Out_channel.(with_open_gen f 0o700 fspath write) with | exception Sys_error e -> S.Response.make_raw ~code:500 e - | () -> S.Response.make_raw ~code:201 req.body + | () -> + let opt = List.assoc_opt "content-type" req.headers in + let content_type = Option.fold ~none:"application/octet-stream" ~some:Fun.id opt in + let headers = [("content-type", content_type); ("Connection", "close")] in + S.Response.make_raw ~headers ~code:201 (Printf.sprintf "%s created" path) ); S.add_route_handler server ~meth:`DELETE S.Route.rest_of_path_urlencoded (fun path _ -> let fspath = Filename.concat dir path in match Sys.remove fspath with | exception Sys_error e -> S.Response.make_raw ~code:404 e - | () -> S.Response.make_raw ~code:200 (Printf.sprintf "%s deleted successfully" path) + | () -> + let headers = [("Connection", "close")] in + S.Response.make_raw ~headers ~code:200 (Printf.sprintf "%s deleted successfully" path) ); let _ = Thread.create S.run server in let gnode = Node.Group.root in