diff --git a/examples/dune b/examples/dune index 985480d9..f623f8d0 100644 --- a/examples/dune +++ b/examples/dune @@ -1,10 +1,26 @@ (executable - (name sse_clock) - (modules sse_clock) + (name sse_server) + (modules sse_server) (libraries tiny_httpd unix ptime ptime.clock.os)) (executable (name sse_client) (modules sse_client) (libraries unix)) + +(rule + (targets test_output.txt) + (deps (:script ./run_test.sh) ./sse_client.exe ./sse_server.exe) + (enabled_if (= %{system} "linux")) + (package tiny_httpd) + (action + (with-stdout-to %{targets} (run %{script})))) + +(rule + (alias runtest) + (package tiny_httpd) + (enabled_if (= %{system} "linux")) + (deps test_output.txt) + (action + (diff test_output.txt.expected test_output.txt))) diff --git a/examples/run_test.sh b/examples/run_test.sh new file mode 100755 index 00000000..e522747d --- /dev/null +++ b/examples/run_test.sh @@ -0,0 +1,10 @@ +#!/usr/bin/env bash + +PORT=8082 + +./sse_server.exe -p $PORT & +sleep 0.1 +./sse_client.exe -p $PORT --alarm=1 /count | tr -d '\r' || true + +kill %1 +echo "success" diff --git a/examples/sse_client.ml b/examples/sse_client.ml index 3e7ef6f5..9ba011e4 100644 --- a/examples/sse_client.ml +++ b/examples/sse_client.ml @@ -1,12 +1,15 @@ let addr = ref "127.0.0.1" let port = ref 8080 +let path = ref "/clock" let bufsize = 1024 let () = Arg.parse (Arg.align [ + "-h", Arg.Set_string addr, " address to connect to"; "-p", Arg.Set_int port, " port to connect to"; - ]) (fun s -> addr := s) "sse_client [opt]* [addr]?"; + "--alarm", Arg.Int (fun i->Unix.alarm i|>ignore), " set alarm (in seconds)"; + ]) (fun s -> path := s) "sse_client [opt]* path?"; Format.printf "connect to %s:%d@." !addr !port; let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in @@ -15,7 +18,7 @@ let () = let ic = Unix.in_channel_of_descr sock in let oc = Unix.out_channel_of_descr sock in - output_string oc "GET /clock HTTP/1.1\r\nHost: localhost\r\n\r\n"; + Printf.fprintf oc "GET %s HTTP/1.1\r\nHost: localhost\r\n\r\n" !path; flush oc; let continue = ref true in @@ -25,4 +28,4 @@ let () = if n=0 then continue := false; output stdout buf 0 n; flush stdout done; - Format.printf "bye!@." + Format.printf "exit!@." diff --git a/examples/sse_clock.ml b/examples/sse_server.ml similarity index 75% rename from examples/sse_clock.ml rename to examples/sse_server.ml index 92dfe961..cf30c95e 100644 --- a/examples/sse_clock.ml +++ b/examples/sse_server.ml @@ -1,5 +1,5 @@ -(* serves a stream of clock events *) +(* serves some streams of events *) module S = Tiny_httpd @@ -17,6 +17,7 @@ let () = "Access-Control-Allow-Methods", "POST, GET, OPTIONS"; ] in + (* tick/tock goes the clock *) S.add_route_server_sent_handler server S.Route.(exact "clock" @/ return) (fun _req (module EV : S.SERVER_SENT_GENERATOR) -> S._debug (fun k->k"new connection"); @@ -33,6 +34,17 @@ let () = done; ); + (* just count *) + S.add_route_server_sent_handler server S.Route.(exact "count" @/ return) + (fun _req (module EV : S.SERVER_SENT_GENERATOR) -> + let n = ref 0 in + while true do + EV.send_event ~data:(string_of_int !n) (); + incr n; + Unix.sleepf 0.1; + done; + ); + Printf.printf "listening on http://localhost:%d/\n%!" (S.port server); match S.run server with | Ok () -> () diff --git a/examples/test_output.txt.expected b/examples/test_output.txt.expected new file mode 100644 index 00000000..5adb0103 --- /dev/null +++ b/examples/test_output.txt.expected @@ -0,0 +1,26 @@ +listening on http://localhost:8082/ +connect to 127.0.0.1:8082 +HTTP/1.1 200 OK +content-type: text/event-stream + +data: 0 + +data: 1 + +data: 2 + +data: 3 + +data: 4 + +data: 5 + +data: 6 + +data: 7 + +data: 8 + +data: 9 + +success diff --git a/tiny_httpd.opam b/tiny_httpd.opam index 095ac64c..92d51273 100644 --- a/tiny_httpd.opam +++ b/tiny_httpd.opam @@ -17,6 +17,7 @@ depends: [ "qtest" { >= "2.9" & with-test} "qcheck" {with-test & >= "0.9" } "ounit2" {with-test} + "ptime" {with-test} ] tags: [ "http" "thread" "server" "tiny_httpd" "http_of_dir" "simplehttpserver" ] homepage: "https://github.com/c-cube/tiny_httpd/"