Skip to content

Commit

Permalink
test: refactor headers test
Browse files Browse the repository at this point in the history
  • Loading branch information
mihaur committed Oct 23, 2024
1 parent d8d0564 commit 1fe9c2d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 10 deletions.
1 change: 0 additions & 1 deletion src/services/headers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ export default async function routes (fastify, options) {
schema,
handler: async (request, reply) => {
const response = { ...request.headers }
delete response.host
await logAccess(response, fastify.mongo.db.collection('access-log'))
await sleep(request.query.delay)
// nosemgrep: javascript.express.security.audit.xss.direct-response-write.direct-response-write
Expand Down
1 change: 1 addition & 0 deletions src/services/ping/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default async function routes (fastify, options) {
})

fastify.get('/:response', {
schema,
handler: async (request, reply) => {
const response = { ping: request.params.response }
await logAccess(response, fastify.mongo.db.collection('access-log'))
Expand Down
27 changes: 18 additions & 9 deletions test/headers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,56 +15,65 @@ afterEach(async (t) => {

test('GET `/api/headers` route', async t => {
const expectedHeaders = {
'user-agent': 'lightMyRequest'
'user-agent': 'lightMyRequest',
host: 'host'
}

const res = await t.app.inject({
method: 'GET',
url: '/api/headers'
url: '/api/headers',
headers: {
host: 'host'
}
})

assert.equal(res.headers['content-type'], 'application/json; charset=utf-8')
assert.equal(res.statusCode, 200)
const headers = res.json()
delete headers.host
assert.deepEqual(headers, expectedHeaders)
assert.deepEqual(await lastLogItem(t.app.mongo.db), expectedHeaders)
})

test('GET `/api/headers` route additional headers', async t => {
const expectedHeaders = {
'x-my-header': '42',
'user-agent': 'lightMyRequest'
'user-agent': 'lightMyRequest',
host: 'host'
}

const res = await t.app.inject({
method: 'GET',
url: '/api/headers',
headers: { 'x-my-header': '42' }
headers: {
'x-my-header': '42',
host: 'host'
},
})

assert.equal(res.headers['content-type'], 'application/json; charset=utf-8')
assert.equal(res.statusCode, 200)
const headers = res.json()
delete headers.host
assert.deepEqual(headers, expectedHeaders)
assert.deepEqual(await lastLogItem(t.app.mongo.db), expectedHeaders)
})

test('GET `/api/headers?delay=1` route', async t => {
const expectedHeaders = {
'user-agent': 'lightMyRequest'
'user-agent': 'lightMyRequest',
host: 'host'
}

const res = await t.app.inject({
method: 'GET',
url: '/api/headers'
url: '/api/headers',
headers: {
host: 'host'
}
})

assert.equal(res.headers['content-type'], 'application/json; charset=utf-8')
assert.equal(res.statusCode, 200)
const headers = res.json()
delete headers.host
assert.deepEqual(headers, expectedHeaders)
assert.deepEqual(await lastLogItem(t.app.mongo.db), expectedHeaders)
})
Expand Down

0 comments on commit 1fe9c2d

Please sign in to comment.