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

test: refactor headers test #452

Closed
wants to merge 2 commits into from
Closed
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
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