Skip to content

Commit

Permalink
Merge pull request #4 from cerebruminc/fix-tests
Browse files Browse the repository at this point in the history
add tests
  • Loading branch information
20k-ultra committed Apr 27, 2023
2 parents 8396bbb + 63bcb28 commit c323cfb
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 27 deletions.
54 changes: 32 additions & 22 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ jobs:
steps:
- name: Checkout svix-proxy-action
uses: actions/checkout@v3
- run: sudo apt-get install colorized-logs # this is to strip ansi chars from svix stdout
shell: bash
- name: Run local copy of svix-proxy-action
uses: ./
id: proxy
Expand All @@ -26,36 +24,48 @@ jobs:
- name: Test postback URL
shell: bash
run: |
LOG_FILE="requests.log"
LOG_FILE="/tmp/svix.log"
# start an HTTP server on port 8080
echo -e "HTTP/1.1 200 OK\n\n" | nc -l -p 8080 -N > $LOG_FILE &
echo -e "HTTP/1.1 200 OK\n\n" | nc -l -p 8080 -N &
# send HTTP request to postback_url
DATA="name=test&type=user"
curl -fX POST -d $DATA ${{ steps.proxy.outputs.postback_url }}
# wait for up to 60 seconds for the server to log the request
ATTEMPTS=60
json_payload=$(cat <<EOF
{
"event_type": "user_created",
"event_id": "01234567-89ab-cdef-0123-456789abcdef",
"timestamp": "2023-04-27T10:15:00.000Z",
"data": {
"user_id": "987654321",
"first_name": "John",
"last_name": "Doe",
"email": "john.doe@example.com",
"created_at": "2023-04-27T10:10:00.000Z"
}
}
EOF
)
curl -sX POST -H "Content-Type: application/json" -d "$json_payload" ${{ steps.proxy.outputs.postback_url }}
# wait for the server to log the request
ATTEMPTS=3
FOUND=false
while [ $ATTEMPTS -gt 0 ]; do
if cat "$LOG_FILE" | ansi2txt | grep -q "$DATA"; then
echo "Webhook data found"
REQUEST_LOG="Forwarding Message to: http://localhost:8080/webhook"
RESPONSE_LOG="Recieved \"200 OK\" response, forwarding to webhook sender"
while [ "$ATTEMPTS" -gt 0 ]; do
if grep -q "$REQUEST_LOG" <"$LOG_FILE" && grep -q "$RESPONSE_LOG" <"$LOG_FILE"; then
echo "Webhook was received!"
FOUND=true
break
else
echo "Did not find webhook data, waiting 1 second and rechecking..."
sleep 1
echo "Did not find webhook request, waiting a few seconds and rechecking..."
sleep 5
ATTEMPTS=$((ATTEMPTS-1))
fi
done
echo "==="
echo "Log file:"
cat "$LOG_FILE"
echo "==="
echo "Svig logs:"
cat "/tmp/svix.log" | ansi2txt
echo "==="
# check if data was found
if ! $FOUND; then
echo "Local server expecting webhook data did not log anything"
echo "Maybe nothing was sent ?"
echo "Expected to find:"
echo "REQUEST: $REQUEST_LOG"
echo "RESPONSE: $RESPONSE_LOG"
echo "Found:"
ansi2txt <"$LOG_FILE"
exit 1
fi
11 changes: 6 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,12 @@ runs:
shell: bash
run: |
# Start the svix command in the background so once this step finishes, svix will still be running in the action runner instance for the next step to use the callback_url output
touch /tmp/svix.log
LOG_FILE="/tmp/svix.log"
touch $LOG_FILE
if [ "${{ inputs.logging }}" = true ]; then
svix listen http://localhost:${{ inputs.port }}${{ inputs.path }} >/tmp/svix.log &
svix listen http://localhost:${{ inputs.port }}${{ inputs.path }} >"$LOG_FILE" &
else
svix listen http://localhost:${{ inputs.port }}${{ inputs.path }} --no-logging >/tmp/svix.log &
svix listen http://localhost:${{ inputs.port }}${{ inputs.path }} --no-logging >"$LOG_FILE" &
fi
# Watch Svix logs for 15 seconds
Expand All @@ -51,12 +52,12 @@ runs:
echo "Waiting for proxy postback URL..."
while [ $SECONDS -lt $timeout ]; do
# return true in grep so that it doesn't return 1 and close the script when no match is found
postback_url=$(cat /tmp/svix.log | ansi2txt | grep "https://play.svix.com/in/" || true)
postback_url=$(ansi2txt <"$LOG_FILE"| grep "https://play.svix.com/in/" || true)
if [ -n "$postback_url" ]; then
# Found postback URL
echo "postback_url=$postback_url" >>"$GITHUB_OUTPUT"
# echo the CLI stdout
cat /tmp/svix.log | ansi2txt
ansi2txt <"$LOG_FILE"
exit 0
fi
sleep 1
Expand Down

0 comments on commit c323cfb

Please sign in to comment.