Blob


1 #!/bin/sh
3 set -e
5 # usage: config <global config> <stuff for localhost>
6 # generates a configuration file reg.conf
7 config() {
8 cat <<EOF > reg.conf
9 daemon off
10 ipv6 off
11 port 10965
12 $1
13 server "localhost" {
14 cert "cert.pem"
15 key "key.pem"
16 root "testdata"
17 $2
18 }
19 EOF
20 }
22 checkconf() {
23 ./../gmid -n -c reg.conf
24 }
26 # usage: get <path>
27 # return the body of the request on stdout
28 get() {
29 ./../gg -b "gemini://localhost:10965/$1"
30 }
32 # usage: head <path>
33 # return the meta response line on stdout
34 head() {
35 ./../gg -h "gemini://localhost:10965/$1"
36 }
38 # usage: raw <path>
39 # return both header and body
40 raw() {
41 ./../gg "gemini://localhost:10965/$1"
42 }
44 run() {
45 # filter out logs for GET requests
46 (./../gmid -c reg.conf 2>&1 | grep -v GET) >&2 &
47 pid=$!
48 # give gmid time to bind the port, otherwise we end up
49 # executing gg when gmid isn't ready yet.
50 sleep 1
51 }
53 # usage: check [exit-message]
54 # check if gmid is still running
55 check() {
56 if ! ps $pid >/dev/null; then
57 echo ${1:-"gmid crashed?"}
58 exit 1
59 fi
60 }
62 # quit gmid
63 quit() {
64 pkill gmid || true
65 wait || true
66 }
68 # usage: eq a b errmsg
69 # if a and b aren't equal strings, exit with errmsg
70 eq() {
71 if ! [ "$1" = "$2" ]; then
72 echo "$3: \"$1\" not equal \"$2\""
73 exit 1
74 fi
75 }
77 onexit() {
78 rm -f bigfile bigfile.sha
79 quit
80 }
82 # tests
84 trap 'onexit' INT TERM EXIT
86 endl=`printf "\r\n"`
87 lf=`echo`
89 config "" ""
90 checkconf
91 run
93 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
94 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
95 echo OK GET /
97 eq "$(head /foo)" "51 not found" "Unexpected head /foo"
98 eq "$(get /foo)" "" "Unexpected body /foo"
99 echo OK GET /foo
101 # should redirect if asked for a directory but without the trailing /
102 eq "$(head /dir)" "30 /dir/" "Unexpected redirect for /dir"
103 eq "$(get /dir)" "" "Unexpected body for redirect"
104 echo OK GET /dir
106 # 51 for a directory without index.gmi
107 eq "$(head /dir/)" "51 not found" "Unexpected head for /"
108 eq "$(get /dir/)" "" "Unexpected body for error"
109 echo OK GET /dir/
111 eq "$(head /dir/foo.gmi)" "20 text/gemini" "Unexpected head for /dir/foo.gmi"
112 eq "$(get /dir/foo.gmi)" "# hello world$ln" "Unexpected body for /dir/foo.gmi"
113 echo OK GET /dir/foo.gmi
115 # try a big file
116 eq "$(head /bigfile)" "20 application/octet-stream" "Unexpected head for /bigfile"
117 get /bigfile > bigfile
118 ./sha bigfile bigfile.sha
119 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /bigfile"
120 echo OK GET /bigfile
122 # shouldn't be executing cgi scripts
123 eq "$(head /hello)" "20 application/octet-stream" "Unexpected head for /hello"
124 echo OK GET /hello
126 check "should be running"
127 quit
129 # try with custom mime
130 config 'mime "text/x-funny-text" "gmi"' 'default type "application/x-trash"'
131 checkconf
132 run
134 eq "$(head /)" "20 text/x-funny-text" "Unexpected head for /"
135 echo OK GET / with custom mime
137 eq "$(head /hello)" "20 application/x-trash" "Unexpected head for /hello"
138 echo OK GET /hello with custom mime
140 check "should be running"
141 quit
143 # try with custom lang
144 config '' 'lang "it"'
145 checkconf
146 run
148 eq "$(head /)" "20 text/gemini; lang=it" "Unexpected head for /"
149 echo OK GET / with custom lang
151 check "should be running"
152 quit
154 # finally try with CGI scripts
155 config '' 'cgi ""'
156 checkconf
157 run
159 eq "$(head /hello)" "20 text/gemini" "Unexpected head for /hello"
160 eq "$(get /hello)" "# hello world$ln" "Unexpected body for /hello"
161 echo OK GET /hello with cgi
163 eq "$(head /slow)" "20 text/gemini" "Unexpected head for /slow"
164 eq "$(get /slow)" "# hello world$ln" "Unexpected body for /slow"
165 echo OK GET /slow with cgi
167 eq "$(head /err)" "" "Unexpected head for /err"
168 eq "$(get /err)" "" "Unexpected body for /err"
169 echo OK GET /err with cgi
171 eq "$(raw /invalid | wc -c | xargs)" 2048 "Unexpected body for /invalid"
172 echo OK GET /invalid with cgi
174 # try a big file
175 eq "$(head /serve-bigfile)" "20 application/octet-stream" "Unexpected head for /serve-bigfile"
176 get /bigfile > bigfile
177 ./sha bigfile bigfile.sha
178 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /serve-bigfile"
179 echo OK GET /serve-bigfile with cgi
181 check "should be running"
182 quit
184 config '' 'index "foo.gmi"'
185 checkconf
186 run
188 eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /"
189 eq "$(get /dir/)" "# hello world$ln" "Unexpected body for error"
190 echo OK GET /dir/ with custom index
192 check "should be running"
193 quit
195 config '' 'location "/dir/" { default type "text/plain" index "hello" }'
196 checkconf
197 run
199 eq "$(head /dir/hello)" "20 text/plain" "Unexpected head for /"
200 echo OK GET /dir/hello with location and default type
202 eq "$(head /dir/)" "20 text/plain" "Unexpected head for /dir"
203 eq "$(get /dir/|tail -1)" 'echo "# hello world"' "Unexpected body for /dir/"
204 echo OK GET /dir/ with location and custom index
206 check "should be running"
207 quit