-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest-api.sh
More file actions
executable file
·235 lines (187 loc) · 7.59 KB
/
test-api.sh
File metadata and controls
executable file
·235 lines (187 loc) · 7.59 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
#!/bin/bash
# Function to test an endpoint
test_endpoint() {
local endpoint=$1
local params=$2
local url="http://localhost:8080${endpoint}${params}"
echo "Testing endpoint: ${url}"
response=$(curl -s -w "\n%{http_code}" "${url}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
echo "$body" | jq . | head -10
echo "Status code: $http_code"
if [[ $http_code -ne 200 ]]; then
echo "Error: Endpoint returned non-200 status code"
exit 1
fi
echo ""
echo "----------------------"
echo ""
}
# Function to test an endpoint with filter verification
test_filter() {
local endpoint=$1
local params=$2
local filter_check=$3
local description=$4
local url="http://localhost:8080${endpoint}${params}"
echo "Testing filter: ${description}"
echo "URL: ${url}"
response=$(curl -s -w "\n%{http_code}" "${url}")
http_code=$(echo "$response" | tail -n1)
body=$(echo "$response" | sed '$d')
if [[ $http_code -ne 200 ]]; then
echo "Error: Endpoint returned status code $http_code"
echo "$body" | jq .
exit 1
fi
# Run the verification check using jq
# The check should return "true" if it passes
check_result=$(echo "$body" | jq "${filter_check}")
if [[ "$check_result" != "true" ]]; then
echo "Error: Filter verification failed for ${description}"
echo "Verification expression: ${filter_check}"
echo "Actual result: ${check_result}"
echo "Sample data:"
echo "$body" | jq . | head -20
exit 1
fi
echo "✓ Filter verification passed"
echo ""
echo "----------------------"
echo ""
}
# Function to test CORS preflight with OPTIONS request
test_cors_preflight() {
local endpoint=$1
local url="http://localhost:8080${endpoint}"
echo "Testing CORS preflight for: ${url}"
# Send OPTIONS request with CORS headers
response=$(curl -s -X OPTIONS -w "\n%{http_code}" \
-H "Origin: http://example.com" \
-H "Access-Control-Request-Method: GET" \
-H "Access-Control-Request-Headers: Content-Type" \
"${url}")
http_code=$(echo "$response" | tail -n1)
headers=$(curl -s -X OPTIONS -I \
-H "Origin: http://example.com" \
-H "Access-Control-Request-Method: GET" \
-H "Access-Control-Request-Headers: Content-Type" \
"${url}")
# Check for CORS headers
echo "CORS Headers:"
echo "$headers" | grep -i "access-control"
echo "Status code: $http_code"
# OPTIONS preflight should return 204 (No Content) or 200
if [[ $http_code -ne 204 && $http_code -ne 200 ]]; then
echo "Error: CORS preflight failed with non-200/204 status code"
exit 1
fi
# Check for required CORS headers
if ! echo "$headers" | grep -q "Access-Control-Allow-Origin"; then
echo "Error: Missing Access-Control-Allow-Origin header"
exit 1
fi
if ! echo "$headers" | grep -q "Access-Control-Allow-Methods"; then
echo "Error: Missing Access-Control-Allow-Methods header"
exit 1
fi
echo "CORS preflight check passed!"
echo ""
echo "----------------------"
echo ""
}
# Start tests
echo "Testing API endpoints..."
echo "----------------------"
echo ""
# Test health check
test_cors_preflight "/"
test_endpoint "/" ""
# Test technologies endpoint
test_cors_preflight "/v1/technologies"
test_endpoint "/v1/technologies" "?onlyname=true"
test_endpoint "/v1/technologies" "?technology=WordPress&onlyname=true"
test_endpoint "/v1/technologies" "?technology=WordPress&onlyname=true&fields=technology,icon"
test_endpoint "/v1/technologies" "?technology=WordPress&category=CMS&fields=technology,icon"
# Test categories endpoint
test_cors_preflight "/v1/categories"
test_endpoint "/v1/categories" "?category=CMS&onlyname=true"
test_endpoint "/v1/categories" "?category=CMS&fields=category"
# Test ranks endpoint
test_endpoint "/v1/ranks" ""
# Test geos endpoint
test_endpoint "/v1/geos" ""
# Test filter correspondences
echo "Testing Filter Correspondences..."
echo "----------------------"
echo ""
# Test adoption defaults (tech=ALL)
test_filter "/v1/adoption" "" \
"all(.[]; .technology == \"ALL\") and length > 0" \
"Adoption defaults (technology=ALL)"
# Test adoption specific technology
test_filter "/v1/adoption" "?technology=WordPress" \
"all(.[]; .technology == \"WordPress\") and length > 0" \
"Adoption specific technology (WordPress)"
# Test adoption specific geo and rank (verifying it returns data)
test_filter "/v1/adoption" "?technology=WordPress&geo=Mexico&rank=Top%201M" \
"all(.[]; .technology == \"WordPress\") and length > 0" \
"Adoption specific geo and rank (returns WordPress data)"
# Test CWV defaults (tech=ALL)
test_filter "/v1/cwv" "" \
"all(.[]; .technology == \"ALL\") and length > 0" \
"CWV defaults (technology=ALL)"
# Test technologies default
test_filter "/v1/technologies" "" \
"length > 0" \
"Technologies list is not empty"
# Test categories default
test_filter "/v1/categories" "" \
"length > 0" \
"Categories list is not empty"
# Test geo-breakdown endpoint
test_cors_preflight "/v1/geo-breakdown"
test_endpoint "/v1/geo-breakdown" ""
test_endpoint "/v1/geo-breakdown" "?technology=WordPress"
test_endpoint "/v1/geo-breakdown" "?technology=WordPress&rank=Top%201M"
# Test geo-breakdown filter correspondences
test_filter "/v1/geo-breakdown" "" \
"all(.[]; .technology == \"ALL\") and length > 0" \
"Geo breakdown defaults (technology=ALL)"
test_filter "/v1/geo-breakdown" "?technology=WordPress" \
"all(.[]; .technology == \"WordPress\") and length > 0" \
"Geo breakdown specific technology (WordPress)"
test_filter "/v1/geo-breakdown" "?technology=WordPress" \
"all(.[]; has(\"geo\")) and length > 0" \
"Geo breakdown response includes geo field"
# Test cwv-distribution endpoint
test_cors_preflight "/v1/cwv-distribution"
test_endpoint "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01"
test_endpoint "/v1/cwv-distribution" "?technology=Wix,WordPress&date=2026-02-01"
test_endpoint "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01&rank=10000"
test_endpoint "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01&geo=ALL"
test_endpoint "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01&geo=United%20States%20of%20America"
# Test cwv-distribution filter correspondences
test_filter "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01" \
"all(.[]; .technology == \"Wix\") and length > 0" \
"CWV distribution single technology (Wix)"
test_filter "/v1/cwv-distribution" "?technology=Wix,WordPress&date=2026-02-01" \
"all(.[]; .technology == \"Wix\" or .technology == \"WordPress\") and length > 0" \
"CWV distribution multiple technologies (Wix, WordPress)"
test_filter "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01" \
"all(.[]; has(\"loading_bucket\") and has(\"lcp_origins\") and has(\"inp_origins\") and has(\"cls_origins\")) and length > 0" \
"CWV distribution response includes histogram bucket fields"
test_filter "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01" \
"[.[].client] | unique | sort == [\"desktop\", \"mobile\"]" \
"CWV distribution returns both desktop and mobile clients"
test_filter "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01" \
"all(.[]; has(\"geo\")) and length > 0" \
"CWV distribution response includes geo field"
test_filter "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01" \
"all(.[]; .geo == \"ALL\") and length > 0" \
"CWV distribution defaults to geo=ALL"
test_filter "/v1/cwv-distribution" "?technology=Wix&date=2026-02-01&geo=United%20States%20of%20America" \
"all(.[]; .geo == \"United States of America\") and length > 0" \
"CWV distribution filters by specific geo (United States of America)"
echo "API tests complete! All endpoints returned 200 and data corresponds to filters."