Skip to content

Commit ec15b02

Browse files
committed
update test cases
1 parent 67b77d5 commit ec15b02

File tree

3 files changed

+87
-24
lines changed

3 files changed

+87
-24
lines changed

zarr-eio/test/test_eio.ml

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,19 @@ let _ =
152152
test_storage (module FilesystemStore) s;
153153
HttpStore.with_open ~net:env#net (Uri.of_string "http://127.0.0.1:8080") (fun store ->
154154
let module S = Tiny_httpd in
155-
let server = S.create ~max_connections:1000 ~addr:"127.0.0.1" ~port:8080 () in
155+
let server = S.create ~max_connections:100 ~addr:"127.0.0.1" ~port:8080 () in
156156
let dir = tmp_dir in
157157
S.add_route_handler server ~meth:`HEAD S.Route.rest_of_path_urlencoded (fun path _ ->
158158
let fspath = Filename.concat dir path in
159159
match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath length) with
160160
| exception Sys_error e -> S.Response.make_raw ~code:404 e
161-
| s ->
161+
| l ->
162162
let headers =
163-
[("Content-Length", Int64.to_string s)
164-
;("Content-Type", "application/octet-stream")]
163+
[("Content-Length", Int64.to_string l)
164+
;("Content-Type",
165+
if String.ends_with ~suffix:".json" path
166+
then "application/json"
167+
else "application/octet-stream")]
165168
in
166169
let r = S.Response.make_raw ~code:200 "" in
167170
S.Response.update_headers (List.append headers) r
@@ -170,22 +173,41 @@ let _ =
170173
let fspath = Filename.concat dir path in
171174
match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath input_all) with
172175
| exception Sys_error _ -> S.Response.make_raw ~code:404 (Printf.sprintf "%s not found" path)
173-
| s -> S.Response.make_raw ~code:200 s
176+
| s ->
177+
let headers =
178+
[("Content-Length", Int.to_string (String.length s))
179+
;("Content-Type",
180+
if String.ends_with ~suffix:".json" path
181+
then "application/json"
182+
else "application/octet-stream")]
183+
in
184+
S.Response.make_raw ~headers ~code:200 s
174185
);
175-
S.add_route_handler server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req ->
176-
let write oc = Out_channel.(output_string oc req.body; flush oc) in
186+
S.add_route_handler_stream server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req ->
187+
let write oc =
188+
let max_size = 1024 * 10 * 1024 in
189+
let req' = S.Request.limit_body_size ~bytes:(Bytes.create 4096) ~max_size req in
190+
S.IO.Input.iter (Out_channel.output oc) req'.body;
191+
Out_channel.flush oc
192+
in
177193
let fspath = Filename.concat dir path in
178194
Zarr.Util.create_parent_dir fspath 0o700;
179195
let f = [Open_wronly; Open_trunc; Open_creat] in
180196
match Out_channel.(with_open_gen f 0o700 fspath write) with
181197
| exception Sys_error e -> S.Response.make_raw ~code:500 e
182-
| () -> S.Response.make_raw ~code:201 req.body
198+
| () ->
199+
let opt = List.assoc_opt "content-type" req.headers in
200+
let content_type = Option.fold ~none:"application/octet-stream" ~some:Fun.id opt in
201+
let headers = [("content-type", content_type); ("Connection", "close")] in
202+
S.Response.make_raw ~headers ~code:201 (Printf.sprintf "%s created" path)
183203
);
184204
S.add_route_handler server ~meth:`DELETE S.Route.rest_of_path_urlencoded (fun path _ ->
185205
let fspath = Filename.concat dir path in
186206
match Sys.remove fspath with
187207
| exception Sys_error e -> S.Response.make_raw ~code:404 e
188-
| () -> S.Response.make_raw ~code:200 (Printf.sprintf "%s deleted successfully" path)
208+
| () ->
209+
let headers = [("Connection", "close")] in
210+
S.Response.make_raw ~headers ~code:200 (Printf.sprintf "%s deleted successfully" path)
189211
);
190212
let _ = Thread.create S.run server in
191213
let gnode = Node.Group.root in

