From 38d9d978ca13b1db0772ddfa56682ba25405018c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9Cspb-superstruct=E2=80=9D?= <“andrei@superstruct.tech”> Date: Sun, 15 Mar 2020 13:11:33 -0700 Subject: [PATCH] feat: add user-agent endpoint --- server.js | 6 ++++++ test.js | 10 ++++++++++ 2 files changed, 16 insertions(+) diff --git a/server.js b/server.js index 1fe2a13..92fb814 100644 --- a/server.js +++ b/server.js @@ -4,6 +4,7 @@ const PORT = process.env.PORT || 3000 const server = http.createServer((req, res) => { if (req.url === '/') return respondHello(req, res) + if (req.url === '/user-agent') return respondUserAgent(req, res) res.end() }) @@ -12,6 +13,11 @@ function respondHello (req, res) { res.end(JSON.stringify({ msg: 'hello' })) } +function respondUserAgent (req, res) { + const ua = req.headers['user-agent'] + res.end(JSON.stringify({ ua })) +} + server.listen(PORT) console.log(`Server listening on port ${PORT}`) diff --git a/test.js b/test.js index 3eb923d..e7cb863 100644 --- a/test.js +++ b/test.js @@ -15,6 +15,16 @@ tape('should respond hello', (t) => { }) }) +tape('should respond user-agent', (t) => { + const opts = { headers: { 'User-Agent': 'tape' } } + jsonist.get(`${urlBase}/user-agent`, opts, (err, body) => { + if (err) t.error(err) + + t.equal(body.ua, 'tape') + t.end() + }) +}) + tape('cleanup', function (t) { server.close() t.end()