Force close the server once the test is finished
This commit is contained in:
@ -42,6 +42,7 @@
|
||||
"simple-git": "^1.105.0",
|
||||
"sinon": "^6.1.5",
|
||||
"standard": "^12.0.0",
|
||||
"stoppable": "^1.0.7",
|
||||
"string-to-stream": "^1.1.1",
|
||||
"tap": "^12.0.1",
|
||||
"workq": "^2.1.0"
|
||||
|
||||
@ -24,6 +24,7 @@ test('Basic (callback)', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -47,7 +48,10 @@ test('Basic (promises)', t => {
|
||||
type: 'doc',
|
||||
q: 'foo:bar'
|
||||
})
|
||||
.then(({ body }) => t.deepEqual(body, { hello: 'world' }))
|
||||
.then(({ body }) => {
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
.catch(t.fail)
|
||||
})
|
||||
})
|
||||
@ -72,6 +76,7 @@ test('Error (callback)', t => {
|
||||
q: 'foo:bar'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -97,7 +102,10 @@ test('Error (promises)', t => {
|
||||
q: 'foo:bar'
|
||||
})
|
||||
.then(t.fail)
|
||||
.catch(err => t.ok(err))
|
||||
.catch(err => {
|
||||
t.ok(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -121,6 +129,7 @@ test('Abort method (callback)', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
|
||||
t.type(request.abort, 'function')
|
||||
@ -147,7 +156,10 @@ test('Abort is not supported in promises', t => {
|
||||
})
|
||||
|
||||
request
|
||||
.then(({ body }) => t.deepEqual(body, { hello: 'world' }))
|
||||
.then(({ body }) => {
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
.catch(t.fail)
|
||||
|
||||
t.type(request.abort, 'undefined')
|
||||
|
||||
@ -92,6 +92,7 @@ test('API', t => {
|
||||
t.strictEqual(connection.resurrectTimeout, 0)
|
||||
t.strictEqual(connection.status, Connection.statuses.ALIVE)
|
||||
t.deepEqual(pool.dead, [])
|
||||
server.stop()
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
@ -114,6 +115,7 @@ test('API', t => {
|
||||
t.true(connection.resurrectTimeout > 0)
|
||||
t.strictEqual(connection.status, Connection.statuses.DEAD)
|
||||
t.deepEqual(pool.dead, [href])
|
||||
server.stop()
|
||||
t.end()
|
||||
})
|
||||
})
|
||||
|
||||
@ -42,6 +42,7 @@ test('Basic (http)', t => {
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -81,6 +82,7 @@ test('Basic (https)', t => {
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -121,6 +123,7 @@ test('Basic (https with ssl agent)', t => {
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -154,6 +157,7 @@ test('Disable keep alive', t => {
|
||||
t.match(res.headers, {
|
||||
connection: 'close'
|
||||
})
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -178,6 +182,7 @@ test('Timeout support', t => {
|
||||
timeout: 500
|
||||
}, (err, res) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -201,6 +206,7 @@ test('querystring', t => {
|
||||
querystring: 'hello=world&you_know=for%20search'
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -223,6 +229,7 @@ test('querystring', t => {
|
||||
querystring: null
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -254,6 +261,7 @@ test('Body request', t => {
|
||||
body: 'hello'
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -293,6 +301,7 @@ test('Should handle compression', t => {
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.deepEqual(JSON.parse(payload), { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -332,6 +341,7 @@ test('Should handle compression', t => {
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.deepEqual(JSON.parse(payload), { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -370,6 +380,7 @@ test('Should not close a connection if there are open requests', t => {
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -394,6 +405,7 @@ test('Url with auth', t => {
|
||||
method: 'GET'
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -416,6 +428,7 @@ test('Url with querystring', t => {
|
||||
querystring: 'baz=faz'
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -446,6 +459,7 @@ test('Custom headers for connection', t => {
|
||||
t.error(err)
|
||||
// should not update the default
|
||||
t.deepEqual(connection.headers, { 'x-foo': 'bar' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -43,6 +43,7 @@ test('Basic', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -86,6 +87,7 @@ test('Send POST', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -141,6 +143,7 @@ test('Send POST (ndjson)', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -172,6 +175,7 @@ test('Not JSON payload from server', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.strictEqual(body, 'hello!')
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -250,6 +254,7 @@ test('DeserializationError', t => {
|
||||
path: '/hello'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof DeserializationError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -293,6 +298,7 @@ test('TimeoutError (should call markDead on the failing connection)', t => {
|
||||
path: '/hello'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -330,6 +336,7 @@ test('ConnectionError (should call markDead on the failing connection)', t => {
|
||||
path: '/hello'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof ConnectionError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -379,6 +386,7 @@ test('Retry mechanism', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -421,6 +429,7 @@ test('Should call markAlive with a successful response', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -462,6 +471,7 @@ test('Should call resurrect on every request', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -501,6 +511,7 @@ test('Should return a request aborter utility', t => {
|
||||
})
|
||||
|
||||
request.abort()
|
||||
server.stop()
|
||||
t.pass('ok')
|
||||
})
|
||||
})
|
||||
@ -535,6 +546,7 @@ test('ResponseError', t => {
|
||||
t.ok(err instanceof ResponseError)
|
||||
t.deepEqual(err.body, { status: 500 })
|
||||
t.strictEqual(err.statusCode, 500)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -569,6 +581,7 @@ test('Override requestTimeout', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -610,6 +623,8 @@ test('sniff', t => {
|
||||
sniffOnStart: true,
|
||||
sniffEndpoint: '/sniff'
|
||||
})
|
||||
|
||||
setTimeout(() => server.stop(), 100)
|
||||
})
|
||||
})
|
||||
|
||||
@ -659,6 +674,8 @@ test('sniff', t => {
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
})
|
||||
|
||||
setTimeout(() => server.stop(), 1100)
|
||||
})
|
||||
})
|
||||
|
||||
@ -708,6 +725,10 @@ test('sniff', t => {
|
||||
setTimeout(() => {
|
||||
transport.request(params, t.error)
|
||||
}, 300)
|
||||
|
||||
setTimeout(() => {
|
||||
server.stop()
|
||||
}, 400)
|
||||
})
|
||||
})
|
||||
|
||||
@ -737,6 +758,7 @@ test('sniff', t => {
|
||||
|
||||
transport.sniff((err, hosts) => {
|
||||
t.ok(err instanceof ConnectionError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -788,6 +810,7 @@ test(`Should mark as dead connections where the statusCode is 502/3/4
|
||||
headers: { 'content-type': 'application/json;utf=8' },
|
||||
statusCode: statusCode
|
||||
})
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -839,6 +862,7 @@ test('Should retry the request if the statusCode is 502/3/4', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -892,6 +916,8 @@ test('Ignore status code', t => {
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof ResponseError)
|
||||
})
|
||||
|
||||
setTimeout(() => server.stop(), 100)
|
||||
})
|
||||
})
|
||||
|
||||
@ -926,6 +952,7 @@ test('Should serialize the querystring', t => {
|
||||
}
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -964,6 +991,7 @@ test('timeout option', t => {
|
||||
path: '/hello'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -994,6 +1022,7 @@ test('timeout option', t => {
|
||||
requestTimeout: 500
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1027,6 +1056,7 @@ test('timeout option', t => {
|
||||
path: '/hello'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1057,6 +1087,7 @@ test('timeout option', t => {
|
||||
requestTimeout: '0.5s'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1095,6 +1126,7 @@ test('Should cast to boolean HEAD request', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.strictEqual(body, true)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1127,6 +1159,7 @@ test('Should cast to boolean HEAD request', t => {
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
t.strictEqual(body, false)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1158,6 +1191,7 @@ test('Should cast to boolean HEAD request', t => {
|
||||
path: '/hello'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof ResponseError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1189,6 +1223,7 @@ test('Should cast to boolean HEAD request', t => {
|
||||
path: '/hello'
|
||||
}, (err, { body }) => {
|
||||
t.ok(err instanceof ResponseError)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1226,6 +1261,7 @@ test('Suggest compression', t => {
|
||||
path: '/hello'
|
||||
}, (err, { body }) => {
|
||||
t.error(err)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1262,6 +1298,7 @@ test('Warning header', t => {
|
||||
t.error(err)
|
||||
t.deepEqual(warnings, [warn])
|
||||
warnings.forEach(w => t.type(w, 'string'))
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1298,6 +1335,7 @@ test('Warning header', t => {
|
||||
t.error(err)
|
||||
t.deepEqual(warnings, [warn1, warn2])
|
||||
warnings.forEach(w => t.type(w, 'string'))
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1330,6 +1368,7 @@ test('Warning header', t => {
|
||||
}, (err, { warnings }) => {
|
||||
t.error(err)
|
||||
t.strictEqual(warnings, null)
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -1375,6 +1414,7 @@ test('asHttpResponse enabled', t => {
|
||||
response.on('error', err => t.fail(err))
|
||||
response.on('end', () => {
|
||||
t.deepEqual(JSON.parse(payload), { hello: 'world' })
|
||||
server.stop()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const stoppable = require('stoppable')
|
||||
|
||||
// allow self signed certificates for testing purposes
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
|
||||
|
||||
@ -20,12 +22,11 @@ function buildServer (handler, opts, cb) {
|
||||
}
|
||||
|
||||
const server = opts.secure
|
||||
? https.createServer(secureOpts)
|
||||
: http.createServer()
|
||||
? stoppable(https.createServer(secureOpts))
|
||||
: stoppable(http.createServer())
|
||||
|
||||
server.on('request', handler)
|
||||
server.listen(0, () => {
|
||||
server.unref()
|
||||
const port = server.address().port
|
||||
cb(Object.assign({}, secureOpts, { port }), server)
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user