mirror of
https://github.com/c-cube/ezcurl.git
synced 2026-01-28 04:14:51 -05:00
test: use tiny_httpd to not depend on external network
This commit is contained in:
parent
2c756473dd
commit
ab39f4cbdc
5 changed files with 51 additions and 46 deletions
|
|
@ -23,6 +23,7 @@
|
||||||
(ocurl
|
(ocurl
|
||||||
(>= 0.8))
|
(>= 0.8))
|
||||||
(odoc :with-doc)
|
(odoc :with-doc)
|
||||||
|
(tiny_httpd (and (>= 0.19) :with-test))
|
||||||
(ocaml
|
(ocaml
|
||||||
(>= 4.03))))
|
(>= 4.03))))
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,6 +13,7 @@ depends: [
|
||||||
"dune" {>= "3.0"}
|
"dune" {>= "3.0"}
|
||||||
"ocurl" {>= "0.8"}
|
"ocurl" {>= "0.8"}
|
||||||
"odoc" {with-doc}
|
"odoc" {with-doc}
|
||||||
|
"tiny_httpd" {>= "0.19" & with-test}
|
||||||
"ocaml" {>= "4.03"}
|
"ocaml" {>= "4.03"}
|
||||||
]
|
]
|
||||||
build: [
|
build: [
|
||||||
|
|
|
||||||
|
|
@ -1,39 +1,8 @@
|
||||||
get: OK
|
get: OK
|
||||||
body=```
|
code=200,body=```
|
||||||
version = 0.27.0
|
hello jeanjacques
|
||||||
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
|
|
||||||
|
|
||||||
```
|
```
|
||||||
streaming get: OK
|
streaming get: OK
|
||||||
body=```
|
code=200, body=```
|
||||||
version = 0.27.0
|
hello reineclaude
|
||||||
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
|
|
||||||
|
|
||||||
```
|
```
|
||||||
same buf? true
|
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,44 @@
|
||||||
let body = ref ""
|
module H = Tiny_httpd
|
||||||
|
|
||||||
let url =
|
(** Start server, return its port and a thread *)
|
||||||
"https://raw.githubusercontent.com/c-cube/ezcurl/refs/heads/main/.ocamlformat"
|
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
|
match Ezcurl.get ~url () with
|
||||||
| Error (code, msg) ->
|
| Error (code, msg) ->
|
||||||
Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg
|
Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg
|
||||||
| Ok res ->
|
| Ok res ->
|
||||||
body := res.body;
|
Format.printf "get: OK@.code=%d,body=```@.%s@.```@." res.code res.body
|
||||||
Format.printf "get: OK@.body=```@.%s@.```@." !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
|
let buf = Buffer.create 32 in
|
||||||
match
|
match
|
||||||
Ezcurl.http_stream ~meth:GET ~url
|
Ezcurl.http_stream ~meth:GET ~url
|
||||||
|
|
@ -24,7 +51,13 @@ let () =
|
||||||
with
|
with
|
||||||
| Error (code, msg) ->
|
| Error (code, msg) ->
|
||||||
Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg
|
Format.eprintf "curl error: code `%s` (%s)@." (Curl.strerror code) msg
|
||||||
| Ok _res ->
|
| Ok res ->
|
||||||
let new_body = Buffer.contents buf in
|
let new_body = Buffer.contents buf in
|
||||||
Format.printf "streaming get: OK@.body=```@.%s@.```@." new_body;
|
Format.printf "streaming get: OK@.code=%d, body=```@.%s@.```@." res.code
|
||||||
Format.printf "same buf? %b@." (new_body = !body)
|
new_body
|
||||||
|
|
||||||
|
let () =
|
||||||
|
let port, _th = start_server () in
|
||||||
|
test1 ~port ();
|
||||||
|
test2 ~port ();
|
||||||
|
exit 0
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
(test
|
(test
|
||||||
(name basic_test)
|
(name basic_test)
|
||||||
(libraries ezcurl))
|
(package ezcurl)
|
||||||
|
(libraries ezcurl tiny_httpd threads.posix))
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue