aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/tst-curl.sh
diff options
context:
space:
mode:
authorEgor Tensin <egor@tensin.name>2026-09-30 11:45:02 +0500
committerEgor Tensin <egor@tensin.name>2026-09-30 12:00:50 +0500
commitff590ddb9ade2de0f21fe5a6d4880752fa143e87 (patch)
tree7020de8df559265b88be647ca03e73590bde1f34 /test/tst-curl.sh
parentMakefile: fix Debian builds (diff)
downloadlinux-status-ff590ddb9ade2de0f21fe5a6d4880752fa143e87.tar.gz
linux-status-ff590ddb9ade2de0f21fe5a6d4880752fa143e87.zip
test: rework using the approach from my other projects
The tests are now run using a framework common to my other projects, like config-links, tag-release & vk-scripts.
Diffstat (limited to 'test/tst-curl.sh')
-rw-r--r--test/tst-curl.sh80
1 files changed, 80 insertions, 0 deletions
diff --git a/test/tst-curl.sh b/test/tst-curl.sh
new file mode 100644
index 0000000..a8f9860
--- /dev/null
+++ b/test/tst-curl.sh
@@ -0,0 +1,80 @@
+test_curl_header=
+test_curl_output=
+
+_curl() {
+ if [ "$#" -ne 1 ]; then
+ log "usage: ${FUNCNAME[0]} URL"
+ return 1
+ fi
+
+ local url="$1"
+
+ curl \
+ --silent --show-error \
+ --dump-header "$test_curl_header" \
+ --output "$test_curl_output" \
+ --connect-timeout 3 \
+ -- \
+ "http://127.0.0.1:$test_server_port$url" \
+ || true
+}
+
+_test_curl_check_status() {
+ if [ "$#" -ne 1 ]; then
+ log "usage: ${FUNCNAME[0]} HTTP_STATUS"
+ return 1
+ fi
+
+ local expected="$1"
+ expected="HTTP/1.0 $expected"$'\r'
+ local actual
+ actual="$( head -n 1 -- "$test_curl_header" )"
+
+ [ "$expected" == "$actual" ] && return 0
+
+ fail "Unexpected HTTP status: $actual"
+ fail_details "Expected: $expected"
+
+ log 'Response headers:'
+ cat -- "$test_curl_header" >&2
+ log 'Response data:'
+ cat -- "$test_curl_output" >&2
+ return 1
+}
+
+_test_curl() {
+ if [ "$#" -lt 1 ]; then
+ log "usage: ${FUNCNAME[0]} URL [KEYWORD...]"
+ return 1
+ fi
+
+ local url="$1"
+ shift
+ log "Running test for URL: $url"
+
+ _curl "$url"
+ _test_curl_check_status '200 OK'
+ test_check_output "$test_curl_output" "$@"
+}
+
+test_run() {
+ test_setup
+
+ test_curl_header="$test_root_dir/header"
+ test_curl_output="$test_root_dir/output"
+
+ # / and /index.html are identical:
+ _test_curl '/' \
+ '<link rel="stylesheet" href="css/bootstrap.min.css">' \
+ 'var status_refresh_interval_seconds'
+ _test_curl '/index.html' \
+ '<link rel="stylesheet" href="css/bootstrap.min.css">' \
+ 'var status_refresh_interval_seconds'
+
+ # /status returns a JSON with a number of fields:
+ _test_curl '/status' '"hostname":' '"thermal":' '"system":' '"user":'
+ # /top is `top` output:
+ _test_curl '/top' 'load average:'
+ # /thermal is also an endpoint:
+ _test_curl '/thermal'
+}