#!/bin/bash

# This test verifies that Connect is accessible on the
# configured addresses. It is run for each possible server address:
# - Server.Address
# - HTTP.Listen
# - HTTPS.Listen

# It's not necessarily an issue if these checks fail,
# because the tests are run from within Connect itself
# and it's possible to configure networking and firewalls
# in such a way that clients can reach Connect but
# Connect cannot reach its configured Server.Address. For example:
# * In a load-balanced configuration, the Connect
#   servers behind the proxy might not have access to the proxy
#   itself. This may not be an issue unless there is content
#   that accesses other content on Connect (for example, a Shiny 
#   app that access a deployed Plumber API).
# * An authenticating proxy might reject the requests issued
#   by this test because they lack credentials. Note, though,
#   that these use the __api__/server_settings endpoint which
#   should be accessible to enable clients such as
#   rsconnect and rsconnect-python to properly access Connect.

# Arguments:
# - $1: label for the server URL
# - $2: server URL

set -euo pipefail
execute() { echo; echo "\$ $*"; "$@"; }
CURL="curl -Lsv -m 10 --retry 0 -o /dev/null"

if [ -n "$2" ]; then
    echo "$1 is '$2'"
    execute $CURL "$2/__api__/server_settings" 2>&1 | sed -E 's/[a-zA-Z0-9_-]{30,}/REDACTED/g'
else
    echo "Warning: $1 is not configured."
fi
