Skip to content

Commit 6103a55

Browse files
committed
test: Add server tests
This adds a way to test ocsigenserver using cram tests. This is extremely valuable while developping a new feature and will help avoid regressions in the future. `server-test-helpers.sh` does the setup and teardown. Making a server test is as simple as: $ source ../../server-test-helpers.sh $ run_server ./test.exe ... Server logs will appear here ... $ curl_ "index.html" A first test is added that shows basic Staticmod and Deflatemod usage in `test/extensions/deflatemod.t`.
1 parent 393bfda commit 6103a55

File tree

7 files changed

+109
-0
lines changed

7 files changed

+109
-0
lines changed

test/dune

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(subdir
2+
extensions
3+
(cram
4+
(package ocsigenserver)
5+
(deps
6+
../server-test-helpers.sh
7+
(package ocsigenserver))))

test/extensions/deflatemod.t/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
(executable
2+
(name test)
3+
(libraries ocsigenserver ocsigenserver.ext.staticmod ocsigenserver.ext.deflatemod))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
(lang dune 3.18)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello world

test/extensions/deflatemod.t/run.t

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
$ source ../../server-test-helpers.sh
2+
$ run_server ./test.exe
3+
ocsigen:main: [WARNING] Command pipe created
4+
ocsigen:access: connection for local-test from (curl/8.12.1): /index.html
5+
ocsigen:ext: [INFO] host found! local-test:0 matches .*
6+
ocsigen:ext:staticmod: [INFO] Is it a static file?
7+
ocsigen:local-file: [INFO] Testing "./index.html".
8+
ocsigen:local-file: [INFO] checking if file index.html can be sent
9+
ocsigen:ext: [INFO] Compiling exclusion regexp $^
10+
ocsigen:local-file: [INFO] Returning "./index.html".
11+
ocsigen:access: connection for local-test from (curl/8.12.1): /index.html
12+
ocsigen:ext: [INFO] host found! local-test:0 matches .*
13+
ocsigen:ext:staticmod: [INFO] Is it a static file?
14+
ocsigen:local-file: [INFO] Testing "./index.html".
15+
ocsigen:local-file: [INFO] checking if file index.html can be sent
16+
ocsigen:local-file: [INFO] Returning "./index.html".
17+
ocsigen:ext:deflate: [INFO] Zlib stream initialized
18+
ocsigen:ext:deflate: [INFO] End of stream: big cleaning for zlib
19+
ocsigen:ext:deflate: [INFO] Zlib.deflate finished, last flush
20+
ocsigen:ext:deflate: [INFO] Flushing!
21+
ocsigen:ext:deflate: [INFO] Zlib stream closed
22+
application: [WARNING] Command received: shutdown
23+
24+
First response is not compressed:
25+
26+
$ curl_ "index.html"
27+
HTTP/1.1 200 OK
28+
content-type: text/html
29+
server: Ocsigen
30+
content-length: 12
31+
32+
Hello world
33+
34+
Second response is compressed:
35+
36+
$ curl_ "index.html" --compressed
37+
HTTP/1.1 200 OK
38+
content-type: text/html
39+
content-encoding: gzip
40+
server: Ocsigen
41+
transfer-encoding: chunked
42+
43+
Hello world
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
let () =
2+
Logs.Src.set_level Deflatemod.section (Some Logs.Debug);
3+
Logs.set_level ~all:true (Some Logs.Debug);
4+
Ocsigen_server.start
5+
~ports:[ (`File "./local.sock", 0) ]
6+
~veryverbose:() ~debugmode:true ~logdir:"log" ~datadir:"data"
7+
~uploaddir:None ~usedefaulthostname:true ~command_pipe:"local.cmd"
8+
~default_charset:(Some "utf-8")
9+
[
10+
Ocsigen_server.host
11+
[ Staticmod.run ~dir:"." (); Deflatemod.run ~mode:(`All_but []) () ];
12+
]

test/server-test-helpers.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
# Bash functions that help test ocsigenserver
2+
3+
# Run the server using 'dune exec -- "$@"' and doing the necessary setup.
4+
# The server must listen on the unix-domain socket named "local.sock" and on
5+
# the command-pipe named "local.cmd".
6+
#
7+
# Usage:
8+
# $ run_server ./test.exe
9+
# $ curl_ "index.html"
10+
#
11+
run_server ()
12+
{
13+
mkdir -p log data # Directories that might be required by the server
14+
dune build "$1"
15+
# Run the server in the background, cut the datetime out of the log output.
16+
dune exec -- "$@" 2>&1 | cut -d ' ' -f 4- &
17+
# Wait for the unix-domain socket and the command-pipe to be created
18+
local timeout=50 # Don't wait more than 0.5s
19+
while ! ( [[ -e ./local.sock ]] && [[ -e ./local.cmd ]] ) && (( timeout-- > 0 )); do
20+
sleep 0.01
21+
done
22+
# Print an error if a file is missing
23+
ls ./local.sock ./local.cmd >/dev/null || return 1
24+
# Shutdown the server at the end of the test
25+
trap 'echo shutdown > local.cmd && wait' EXIT
26+
}
27+
28+
# Wrapper around 'curl' that connects to the server. First argument is the
29+
# request path, the other arguments are directly passed to 'curl'.
30+
#
31+
# Usage:
32+
# $ curl_ ""
33+
# $ curl_ "index.html"
34+
# $ curl_ "index.html" --no-show-headers # Supress the headers
35+
#
36+
curl_ ()
37+
{
38+
local path=$1; shift
39+
# Remove the 'date' header, which is unreproducible
40+
curl --unix-socket ./local.sock -s -i "$@" "http://local-test/$path" | \
41+
grep -v "^date: "
42+
}

0 commit comments

Comments
 (0)