diff --git a/server/__tests__/util.spec.js b/server/__tests__/util.spec.js index c4aba26c..04f894c6 100644 --- a/server/__tests__/util.spec.js +++ b/server/__tests__/util.spec.js @@ -132,4 +132,36 @@ describe('util.js tests', () => { expect(error.message).toEqual('Requested resource returned a non 200 status code') } }) + + test('get includes User-Agent header in the request', async () => { + const url = MOCK_URL + const options = {} + const response = { res: { statusCode: STATUS_CODES.HTTP_STATUS_OK }, payload: 'response' } + + wreck.get.mockResolvedValue(response) + + await util.get(url, options) + + expect(wreck.get).toHaveBeenCalledWith(url, expect.objectContaining({ + headers: expect.objectContaining({ + 'User-Agent': 'hapi-wreck/18 (cyltfr-app)' + }) + })) + }) + + test('post includes User-Agent header in the request', async () => { + const url = MOCK_URL + const options = {} + const response = { res: { statusCode: STATUS_CODES.HTTP_STATUS_OK }, payload: 'response' } + + wreck.post.mockResolvedValue(response) + + await util.post(url, options) + + expect(wreck.post).toHaveBeenCalledWith(url, expect.objectContaining({ + headers: expect.objectContaining({ + 'User-Agent': 'hapi-wreck/18 (cyltfr-app)' + }) + })) + }) }) diff --git a/server/util.js b/server/util.js index 05d3e35f..13e055b7 100644 --- a/server/util.js +++ b/server/util.js @@ -14,11 +14,17 @@ if (config.http_proxy) { agent: new HttpsProxyAgent(config.http_proxy) }) } - +const customUaHeader = 'hapi-wreck/18 (cyltfr-app)' const { performance } = require('node:perf_hooks') -const get = (url, options, ext = false) => { +const get = (url, options = {}, ext = false) => { const thisWreck = (ext && wreckExt) ? wreckExt : wreck const startTick = performance.now() + // Header with UA added for AWS WAF as it is required, otherwise it will block the request + options.headers = { + ...options.headers, + 'User-Agent': customUaHeader + } + return thisWreck.get(url, options) .then(response => { if (config.performanceLogging) { @@ -38,9 +44,15 @@ const get = (url, options, ext = false) => { }) } -const post = (url, options, ext = false) => { +const post = (url, options = {}, ext = false) => { const thisWreck = (ext && wreckExt) ? wreckExt : wreck const startTick = performance.now() + // Header with UA added for AWS WAF as it is required, otherwise it will block the request + options.headers = { + ...options.headers, + 'User-Agent': customUaHeader + } + return thisWreck.post(url, options) .then(response => { if (config.performanceLogging) {