diff options
| author | Li Zhineng <[email protected]> | 2025-07-01 12:13:45 +0800 |
|---|---|---|
| committer | Li Zhineng <[email protected]> | 2025-07-01 12:13:45 +0800 |
| commit | 7beec630f784d72d21ee621700df42aaa4f4b498 (patch) | |
| tree | 58a51c67bd9471d207aa63774fee8db18ac3503a | |
| parent | 094ce04761766b75d1c3dbc575e4022d1a52a998 (diff) | |
| download | server-7beec630f784d72d21ee621700df42aaa4f4b498.tar.gz server-7beec630f784d72d21ee621700df42aaa4f4b498.zip | |
use sqlite
| -rw-r--r-- | server.mjs | 23 | ||||
| -rw-r--r-- | server.test.mjs | 26 |
2 files changed, 47 insertions, 2 deletions
@@ -1,7 +1,18 @@ 'use strict' +import { DatabaseSync } from 'node:sqlite' import { createServer } from 'node:http' +const database = new DatabaseSync(':memory:') + +database.exec(` + CREATE TABLE eagles( + id INTEGER PRIMARY KEY, + mac_address TEXT, + key TEXT + ) STRICT +`) + const server = createServer((req, res) => { if (req.method === 'GET' && req.url === '/') { res.writeHead(200, { 'Content-Type': 'text/plain' }) @@ -10,8 +21,18 @@ const server = createServer((req, res) => { res.writeHead(200, { 'Content-Type': 'application/json' }) res.end(JSON.stringify({ time: Math.floor(Date.now() / 1000) })) } else if (req.method === 'GET' && req.url.startsWith('/eagle')) { + const params = new URLSearchParams(req.url.substring('/eagle'.length)) + const mac = params.get('mac') + const key = params.get('key') + if (mac === null || key === null) { + res.writeHead(400, { 'Content-Type': 'application/json' }) + res.end('{}\n') + return + } + const insert = database.prepare('INSERT INTO eagles(mac_address, key) VALUES(?, ?)') + const record = insert.run(mac, key) res.writeHead(200, { 'Content-Type': 'application/json' }) - res.end(JSON.stringify({ status: 200, data: { eagleId: 0 } })) + res.end(JSON.stringify({ status: 200, data: { eagleId: record.lastInsertRowid } })) } else { res.writeHead(404) res.end() diff --git a/server.test.mjs b/server.test.mjs index 99a543c..f59f09b 100644 --- a/server.test.mjs +++ b/server.test.mjs @@ -67,7 +67,9 @@ test('current time endpoint', (t, done) => { }) test('device registration endpoint', (t, done) => { - http.get(`${baseUrl}/eagle`, (res) => { + const stubMacAddress = '00:00:00:00:00' + const stubKey = '00000000000000000000000000000000' + http.get(`${baseUrl}/eagle?mac=${stubMacAddress}&key=${stubKey}`, (res) => { assert.strictEqual(res.statusCode, 200) assert.strictEqual(res.headers['content-type'], 'application/json') @@ -87,3 +89,25 @@ test('device registration endpoint', (t, done) => { assert.fail(`HTTP request failed: ${err.message}`) }) }) + +test('device registration endpoint returns bad request if missing mac address', (t, done) => { + const stubKey = '00000000000000000000000000000000' + http.get(`${baseUrl}/eagle?key=${stubKey}`, (res) => { + assert.strictEqual(res.statusCode, 400) + assert.strictEqual(res.headers['content-type'], 'application/json') + done() + }).on('error', (err) => { + assert.fail(`HTTP request failed: ${err.message}`) + }) +}) + +test('device registration endpoint returns bad request if missing encryption key', (t, done) => { + const stubMacAddress = '00:00:00:00:00' + http.get(`${baseUrl}/eagle?mac=${stubMacAddress}`, (res) => { + assert.strictEqual(res.statusCode, 400) + assert.strictEqual(res.headers['content-type'], 'application/json') + done() + }).on('error', (err) => { + assert.fail(`HTTP request failed: ${err.message}`) + }) +}) |
