-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest-api.js
74 lines (62 loc) · 1.76 KB
/
test-api.js
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
const http = require('http');
function makeRequest(options, data = null) {
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
let responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
try {
const parsedData = JSON.parse(responseData);
resolve({
statusCode: res.statusCode,
headers: res.headers,
data: parsedData
});
} catch (error) {
reject(new Error(`Failed to parse response: ${error.message}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
if (data) {
req.write(data);
}
req.end();
});
}
async function testApi() {
try {
console.log('Testing API endpoint...');
// Test the test endpoint
console.log('Testing /api/test endpoint...');
const testResponse = await makeRequest({
hostname: 'localhost',
port: 3001,
path: '/api/test',
method: 'GET'
});
console.log('Test endpoint response:', testResponse);
// Test the chat endpoint
console.log('\nTesting /api/chat endpoint...');
const postData = JSON.stringify({ message: 'Hello, tell me about taxes' });
const chatResponse = await makeRequest({
hostname: 'localhost',
port: 3001,
path: '/api/chat',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Content-Length': Buffer.byteLength(postData)
}
}, postData);
console.log('Chat endpoint response:', chatResponse);
console.log('\nAPI test completed.');
} catch (error) {
console.error('Error testing API:', error);
}
}
testApi();