Updating Files
هذا الالتزام موجود في:
80
test_api.sh
80
test_api.sh
@@ -1,57 +1,39 @@
|
||||
#!/usr/bin/env bash
|
||||
#!/usr/bin/bash
|
||||
set -e
|
||||
|
||||
# test_api.sh
|
||||
# Script to test Fruit API endpoints and log responses
|
||||
echo "🚀 Running API tests at $(date)"
|
||||
echo "======================================"
|
||||
|
||||
API_URL="http://localhost:5000"
|
||||
BASE_URL="http://localhost:5000"
|
||||
LOG_FILE="test_results.log"
|
||||
|
||||
echo "🚀 Running API tests at $(date)" | tee -a "$LOG_FILE"
|
||||
echo "======================================" | tee -a "$LOG_FILE"
|
||||
check_status() {
|
||||
local status=$1
|
||||
if [[ "$status" == "200" || "$status" == "201" ]]; then
|
||||
echo "✅ Status: $status"
|
||||
else
|
||||
echo "❌ Status: $status"
|
||||
fi
|
||||
}
|
||||
|
||||
# 1. Health Check
|
||||
echo "🔹 Testing /health" | tee -a "$LOG_FILE"
|
||||
curl -s -w "\nStatus: %{http_code}\n" "$API_URL/health" | tee -a "$LOG_FILE"
|
||||
echo "--------------------------------------" | tee -a "$LOG_FILE"
|
||||
# Health
|
||||
status=$(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/health)
|
||||
echo "🔹 Testing /health"
|
||||
check_status $status | tee -a $LOG_FILE
|
||||
|
||||
# 2. Get all fruits
|
||||
echo "🔹 Testing GET /fruits" | tee -a "$LOG_FILE"
|
||||
curl -s -w "\nStatus: %{http_code}\n" "$API_URL/fruits" | tee -a "$LOG_FILE"
|
||||
echo "--------------------------------------" | tee -a "$LOG_FILE"
|
||||
# GET fruits
|
||||
status=$(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/fruits)
|
||||
echo "🔹 Testing GET /fruits"
|
||||
check_status $status | tee -a $LOG_FILE
|
||||
|
||||
# 3. Get fruit by ID (1)
|
||||
echo "🔹 Testing GET /fruits/1" | tee -a "$LOG_FILE"
|
||||
curl -s -w "\nStatus: %{http_code}\n" "$API_URL/fruits/1" | tee -a "$LOG_FILE"
|
||||
echo "--------------------------------------" | tee -a "$LOG_FILE"
|
||||
# GET fruit 1
|
||||
status=$(curl -s -o /dev/null -w "%{http_code}" $BASE_URL/fruits/1)
|
||||
echo "🔹 Testing GET /fruits/1"
|
||||
check_status $status | tee -a $LOG_FILE
|
||||
|
||||
# 4. Create new fruit
|
||||
echo "🔹 Testing POST /fruits" | tee -a "$LOG_FILE"
|
||||
curl -s -X POST -H "Content-Type: application/json" \
|
||||
-d '{"name":"Mango","color":"Yellow","price":2.5,"quantity":50,"category":"Tropical"}' \
|
||||
-w "\nStatus: %{http_code}\n" "$API_URL/fruits" | tee -a "$LOG_FILE"
|
||||
echo "--------------------------------------" | tee -a "$LOG_FILE"
|
||||
|
||||
# 5. Update fruit ID 1
|
||||
echo "🔹 Testing PUT /fruits/1" | tee -a "$LOG_FILE"
|
||||
curl -s -X PUT -H "Content-Type: application/json" \
|
||||
-d '{"name":"Green Apple","price":1.8}' \
|
||||
-w "\nStatus: %{http_code}\n" "$API_URL/fruits/1" | tee -a "$LOG_FILE"
|
||||
echo "--------------------------------------" | tee -a "$LOG_FILE"
|
||||
|
||||
# 6. Delete fruit ID 1
|
||||
echo "🔹 Testing DELETE /fruits/1" | tee -a "$LOG_FILE"
|
||||
curl -s -X DELETE -w "\nStatus: %{http_code}\n" "$API_URL/fruits/1" | tee -a "$LOG_FILE"
|
||||
echo "--------------------------------------" | tee -a "$LOG_FILE"
|
||||
|
||||
# 7. Search fruits
|
||||
echo "🔹 Testing GET /fruits/search?name=apple" | tee -a "$LOG_FILE"
|
||||
curl -s -w "\nStatus: %{http_code}\n" "$API_URL/fruits/search?name=apple" | tee -a "$LOG_FILE"
|
||||
echo "--------------------------------------" | tee -a "$LOG_FILE"
|
||||
|
||||
# 8. Get fruits by category
|
||||
echo "🔹 Testing GET /fruits/category/Tropical" | tee -a "$LOG_FILE"
|
||||
curl -s -w "\nStatus: %{http_code}\n" "$API_URL/fruits/category/Tropical" | tee -a "$LOG_FILE"
|
||||
echo "--------------------------------------" | tee -a "$LOG_FILE"
|
||||
|
||||
echo "✅ Tests completed at $(date)" | tee -a "$LOG_FILE"
|
||||
echo "======================================" | tee -a "$LOG_FILE"
|
||||
# POST fruit
|
||||
status=$(curl -s -o /dev/null -w "%{http_code}" -X POST $BASE_URL/fruits \
|
||||
-H "Content-Type: application/json" \
|
||||
-d '{"name":"Mango","color":"Yellow","price":2.5,"quantity":50,"category":"Tropical"}')
|
||||
echo "🔹 Testing POST /fruits"
|
||||
check_status $status | tee -a $LOG_FILE
|
||||
|
المرجع في مشكلة جديدة
حظر مستخدم