Skip to content

Commit

Permalink
eio(client): add get, post test
Browse files Browse the repository at this point in the history
  • Loading branch information
bikallem committed Jul 25, 2022
1 parent 80bee7c commit becd6ab
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .ocamlformat
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
version=0.24.1
version=0.21.0
profile=conventional
break-infix=fit-or-vertical
parse-docstrings=true
4 changes: 2 additions & 2 deletions cohttp-eio/examples/client1.ml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ open Eio
open Cohttp_eio

let connect_info url =
let uri = Uri.of_string url in
let uri = Uri.of_string url in
let host = Uri.host uri |> Option.get in
let port = Uri.port uri |> Option.value ~default:80 in
let path = Uri.of_string (Uri.path uri) in
Expand All @@ -15,7 +15,7 @@ let connect_info url =
let () =
Eio_main.run @@ fun env ->
Switch.run @@ fun sw ->
let (addr, port, path) = connect_info "http://www.reddit.com/" in
let addr, port, path = connect_info "http://www.reddit.com/" in
let res =
Client.get
~headers:(Http.Header.of_list [ ("Accept", "application/json") ])
Expand Down
14 changes: 12 additions & 2 deletions cohttp-eio/tests/dune
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,22 @@
(modules crlf)
(libraries unix))

(executable
(name test_client)
(modules test_client)
(libraries eio_main uri cohttp-eio))

(env
(_
(binaries
(test_server.exe as test-server)
crlf.exe
(test_chunk_server.exe as test-chunk-server))))
(test_chunk_server.exe as test-chunk-server)
(test_client.exe as test-client))))

(cram
(deps %{bin:test-server} %{bin:test-chunk-server} %{bin:crlf}))
(deps
%{bin:test-server}
%{bin:test-chunk-server}
%{bin:crlf}
%{bin:test-client}))
38 changes: 38 additions & 0 deletions cohttp-eio/tests/test_client.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
open Eio
open Cohttp_eio

let get () =
Eio_main.run @@ fun env ->
Switch.run @@ fun sw ->
let res =
Client.get
~headers:(Http.Header.of_list [ ("Accept", "application/json") ])
env sw
(`Tcp (Eio.Net.Ipaddr.V4.loopback, 8080))
(Uri.of_string "/get")
in
match Client.read_fixed res with Some s -> print_string s | None -> ()

let post () =
Eio_main.run @@ fun env ->
Switch.run @@ fun sw ->
let content = "hello world!" in
let content_length = String.length content |> string_of_int in
let res =
Client.post
~headers:
(Http.Header.of_list
[
("Accept", "application/json"); ("Content-Length", content_length);
])
~body:(Body.Fixed content) env sw
(`Tcp (Eio.Net.Ipaddr.V4.loopback, 8080))
(Uri.of_string "/post")
in
match Client.read_fixed res with Some s -> print_string s | None -> ()

let () =
match Sys.argv.(1) with
| "get" -> get ()
| "post" -> post ()
| _ -> print_string "Usage: test-client [get|post]"
25 changes: 25 additions & 0 deletions cohttp-eio/tests/test_client.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Test Client.get

$ test-server &
$ running_pid=$!
$ test-client get
meth: GET
resource: /get
version: HTTP/1.1
headers: Header { Accept = "application/json" }

$ kill ${running_pid}

Test Client.post

$ test-server &
$ running_pid=$!
$ test-client post
meth: POST
resource: /post
version: HTTP/1.1
headers: Header { Accept = "application/json"; Content-Length = "12" }

hello world!

$ kill ${running_pid}
16 changes: 7 additions & 9 deletions cohttp-eio/tests/test_server.ml
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
open Cohttp_eio

let read_body (req, reader) =
let body = Server.read_fixed (req, reader) |> Option.get in
let buf = Buffer.create 0 in
let fmt = Format.formatter_of_buffer buf in
Http.Request.pp fmt req;
Format.fprintf fmt "\n\n%s%!" body;
Server.text_response (Buffer.contents buf)

let app (req, reader) =
match Http.Request.resource req with
| "/get" ->
Expand All @@ -20,7 +12,13 @@ let app (req, reader) =
match Server.read_fixed (req, reader) with
| Some _ -> Server.text_response "FAIL"
| None -> Server.text_response "PASS")
| "/post" -> read_body (req, reader)
| "/post" ->
let body = Server.read_fixed (req, reader) |> Option.get in
let buf = Buffer.create 0 in
let fmt = Format.formatter_of_buffer buf in
Http.Request.pp fmt req;
Format.fprintf fmt "\n\n%s%!" body;
Server.text_response (Buffer.contents buf)
| _ -> Server.bad_request_response

let () =
Expand Down

0 comments on commit becd6ab

Please sign in to comment.