5 if [ "${SKIP_RUNTIME_TESTS:-0}" -eq 1 ]; then
7 echo "======================"
8 echo "runtime tests skipped!"
9 echo "======================"
21 # usage: config <global config> <stuff for localhost>
22 # generates a configuration file reg.conf
37 ./../gmid -n -c reg.conf
41 # return the body of the request on stdout
43 ./gg -T30 -b $ggflags "gemini://localhost:10965/$1"
47 # return the meta response line on stdout
49 ./gg -T30 -h $ggflags "gemini://localhost:10965/$1"
53 # return both header and body
55 ./gg -T30 $ggflags "gemini://localhost:10965/$1"
59 ./../gmid -f -c reg.conf &
61 # give gmid time to bind the port, otherwise we end up
62 # executing gg when gmid isn't ready yet.
66 # usage: check [exit-message]
67 # check if gmid is still running
69 if ! ps $pid >/dev/null; then
70 echo ${1:-"gmid crashed?"}
90 # usage: eq a b errmsg
91 # if a and b aren't equal strings, exit with errmsg
93 if ! [ "$1" = "$2" ]; then
94 echo "$3: \"$1\" not equal \"$2\""
100 rm -f bigfile bigfile.sha
106 trap 'onexit' INT TERM EXIT
115 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
116 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
119 eq "$(head /foo)" "51 not found" "Unexpected head /foo"
120 eq "$(get /foo)" "" "Unexpected body /foo"
123 # should redirect if asked for a directory but without the trailing /
124 eq "$(head /dir)" "30 /dir/" "Unexpected redirect for /dir"
125 eq "$(get /dir)" "" "Unexpected body for redirect"
128 # 51 for a directory without index.gmi
129 eq "$(head /dir/)" "51 not found" "Unexpected head for /"
130 eq "$(get /dir/)" "" "Unexpected body for error"
133 eq "$(head /dir/foo.gmi)" "20 text/gemini" "Unexpected head for /dir/foo.gmi"
134 eq "$(get /dir/foo.gmi)" "# hello world$ln" "Unexpected body for /dir/foo.gmi"
135 echo OK GET /dir/foo.gmi
138 eq "$(head /bigfile)" "20 application/octet-stream" "Unexpected head for /bigfile"
139 get /bigfile > bigfile
140 ./sha bigfile bigfile.sha
141 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /bigfile"
144 # shouldn't be executing cgi scripts
145 eq "$(head /hello)" "20 application/octet-stream" "Unexpected head for /hello"
148 check "should be running"
150 # try with custom mime
151 config 'map "text/x-funny-text" to-ext "gmi"' 'default type "application/x-trash"'
155 eq "$(head /)" "20 text/x-funny-text" "Unexpected head for /"
156 echo OK GET / with custom mime
158 eq "$(head /hello)" "20 application/x-trash" "Unexpected head for /hello"
159 echo OK GET /hello with custom mime
161 check "should be running"
163 # try with custom lang
164 config '' 'lang "it"'
168 eq "$(head /)" "20 text/gemini; lang=it" "Unexpected head for /"
169 echo OK GET / with custom lang
171 check "should be running"
173 # make sure we can use different lang in different location rules
174 config '' 'lang "it" location "/en/*" { lang "en" } location "/de/*" { lang "de" }'
176 echo OK parse multiple locations correctly
179 # try with CGI scripts
184 eq "$(head /hello)" "20 text/gemini" "Unexpected head for /hello"
185 eq "$(get /hello)" "# hello world$ln" "Unexpected body for /hello"
186 echo OK GET /hello with cgi
188 eq "$(head /slow)" "20 text/gemini" "Unexpected head for /slow"
189 eq "$(get /slow)" "# hello world$ln" "Unexpected body for /slow"
190 echo OK GET /slow with cgi
192 eq "$(head /err)" "42 CGI error" "Unexpected head for /err"
193 eq "$(get /err)" "" "Unexpected body for /err"
194 echo OK GET /err with cgi
196 eq "$(raw /invalid | wc -c | xargs)" 2048 "Unexpected body for /invalid"
197 echo OK GET /invalid with cgi
199 eq "$(raw /max-length-reply | wc -c | xargs)" 1029 "Unexpected header for /max-length-reply"
200 echo OK GET /max-length-reply with cgi
203 eq "$(head /serve-bigfile)" "20 application/octet-stream" "Unexpected head for /serve-bigfile"
204 get /bigfile > bigfile
205 ./sha bigfile bigfile.sha
206 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /serve-bigfile"
207 echo OK GET /serve-bigfile with cgi
209 # ensure we split the query correctly
210 eq "$(get /env | awk /^-/ | count)" 1 "Unexpected number of arguments"
211 eq "$(get /env?foo | awk /^-/ | count)" 2 "Unexpected number of arguments"
212 eq "$(get /env?foo+bar | awk /^-/ | count)" 3 "Unexpected number of arguments"
213 eq "$(get /env?foo+bar=5 | awk /^-/ | count)" 1 "Unexpected number of arguments"
214 eq "$(get /env?foo+bar%3d5 | awk /^-/ | count)" 3 "Unexpected number of arguments"
216 check "should be running"
218 config '' 'index "foo.gmi"'
222 eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /"
223 eq "$(get /dir/)" "# hello world$ln" "Unexpected body for error"
224 echo OK GET /dir/ with custom index
226 check "should be running"
228 config '' 'location "/dir/*" { default type "text/plain" index "hello" }'
232 eq "$(head /dir/hello)" "20 text/plain" "Unexpected head for /"
233 echo OK GET /dir/hello with location and default type
235 eq "$(head /dir/)" "20 text/plain" "Unexpected head for /dir/"
236 eq "$(get /dir/|tail -1)" 'echo "# hello world"' "Unexpected body for /dir/"
237 echo OK GET /dir/ with location and custom index
239 check "should be running"
241 config '' 'location "/dir/*" { auto index on }'
245 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
246 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
247 echo OK GET / with auto index
249 eq "$(head /dir)" "30 /dir/" "Unexpected head for /dir"
250 eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /dir/"
251 eq "$(get /dir/|wc -l|xargs)" "5" "Unexpected body for /dir/"
252 echo OK GET /dir/ with auto index on
254 check "should be running"
256 # test block return and strip
258 config '' 'location "*" { block }'
262 eq "$(head /)" "40 temporary failure" "Unexpected head for /"
263 eq "$(get /)" "" "Unexpected body for /"
264 echo OK GET / with block
266 eq "$(head /nonexists)" "40 temporary failure" "Unexpected head for /nonexists"
267 eq "$(get /nonexists)" "" "Unexpected body for /nonexists"
268 echo OK GET /nonexists with block
270 check "should be running"
275 block return 40 "%% %p %q %P %N test"
279 block return 40 "%% %p %q %P %N test"
284 eq "$(head /dir/foo.gmi)" "40 % /foo.gmi 10965 localhost test"
285 echo OK GET /dir/foo.gmi with strip and block
287 eq "$(head /bigfile)" "40 % / 10965 localhost test"
288 echo OK GET /bigfile with strip and block
290 check "should be running"
292 # test the entrypoint
294 config '' 'entrypoint "/env"'
298 eq "$(head /foo/bar)" "20 text/plain; lang=en" "Unknown head for /foo/bar"
299 eq "$(get /foo/bar|grep PATH_INFO)" "PATH_INFO=/foo/bar" "Unexpected PATH_INFO"
300 echo OK GET /foo/bar with entrypoint
302 # test with require ca
304 config '' 'require client ca "'$PWD'/testca.pem"'
308 eq "$(head /)" "60 client certificate required" "Unexpected head for /"
309 echo OK GET / without client certificate
311 ggflags="-C valid.crt -K valid.key"
312 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
313 echo OK GET / with valid client certificate
315 ggflags="-C invalid.cert.pem -K invalid.key.pem"
316 eq "$(head /)" "61 certificate not authorised" "Unexpected head for /"
317 echo OK GET / with invalid client certificate
322 # test with root inside a location
324 config '' 'location "/foo/*" { root "'$PWD'/testdata" strip 1 }'
328 eq "$(head /foo)" "51 not found" "Unexpected head for /foo"
329 eq "$(head /foo/)" "20 text/gemini" "Unexpected head for /foo/"
330 echo OK /foo and /foo/ with root inside location
332 # how to match both /foo and /foo/*
334 location "/foo" { block return 31 "%p/" }
335 location "/foo/*" { root "'$PWD'/testdata" strip 1 }
340 eq "$(head /foo)" "31 /foo/" "Unexpected head for /foo"
341 eq "$(head /foo/)" "20 text/gemini" "Unexpected head for /foo/"
342 echo OK /foo and /foo/ with root inside location
346 # NB: the fcgi spawn is NOT supported outside of this test suite
348 config 'prefork 1' 'fastcgi spawn "'$PWD'/fcgi-test"'
352 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
353 eq "$(get /)" "# Hello, world!" "Unexpected body for /"
354 echo OK GET / with fastcgi
356 # test macro expansion
363 # the quoting of \$ is for sh
364 cert \$pwd "/cert.pem"
366 root \$pwd "/testdata"
372 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
373 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
374 echo OK GET / with macro expansion