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 "cert.pem"
14 key "key.pem"
15 root "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 # quit gmid
62 quit() {
63 pkill gmid || true
64 wait || true
65 }
67 # usage: eq a b errmsg
68 # if a and b aren't equal strings, exit with errmsg
69 eq() {
70 if ! [ "$1" = "$2" ]; then
71 echo "$3: \"$1\" not equal \"$2\""
72 exit 1
73 fi
74 }
76 onexit() {
77 rm -f bigfile bigfile.sha
78 quit
79 }
81 # tests
83 trap 'onexit' INT TERM EXIT
85 endl=`printf "\r\n"`
86 lf=`echo`
88 config "" ""
89 checkconf
90 run
92 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
93 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
94 echo OK GET /
96 eq "$(head /foo)" "51 not found" "Unexpected head /foo"
97 eq "$(get /foo)" "" "Unexpected body /foo"
98 echo OK GET /foo
100 # should redirect if asked for a directory but without the trailing /
101 eq "$(head /dir)" "30 /dir/" "Unexpected redirect for /dir"
102 eq "$(get /dir)" "" "Unexpected body for redirect"
103 echo OK GET /dir
105 # 51 for a directory without index.gmi
106 eq "$(head /dir/)" "51 not found" "Unexpected head for /"
107 eq "$(get /dir/)" "" "Unexpected body for error"
108 echo OK GET /dir/
110 eq "$(head /dir/foo.gmi)" "20 text/gemini" "Unexpected head for /dir/foo.gmi"
111 eq "$(get /dir/foo.gmi)" "# hello world$ln" "Unexpected body for /dir/foo.gmi"
112 echo OK GET /dir/foo.gmi
114 # try a big file
115 eq "$(head /bigfile)" "20 application/octet-stream" "Unexpected head for /bigfile"
116 get /bigfile > bigfile
117 ./sha bigfile bigfile.sha
118 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /bigfile"
119 echo OK GET /bigfile
121 # shouldn't be executing cgi scripts
122 eq "$(head /hello)" "20 application/octet-stream" "Unexpected head for /hello"
123 echo OK GET /hello
125 check "should be running"
126 quit
128 # try with custom mime
129 config 'mime "text/x-funny-text" "gmi"' 'default type "application/x-trash"'
130 checkconf
131 run
133 eq "$(head /)" "20 text/x-funny-text" "Unexpected head for /"
134 echo OK GET / with custom mime
136 eq "$(head /hello)" "20 application/x-trash" "Unexpected head for /hello"
137 echo OK GET /hello with custom mime
139 check "should be running"
140 quit
142 # try with custom lang
143 config '' 'lang "it"'
144 checkconf
145 run
147 eq "$(head /)" "20 text/gemini; lang=it" "Unexpected head for /"
148 echo OK GET / with custom lang
150 check "should be running"
151 quit
153 # finally try with CGI scripts
154 config '' 'cgi ""'
155 checkconf
156 run
158 eq "$(head /hello)" "20 text/gemini" "Unexpected head for /hello"
159 eq "$(get /hello)" "# hello world$ln" "Unexpected body for /hello"
160 echo OK GET /hello with cgi
162 eq "$(head /slow)" "20 text/gemini" "Unexpected head for /slow"
163 eq "$(get /slow)" "# hello world$ln" "Unexpected body for /slow"
164 echo OK GET /slow with cgi
166 eq "$(head /err)" "" "Unexpected head for /err"
167 eq "$(get /err)" "" "Unexpected body for /err"
168 echo OK GET /err with cgi
170 eq "$(raw /invalid | wc -c | xargs)" 2048 "Unexpected body for /invalid"
171 echo OK GET /invalid with cgi
173 # try a big file
174 eq "$(head /serve-bigfile)" "20 application/octet-stream" "Unexpected head for /serve-bigfile"
175 get /bigfile > bigfile
176 ./sha bigfile bigfile.sha
177 eq "$(cat bigfile.sha)" "$(cat testdata/bigfile.sha)" "Unexpected sha for /serve-bigfile"
178 echo OK GET /serve-bigfile with cgi
180 check "should be running"
181 quit
183 config '' 'index "foo.gmi"'
184 checkconf
185 run
187 eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /"
188 eq "$(get /dir/)" "# hello world$ln" "Unexpected body for error"
189 echo OK GET /dir/ with custom index
191 check "should be running"
192 quit
194 config '' 'location "/dir/" { default type "text/plain" index "hello" }'
195 checkconf
196 run
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"
206 quit
208 config '' 'location "/dir/" { auto index on }'
209 checkconf
210 run
212 eq "$(head /)" "20 text/gemini" "Unexpected head for /"
213 eq "$(get /)" "# hello world$ln" "Unexpected body for /"
214 echo OK GET / with auto index
216 eq "$(head /dir)" "30 /dir/" "Unexpected head for /dir"
217 eq "$(head /dir/)" "20 text/gemini" "Unexpected head for /dir/"
218 eq "$(get /dir/|wc -l|xargs)" "3" "Unexpected body for /dir/"
219 echo OK GET /dir/ with auto index on
221 check "should be running"
222 quit