From 012016f9b40f652db5f429ee3e4b5dd6f4406976 Mon Sep 17 00:00:00 2001 From: Simon Cruanes Date: Thu, 3 Oct 2024 10:01:37 -0400 Subject: [PATCH] test: add a test for `http_stream` --- Makefile | 3 +++ README.md | 2 +- test/basic_test.expected | 39 +++++++++++++++++++++++++++++++++++++++ test/basic_test.ml | 28 ++++++++++++++++++++++++---- 4 files changed, 67 insertions(+), 5 deletions(-) create mode 100644 test/basic_test.expected diff --git a/Makefile b/Makefile index 1e9dc66..f60c62e 100644 --- a/Makefile +++ b/Makefile @@ -7,6 +7,9 @@ build: test: @dune runtest --no-buffer --force +test-autopromote: + @dune runtest --no-buffer --force --auto-promote + clean: @dune clean diff --git a/README.md b/README.md index bd64394..0964408 100644 --- a/README.md +++ b/README.md @@ -33,7 +33,7 @@ val url : string = "https://curl.haxx.se/" # let res = Ezcurl.get ~url ();; ... # 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;; diff --git a/test/basic_test.expected b/test/basic_test.expected new file mode 100644 index 0000000..890dbfc --- /dev/null +++ b/test/basic_test.expected @@ -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 diff --git a/test/basic_test.ml b/test/basic_test.ml index 2a060af..1c0a1e0 100644 --- a/test/basic_test.ml +++ b/test/basic_test.ml @@ -1,10 +1,30 @@ +let body = ref "" + +let url = + "https://raw.githubusercontent.com/c-cube/ezcurl/refs/heads/main/.ocamlformat" + 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 - Ezcurl.get - ~url: - "https://archive.softwareheritage.org/api/1/content/sha1_git:7bdf38d4468c114206c9b6ebd9cf1176e085d346/" + Ezcurl.http_stream ~meth:GET ~url + ~write_into: + (object + method on_input bs i len = Buffer.add_subbytes buf bs i len + method on_close () = () + end) () with | Error (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)