#!/usr/bin/env bash # test_api.sh # Script to test Fruit API endpoints and log responses API_URL="http://localhost:5000" LOG_FILE="test_results.log" echo "🚀 Running API tests at $(date)" | tee -a "$LOG_FILE" echo "======================================" | tee -a "$LOG_FILE" # 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" # 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" # 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" # 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"