From ab39f4cbdcbafd6db5f53fffdc0e30a9d32d4f6c Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Wed, 21 Jan 2026 20:43:48 -0500 Subject: [PATCH] test: use tiny_httpd to not depend on external network --- dune-project | 1 + ezcurl.opam | 1 + test/basic_test.expected | 39 +++-------------------------- test/basic_test.ml | 53 ++++++++++++++++++++++++++++++++-------- test/dune | 3 ++- 5 files changed, 51 insertions(+), 46 deletions(-) diff --git a/dune-project b/dune-project index f9b0352..967728c 100644 --- a/dune-project +++ b/dune-project @@ -23,6 +23,7 @@ (ocurl (>= 0.8)) (odoc :with-doc) + (tiny_httpd (and (>= 0.19) :with-test)) (ocaml (>= 4.03)))) diff --git a/ezcurl.opam b/ezcurl.opam index 666ea8b..537554e 100644 --- a/ezcurl.opam +++ b/ezcurl.opam @@ -13,6 +13,7 @@ depends: [ "dune" {>= "3.0"} "ocurl" {>= "0.8"} "odoc" {with-doc} + "tiny_httpd" {>= "0.19" & with-test} "ocaml" {>= "4.03"} ] build: [ diff --git a/test/basic_test.expected b/test/basic_test.expected index febe820..844d2ea 100644 --- a/test/basic_test.expected +++ b/test/basic_test.expected @@ -1,39 +1,8 @@ get: OK -body=``` -version = 0.27.0 -profile=conventional -margin=80 -if-then-else=k-r -parens-ite=true -parens-tuple=multi-line-only -sequence-style=terminator -type-decl=sparse -break-cases=toplevel -cases-exp-indent=2 -field-space=tight-decl -leading-nested-match-parens=true -module-item-spacing=compact -quiet=true -ocaml-version=4.08.0 - +code=200,body=``` +hello jeanjacques ``` streaming get: OK -body=``` -version = 0.27.0 -profile=conventional -margin=80 -if-then-else=k-r -parens-ite=true -parens-tuple=multi-line-only -sequence-style=terminator -type-decl=sparse -break-cases=toplevel -cases-exp-indent=2 -field-space=tight-decl -leading-nested-match-parens=true -module-item-spacing=compact -quiet=true -ocaml-version=4.08.0 - +code=200, body=``` +hello reineclaude ``` -same buf? true diff --git a/test/basic_test.ml b/test/basic_test.ml index 1c0a1e0..e32609b 100644 --- a/test/basic_test.ml +++ b/test/basic_test.ml @@ -1,17 +1,44 @@ -let body = ref "" +module H = Tiny_httpd -let url = - "https://raw.githubusercontent.com/c-cube/ezcurl/refs/heads/main/.ocamlformat" +(** Start server, return its port and a thread *) +let start_server () : int * Thread.t = + let port = ref (-1) in + let cond = Condition.create () in + let mutex = Mutex.create () in -let () = + let server = H.create ~masksigpipe:true ~addr:"127.0.0.1" ~port:0 () in + H.add_route_handler server + H.Route.(exact "test" @/ string @/ return) + (fun str _req -> + H.Response.make_string ~code:200 @@ Ok (Printf.sprintf "hello %s" str)); + let th = + Thread.create + (H.run_exn ~after_init:(fun () -> + port := H.port server; + Condition.broadcast cond)) + server + in + + (* wait for server to start *) + while !port < 0 do + Mutex.lock mutex; + Condition.wait cond mutex; + Mutex.unlock mutex + done; + !port, th + +let test1 ~port () = + let name = "jeanjacques" in + let url = Printf.sprintf "http://127.0.0.1:%d/test/%s" port name in match Ezcurl.get ~url () with | Error (code, msg) -> Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg | Ok res -> - body := res.body; - Format.printf "get: OK@.body=```@.%s@.```@." !body + Format.printf "get: OK@.code=%d,body=```@.%s@.```@." res.code res.body -let () = +let test2 ~port () = + let name = "reineclaude" in + let url = Printf.sprintf "http://127.0.0.1:%d/test/%s" port name in let buf = Buffer.create 32 in match Ezcurl.http_stream ~meth:GET ~url @@ -24,7 +51,13 @@ let () = with | Error (code, msg) -> Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg - | Ok _res -> + | Ok res -> let new_body = Buffer.contents buf in - Format.printf "streaming get: OK@.body=```@.%s@.```@." new_body; - Format.printf "same buf? %b@." (new_body = !body) + Format.printf "streaming get: OK@.code=%d, body=```@.%s@.```@." res.code + new_body + +let () = + let port, _th = start_server () in + test1 ~port (); + test2 ~port (); + exit 0 diff --git a/test/dune b/test/dune index 9d50827..917693d 100644 --- a/test/dune +++ b/test/dune @@ -1,3 +1,4 @@ (test (name basic_test) - (libraries ezcurl)) + (package ezcurl) + (libraries ezcurl tiny_httpd threads.posix))