-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest-api.js
More file actions
132 lines (114 loc) · 3.3 KB
/
test-api.js
File metadata and controls
132 lines (114 loc) · 3.3 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// Simple test script for API endpoints
const http = require('http');
function makeRequest(method, path, body) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'localhost',
port: 3001,
path: path,
method: method,
headers: {
'Content-Type': 'application/json',
'Content-Length': body ? Buffer.byteLength(body) : 0
}
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
resolve({
statusCode: res.statusCode,
body: data
});
});
});
req.on('error', reject);
if (body) {
req.write(body);
}
req.end();
});
}
async function testEndpoints() {
console.log('Testing API Endpoints...\n');
const testEmail = 'testuser' + Date.now() + '@example.com';
const testPassword = 'password123';
const testName = 'Test User';
// Test 1: Signup
console.log('1. Testing /signup endpoint');
try {
const signupData = JSON.stringify({
username: testEmail,
password: testPassword,
name: testName
});
const signupResponse = await makeRequest('POST', '/signup', signupData);
console.log('Status:', signupResponse.statusCode);
console.log('Response:', signupResponse.body);
console.log('');
} catch (error) {
console.error('Signup failed:', error.message);
console.log('');
}
// Test 2: Signin
console.log('2. Testing /signin endpoint');
try {
const signinData = JSON.stringify({
username: testEmail,
password: testPassword
});
const signinResponse = await makeRequest('POST', '/signin', signinData);
console.log('Status:', signinResponse.statusCode);
console.log('Response:', signinResponse.body);
// Extract token if signin successful
let token = null;
if (signinResponse.statusCode === 200) {
const responseData = JSON.parse(signinResponse.body);
token = responseData.token;
console.log('Token extracted:', token ? 'Yes' : 'No');
}
console.log('');
// Test 3: Create Room (requires authentication)
if (token) {
console.log('3. Testing /room endpoint (with valid token)');
const roomData = JSON.stringify({
name: 'TestRoom'
});
const roomOptions = {
hostname: 'localhost',
port: 3001,
path: '/room',
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': token,
'Content-Length': Buffer.byteLength(roomData)
}
};
const roomReq = http.request(roomOptions, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log('Status:', res.statusCode);
console.log('Response:', data);
console.log('');
});
});
roomReq.on('error', (error) => {
console.error('Room creation failed:', error.message);
});
roomReq.write(roomData);
roomReq.end();
} else {
console.log('3. Skipping /room test - no valid token\n');
}
} catch (error) {
console.error('Signin failed:', error.message);
console.log('');
}
}
testEndpoints();