-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest-api.js
More file actions
87 lines (72 loc) · 3.01 KB
/
test-api.js
File metadata and controls
87 lines (72 loc) · 3.01 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
#!/usr/bin/env node
const axios = require('axios');
const API_BASE = 'http://localhost:3000/api';
async function testAPI() {
console.log('🧪 Testing Advanced Crawler API\n');
try {
// Test 1: Health Check
console.log('1. Testing health endpoint...');
const health = await axios.get(`${API_BASE}/health`);
console.log(` ✅ Health: ${health.data.status}`);
// Test 2: Crawler Test Endpoint
console.log('2. Testing crawler test endpoint...');
const crawlerTest = await axios.get(`${API_BASE}/crawler/test`);
console.log(` ✅ Crawler test: ${crawlerTest.data.message}`);
// Test 3: Single URL Crawl
console.log('3. Testing single URL crawl...');
const crawlResult = await axios.post(`${API_BASE}/crawler/crawl`, {
url: 'https://httpbin.org/html',
timeout: 15000,
extractHeadings: true,
extractLinks: true
});
console.log(` ✅ Crawl successful: ${crawlResult.data.url} (${crawlResult.data.status})`);
console.log(` 📊 Duration: ${crawlResult.data.duration}ms`);
console.log(` 📄 Title: ${crawlResult.data.html?.title || 'N/A'}`);
// Test 4: Create Session
console.log('4. Testing session creation...');
const session = await axios.post(`${API_BASE}/crawler/session`, {
userAgent: 'Test Bot 1.0',
description: 'API test session'
});
console.log(` ✅ Session created: ${session.data.sessionId}`);
// Test 5: Get Session Info
console.log('5. Testing session retrieval...');
const sessionInfo = await axios.get(`${API_BASE}/crawler/session/${session.data.sessionId}`);
console.log(` ✅ Session info retrieved: ${sessionInfo.data.session.requestCount} requests`);
// Test 6: Batch Crawl
console.log('6. Testing batch crawl...');
const batchResult = await axios.post(`${API_BASE}/crawler/batch`, {
urls: [
'https://httpbin.org/html',
'https://httpbin.org/json'
],
sessionId: session.data.sessionId,
concurrency: 2,
delay: 500
});
console.log(` ✅ Batch crawl: ${batchResult.data.summary.successful}/${batchResult.data.summary.total} successful`);
// Test 7: Service Status
console.log('7. Testing service status...');
const status = await axios.get(`${API_BASE}/crawler/status`);
console.log(` ✅ Service status: ${status.data.status}`);
console.log(` 📈 Active sessions: ${status.data.statistics.activeSessions}`);
// Test 8: Clean up session
console.log('8. Cleaning up session...');
await axios.delete(`${API_BASE}/crawler/session/${session.data.sessionId}`);
console.log(` ✅ Session deleted successfully`);
console.log('\n🎉 All tests passed! The API is working correctly.');
} catch (error) {
console.error('\n❌ Test failed:', {
message: error.message,
status: error.response?.status,
data: error.response?.data
});
process.exit(1);
}
}
// Run tests if this file is executed directly
if (require.main === module) {
testAPI();
}
module.exports = testAPI;