test-api-endpoints.js•2.31 kB
// Test SpiderFoot API endpoints directly
import axios from 'axios';
const API_BASE_URL = 'http://localhost:5001';
async function testEndpoint(endpoint, method = 'GET', data = null) {
try {
const url = `${API_BASE_URL}${endpoint}`;
const config = {
method,
url,
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
};
if (data) {
if (method === 'GET') {
config.params = data;
} else {
config.data = data;
}
}
console.log(`\n=== Testing ${method} ${endpoint} ===`);
const response = await axios(config);
console.log('Status:', response.status);
console.log('Response:', response.data);
return response.data;
} catch (error) {
console.error(`Error testing ${endpoint}:`);
if (error.response) {
console.error('Status:', error.response.status);
console.error('Data:', error.response.data);
console.error('Headers:', error.response.headers);
} else if (error.request) {
console.error('No response received:', error.request);
} else {
console.error('Error:', error.message);
}
throw error;
}
}
async function testAllEndpoints() {
try {
// 1. Test ping endpoint
await testEndpoint('/ping');
// 2. Test modules endpoint
await testEndpoint('/modules');
// 3. Test scans endpoint
await testEndpoint('/scans');
// 4. Test scan start with form data
try {
const formData = new URLSearchParams();
formData.append('scanname', `test-scan-${Date.now()}`);
formData.append('scantarget', 'example.com');
formData.append('modulelist', 'sfp_dnsresolve');
formData.append('scantype', 'domain');
formData.append('usecase', 'all');
await testEndpoint('/scan/new', 'POST', formData, {
'Content-Type': 'application/x-www-form-urlencoded'
});
} catch (error) {
console.error('Note: Start scan failed, but we can continue testing other endpoints');
}
// 5. Test info endpoint
await testEndpoint('/info');
console.log('\n✅ All tests completed!');
} catch (error) {
console.error('\n❌ Test failed:', error.message);
process.exit(1);
}
}
testAllEndpoints();