forked from Manuel1234477/Stellar-Micro-Donation-API
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-rate-limit.js
More file actions
198 lines (165 loc) · 5.88 KB
/
test-rate-limit.js
File metadata and controls
198 lines (165 loc) · 5.88 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
/**
* Simple test script to verify rate limiting functionality
* Tests that donation endpoints properly enforce rate limits
*
* Usage: node test-rate-limit.js
*/
const http = require('http');
const BASE_URL = 'http://localhost:3000';
// Helper function to make HTTP POST request
function makeRequest(path, data) {
return new Promise((resolve, reject) => {
const postData = JSON.stringify(data);
const options = {
hostname: 'localhost',
port: 3000,
path: path,
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData),
'X-API-Key': 'test-api-key',
'Idempotency-Key': `test-${Date.now()}-${Math.random()}`
}
};
const req = http.request(options, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
resolve({
statusCode: res.statusCode,
headers: res.headers,
body: body
});
});
});
req.on('error', (error) => {
reject(error);
});
req.write(postData);
req.end();
});
}
// Test rate limiting on donation creation endpoint
async function testDonationRateLimit() {
console.log('Testing Rate Limiting on POST /donations');
console.log('Expected: First 10 requests succeed, 11th request gets 429\n');
const testData = {
amount: 10,
recipient: 'GXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
donor: 'Anonymous'
};
let successCount = 0;
let rateLimitedCount = 0;
for (let i = 1; i <= 12; i++) {
try {
const response = await makeRequest('/donations', testData);
console.log(`Request ${i}: Status ${response.statusCode}`);
if (response.statusCode === 201 || response.statusCode === 200) {
successCount++;
console.log(` ✓ Success`);
} else if (response.statusCode === 429) {
rateLimitedCount++;
const body = JSON.parse(response.body);
console.log(` ✗ Rate Limited: ${body.error.message}`);
console.log(` Rate Limit Headers:`);
console.log(` - Limit: ${response.headers['ratelimit-limit']}`);
console.log(` - Remaining: ${response.headers['ratelimit-remaining']}`);
console.log(` - Reset: ${response.headers['ratelimit-reset']}`);
} else {
console.log(` ? Unexpected status: ${response.statusCode}`);
console.log(` Body: ${response.body}`);
}
// Small delay between requests
await new Promise(resolve => setTimeout(resolve, 100));
} catch (error) {
console.error(`Request ${i} failed:`, error.message);
}
}
console.log('\n--- Test Summary ---');
console.log(`Successful requests: ${successCount}`);
console.log(`Rate limited requests: ${rateLimitedCount}`);
if (successCount === 10 && rateLimitedCount === 2) {
console.log('✓ Rate limiting is working correctly!');
} else {
console.log('✗ Rate limiting may not be configured correctly');
console.log(' Expected: 10 successful, 2 rate limited');
}
}
// Test verification endpoint rate limit (should be more lenient)
async function testVerificationRateLimit() {
console.log('\n\nTesting Rate Limiting on POST /donations/verify');
console.log('Expected: First 30 requests succeed, 31st request gets 429\n');
const testData = {
transactionHash: 'test-hash-' + Date.now()
};
let successCount = 0;
let rateLimitedCount = 0;
let errorCount = 0;
// Test first 32 requests
for (let i = 1; i <= 32; i++) {
try {
const response = await makeRequest('/donations/verify', testData);
if (i === 1 || i === 30 || i === 31 || i === 32) {
console.log(`Request ${i}: Status ${response.statusCode}`);
} else if (i === 2) {
console.log('... (requests 2-29) ...');
}
if (response.statusCode === 200 || response.statusCode === 500) {
// 500 is expected since we're using fake data
successCount++;
} else if (response.statusCode === 429) {
rateLimitedCount++;
if (i >= 31) {
const body = JSON.parse(response.body);
console.log(` ✗ Rate Limited: ${body.error.message}`);
}
}
await new Promise(resolve => setTimeout(resolve, 50));
} catch (error) {
errorCount++;
}
}
console.log('\n--- Test Summary ---');
console.log(`Successful requests: ${successCount}`);
console.log(`Rate limited requests: ${rateLimitedCount}`);
if (successCount === 30 && rateLimitedCount === 2) {
console.log('✓ Verification rate limiting is working correctly!');
} else {
console.log('✗ Verification rate limiting may not be configured correctly');
console.log(' Expected: 30 successful, 2 rate limited');
}
}
// Run tests
async function runTests() {
console.log('='.repeat(60));
console.log('Rate Limiting Test Suite');
console.log('='.repeat(60));
console.log('\nMake sure the API server is running on http://localhost:3000\n');
try {
await testDonationRateLimit();
// Wait for rate limit to reset
console.log('\n\nWaiting 60 seconds for rate limit to reset...');
await new Promise(resolve => setTimeout(resolve, 60000));
await testVerificationRateLimit();
console.log('\n' + '='.repeat(60));
console.log('Tests completed!');
console.log('='.repeat(60));
} catch (error) {
console.error('\nTest suite failed:', error);
}
}
// Check if server is running before starting tests
http.get('http://localhost:3000/health', (res) => {
if (res.statusCode === 200) {
runTests();
} else {
console.error('Server is not responding correctly. Please start the API server first.');
}
}).on('error', (error) => {
console.error('Cannot connect to server. Please start the API server first:');
console.error(' npm start');
process.exit(1);
});