aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/test/tst-curl.sh
blob: a8f9860812e48294c4ba145e163603c4a3c698c6 (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
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'
}