Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add user-agent endpoint #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
})
Expand All @@ -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}`)

Expand Down
10 changes: 10 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down