Skip to content

Commit

Permalink
update test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
zoj613 committed Nov 30, 2024
1 parent 67b77d5 commit ec15b02
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 24 deletions.
40 changes: 31 additions & 9 deletions zarr-eio/test/test_eio.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
33 changes: 26 additions & 7 deletions zarr-lwt/test/test_lwt.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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

Expand Down
38 changes: 30 additions & 8 deletions zarr-sync/test/test_sync.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down

0 comments on commit ec15b02

Please sign in to comment.