-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathtest-api.js
More file actions
111 lines (99 loc) · 2.83 KB
/
test-api.js
File metadata and controls
111 lines (99 loc) · 2.83 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
const http = require('http');
const testEndpoints = async () => {
// Use environment variables or defaults for test data
const userId = process.env.TEST_USER_ID || 'test-user-1';
const pwdHash = process.env.TEST_PWD_HASH || 'test-hash-default';
const baseUrl = process.env.API_BASE_URL || 'http://localhost:7001';
// Test GET /sync/lock
console.log('\n📍 Testing GET /sync/lock');
await makeRequest('GET', `/sync/lock?userId=${userId}`);
// Test POST /sync/lock
console.log('\n📍 Testing POST /sync/lock');
await makeRequest('POST', `/sync/lock?userId=${userId}`, {
pwdHash: pwdHash,
lock: {
encryptedPassword: 'encrypted-pwd',
salt: 'salt-123',
},
});
// Test POST /sync/check
console.log('\n📍 Testing POST /sync/check');
await makeRequest('POST', `/sync/check?userId=${userId}`, {
pwdHash: pwdHash,
localData: [],
onlyCheckLocalDataType: [],
});
// Test POST /sync/download
console.log('\n📍 Testing POST /sync/download');
await makeRequest('POST', `/sync/download?userId=${userId}`, {
pwdHash: pwdHash,
skip: 0,
limit: 10,
});
// Test POST /sync/upload
console.log('\n📍 Testing POST /sync/upload');
await makeRequest('POST', `/sync/upload?userId=${userId}`, {
pwdHash: pwdHash,
localData: [
{
key: 'test-key-1',
dataType: 'settings',
data: { test: 'data' },
dataTimestamp: Date.now(),
isDeleted: false,
},
],
});
// Test POST /sync/flush
console.log('\n📍 Testing POST /sync/flush');
await makeRequest('POST', `/sync/flush?userId=${userId}`, {
pwdHash: `${pwdHash}-new`,
localData: [],
lock: {
encryptedPassword: 'new-encrypted-pwd',
salt: 'new-salt-456',
},
});
};
function makeRequest(method, path, body) {
return new Promise((resolve, reject) => {
const options = {
hostname: 'localhost',
port: 7001,
path,
method,
headers: {
'Content-Type': 'application/json',
},
};
const req = http.request(options, (res) => {
let data = '';
res.on('data', (chunk) => {
data += chunk;
});
res.on('end', () => {
console.log(`Response Status: ${res.statusCode}`);
try {
const parsed = JSON.parse(data);
console.log('Response:', JSON.stringify(parsed, null, 2));
} catch (e) {
console.log('Response:', data);
}
resolve();
});
});
req.on('error', (error) => {
console.error('Request failed:', error);
reject(error);
});
if (body) {
req.write(JSON.stringify(body));
}
req.end();
});
}
// Run tests
console.log('🚀 Testing Sync Mock App Endpoints...');
testEndpoints()
.then(() => console.log('\n✅ All tests completed'))
.catch((err) => console.error('\n❌ Test failed:', err));