49 أسطر
1.3 KiB
Bash
49 أسطر
1.3 KiB
Bash
|
|
BASE="http://localhost:5000"
|
|
OUT_LOG="test_results.log"
|
|
echo "Starting API tests at $(date -u)" > "$OUT_LOG"
|
|
|
|
# helper
|
|
do_request() {
|
|
local method=$1
|
|
local url=$2
|
|
local data=$3
|
|
echo "---- $method $url" | tee -a "$OUT_LOG"
|
|
if [ -n "$data" ]; then
|
|
curl -s -w "\nHTTP_CODE:%{http_code}\nTIME_TOTAL:%{time_total}\n" -X "$method" -H "Content-Type: application/json" -d "$data" "$url" >> "$OUT_LOG"
|
|
else
|
|
curl -s -w "\nHTTP_CODE:%{http_code}\nTIME_TOTAL:%{time_total}\n" -X "$method" "$url" >> "$OUT_LOG"
|
|
fi
|
|
echo -e "\n" >> "$OUT_LOG"
|
|
sleep 0.5
|
|
}
|
|
|
|
|
|
do_request GET "$BASE/items"
|
|
|
|
# 2) POST create item
|
|
do_request POST "$BASE/items" '{"name":"Test Item 1","price":12.5}'
|
|
|
|
# 3) POST create another
|
|
do_request POST "$BASE/items" '{"name":"Test Item 2","price":5.0}'
|
|
|
|
# 4) GET items (should list 2)
|
|
do_request GET "$BASE/items"
|
|
|
|
# 5) GET single item (id 1)
|
|
do_request GET "$BASE/items/1"
|
|
|
|
# 6) PUT update item 1
|
|
do_request PUT "$BASE/items/1" '{"name":"Test Item 1 updated","price":15.0}'
|
|
|
|
# 7) POST create order (valid)
|
|
do_request POST "$BASE/orders" '{"item_id":1,"quantity":2}'
|
|
|
|
# 8) POST create order (invalid item -> triggers 400)
|
|
do_request POST "$BASE/orders" '{"item_id":9999,"quantity":1}'
|
|
do_request DELETE "$BASE/items/2"
|
|
do_request GET "$BASE/items"
|
|
|
|
echo "Tests finished at $(date -u)" >> "$OUT_LOG"
|
|
echo "Results written to $OUT_LOG"
|