#!/bin/bash

# sanity check for build env
if [[ ! -e zap ]]; then
    echo "no zap binary present"
    exit 1
fi

# start zap, fork.
./zap --port 16000 1>/dev/null 2>/dev/null &
ZAP_PID=$!

# pull results.
RESP="$(curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' -H 'Host: g' localhost:16000/z)"

# Check response
expected="302 https://github.com/issmirnov/zap"
if [[ $RESP != $expected ]];then
    echo "Status code or location don't match expectations"
    echo "expected: $expected"
    echo "got: $RESP"
    kill -9 $ZAP_PID
    exit 2
fi

# cleanup
kill -9 $ZAP_PID
echo "End to end test passed."

#!/bin/bash
PORT=16000
# sanity check for build env
if [[ ! -e zap ]]; then
    echo "no zap binary present"
    exit 1
fi

# start zap, fork.
./zap --port $PORT 1>/dev/null 2>/dev/null &
ZAP_PID=$!

# wait for port to open
while ! nc -z localhost $PORT </dev/null 2>/dev/null; do sleep 1; done

# pull results.
RESP="$(curl -s -o /dev/null -w '%{http_code} %{redirect_url}\n' -H 'Host: g' localhost:$PORT/z)"

# Check response
expected="302 https://github.com/issmirnov/zap"
if [[ $RESP != $expected ]];then
    echo "Status code or location don't match expectations"
    echo "expected: $expected"
    echo "got: $RESP"
    kill -9 $ZAP_PID
    exit 2
fi

# cleanup
kill -9 $ZAP_PID
echo "End to end test passed."

Fix race condition in test
Fix race condition in test

On OSX systems, zap takes longer to start, and as a result the curl command hits a closed port. Added a busy loop that will wait for the port to be open and accepting connections before we hit it with the test.