feat: add user-agent endpoint

This commit is contained in:
David
2019-06-14 13:26:16 -07:00
parent 7f2f4661c7
commit 33e41571c2
2 changed files with 16 additions and 0 deletions

View File

@@ -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}`)

10
test.js
View File

@@ -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()