test: use tiny_httpd to not depend on external network

This commit is contained in:
Simon Cruanes 2026-01-21 20:43:48 -05:00
parent 2c756473dd
commit ab39f4cbdc
No known key found for this signature in database
GPG key ID: EBFFF6F283F3A2B4
5 changed files with 51 additions and 46 deletions

View file

@ -23,6 +23,7 @@
(ocurl
(>= 0.8))
(odoc :with-doc)
(tiny_httpd (and (>= 0.19) :with-test))
(ocaml
(>= 4.03))))

View file

@ -13,6 +13,7 @@ depends: [
"dune" {>= "3.0"}
"ocurl" {>= "0.8"}
"odoc" {with-doc}
"tiny_httpd" {>= "0.19" & with-test}
"ocaml" {>= "4.03"}
]
build: [

View file

@ -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

View file

@ -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

View file

@ -1,3 +1,4 @@
(test
(name basic_test)
(libraries ezcurl))
(package ezcurl)
(libraries ezcurl tiny_httpd threads.posix))