#!/bin/bash
set -eu

TESTDIR=$(readlink -f $(dirname $0))

case "$APT_TEST_METHOD" in
	http*_proxy6*) # and list other quasi-methods like this if any!
		:
		;;
	*)
		echo 'SKIP (makes sense only for unusual connection to proxy, like IPv6)' >&2
		exit 0
		;;
esac

. $TESTDIR/framework

setupenvironment

buildpackage 'simple-package'

generaterepository_and_switch_sources "$TMPWORKINGDIRECTORY/usr/src/RPM/RPMS"

# First, check that we can't connect to the "usual" localhost proxy.

msgmsg "Substituting usual localhost into http_proxy ($http_proxy)"
readonly APT_TEST_HTTP_PROXY="$http_proxy"
rm -f "$APT_TEST_PROXY_FOR_HTTP_CONF" ||:
unset http{,s}_proxy # if they were exported, now nothing will be set for children
http_proxy="${APT_TEST_HTTP_PROXY/"$TINYPROXY_HOST":/localhost:}"
http_proxy="${http_proxy/"$TINYPROXY_IP":/localhost:}"
export http_proxy
msgmsg "Substituted usual localhost in http_proxy ($http_proxy)"

testfailure aptget update

testpkgnotinstalled 'simple-package'
testfailure aptget install 'simple-package'
testpkgnotinstalled 'simple-package'

# And then, check that connection works with the "special" host proxy.

msgmsg 'Restoring http_proxy env'
# use the saved value
export http_proxy="$APT_TEST_HTTP_PROXY"

testsuccess aptget update

testpkgnotinstalled 'simple-package'
testsuccess aptget install 'simple-package'
testpkginstalled 'simple-package'
