mirror of
https://github.com/c-cube/tiny_httpd.git
synced 2025-12-05 19:00:32 -05:00
test: expect test for server-send events (linux only)
This commit is contained in:
parent
95d9bd10ef
commit
b292664b2e
6 changed files with 74 additions and 6 deletions
|
|
@ -1,10 +1,26 @@
|
||||||
|
|
||||||
(executable
|
(executable
|
||||||
(name sse_clock)
|
(name sse_server)
|
||||||
(modules sse_clock)
|
(modules sse_server)
|
||||||
(libraries tiny_httpd unix ptime ptime.clock.os))
|
(libraries tiny_httpd unix ptime ptime.clock.os))
|
||||||
|
|
||||||
(executable
|
(executable
|
||||||
(name sse_client)
|
(name sse_client)
|
||||||
(modules sse_client)
|
(modules sse_client)
|
||||||
(libraries unix))
|
(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)))
|
||||||
|
|
|
||||||
10
examples/run_test.sh
Executable file
10
examples/run_test.sh
Executable file
|
|
@ -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"
|
||||||
|
|
@ -1,12 +1,15 @@
|
||||||
let addr = ref "127.0.0.1"
|
let addr = ref "127.0.0.1"
|
||||||
let port = ref 8080
|
let port = ref 8080
|
||||||
|
let path = ref "/clock"
|
||||||
|
|
||||||
let bufsize = 1024
|
let bufsize = 1024
|
||||||
|
|
||||||
let () =
|
let () =
|
||||||
Arg.parse (Arg.align [
|
Arg.parse (Arg.align [
|
||||||
|
"-h", Arg.Set_string addr, " address to connect to";
|
||||||
"-p", Arg.Set_int port, " port 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;
|
Format.printf "connect to %s:%d@." !addr !port;
|
||||||
let sock = Unix.socket Unix.PF_INET Unix.SOCK_STREAM 0 in
|
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 ic = Unix.in_channel_of_descr sock in
|
||||||
let oc = Unix.out_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;
|
flush oc;
|
||||||
|
|
||||||
let continue = ref true in
|
let continue = ref true in
|
||||||
|
|
@ -25,4 +28,4 @@ let () =
|
||||||
if n=0 then continue := false;
|
if n=0 then continue := false;
|
||||||
output stdout buf 0 n; flush stdout
|
output stdout buf 0 n; flush stdout
|
||||||
done;
|
done;
|
||||||
Format.printf "bye!@."
|
Format.printf "exit!@."
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
|
|
||||||
(* serves a stream of clock events *)
|
(* serves some streams of events *)
|
||||||
|
|
||||||
module S = Tiny_httpd
|
module S = Tiny_httpd
|
||||||
|
|
||||||
|
|
@ -17,6 +17,7 @@ let () =
|
||||||
"Access-Control-Allow-Methods", "POST, GET, OPTIONS";
|
"Access-Control-Allow-Methods", "POST, GET, OPTIONS";
|
||||||
] in
|
] in
|
||||||
|
|
||||||
|
(* tick/tock goes the clock *)
|
||||||
S.add_route_server_sent_handler server S.Route.(exact "clock" @/ return)
|
S.add_route_server_sent_handler server S.Route.(exact "clock" @/ return)
|
||||||
(fun _req (module EV : S.SERVER_SENT_GENERATOR) ->
|
(fun _req (module EV : S.SERVER_SENT_GENERATOR) ->
|
||||||
S._debug (fun k->k"new connection");
|
S._debug (fun k->k"new connection");
|
||||||
|
|
@ -33,6 +34,17 @@ let () =
|
||||||
done;
|
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);
|
Printf.printf "listening on http://localhost:%d/\n%!" (S.port server);
|
||||||
match S.run server with
|
match S.run server with
|
||||||
| Ok () -> ()
|
| Ok () -> ()
|
||||||
26
examples/test_output.txt.expected
Normal file
26
examples/test_output.txt.expected
Normal file
|
|
@ -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
|
||||||
|
|
@ -17,6 +17,7 @@ depends: [
|
||||||
"qtest" { >= "2.9" & with-test}
|
"qtest" { >= "2.9" & with-test}
|
||||||
"qcheck" {with-test & >= "0.9" }
|
"qcheck" {with-test & >= "0.9" }
|
||||||
"ounit2" {with-test}
|
"ounit2" {with-test}
|
||||||
|
"ptime" {with-test}
|
||||||
]
|
]
|
||||||
tags: [ "http" "thread" "server" "tiny_httpd" "http_of_dir" "simplehttpserver" ]
|
tags: [ "http" "thread" "server" "tiny_httpd" "http_of_dir" "simplehttpserver" ]
|
||||||
homepage: "https://github.com/c-cube/tiny_httpd/"
|
homepage: "https://github.com/c-cube/tiny_httpd/"
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue