#!/bin/sh
# Smoke-test gzip crash upload (plain JSON still accepted).
# Usage: BASE=http://127.0.0.1:8080/app/androidcast_project/crashes ./scripts/test_gzip_upload.sh
set -eu

ROOT="$(cd "$(dirname "$0")/.." && pwd)"
SAMPLE="${ROOT}/../sample_crash_java.json"
BASE="${BASE:-http://127.0.0.1:8080/app/androidcast_project/crashes}"
URL="${BASE}/api/upload.php"
RID="gzip-test-$(date +%s)"

if [ ! -f "$SAMPLE" ]; then
  echo "Missing $SAMPLE" >&2
  exit 1
fi

BODY="$(mktemp)"
jq --arg rid "$RID" '.report_id = $rid' "$SAMPLE" > "$BODY"
GZIP="$(mktemp)"
gzip -c -f "$BODY" > "$GZIP"

echo "POST plain JSON..."
curl -sS -o /dev/null -w "plain:%{http_code}\n" -X POST "$URL" \
  -H 'Content-Type: application/json' \
  --data-binary @"$BODY"

echo "POST gzip (Content-Encoding: gzip)..."
curl -sS -w "\n%{http_code}\n" -X POST "$URL" \
  -H 'Content-Type: application/json' \
  -H 'Content-Encoding: gzip' \
  --data-binary @"$GZIP"

rm -f "$BODY" "$GZIP"
