-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathserver-test.bash
More file actions
261 lines (211 loc) · 9.65 KB
/
server-test.bash
File metadata and controls
261 lines (211 loc) · 9.65 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
#!/bin/bash
# Colors for output
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
echo -e "${BLUE}🧪 Testing JavaScript Executor Microservice${NC}"
echo "=============================================="
# Configuration
BASE_URL="http://localhost:3217"
API_SECRET="secret-key-here"
# Test counters
TOTAL_TESTS=0
PASSED_TESTS=0
FAILED_TESTS=0
# Function to test and display result
test_endpoint() {
local description="$1"
local method="$2"
local endpoint="$3"
local data="$4"
local expected_status="$5"
local expected_result="$6"
local test_type="$7" # "auth" or "normal"
TOTAL_TESTS=$((TOTAL_TESTS + 1))
echo ""
echo -e "${YELLOW}📋 Test $TOTAL_TESTS: $description${NC}"
echo "➡️ Executing..."
# Prepare headers based on test type
if [ "$test_type" = "auth" ]; then
headers="-H 'X-API-Secret: $API_SECRET' -H 'User-Agent: GuzzleHttp/7.0' -H 'Content-Type: application/json'"
else
headers="-H 'Content-Type: application/json'"
fi
# Execute request
if [ "$method" = "GET" ]; then
if [ "$test_type" = "auth" ]; then
response=$(curl -s -w "\n%{http_code}" -H "X-API-Secret: $API_SECRET" -H "User-Agent: GuzzleHttp/7.0" "$BASE_URL$endpoint")
else
response=$(curl -s -w "\n%{http_code}" "$BASE_URL$endpoint")
fi
else
if [ "$test_type" = "auth" ]; then
response=$(curl -s -w "\n%{http_code}" -X "$method" "$BASE_URL$endpoint" \
-H "X-API-Secret: $API_SECRET" \
-H "User-Agent: GuzzleHttp/7.0" \
-H "Content-Type: application/json" \
-d "$data")
else
response=$(curl -s -w "\n%{http_code}" -X "$method" "$BASE_URL$endpoint" \
-H "Content-Type: application/json" \
-d "$data")
fi
fi
# Separate body and status code
body=$(echo "$response" | head -n -1)
status_code=$(echo "$response" | tail -n 1)
echo "🔍 Status: $status_code"
echo "📤 Response: $body"
# Validate results
local test_passed=true
# Check status code
if [ "$status_code" != "$expected_status" ]; then
echo -e "${RED}❌ Expected status $expected_status, got $status_code${NC}"
test_passed=false
fi
# Show raw body for debugging
echo "📝 Raw body: $body"
# Check result if provided
if [ -n "$expected_result" ] && [ "$status_code" = "200" ]; then
# Extract the result field, preserving types (null, boolean, number, string)
if command -v jq >/dev/null 2>&1; then
result=$(echo "$body" | jq -cM '.result' 2>/dev/null)
else
result=""
fi
# Show for debugging
echo "🧪 Raw .result: $result"
if [ -z "$result" ]; then
echo -e "${YELLOW}⚠️ .result extraction returned empty!${NC}"
fi
# Normalize for comparison
if [ "$expected_result" = "null" ]; then
if [ "$result" != "null" ]; then
echo -e "${RED}❌ Expected result 'null', got '$result'${NC}"
test_passed=false
fi
elif [ "$expected_result" = "true" ] || [ "$expected_result" = "false" ]; then
if [ "$result" != "$expected_result" ]; then
echo -e "${RED}❌ Expected result '$expected_result', got '$result'${NC}"
test_passed=false
fi
elif [[ "$expected_result" =~ ^[0-9]+(\.[0-9]+)?$ ]]; then
# Number: compare as string
if [ "$result" != "$expected_result" ]; then
echo -e "${RED}❌ Expected result '$expected_result', got '$result'${NC}"
test_passed=false
fi
elif [ "$description" = "JSON Stringify" ]; then
# For JSON Stringify, compare ignoring outer quotes
result_stripped=$(echo "$result" | sed 's/^"//;s/"$//')
expected_stripped=$(echo "$expected_result" | sed 's/^"//;s/"$//')
if [ "$result_stripped" != "$expected_stripped" ]; then
echo -e "${RED}❌ Expected result '$expected_stripped', got '$result_stripped'${NC}"
test_passed=false
fi
else
# String: remove quotes from jq
result_stripped=$(echo "$result" | sed 's/^"//;s/"$//')
if [ "$result_stripped" != "$expected_result" ]; then
echo -e "${RED}❌ Expected result '$expected_result', got '$result_stripped'${NC}"
test_passed=false
fi
fi
fi
# Check for error if expected
if [ "$expected_status" = "400" ] || [ "$expected_status" = "403" ]; then
if command -v jq >/dev/null 2>&1; then
error=$(echo "$body" | jq -r '.error // empty' 2>/dev/null)
else
error=""
fi
if [ -z "$error" ]; then
echo -e "${RED}❌ Expected error message but got none${NC}"
test_passed=false
fi
fi
if [ "$test_passed" = true ]; then
echo -e "${GREEN}✅ PASSED${NC}"
PASSED_TESTS=$((PASSED_TESTS + 1))
else
echo -e "${RED}❌ FAILED${NC}"
FAILED_TESTS=$((FAILED_TESTS + 1))
fi
echo "---"
}
echo -e "${BLUE}🔒 Security Tests (Should Fail)${NC}"
echo "================================"
# Security Test 1: No authentication
test_endpoint "Health Check without Auth" "GET" "/health" "" "403" "" "normal"
# Security Test 2: Wrong User-Agent
test_endpoint "Wrong User-Agent" "POST" "/execute" '{"code":"1+1"}' "403" "" "normal"
# Security Test 3: Missing API Secret
test_endpoint "Missing API Secret" "POST" "/execute" '{"code":"1+1"}' "403" "" "normal"
echo -e "${BLUE}✅ Functional Tests (Should Pass)${NC}"
echo "================================="
# Test 1: Health Check with auth
test_endpoint "Health Check with Auth" "GET" "/health" "" "200" "" "auth"
# Test 2: Simple arithmetic
test_endpoint "Simple Code (1+1)" "POST" "/execute" '{"code":"1 + 1"}' "200" "2" "auth"
# Test 3: Return statement
test_endpoint "Explicit Return" "POST" "/execute" '{"code":"return 1 + 1;"}' "200" "2" "auth"
# Test 4: Variable assignment and return
test_endpoint "Variable Assignment" "POST" "/execute" '{"code":"const x = 5; const y = 10; return x * y;"}' "200" "50" "auth"
# Test 5: Fibonacci function
test_endpoint "Fibonacci (10)" "POST" "/execute" '{"code":"function fibonacci(n) { if (n <= 1) return n; return fibonacci(n-1) + fibonacci(n-2); } return fibonacci(10);"}' "200" "55" "auth"
# Test 6: Array operations
test_endpoint "Array Manipulation" "POST" "/execute" '{"code":"const arr = [1, 2, 3, 4, 5]; return arr.map(x => x * 2).reduce((a, b) => a + b, 0);"}' "200" "30" "auth"
# Test 7: JSON operations
test_endpoint "JSON Stringify" "POST" "/execute" '{"code":"const obj = { name: \"John\", age: 30 }; return JSON.stringify(obj);"}' "200" '"{\"name\":\"John\",\"age\":30}"' "auth"
# Test 8: Math operations
test_endpoint "Math Operations" "POST" "/execute" '{"code":"return Math.PI * Math.pow(5, 2);"}' "200" "78.53981633974483" "auth"
# Test 9: String operations
test_endpoint "String Operations" "POST" "/execute" '{"code":"const str = \"Hello World\"; return str.toUpperCase().split(\" \" ).join(\"-\");"}' "200" "HELLO-WORLD" "auth"
# Test 10: Boolean logic
test_endpoint "Boolean Logic" "POST" "/execute" '{"code":"const a = true; const b = false; return a && !b;"}' "200" "true" "auth"
echo -e "${BLUE}🚨 Error Handling Tests${NC}"
echo "======================"
# Error Test 1: Undefined variable
test_endpoint "Undefined Variable" "POST" "/execute" '{"code":"return undefinedVariable;"}' "400" "" "auth"
# Error Test 2: Syntax error
test_endpoint "Syntax Error" "POST" "/execute" '{"code":"return 1 + ;"}' "400" "" "auth"
# Error Test 3: Empty code
test_endpoint "Empty Code" "POST" "/execute" '{"code":""}' "400" "" "auth"
# Error Test 4: Only whitespace
test_endpoint "Whitespace Only" "POST" "/execute" '{"code":" "}' "400" "" "auth"
# Error Test 5: Missing code field
test_endpoint "Missing Code Field" "POST" "/execute" '{}' "400" "" "auth"
# Error Test 6: Invalid JSON
test_endpoint "Invalid JSON" "POST" "/execute" 'invalid json' "400" "" "auth"
echo -e "${BLUE}🔄 Async Tests${NC}"
echo "=============="
# Async Test 1: Simple Promise resolution
test_endpoint "Promise Resolution" "POST" "/execute" '{"code":"const result = await Promise.resolve(42); return result;"}' "200" "42" "auth"
# Async Test 2: Multiple awaits
test_endpoint "Multiple Awaits" "POST" "/execute" '{"code":"const a = await Promise.resolve(10); const b = await Promise.resolve(20); return a + b;"}' "200" "30" "auth"
# Async Test 3: Async function
test_endpoint "Async Function" "POST" "/execute" '{"code":"async function getValue() { return await Promise.resolve(100); } return await getValue();"}' "200" "100" "auth"
echo -e "${BLUE}🎯 Edge Cases${NC}"
echo "============="
# Edge Case 1: Null return
test_endpoint "Null Return" "POST" "/execute" '{"code":"return null;"}' "200" "null" "auth"
# Edge Case 2: Undefined return
test_endpoint "Undefined Return" "POST" "/execute" '{"code":"return undefined;"}' "200" "null" "auth"
# Edge Case 3: Large number
test_endpoint "Large Number" "POST" "/execute" '{"code":"return 9007199254740991;"}' "200" "9007199254740991" "auth"
# Summary
echo ""
echo -e "${BLUE}📊 Test Summary${NC}"
echo "==============="
echo -e "Total Tests: ${YELLOW}$TOTAL_TESTS${NC}"
echo -e "Passed: ${GREEN}$PASSED_TESTS${NC}"
echo -e "Failed: ${RED}$FAILED_TESTS${NC}"
if [ $FAILED_TESTS -eq 0 ]; then
echo -e "${GREEN}🎉 All tests passed!${NC}"
exit 0
else
echo -e "${RED}💥 Some tests failed!${NC}"
exit 1
fi