feat: add user-agent endpoint
This commit is contained in:
@@ -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
10
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()
|
||||
|
||||
Reference in New Issue
Block a user