-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrouteTester.js
More file actions
104 lines (91 loc) · 2.95 KB
/
routeTester.js
File metadata and controls
104 lines (91 loc) · 2.95 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
/**
* Route testing utility
* Place this file in your project root and run it with:
* node routeTester.js [routeToTest] [method]
*
* Example: node routeTester.js /api/users/profile/8 GET
*/
require('dotenv').config();
const axios = require('axios');
const jwt = require('jsonwebtoken');
const { User } = require('./models');
// Configuration
const BASE_URL = 'http://localhost:5000';
const DEFAULT_USER_ID = 8; // User ID to use for testing
// Create a test token for authentication
const createTestToken = async (userId) => {
try {
const user = await User.findByPk(userId);
if (!user) {
console.error(`User with ID ${userId} not found`);
return null;
}
const token = jwt.sign(
{ id: user.id, role: user.role },
process.env.JWT_SECRET || 'your-secret-key',
{ expiresIn: '1h' }
);
return token;
} catch (error) {
console.error('Error creating test token:', error);
return null;
}
};
const testRoute = async (route, method = 'GET') => {
try {
// Create test token
const token = await createTestToken(DEFAULT_USER_ID);
if (!token) {
console.error('Failed to create authentication token. Check database connection.');
return;
}
console.log(`Testing ${method} ${route}`);
console.log('Using auth token for user ID:', DEFAULT_USER_ID);
// Make request
const response = await axios({
method: method.toLowerCase(),
url: `${BASE_URL}${route}`,
headers: {
'Authorization': `Bearer ${token}`,
'Content-Type': 'application/json'
}
});
console.log('\n✅ SUCCESS! Status:', response.status);
console.log('Response data:');
console.log(JSON.stringify(response.data, null, 2).slice(0, 500)); // Show first 500 chars
if (JSON.stringify(response.data).length > 500) {
console.log('... (response truncated)');
}
} catch (error) {
console.error('\n❌ ERROR!');
if (error.response) {
// Server responded with an error
console.error('Status:', error.response.status);
console.error('Response data:', error.response.data);
console.error('Headers:', error.response.headers);
} else if (error.request) {
// Request was made but no response received
console.error('No response received. The server might be down or the route might not exist.');
} else {
// Error setting up the request
console.error('Error setting up the request:', error.message);
}
}
};
// Main function
const main = async () => {
const args = process.argv.slice(2);
const route = args[0] || '/api/users/profile/8';
const method = args[1] || 'GET';
await testRoute(route, method);
};
// Run the test
main()
.then(() => {
console.log('\nTest completed.');
process.exit(0);
})
.catch(error => {
console.error('Unhandled error:', error);
process.exit(1);
});