-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.sh
More file actions
executable file
Β·279 lines (222 loc) Β· 8.57 KB
/
test-api.sh
File metadata and controls
executable file
Β·279 lines (222 loc) Β· 8.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
#!/bin/bash
# Complete API Test Script for Task Management API
# Tests all endpoints and various scenarios
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Base URL
BASE_URL="http://localhost:9000"
# Test counter
TESTS_PASSED=0
TESTS_FAILED=0
# Function to print test results
print_test_result() {
local test_name="$1"
local http_code="$2"
local expected_code="$3"
local response="$4"
if [ "$http_code" = "$expected_code" ]; then
echo -e "${GREEN}β
PASS${NC}: $test_name (HTTP $http_code)"
((TESTS_PASSED++))
else
echo -e "${RED}β FAIL${NC}: $test_name (Expected HTTP $expected_code, got $http_code)"
echo -e "${YELLOW}Response:${NC} $response"
((TESTS_FAILED++))
fi
}
# Function to make API request and test response
test_api() {
local test_name="$1"
local method="$2"
local endpoint="$3"
local data="$4"
local expected_code="$5"
if [ -n "$data" ]; then
response=$(curl -s -w "HTTPSTATUS:%{http_code}" -X "$method" \
-H "Content-Type: application/json" \
-d "$data" \
"$BASE_URL$endpoint")
else
response=$(curl -s -w "HTTPSTATUS:%{http_code}" -X "$method" \
"$BASE_URL$endpoint")
fi
http_code=$(echo "$response" | grep -o "HTTPSTATUS:[0-9]*" | cut -d: -f2)
body=$(echo "$response" | sed -E 's/HTTPSTATUS:[0-9]*$//')
print_test_result "$test_name" "$http_code" "$expected_code" "$body"
# Return the response body for further processing
echo "$body"
}
# Function to extract ID from JSON response
extract_id() {
echo "$1" | grep -o '"id":[0-9]*' | cut -d: -f2 | head -1
}
echo -e "${BLUE}π Starting Task Management API Test Suite${NC}"
echo -e "${BLUE}===========================================${NC}"
# Test 1: Health Check
echo -e "\n${YELLOW}π Testing Health & Basic Endpoints${NC}"
test_api "Health Check" "GET" "/health" "" "200"
test_api "Home Page" "GET" "/" "" "200"
# Test 2: Get All Tasks (should have sample data)
echo -e "\n${YELLOW}π Testing Task Retrieval${NC}"
all_tasks_response=$(test_api "Get All Tasks" "GET" "/api/tasks" "" "200")
# Test 3: Get Task Statistics
test_api "Get Task Statistics" "GET" "/api/tasks/stats" "" "200"
# Test 4: Get Tasks by Status
test_api "Get Completed Tasks" "GET" "/api/tasks/completed" "" "200"
test_api "Get Pending Tasks" "GET" "/api/tasks/pending" "" "200"
# Test 5: Get Tasks by Priority
test_api "Get High Priority Tasks" "GET" "/api/tasks/priority/3" "" "200"
test_api "Get Medium Priority Tasks" "GET" "/api/tasks/priority/2" "" "200"
test_api "Get Low Priority Tasks" "GET" "/api/tasks/priority/1" "" "200"
# Test 6: Invalid Priority
test_api "Get Invalid Priority Tasks" "GET" "/api/tasks/priority/99" "" "400"
# Test 7: Get Overdue Tasks
test_api "Get Overdue Tasks" "GET" "/api/tasks/overdue" "" "200"
# Test 8: Get Specific Task (assuming task with ID 1 exists)
test_api "Get Task by ID (1)" "GET" "/api/tasks/1" "" "200"
# Test 9: Get Non-existent Task
test_api "Get Non-existent Task" "GET" "/api/tasks/999" "" "404"
echo -e "\n${YELLOW}β Testing Task Creation${NC}"
# Test 10: Create Valid Task
create_response=$(test_api "Create Valid Task" "POST" "/api/tasks" '{
"title": "Test Task 1",
"description": "This is a test task created by the test script",
"priority": 2,
"completed": false
}' "201")
# Extract the created task ID for further tests
if [ $? -eq 0 ]; then
created_task_id=$(extract_id "$create_response")
echo -e "${GREEN}Created task with ID: $created_task_id${NC}"
fi
# Test 11: Create Task with All Fields
test_api "Create Task with Due Date" "POST" "/api/tasks" '{
"title": "Task with Due Date",
"description": "Task that has a due date",
"priority": 3,
"completed": false,
"dueDate": "2025-06-25"
}' "201"
# Test 12: Create Task - Invalid Data
test_api "Create Task - Empty Title" "POST" "/api/tasks" '{
"title": "",
"description": "Task with empty title",
"priority": 1
}' "400"
# Test 13: Create Task - Invalid JSON
test_api "Create Task - Invalid JSON" "POST" "/api/tasks" '{
"title": "Test Task"
"missing_comma": true
}' "400"
# Test 14: Create Task - Invalid Priority
test_api "Create Task - Invalid Priority" "POST" "/api/tasks" '{
"title": "Test Task",
"priority": 99
}' "400"
echo -e "\n${YELLOW}βοΈ Testing Task Updates${NC}"
# Test 15: Update Task (if we have a created task ID)
if [ -n "$created_task_id" ]; then
test_api "Update Task" "PUT" "/api/tasks/$created_task_id" '{
"title": "Updated Test Task",
"description": "This task has been updated",
"priority": 3,
"completed": true
}' "200"
# Test 16: Mark Task as Completed
test_api "Mark Task as Completed" "PATCH" "/api/tasks/$created_task_id/complete" "" "200"
else
echo -e "${YELLOW}β οΈ Skipping update tests - no created task ID${NC}"
fi
# Test 17: Update Non-existent Task
test_api "Update Non-existent Task" "PUT" "/api/tasks/999" '{
"title": "Non-existent Task",
"priority": 1
}' "400"
echo -e "\n${YELLOW}π Testing Advanced Queries${NC}"
# Test 18: Create Multiple Tasks for Testing
echo -e "${BLUE}Creating test data for advanced queries...${NC}"
# Create high priority task
high_priority_response=$(test_api "Create High Priority Task" "POST" "/api/tasks" '{
"title": "Urgent Task",
"description": "High priority urgent task",
"priority": 3,
"completed": false
}' "201")
high_priority_id=$(extract_id "$high_priority_response")
# Create completed task
completed_response=$(test_api "Create Completed Task" "POST" "/api/tasks" '{
"title": "Completed Task",
"description": "This task is already done",
"priority": 1,
"completed": true
}' "201")
completed_id=$(extract_id "$completed_response")
# Create overdue task (past due date)
test_api "Create Overdue Task" "POST" "/api/tasks" '{
"title": "Overdue Task",
"description": "This task is overdue",
"priority": 2,
"completed": false,
"dueDate": "2025-01-01"
}' "201"
# Test updated statistics
test_api "Get Updated Statistics" "GET" "/api/tasks/stats" "" "200"
echo -e "\n${YELLOW}ποΈ Testing Task Deletion${NC}"
# Test 19: Delete Task (if we have IDs)
if [ -n "$completed_id" ]; then
test_api "Delete Task" "DELETE" "/api/tasks/$completed_id" "" "200"
# Test 20: Verify Deletion
test_api "Get Deleted Task (should fail)" "GET" "/api/tasks/$completed_id" "" "404"
fi
# Test 21: Delete Non-existent Task
test_api "Delete Non-existent Task" "DELETE" "/api/tasks/999" "" "404"
echo -e "\n${YELLOW}π Testing Edge Cases${NC}"
# Test 22: Very Long Title
test_api "Create Task - Very Long Title" "POST" "/api/tasks" "{
\"title\": \"$(printf 'a%.0s' {1..250})\",
\"description\": \"Task with very long title\",
\"priority\": 1
}" "400"
# Test 23: Empty Request Body
test_api "Create Task - Empty Body" "POST" "/api/tasks" "" "415"
# Test 24: Invalid HTTP Method
test_api "Invalid HTTP Method" "PATCH" "/api/tasks" "" "404"
# Test 25: Invalid Endpoint
test_api "Invalid Endpoint" "GET" "/api/invalid" "" "404"
echo -e "\n${YELLOW}π― Testing Content Type Handling${NC}"
# Test 26: Wrong Content Type
response=$(curl -s -w "HTTPSTATUS:%{http_code}" -X "POST" \
-H "Content-Type: text/plain" \
-d "not json" \
"$BASE_URL/api/tasks")
http_code=$(echo "$response" | grep -o "HTTPSTATUS:[0-9]*" | cut -d: -f2)
print_test_result "Wrong Content Type" "$http_code" "415" ""
echo -e "\n${YELLOW}π Final Verification${NC}"
# Test 27: Final state check
final_tasks_response=$(test_api "Final - Get All Tasks" "GET" "/api/tasks" "" "200")
final_stats_response=$(test_api "Final - Get Statistics" "GET" "/api/tasks/stats" "" "200")
echo -e "\n${BLUE}===========================================${NC}"
echo -e "${BLUE}π Test Suite Complete${NC}"
echo -e "${GREEN}β
Tests Passed: $TESTS_PASSED${NC}"
echo -e "${RED}β Tests Failed: $TESTS_FAILED${NC}"
if [ $TESTS_FAILED -eq 0 ]; then
echo -e "${GREEN}π All tests passed! Your API is working perfectly!${NC}"
exit 0
else
echo -e "${RED}β οΈ Some tests failed. Check the output above for details.${NC}"
exit 1
fi
# Performance Test (Optional)
echo -e "\n${YELLOW}β‘ Performance Test (Optional)${NC}"
echo -e "${BLUE}Testing API response times...${NC}"
for i in {1..5}; do
start_time=$(date +%s%N)
curl -s "$BASE_URL/api/tasks" > /dev/null
end_time=$(date +%s%N)
duration=$(( (end_time - start_time) / 1000000 ))
echo "Request $i: ${duration}ms"
done
echo -e "\n${GREEN}β¨ Test script completed!${NC}"