Skip to content
This repository has been archived by the owner on Aug 30, 2024. It is now read-only.

Commit

Permalink
refactored the httpclients to remove the duplication after introducin…
Browse files Browse the repository at this point in the history
…g the basic http-client. Adding timeout error
  • Loading branch information
ildella committed Nov 22, 2023
1 parent 4bc27dd commit d1d7b5e
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 25 deletions.
29 changes: 4 additions & 25 deletions axios/http-json-client.js
Original file line number Diff line number Diff line change
@@ -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,
})
6 changes: 6 additions & 0 deletions tests/http/http-timeout.test.js
Original file line number Diff line number Diff line change
@@ -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)
})
6 changes: 6 additions & 0 deletions tests/http/routes.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const {setTimeout: sleep} = require('timers/promises')
const {defaultHttpJsonClient} = require('../../axios')
const {__} = require('../../fusto/')

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d1d7b5e

Please sign in to comment.