From 1fe9c2dda091a3a34c9c3d0836727f84dffcfe43 Mon Sep 17 00:00:00 2001 From: Miha Urbanija <10325909+mihaur@users.noreply.github.com> Date: Wed, 23 Oct 2024 19:27:24 +0200 Subject: [PATCH 1/2] test: refactor headers test --- src/services/headers/index.js | 1 - src/services/ping/index.js | 1 + test/headers.test.js | 27 ++++++++++++++++++--------- 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/services/headers/index.js b/src/services/headers/index.js index a92912a..439baf0 100644 --- a/src/services/headers/index.js +++ b/src/services/headers/index.js @@ -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 diff --git a/src/services/ping/index.js b/src/services/ping/index.js index 58aa2f9..8ac6a0d 100644 --- a/src/services/ping/index.js +++ b/src/services/ping/index.js @@ -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')) diff --git a/test/headers.test.js b/test/headers.test.js index a393d08..37a0104 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -15,18 +15,21 @@ 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) }) @@ -34,37 +37,43 @@ test('GET `/api/headers` route', async t => { 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) }) From cfb12b412337e20dc2a8e378a7bfd18a947da158 Mon Sep 17 00:00:00 2001 From: mihaur Date: Wed, 23 Oct 2024 18:00:02 +0000 Subject: [PATCH 2/2] [MegaLinter] Apply linters fixes --- test/headers.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/headers.test.js b/test/headers.test.js index 37a0104..58fc74c 100644 --- a/test/headers.test.js +++ b/test/headers.test.js @@ -47,7 +47,7 @@ test('GET `/api/headers` route additional headers', async t => { headers: { 'x-my-header': '42', host: 'host' - }, + } }) assert.equal(res.headers['content-type'], 'application/json; charset=utf-8')