#!/bin/bash

set -e

server="$1"

source "$(dirname "$0")"/webapp_validate_common.sh

PORT=${PORT:-8080}
CONTEXT_PATH="vnu/"

start_server "$server" "${PORT}" "$CONTEXT_PATH"

#echo "current path: $PWD"

# vnu is rather long to start the first time and the 1st call may timeout like this:
#   java.net.SocketTimeoutException: Read timed out
# To prevent timeout errors while waiting the response, set
# infinite read timeout with property nu.validator.client.readtimeout=0
export JAVA_ARGS="-Dnu.validator.client.readtimeout=0"

echo "Starting test."
vnu-client build/minDoc.html > "$tempfile"
echo "Done."

if [ ! -s "$tempfile" ]; then
  # File is empty, so validation was successful.
  echo "Validation of file was successful."
else
  debug_failure
  echo "-------------"
  cat "$tempfile"
  echo "-------------"
  echo "Issue validating the file. Output is above."
  exit 1
fi

rm "$tempfile"
