From d1d7b5e8ccc908115eba80715e2ec20393282f92 Mon Sep 17 00:00:00 2001 From: Daniele Dellafiore <66707+ildella@users.noreply.github.com> Date: Wed, 22 Nov 2023 13:32:04 +0000 Subject: [PATCH] refactored the httpclients to remove the duplication after introducing the basic http-client. Adding timeout error --- axios/http-json-client.js | 29 ++++------------------------- tests/http/http-timeout.test.js | 6 ++++++ tests/http/routes.js | 6 ++++++ 3 files changed, 16 insertions(+), 25 deletions(-) create mode 100644 tests/http/http-timeout.test.js diff --git a/axios/http-json-client.js b/axios/http-json-client.js index 344d5cd..eb4c66a 100644 --- a/axios/http-json-client.js +++ b/axios/http-json-client.js @@ -1,33 +1,12 @@ -const http = require('http') -const https = require('https') -const axios = require('axios') +const httpClient = require('./http-client') -module.exports = ({ - baseURL, - timeout = 2500, - secure = true, - address = '0.0.0.0', - hostname = address || '0.0.0.0', - port = secure === true ? 443 : 80, - rejectUnauthorized = secure !== false, - protocol = secure === true ? 'https' : 'http', - httpsAgent = new https.Agent({keepAlive: true, rejectUnauthorized}), - httpAgent = new http.Agent({keepAlive: true}), - headers = {}, -} = {}) => axios.create({ - baseURL: baseURL || `${protocol}://${hostname}:${port}`, - timeout, - httpAgent, - httpsAgent, - transitional: { - clarifyTimeoutError: true, - // forcedJSONParsing: false, - silentJSONParsing: false, - }, +module.exports = ({headers = {}, ...options} = {}) => httpClient({ + timeout: 2500, headers: { 'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'Content-Type': 'application/json; charset=utf-8', ...headers, }, + ...options, }) diff --git a/tests/http/http-timeout.test.js b/tests/http/http-timeout.test.js new file mode 100644 index 0000000..fb4379a --- /dev/null +++ b/tests/http/http-timeout.test.js @@ -0,0 +1,6 @@ +const {client} = global.t + +test('basic http test', async () => { + const {status} = await client({timeout: 100}).post('/long', {}) + expect(status).toEqual(200) +}) diff --git a/tests/http/routes.js b/tests/http/routes.js index 36a80d4..dbf57c1 100644 --- a/tests/http/routes.js +++ b/tests/http/routes.js @@ -1,3 +1,4 @@ +const {setTimeout: sleep} = require('timers/promises') const {defaultHttpJsonClient} = require('../../axios') const {__} = require('../../fusto/') @@ -35,6 +36,11 @@ module.exports = app => { await get('/') }) + app.post('/long', async () => { + await sleep(400) + return {slow: true} + }) + app.get('/async-pipe-booom', async (request, reply) => { await __([1]) // eslint-disable-next-line require-await