#!/usr/bin/env bash
# Test double for curl. It ignores the request and "answers" with FAKE_CODE, and it
# keeps real curl's exit rules so a test cannot pass on behaviour curl does not have:
# FAKE_FAIL=1 is a connection failure (prints 000, exit 7); with -f or --fail an
# answer of 400 or more exits 22 after printing the code.
fail=0
for arg in "$@"; do
  case $arg in
    --fail) fail=1 ;;
    --*) ;;
    -*f*) fail=1 ;; # -f alone or in a group such as -fsS
  esac
done
if [[ ${FAKE_FAIL:-0} == 1 ]]; then
  printf '000'
  exit 7
fi
code=${FAKE_CODE:-200}
printf '%s' "$code"
if ((fail && code >= 400)); then exit 22; fi
