-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.js
More file actions
56 lines (47 loc) · 1.77 KB
/
test-api.js
File metadata and controls
56 lines (47 loc) · 1.77 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
const axios = require('axios');
const BASE_URL = 'http://localhost:3000';
async function testAPI() {
console.log('🧪 Testing User API...\n');
try {
// Test health endpoint
console.log('1. Testing health endpoint...');
const healthResponse = await axios.get(`${BASE_URL}/health`);
console.log('✅ Health check passed:', healthResponse.data);
console.log('');
// Test with invalid ObjectId
console.log('2. Testing invalid ObjectId...');
try {
await axios.get(`${BASE_URL}/users/invalid-id`);
} catch (error) {
if (error.response?.status === 400) {
console.log('✅ Invalid ObjectId handled correctly:', error.response.data);
} else {
console.log('❌ Unexpected error for invalid ObjectId:', error.message);
}
}
console.log('');
// Test with non-existent user
console.log('3. Testing non-existent user...');
try {
await axios.get(`${BASE_URL}/users/507f1f77bcf86cd799439011`);
} catch (error) {
if (error.response?.status === 404) {
console.log('✅ Non-existent user handled correctly:', error.response.data);
} else {
console.log('❌ Unexpected error for non-existent user:', error.message);
}
}
console.log('');
console.log('🎉 API tests completed!');
console.log('\n📝 Note: To test with real data, insert users into your MongoDB collection first.');
console.log(' Example: db.users.insertOne({ name: "John Doe", email: "john@example.com", age: 30 })');
} catch (error) {
console.error('❌ Test failed:', error.message);
console.log('\n💡 Make sure the server is running on port 3000');
}
}
// Only run if this file is executed directly
if (require.main === module) {
testAPI();
}
module.exports = { testAPI };