diff options
| author | Li Zhineng <[email protected]> | 2025-07-02 20:45:20 +0800 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-07-02 20:45:20 +0800 |
| commit | fd5876743c2070a5259c36110e7e02f80ed80458 (patch) | |
| tree | a91673aba254eab609f086d203fd1fe5c90c36bb | |
| parent | 6c131da8eb6e20c6fceed35f548c8c0921239e6b (diff) | |
| parent | 40085a4679d88d2c0a141b95ee6818ac7420dae3 (diff) | |
| download | server-fd5876743c2070a5259c36110e7e02f80ed80458.tar.gz server-fd5876743c2070a5259c36110e7e02f80ed80458.zip | |
Merge pull request #2 from openairmx/genid
Update the payload data structure for the device registration endpoint
| -rw-r--r-- | server.mjs | 20 | ||||
| -rw-r--r-- | server.test.mjs | 74 |
2 files changed, 84 insertions, 10 deletions
@@ -30,10 +30,24 @@ const getTimeControlller = (req, res) => { const eagleController = (req, res) => { const [, query] = req.url.split('?') const params = new URLSearchParams(query) - const mac = params.get('mac') - const key = params.get('key') - if (mac === null || key === null) { + switch (params.get('path')) { + case 'eagle/GET/genId': + eagleGenIdController(req, res) + break + default: + notFoundController(req, res) + break + } +} + +const eagleGenIdController = (req, res) => { + const [, query] = req.url.split('?') + const params = new URLSearchParams(query) + const { mac, key } = JSON.parse(params.get('params') || '{}') + + if (mac === undefined || mac === null + || key === undefined || key === null) { res.writeHead(400, { 'Content-Type': 'application/json' }) res.end('{}\n') return diff --git a/server.test.mjs b/server.test.mjs index b488b80..1e44083 100644 --- a/server.test.mjs +++ b/server.test.mjs @@ -10,7 +10,21 @@ const baseUrl = `http://${hostname}:${port}` let server = null const createDevice = async (mac, key) => { - const response = await fetch(`${baseUrl}/eagle?mac=${mac}&key=${key}`) + const params = new URLSearchParams({ + source: 5, + reqid: '0000000000', + eagleId: '', + path: 'eagle/GET/genId', + params: JSON.stringify({ + mac, + key, + version: '00.00.00', + isDenoise: 1, + hardVersion: 1 + }), + sig: '00000000000000000000000000000000' + }) + const response = await fetch(`${baseUrl}/eagle?${params.toString()}`) const data = await response.json() return data.data.eagleId } @@ -58,9 +72,23 @@ test('current time endpoint', async () => { }) test('device registration endpoint', async () => { - const stubMacAddress = '00:00:00:00:00' + const stubMacAddress = '0000000000' const stubKey = '00000000000000000000000000000000' - const res = await fetch(`${baseUrl}/eagle?mac=${stubMacAddress}&key=${stubKey}`) + const params = new URLSearchParams({ + source: 5, + reqid: '0000000000', + eagleId: '', + path: 'eagle/GET/genId', + params: JSON.stringify({ + mac: stubMacAddress, + key: stubKey, + version: '00.00.00', + isDenoise: 1, + hardVersion: 1 + }), + sig: '00000000000000000000000000000000' + }) + const res = await fetch(`${baseUrl}/eagle?${params.toString()}`) assert.strictEqual(res.status, 200) assert.strictEqual(res.headers.get('content-type'), 'application/json') const data = await res.json() @@ -70,20 +98,52 @@ test('device registration endpoint', async () => { test('device registration endpoint returns bad request if missing mac address', async () => { const stubKey = '00000000000000000000000000000000' - const res = await fetch(`${baseUrl}/eagle?key=${stubKey}`) + const params = new URLSearchParams({ + source: 5, + reqid: '0000000000', + eagleId: '', + path: 'eagle/GET/genId', + params: JSON.stringify({ + key: stubKey, + version: '00.00.00', + isDenoise: 1, + hardVersion: 1 + }), + sig: '00000000000000000000000000000000' + }) + const res = await fetch(`${baseUrl}/eagle?${params.toString()}`) assert.strictEqual(res.status, 400) assert.strictEqual(res.headers.get('content-type'), 'application/json') }) test('device registration endpoint returns bad request if missing encryption key', async () => { - const stubMacAddress = '00:00:00:00:00' - const res = await fetch(`${baseUrl}/eagle?mac=${stubMacAddress}`) + const stubMacAddress = '0000000000' + const params = new URLSearchParams({ + source: 5, + reqid: '0000000000', + eagleId: '', + path: 'eagle/GET/genId', + params: JSON.stringify({ + mac: stubMacAddress, + version: '00.00.00', + isDenoise: 1, + hardVersion: 1 + }), + sig: '00000000000000000000000000000000' + }) + const res = await fetch(`${baseUrl}/eagle?${params.toString()}`) assert.strictEqual(res.status, 400) assert.strictEqual(res.headers.get('content-type'), 'application/json') }) +test('device registration endpoint returns not found if path is not supported', async () => { + const params = new URLSearchParams({ path: 'eagle/GET/foo' }) + const res = await fetch(`${baseUrl}/eagle?${params.toString()}`) + assert.strictEqual(res.status, 404) +}) + test('exchange endpoint', async () => { - const stubMacAddress = '00:00:00:00:00' + const stubMacAddress = '0000000000' const stubKey = '00000000000000000000000000000000' const deviceId = await createDevice(stubMacAddress, stubKey) |
