blob: 46c136f9f9e2342fdb700244f281320e1f6497fe (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
|
#!/usr/bin/env bash
# Copyright (c) 2022 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "void" project.
# For details, see https://github.com/egor-tensin/void.
# Distributed under the MIT License.
set -o errexit -o nounset -o pipefail
script_dir="$( dirname -- "${BASH_SOURCE[0]}" )"
script_dir="$( cd -- "$script_dir" && pwd )"
readonly script_dir
script_name="$( basename -- "${BASH_SOURCE[0]}" )"
readonly script_name
readonly server_port=23666
server_pid=
curl_header_file=
curl_output_file=
dump() {
local msg
for msg; do
echo "$script_name: $msg"
done
}
run_server() {
dump "Starting up server..."
"$script_dir/../server.py" --port "$server_port" &
server_pid="$!"
dump "Its PID is $server_pid"
sleep 5
}
kill_server() {
[ -z "$server_pid" ] && return
dump "Killing server with PID $server_pid..."
kill "$server_pid"
dump "Waiting for it to terminate..."
wait "$server_pid" || true
dump "Done"
}
create_files() {
curl_header_file="$( mktemp )"
curl_output_file="$( mktemp )"
dump "curl header file: $curl_header_file"
dump "curl output file: $curl_output_file"
}
cleanup_files() {
dump "Cleaning up curl files..."
[ -n "$curl_header_file" ] && rm -f -- "$curl_header_file"
[ -n "$curl_output_file" ] && rm -f -- "$curl_output_file"
}
prepare() {
run_server
create_files
}
cleanup() {
kill_server || true
cleanup_files || true
}
run_curl() {
if [ "$#" -ne 1 ]; then
echo "usage: ${FUNCNAME[0]} URL" >&2
return 1
fi
local url="$1"
curl \
--silent --show-error \
--dump-header "$curl_header_file" \
--output "$curl_output_file" \
--connect-timeout 3 \
-- "http://localhost:$server_port$url" || true
}
curl_check_status() {
if [ "$#" -ne 1 ]; then
echo "usage: ${FUNCNAME[0]} HTTP_STATUS" >&2
return 1
fi
local expected="$1"
expected="HTTP/1.0 $expected"$'\r'
local actual
actual="$( head -n 1 -- "$curl_header_file" )"
[ "$expected" == "$actual" ] && return 0
dump "Actual HTTP response: $actual" >&2
dump "Expected: $expected" >&2
dump 'HTTP headers:' >&2
cat -- "$curl_header_file" >&2
dump 'HTTP response:' >&2
cat -- "$curl_output_file" >&2
return 1
}
curl_check_keyword() {
local keyword
for keyword; do
if ! grep --fixed-strings --quiet -- "$keyword" "$curl_output_file"; then
dump "The following pattern hasn't been found:"
dump "$keyword"
dump "The output was:"
cat -- "$curl_output_file"
return 1
fi
done
}
run_curl_test() {
if [ "$#" -lt 1 ]; then
echo "usage: ${FUNCNAME[0]} URL [KEYWORD...]" >&2
return 1
fi
local url="$1"
shift
dump "Running test for URL: $url"
run_curl "$url"
curl_check_status '200 OK'
local keyword
for keyword; do
curl_check_keyword "$keyword"
done
}
run_curl_tests() {
# / and /index.html are identical:
run_curl_test / '<title>Орём!</title>' 'var screams_now'
run_curl_test /index.html '<title>Орём!</title>' 'var screams_now'
run_curl_test /screams 0
run_curl_test /scream 1
run_curl_test /scream 2
run_curl_test /scream 3
run_curl_test /screams 3
}
cgi_check_header() {
local expected='Content-Type: text/html; charset=utf-8'
local actual
actual="$( head -n 1 -- "$curl_output_file" )"
[ "$expected" == "$actual" ] && return 0
dump "Actual CGI header: $actual" >&2
dump "Expected: $expected" >&2
diff <( echo "$actual" ) <( echo "$expected" ) | cat -te
return 1
}
run_cgi_test() {
if [ "$#" -lt 1 ]; then
echo "usage: ${FUNCNAME[0]} WHAT [KEYWORD...]" >&2
return 1
fi
local what="$1"
shift
local query_string="what=$what"
dump "Running CGI test for query string: $query_string"
QUERY_STRING="$query_string" "$script_dir/../app.py" > "$curl_output_file"
cgi_check_header
local keyword
for keyword; do
curl_check_keyword "$keyword"
done
}
run_cgi_tests() {
# Check that app.py still works as a CGI script.
# app.py doesn't save state between invocations, obviously.
run_cgi_test screams 0
run_cgi_test scream 1
run_cgi_test scream 1
run_cgi_test scream 1
run_cgi_test screams 0
}
main() {
trap cleanup EXIT
prepare
run_curl_tests
run_cgi_tests
}
main "$@"
|