zarr-lwt/test/test_lwt.ml

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ let _ =
163163
let fspath = Filename.concat dir path in
164164
match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath length) with
165165
| exception Sys_error e -> S.Response.make_raw ~code:404 e
166-
| s ->
166+
| l ->
167167
let headers =
168-
[("Content-Length", Int64.to_string s)
168+
[("Content-Length", Int64.to_string l)
169169
;("Content-Type",
170170
if String.ends_with ~suffix:".json" path
171171
then "application/json"
@@ -178,22 +178,41 @@ let _ =
178178
let fspath = Filename.concat dir path in
179179
match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath input_all) with
180180
| exception Sys_error _ -> S.Response.make_raw ~code:404 (Printf.sprintf "%s not found" path)
181-
| s -> S.Response.make_raw ~code:200 s
181+
| s ->
182+
let headers =
183+
[("Content-Length", Int.to_string (String.length s))
184+
;("Content-Type",
185+
if String.ends_with ~suffix:".json" path
186+
then "application/json"
187+
else "application/octet-stream")]
188+
in
189+
S.Response.make_raw ~headers ~code:200 s
182190
);
183-
S.add_route_handler server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req ->
184-
let write oc = Out_channel.(output_string oc req.body; flush oc) in
191+
S.add_route_handler_stream server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req ->
192+
let write oc =
193+
let max_size = 1024 * 10 * 1024 in
194+
let req' = S.Request.limit_body_size ~bytes:(Bytes.create 4096) ~max_size req in
195+
S.IO.Input.iter (Out_channel.output oc) req'.body;
196+
Out_channel.flush oc
197+
in
185198
let fspath = Filename.concat dir path in
186199
Zarr.Util.create_parent_dir fspath 0o700;
187200
let f = [Open_wronly; Open_trunc; Open_creat] in
188201
match Out_channel.(with_open_gen f 0o700 fspath write) with
189202
| exception Sys_error e -> S.Response.make_raw ~code:500 e
190-
| () -> S.Response.make_raw ~code:201 req.body
203+
| () ->
204+
let opt = List.assoc_opt "content-type" req.headers in
205+
let content_type = Option.fold ~none:"application/octet-stream" ~some:Fun.id opt in
206+
let headers = [("content-type", content_type); ("Connection", "close")] in
207+
S.Response.make_raw ~headers ~code:201 (Printf.sprintf "%s created" path)
191208
);
192209
S.add_route_handler server ~meth:`DELETE S.Route.rest_of_path_urlencoded (fun path _ ->
193210
let fspath = Filename.concat dir path in
194211
match Sys.remove fspath with
195212
| exception Sys_error e -> S.Response.make_raw ~code:404 e
196-
| () -> S.Response.make_raw ~code:200 (Printf.sprintf "%s deleted successfully" path)
213+
| () ->
214+
let headers = [("Connection", "close")] in
215+
S.Response.make_raw ~headers ~code:200 (Printf.sprintf "%s deleted successfully" path)
197216
);
198217
let _ = Thread.create S.run server in
199218

zarr-sync/test/test_sync.ml

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -218,10 +218,13 @@ let _ =
218218
let fspath = Filename.concat dir path in
219219
match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath length) with
220220
| exception Sys_error e -> S.Response.make_raw ~code:404 e
221-
| s ->
221+
| l ->
222222
let headers =
223-
[("Content-Length", Int64.to_string s)
224-
;("Content-Type", "application/octet-stream")]
223+
[("Content-Length", Int64.to_string l)
224+
;("Content-Type",
225+
if String.ends_with ~suffix:".json" path
226+
then "application/json"
227+
else "application/octet-stream")]
225228
in
226229
let r = S.Response.make_raw ~code:200 "" in
227230
S.Response.update_headers (List.append headers) r
@@ -230,22 +233,41 @@ let _ =
230233
let fspath = Filename.concat dir path in
231234
match In_channel.(with_open_gen [Open_rdonly] 0o700 fspath input_all) with
232235
| exception Sys_error _ -> S.Response.make_raw ~code:404 (Printf.sprintf "%s not found" path)
233-
| s -> S.Response.make_raw ~code:200 s
236+
| s ->
237+
let headers =
238+
[("Content-Length", Int.to_string (String.length s))
239+
;("Content-Type",
240+
if String.ends_with ~suffix:".json" path
241+
then "application/json"
242+
else "application/octet-stream")]
243+
in
244+
S.Response.make_raw ~headers ~code:200 s
234245
);
235-
S.add_route_handler server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req ->
236-
let write oc = Out_channel.(output_string oc req.body; flush oc) in
246+
S.add_route_handler_stream server ~meth:`POST S.Route.rest_of_path_urlencoded (fun path req ->
247+
let write oc =
248+
let max_size = 1024 * 10 * 1024 in
249+
let req' = S.Request.limit_body_size ~bytes:(Bytes.create 4096) ~max_size req in
250+
S.IO.Input.iter (Out_channel.output oc) req'.body;
251+
Out_channel.flush oc
252+
in
237253
let fspath = Filename.concat dir path in
238254
Zarr.Util.create_parent_dir fspath 0o700;
239255
let f = [Open_wronly; Open_trunc; Open_creat] in
240256
match Out_channel.(with_open_gen f 0o700 fspath write) with
241257
| exception Sys_error e -> S.Response.make_raw ~code:500 e
242-
| () -> S.Response.make_raw ~code:201 req.body
258+
| () ->
259+
let opt = List.assoc_opt "content-type" req.headers in
260+
let content_type = Option.fold ~none:"application/octet-stream" ~some:Fun.id opt in
261+
let headers = [("content-type", content_type); ("Connection", "close")] in
262+
S.Response.make_raw ~headers ~code:201 (Printf.sprintf "%s created" path)
243263
);
244264
S.add_route_handler server ~meth:`DELETE S.Route.rest_of_path_urlencoded (fun path _ ->
245265
let fspath = Filename.concat dir path in
246266
match Sys.remove fspath with
247267
| exception Sys_error e -> S.Response.make_raw ~code:404 e
248-
| () -> S.Response.make_raw ~code:200 (Printf.sprintf "%s deleted successfully" path)
268+
| () ->
269+
let headers = [("Connection", "close")] in
270+
S.Response.make_raw ~headers ~code:200 (Printf.sprintf "%s deleted successfully" path)
249271
);
250272
let _ = Thread.create S.run server in
251273
let gnode = Node.Group.root in

0 commit comments

Comments
 (0)