mirror of
https://github.com/c-cube/ezcurl.git
synced 2025-12-06 03:05:31 -05:00
test: add a test for http_stream
This commit is contained in:
parent
49b265ce56
commit
012016f9b4
4 changed files with 67 additions and 5 deletions
3
Makefile
3
Makefile
|
|
@ -7,6 +7,9 @@ build:
|
||||||
test:
|
test:
|
||||||
@dune runtest --no-buffer --force
|
@dune runtest --no-buffer --force
|
||||||
|
|
||||||
|
test-autopromote:
|
||||||
|
@dune runtest --no-buffer --force --auto-promote
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@dune clean
|
@dune clean
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,7 @@ val url : string = "https://curl.haxx.se/"
|
||||||
# let res = Ezcurl.get ~url ();;
|
# let res = Ezcurl.get ~url ();;
|
||||||
...
|
...
|
||||||
# let content = match res with Ok c -> c | Error (_,s) -> failwith s;;
|
# let content = match res with Ok c -> c | Error (_,s) -> failwith s;;
|
||||||
val content : Ezcurl_core.response =
|
val content : string Ezcurl_core.response =
|
||||||
...
|
...
|
||||||
|
|
||||||
# content.Ezcurl.code;;
|
# content.Ezcurl.code;;
|
||||||
|
|
|
||||||
39
test/basic_test.expected
Normal file
39
test/basic_test.expected
Normal file
|
|
@ -0,0 +1,39 @@
|
||||||
|
get: OK
|
||||||
|
body=```
|
||||||
|
version = 0.26.2
|
||||||
|
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
|
||||||
|
body=```
|
||||||
|
version = 0.26.2
|
||||||
|
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,10 +1,30 @@
|
||||||
|
let body = ref ""
|
||||||
|
|
||||||
|
let url =
|
||||||
|
"https://raw.githubusercontent.com/c-cube/ezcurl/refs/heads/main/.ocamlformat"
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
|
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
|
||||||
|
|
||||||
|
let () =
|
||||||
|
let buf = Buffer.create 32 in
|
||||||
match
|
match
|
||||||
Ezcurl.get
|
Ezcurl.http_stream ~meth:GET ~url
|
||||||
~url:
|
~write_into:
|
||||||
"https://archive.softwareheritage.org/api/1/content/sha1_git:7bdf38d4468c114206c9b6ebd9cf1176e085d346/"
|
(object
|
||||||
|
method on_input bs i len = Buffer.add_subbytes buf bs i len
|
||||||
|
method on_close () = ()
|
||||||
|
end)
|
||||||
()
|
()
|
||||||
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 _response -> Format.printf "OK@."
|
| 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)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue