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 ipv6 off
10 port 10965
11 $1
12 server "localhost" {
13 cert "$PWD/cert.pem"
14 key "$PWD/key.pem"
15 root "$PWD/testdata"
16 $2
17 }
18 EOF
19 }
21 checkconf() {
22 ./../gmid -n -c reg.conf
23 }
25 # usage: get <path>
26 # return the body of the request on stdout
27 get() {
28 ./../gg -b "gemini://localhost:10965/$1"
29 }
31 # usage: head <path>
32 # return the meta response line on stdout
33 head() {
34 ./../gg -h "gemini://localhost:10965/$1"
35 }
37 # usage: raw <path>
38 # return both header and body
39 raw() {
40 ./../gg "gemini://localhost:10965/$1"
41 }
43 run() {
44 # filter out logs for GET requests
45 (./../gmid -f -c reg.conf 2>&1 | grep -v GET) >&2 &
46 pid=$!
47 # give gmid time to bind the port, otherwise we end up
48 # executing gg when gmid isn't ready yet.
49 sleep 1
50 }
52 # usage: check [exit-message]
53 # check if gmid is still running
54 check() {
55 if ! ps $pid >/dev/null; then
56 echo ${1:-"gmid crashed?"}
57 exit 1
58 fi
59 }
61 restart() {
62 pkill -SIGHUP gmid
63 sleep 1
64 }
66 # quit gmid
67 quit() {
68 pkill gmid || true
69 wait || true
70 }
72 # usage: eq a b errmsg
73 # if a and b aren't equal strings, exit with errmsg
74 eq() {
75 if ! [ "$1" = "$2" ]; then
76 echo "$3: \"$1\" not equal \"$2\""
77 exit 1
78 fi
79 }
81 onexit() {
82 rm -f bigfile bigfile.sha
83 quit
84 }
86 # tests
88 trap 'onexit' INT TERM EXIT
90 endl=`printf "\r\n"`
91 lf=`echo`
93 config "" ""
94 checkconf
95 run
97 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
98 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
99 echo OK GET /
101 eq "$(head /foo)" "51 not found" "Unexpected head /foo"
102 eq "$(get /foo)" "" "Unexpected body /foo"
103 echo OK GET /foo
105 # should redirect if asked for a directory but without the trailing /
106 eq "$(head /dir)" "30 /dir/" "Unexpected redirect for /dir"
107 eq "$(get /dir)" "" "Unexpected body for redirect"
108 echo OK GET /dir
110 # 51 for a directory without index.gmi
111 eq "$(head /dir/)" "51 not found" "Unexpected head for /"
112 eq "$(get /dir/)" "" "Unexpected body for error"
113 echo OK GET /dir/
115 eq "$(head /dir/foo.gmi)" "20 text/gemini" "Unexpected head for /dir/foo.gmi"
116 eq "$(get /dir/foo.gmi)" "# hello world$ln" "Unexpected body for /dir/foo.gmi"
117 echo OK GET /dir/foo.gmi
119 # try a big file
120 eq "$(head /bigfile)" "20 application/octet-stream" "Unexpected head for /bigfile"
121 get /bigfile > bigfile
122 ./sha bigfile bigfile.sha
123 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /bigfile"
124 echo OK GET /bigfile
126 # shouldn't be executing cgi scripts
127 eq "$(head /hello)" "20 application/octet-stream" "Unexpected head for /hello"
128 echo OK GET /hello
130 check "should be running"
132 # try with custom mime
133 config 'mime "text/x-funny-text" "gmi"' 'default type "application/x-trash"'
134 checkconf
135 restart
137 eq "$(head /)" "20 text/x-funny-text" "Unexpected head for /"
138 echo OK GET / with custom mime
140 eq "$(head /hello)" "20 application/x-trash" "Unexpected head for /hello"
141 echo OK GET /hello with custom mime
143 check "should be running"
145 # try with custom lang
146 config '' 'lang "it"'
147 checkconf
148 restart
150 eq "$(head /)" "20 text/gemini; lang=it" "Unexpected head for /"
151 echo OK GET / with custom lang
153 check "should be running"
155 # finally try with CGI scripts
156 config '' 'cgi "*"'
157 checkconf
158 restart
160 eq "$(head /hello)" "20 text/gemini" "Unexpected head for /hello"
161 eq "$(get /hello)" "# hello world$ln" "Unexpected body for /hello"
162 echo OK GET /hello with cgi
164 eq "$(head /slow)" "20 text/gemini" "Unexpected head for /slow"
165 eq "$(get /slow)" "# hello world$ln" "Unexpected body for /slow"
166 echo OK GET /slow with cgi
168 eq "$(head /err)" "42 CGI error" "Unexpected head for /err"
169 eq "$(get /err)" "" "Unexpected body for /err"
170 echo OK GET /err with cgi
172 eq "$(raw /invalid | wc -c | xargs)" 2048 "Unexpected body for /invalid"
173 echo OK GET /invalid with cgi
175 # try a big file
176 eq "$(head /serve-bigfile)" "20 application/octet-stream" "Unexpected head for /serve-bigfile"
177 get /bigfile > bigfile
178 ./sha bigfile bigfile.sha
179 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /serve-bigfile"
180 echo OK GET /serve-bigfile with cgi
182 check "should be running"
184 config '' 'index "foo.gmi"'
185 checkconf
186 restart
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"
194 config '' 'location "/dir/" { default type "text/plain" index "hello" }'
195 checkconf
196 restart
198 eq "$(head /dir/hello)" "20 text/plain" "Unexpected head for /"
199 echo OK GET /dir/hello with location and default type
201 eq "$(head /dir/)" "20 text/plain" "Unexpected head for /dir/"
202 eq "$(get /dir/|tail -1)" 'echo "# hello world"' "Unexpected body for /dir/"
203 echo OK GET /dir/ with location and custom index
205 check "should be running"
207 config '' 'location "/dir/" { auto index on }'
208 checkconf
209 restart
211 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
212 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
213 echo OK GET / with auto index
215 eq "$(head /dir)" "30 /dir/" "Unexpected head for /dir"
216 eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /dir/"
217 eq "$(get /dir/|wc -l|xargs)" "5" "Unexpected body for /dir/"
218 echo OK GET /dir/ with auto index on
220 check "should be running"
222 # test block return and strip
224 config '' 'location "*" { block }'
225 checkconf
226 restart
228 eq "$(head /)" "40 temporary failure" "Unexpected head for /"
229 eq "$(get /)" "" "Unexpected body for /"
230 echo OK GET / with block
232 eq "$(head /nonexists)" "40 temporary failure" "Unexpected head for /nonexists"
233 eq "$(get /nonexists)" "" "Unexpected body for /nonexists"
234 echo OK GET /nonexists with block
236 check "should be running"
238 config '' '
239 location "/dir" {
240 strip 1
241 block return 40 "%% %p %q %P %N test"
243 location "*" {
244 strip 99
245 block return 40 "%% %p %q %P %N test"
246 }'
247 checkconf
248 restart
250 eq "$(head /dir/foo.gmi)" "40 % /foo.gmi 10965 localhost test"
251 echo OK GET /dir/foo.gmi with strip and block
253 eq "$(head /bigfile)" "40 % 10965 localhost test"
254 echo OK GET /bigfile with strip and block
256 check "should be running"
258 # test the entrypoint
260 config '' 'entrypoint "/env"'
261 checkconf
262 restart
264 eq "$(head /foo/bar)" "20 text/plain; lang=en" "Unknown head for /foo/bar"
265 eq "$(get /foo/bar|grep PATH_INFO)" "PATH_INFO=/foo/bar" "Unexpected PATH_INFO"
266 echo OK GET /foo/bar with entrypoint
268 quit