WIP: Added some basic unit tests
- Added unit test - Added test fixtures - Added test utils
This commit is contained in:
19
test/fixtures/https.cert
vendored
Normal file
19
test/fixtures/https.cert
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
-----BEGIN CERTIFICATE-----
|
||||
MIIDBzCCAe+gAwIBAgIJALbQMeb7k/WqMA0GCSqGSIb3DQEBBQUAMBoxGDAWBgNV
|
||||
BAMMD3d3dy5mYXN0aWZ5Lm9yZzAeFw0xNzAyMDcyMDE5NDJaFw0yNzAyMDUyMDE5
|
||||
NDJaMBoxGDAWBgNVBAMMD3d3dy5mYXN0aWZ5Lm9yZzCCASIwDQYJKoZIhvcNAQEB
|
||||
BQADggEPADCCAQoCggEBAKtfXzDMmU+n3A7oVVOiqp6Z5cgu1t+qgj7TadwXONvO
|
||||
RZvuOcE8BZpM9tQEDE5XEIdcszDx0tWKHHSobgZAxDaEuK1PMhh/RTNvw1KzYJFm
|
||||
2G38mqgm11JUni87xmIFqpgJfeCApHnWUv+3/npuQniOoVSL13jdXEifeFM8onQn
|
||||
R73TVDyvMOjljTulMo0n9V8pYhVSzPnm2uxTu03p5+HosQE2bU0QKj7k8/8dwRVX
|
||||
EqnTtbLoW+Wf7V2W3cr/UnfPH8JSaBWTqct0pgXqYIqOSTiWQkO7pE69mGPHrRlm
|
||||
7+whp4WRriTacB3Ul+Cbx28wHU+D83ver4A8LKGVDSECAwEAAaNQME4wHQYDVR0O
|
||||
BBYEFHVzTr/tNziIUrR75UHXXA84yqmgMB8GA1UdIwQYMBaAFHVzTr/tNziIUrR7
|
||||
5UHXXA84yqmgMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAKVSdGeF
|
||||
vYcZOi0TG2WX7O3tSmu4G4nGxTldFiEVF89G0AU+HhNy9iwKXQLjDB7zMe/ZKbtJ
|
||||
cQgc6s8eZWxBk/OoPD1WNFGstx2EO2kRkSUBKhwnOct7CIS5X+NPXyHx2Yi03JHX
|
||||
unMA4WaHyo0dK4vAuali4OYdQqajNwL74avkRIxXFnZQeHzaq6tc6gX+ryB4dDSr
|
||||
tYn46Lo14D5jH6PtZ8DlGK+jIzM4IE7TEp2iv0CgaTU4ryt/SHPnLxfwZUpl7gSO
|
||||
EqkMAy3TlRMpv0oXM2Vh/CsyJzq2P/nY/O3bolsashSPWo9WsQTH4giYVA51ZVDK
|
||||
lGksQD+oWpfa3X0=
|
||||
-----END CERTIFICATE-----
|
||||
27
test/fixtures/https.key
vendored
Normal file
27
test/fixtures/https.key
vendored
Normal file
@ -0,0 +1,27 @@
|
||||
-----BEGIN RSA PRIVATE KEY-----
|
||||
MIIEpQIBAAKCAQEAq19fMMyZT6fcDuhVU6KqnpnlyC7W36qCPtNp3Bc4285Fm+45
|
||||
wTwFmkz21AQMTlcQh1yzMPHS1YocdKhuBkDENoS4rU8yGH9FM2/DUrNgkWbYbfya
|
||||
qCbXUlSeLzvGYgWqmAl94ICkedZS/7f+em5CeI6hVIvXeN1cSJ94UzyidCdHvdNU
|
||||
PK8w6OWNO6UyjSf1XyliFVLM+eba7FO7Tenn4eixATZtTRAqPuTz/x3BFVcSqdO1
|
||||
suhb5Z/tXZbdyv9Sd88fwlJoFZOpy3SmBepgio5JOJZCQ7ukTr2YY8etGWbv7CGn
|
||||
hZGuJNpwHdSX4JvHbzAdT4Pze96vgDwsoZUNIQIDAQABAoIBAG278ys/R8he1yVg
|
||||
lgqo9ZH7P8zwWTz9ZMsv+vAomor9SUtwvuDCO2AzejYGpY6gZ4AV1tQ3dOaxukjk
|
||||
9Rbh8AJs+AhZ1t0i2b/3B95z6BkS/vFmt+2GeYhJkMT0BLMNp9AU+9p+5VLy71C5
|
||||
k6T3525k/l8x8HZ/YDFMk/LQt8GhvM6A3J3BNElKraiDVO6ZIWgQQ5wiefJkApo1
|
||||
BsptHNTx83FbnkEbAahmOR8PfKcRdKY/mZDM2WrlfoU2uwVzPV0/KdYucpsfg2et
|
||||
jb5bdJzcvZDuDF4GsPi1asCSC1c403R0XGuPFW9TiBuOPxbfhYK2o60yTggX6H2X
|
||||
39WBc/ECgYEA3KNGgXEWzDSLpGciUisP+MzulOdQPawBTUHNykpQklEppnZbNWCX
|
||||
07dv6uasnp0pFHG4WlhZJ4+IQBpZH6xAVy9y68PvN7IDYdgMiEiYPSyqQu0rvJGa
|
||||
2ZR79SHDokZ8K5oofocC839RzleNRqWqxIwhHt29sxVs73kvml6OQm0CgYEAxtbA
|
||||
zbQwf6DXtFwutSgfOLgdXQK72beBdyeTcpUGbkonl5xHSbtz0CFmRpKiPnXfgg4W
|
||||
GXlTrqlYF/o048B7dU9+jCKY5DXx1Yzg/EFisEIClad3WXMhNOz1vBYVH6xU3Zq1
|
||||
YuYr5dcqiCWDv89e6Y6WJOhwIDZi6RqikD2EJQUCgYEAnWSAJFCnIa8OOo4z5oe/
|
||||
kg2m2GQWUphEKXeatQbEaUwquQvPTsmEJUzDMr+xPkkAiAwDpbdGijkSyh/Bmh2H
|
||||
nGpFwbf5CzMaxI6ZihK3P1SAdNO5koAQBcytjJW0eCtt4rDK2E+5pDgcBGVia5Y8
|
||||
to78BYfLDlhnaIF7mtR/CRUCgYEAvGCuzvOcUv4F/eirk5NMaQb9QqYZZD2XWVTU
|
||||
O2T2b7yvX9J+M1t1cESESe4X6cbwlp1T0JSCdGIZhLXWL8Om80/52zfX07VLxP6w
|
||||
FCy6G7SeEDxVNRh+6E5qzOO65YP17vDoUacxBZJgyBWKiUkkaW9dzd+sgsgj0yYZ
|
||||
xz+QlyUCgYEAxdNWQnz0pR5Rt2dbIedPs7wmiZ7eAe0VjCdhMa52IyJpejdeB6Bn
|
||||
Es+3lkHr0Xzty8XlQZcpbswhM8UZRgPVoBvvwQdQbv5yV+LdUu69pLM7InsdZy8u
|
||||
opPY/+q9lRdJt4Pbep3pOWYeLP7k5l4vei2vOEMHRjHnoqM5etSb6RU=
|
||||
-----END RSA PRIVATE KEY-----
|
||||
137
test/unit/connection-pool.test.js
Normal file
137
test/unit/connection-pool.test.js
Normal file
@ -0,0 +1,137 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { URL } = require('url')
|
||||
const ConnectionPool = require('../../lib/ConnectionPool')
|
||||
const Connection = require('../../lib/Connection')
|
||||
const { RoundRobinSelector } = require('../../lib/Selectors')
|
||||
|
||||
test('API', t => {
|
||||
t.test('addConnection', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
pool.addConnection(href)
|
||||
t.ok(pool.connections.get(href) instanceof Connection)
|
||||
t.deepEqual(pool.alive, [href])
|
||||
t.deepEqual(pool.dead, [])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('markDead', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
var connection = pool.addConnection(href)
|
||||
pool.markDead(connection)
|
||||
connection = pool.connections.get(href)
|
||||
t.strictEqual(connection.deadCount, 1)
|
||||
t.true(connection.resurrectTimeout > 0)
|
||||
t.deepEqual(pool.alive, [])
|
||||
t.deepEqual(pool.dead, [href])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('markAlive', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
var connection = pool.addConnection(href)
|
||||
pool.markDead(connection)
|
||||
pool.markAlive(connection)
|
||||
connection = pool.connections.get(href)
|
||||
t.strictEqual(connection.deadCount, 0)
|
||||
t.strictEqual(connection.resurrectTimeout, 0)
|
||||
t.deepEqual(pool.alive, [href])
|
||||
t.deepEqual(pool.dead, [])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('resurrect', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
var connection = pool.addConnection(href)
|
||||
pool.markDead(connection)
|
||||
connection = pool.resurrect(Date.now() + 1000 * 60 * 3)
|
||||
t.strictEqual(connection.deadCount, 1)
|
||||
t.true(connection.resurrectTimeout > 0)
|
||||
t.deepEqual(pool.alive, [href])
|
||||
t.deepEqual(pool.dead, [])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('getConnection', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
pool.addConnection(href)
|
||||
t.ok(pool.getConnection() instanceof Connection)
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('removeConnection', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const href = 'http://localhost:9200/'
|
||||
var connection = pool.addConnection(href)
|
||||
t.ok(pool.getConnection() instanceof Connection)
|
||||
pool.removeConnection(connection)
|
||||
t.strictEqual(pool.getConnection(), null)
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('empty', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
pool.addConnection('http://localhost:9200/')
|
||||
pool.addConnection('http://localhost:9201/')
|
||||
pool.empty()
|
||||
t.strictEqual(pool.connections.size, 0)
|
||||
t.deepEqual(pool.alive, [])
|
||||
t.deepEqual(pool.dead, [])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('urlToHost', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const url = 'http://localhost:9200'
|
||||
t.deepEqual(
|
||||
pool.urlToHost(url),
|
||||
{ host: new URL(url) }
|
||||
)
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.test('nodesToHost', t => {
|
||||
const pool = new ConnectionPool({ selector: new RoundRobinSelector() })
|
||||
const nodes = {
|
||||
a1: {
|
||||
http: {
|
||||
publish_address: '127.0.0.1:9200'
|
||||
},
|
||||
roles: ['master', 'data', 'ingest']
|
||||
},
|
||||
a2: {
|
||||
http: {
|
||||
publish_address: '127.0.0.1:9202'
|
||||
},
|
||||
roles: ['master', 'data', 'ingest']
|
||||
}
|
||||
}
|
||||
|
||||
t.deepEqual(pool.nodesToHost(nodes), [{
|
||||
host: new URL('http://127.0.0.1:9200'),
|
||||
id: 'a1',
|
||||
roles: {
|
||||
master: true,
|
||||
data: true,
|
||||
ingest: true
|
||||
}
|
||||
}, {
|
||||
host: new URL('http://127.0.0.1:9201'),
|
||||
id: 'a2',
|
||||
roles: {
|
||||
master: true,
|
||||
data: true,
|
||||
ingest: true
|
||||
}
|
||||
}])
|
||||
t.end()
|
||||
})
|
||||
|
||||
t.end()
|
||||
})
|
||||
194
test/unit/connection.test.js
Normal file
194
test/unit/connection.test.js
Normal file
@ -0,0 +1,194 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { buildServer } = require('../utils')
|
||||
const Connection = require('../../lib/Connection')
|
||||
|
||||
test('Basic (http)', t => {
|
||||
t.plan(4)
|
||||
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'x-custom-test': 'true',
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
res.end('ok')
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `http://localhost:${port}`,
|
||||
protocol: 'http:'
|
||||
}
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Custom-Test': true
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
|
||||
t.match(res.headers, {
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
|
||||
var payload = ''
|
||||
res.setEncoding('utf8')
|
||||
res.on('data', chunk => { payload += chunk })
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Basic (https)', t => {
|
||||
t.plan(4)
|
||||
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'x-custom-test': 'true',
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
res.end('ok')
|
||||
}
|
||||
|
||||
buildServer(handler, { secure: true }, ({ port }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `https://localhost:${port}`,
|
||||
protocol: 'https:'
|
||||
}
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Custom-Test': true
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
|
||||
t.match(res.headers, {
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
|
||||
var payload = ''
|
||||
res.setEncoding('utf8')
|
||||
res.on('data', chunk => { payload += chunk })
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Basic (https with ssl agent)', t => {
|
||||
t.plan(4)
|
||||
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'x-custom-test': 'true',
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
res.end('ok')
|
||||
}
|
||||
|
||||
buildServer(handler, { secure: true }, ({ port, key, cert }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `https://localhost:${port}`,
|
||||
protocol: 'https:'
|
||||
},
|
||||
ssl: { key, cert }
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Custom-Test': true
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
|
||||
t.match(res.headers, {
|
||||
connection: 'keep-alive'
|
||||
})
|
||||
|
||||
var payload = ''
|
||||
res.setEncoding('utf8')
|
||||
res.on('data', chunk => { payload += chunk })
|
||||
res.on('error', err => t.fail(err))
|
||||
res.on('end', () => {
|
||||
t.strictEqual(payload, 'ok')
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Disable keep alive', t => {
|
||||
t.plan(3)
|
||||
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'x-custom-test': 'true',
|
||||
connection: 'close'
|
||||
})
|
||||
res.end('ok')
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `http://localhost:${port}`,
|
||||
protocol: 'http:'
|
||||
},
|
||||
agent: { keepAlive: false }
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'X-Custom-Test': true
|
||||
}
|
||||
}, (err, res) => {
|
||||
t.error(err)
|
||||
|
||||
t.match(res.headers, {
|
||||
connection: 'close'
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Timeout support', t => {
|
||||
t.plan(1)
|
||||
|
||||
function handler (req, res) {
|
||||
setTimeout(
|
||||
() => res.end('ok'),
|
||||
1000
|
||||
)
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const connection = new Connection({
|
||||
host: {
|
||||
href: `http://localhost:${port}`,
|
||||
protocol: 'http:'
|
||||
}
|
||||
})
|
||||
connection.request({
|
||||
path: '/hello',
|
||||
method: 'GET',
|
||||
timeout: 500
|
||||
}, (err, res) => {
|
||||
t.ok(err.message, 'Request timed out')
|
||||
})
|
||||
})
|
||||
})
|
||||
24
test/unit/selectors.test.js
Normal file
24
test/unit/selectors.test.js
Normal file
@ -0,0 +1,24 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { RoundRobinSelector, RandomSelector } = require('../../lib/Selectors')
|
||||
|
||||
test('RoundRobinSelector', t => {
|
||||
const s = new RoundRobinSelector()
|
||||
const arr = [0, 1, 2, 3, 4, 5]
|
||||
|
||||
t.plan(arr.length + 1)
|
||||
for (var i = 0; i <= arr.length; i++) {
|
||||
t.strictEqual(
|
||||
s.select(arr),
|
||||
i === arr.length ? arr[0] : arr[i]
|
||||
)
|
||||
}
|
||||
})
|
||||
|
||||
test('RandomSelector', t => {
|
||||
t.plan(1)
|
||||
const s = new RandomSelector()
|
||||
const arr = [0, 1, 2, 3, 4, 5]
|
||||
t.type(s.select(arr), 'number')
|
||||
})
|
||||
39
test/unit/serializer.test.js
Normal file
39
test/unit/serializer.test.js
Normal file
@ -0,0 +1,39 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const Serializer = require('../../lib/Serializer')
|
||||
const { SerializationError, DeserializationError } = require('../../lib/errors')
|
||||
|
||||
test('Basic', t => {
|
||||
t.plan(2)
|
||||
const s = new Serializer()
|
||||
const obj = { hello: 'world' }
|
||||
const json = JSON.stringify(obj)
|
||||
t.strictEqual(s.serialize(obj), json)
|
||||
t.deepEqual(s.deserialize(json), obj)
|
||||
})
|
||||
|
||||
test('SerializationError', t => {
|
||||
t.plan(1)
|
||||
const s = new Serializer()
|
||||
const obj = { hello: 'world' }
|
||||
obj.o = obj
|
||||
try {
|
||||
s.serialize(obj)
|
||||
t.fail('Should fail')
|
||||
} catch (err) {
|
||||
t.ok(err instanceof SerializationError)
|
||||
}
|
||||
})
|
||||
|
||||
test('DeserializationError', t => {
|
||||
t.plan(1)
|
||||
const s = new Serializer()
|
||||
const json = '{"hello'
|
||||
try {
|
||||
s.deserialize(json)
|
||||
t.fail('Should fail')
|
||||
} catch (err) {
|
||||
t.ok(err instanceof DeserializationError)
|
||||
}
|
||||
})
|
||||
471
test/unit/transport.test.js
Normal file
471
test/unit/transport.test.js
Normal file
@ -0,0 +1,471 @@
|
||||
'use strict'
|
||||
|
||||
const { test } = require('tap')
|
||||
const { URL } = require('url')
|
||||
const { buildServer } = require('../utils')
|
||||
const {
|
||||
NoLivingConnectionsError,
|
||||
SerializationError,
|
||||
DeserializationError,
|
||||
TimeoutError,
|
||||
ResponseError
|
||||
} = require('../../lib/errors')
|
||||
|
||||
const ConnectionPool = require('../../lib/ConnectionPool')
|
||||
const Serializer = require('../../lib/Serializer')
|
||||
const Transport = require('../../lib/Transport')
|
||||
const { RoundRobinSelector } = require('../../lib/Selectors')
|
||||
|
||||
test('Basic', t => {
|
||||
t.plan(2)
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Send POST', t => {
|
||||
t.plan(4)
|
||||
function handler (req, res) {
|
||||
t.match(req.headers, {
|
||||
'content-type': 'application/json',
|
||||
'content-length': '17'
|
||||
})
|
||||
var json = ''
|
||||
req.setEncoding('utf8')
|
||||
req.on('data', chunk => { json += chunk })
|
||||
req.on('error', err => t.fail(err))
|
||||
req.on('end', () => {
|
||||
t.deepEqual(JSON.parse(json), { hello: 'world' })
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
})
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'POST',
|
||||
path: '/hello',
|
||||
body: { hello: 'world' }
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('NoLivingConnectionsError', t => {
|
||||
t.plan(1)
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof NoLivingConnectionsError)
|
||||
})
|
||||
})
|
||||
|
||||
test('SerializationError', t => {
|
||||
t.plan(1)
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection('http://localhost:9200')
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
const body = { hello: 'world' }
|
||||
body.o = body
|
||||
transport.request({
|
||||
method: 'POST',
|
||||
path: '/hello',
|
||||
body
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof SerializationError)
|
||||
})
|
||||
})
|
||||
|
||||
test('DeserializationError', t => {
|
||||
t.plan(1)
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end('{"hello)')
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof DeserializationError)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('TimeoutError (should call markDead on the failing connection)', t => {
|
||||
t.plan(2)
|
||||
|
||||
class CustomConnectionPool extends ConnectionPool {
|
||||
markDead (connection) {
|
||||
t.strictEqual(connection.id, 'node1')
|
||||
super.markDead(connection)
|
||||
}
|
||||
}
|
||||
|
||||
function handler (req, res) {
|
||||
setTimeout(() => {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new CustomConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection({
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 0,
|
||||
requestTimeout: 500,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof TimeoutError)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Retry mechanism', t => {
|
||||
t.plan(2)
|
||||
|
||||
var count = 0
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
if (count++ === 1) {
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
} else {
|
||||
setTimeout(() => {
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}, 1000)
|
||||
}
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection([{
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
}, {
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node2'
|
||||
}])
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 1,
|
||||
requestTimeout: 500,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Should call markAlive with a successful response', t => {
|
||||
t.plan(3)
|
||||
|
||||
class CustomConnectionPool extends ConnectionPool {
|
||||
markAlive (connection) {
|
||||
t.strictEqual(connection.id, 'node1')
|
||||
super.markAlive(connection)
|
||||
}
|
||||
}
|
||||
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new CustomConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection({
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Should call resurrect on every request', t => {
|
||||
t.plan(3)
|
||||
|
||||
class CustomConnectionPool extends ConnectionPool {
|
||||
resurrect (now) {
|
||||
t.type(now, 'number')
|
||||
}
|
||||
}
|
||||
|
||||
function handler (req, res) {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new CustomConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection({
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Should return a request aborter utility', t => {
|
||||
t.plan(1)
|
||||
|
||||
function handler (req, res) {
|
||||
setTimeout(() => {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection({
|
||||
host: new URL(`http://localhost:${port}`),
|
||||
id: 'node1'
|
||||
})
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
const request = transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (_err, body) => {
|
||||
t.fail('Should not be called')
|
||||
})
|
||||
|
||||
request.abort()
|
||||
t.pass('ok')
|
||||
})
|
||||
})
|
||||
|
||||
test('ResponseError', t => {
|
||||
t.plan(3)
|
||||
|
||||
function handler (req, res) {
|
||||
res.statusCode = 500
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ status: 500 }))
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 30000,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello'
|
||||
}, (err, body) => {
|
||||
t.ok(err instanceof ResponseError)
|
||||
t.deepEqual(err.response, { status: 500 })
|
||||
t.strictEqual(err.statusCode, 500)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('Override requestTimeout', t => {
|
||||
t.plan(2)
|
||||
function handler (req, res) {
|
||||
setTimeout(() => {
|
||||
res.setHeader('Content-Type', 'application/json;utf=8')
|
||||
res.end(JSON.stringify({ hello: 'world' }))
|
||||
}, 1000)
|
||||
}
|
||||
|
||||
buildServer(handler, ({ port }, server) => {
|
||||
const pool = new ConnectionPool({
|
||||
selector: new RoundRobinSelector()
|
||||
})
|
||||
pool.addConnection(`http://localhost:${port}`)
|
||||
|
||||
const transport = new Transport({
|
||||
emit: () => {},
|
||||
connectionPool: pool,
|
||||
serializer: new Serializer(),
|
||||
maxRetries: 3,
|
||||
requestTimeout: 500,
|
||||
sniffInterval: false,
|
||||
sniffOnStart: false
|
||||
})
|
||||
|
||||
transport.request({
|
||||
method: 'GET',
|
||||
path: '/hello',
|
||||
timeout: 2000
|
||||
}, (err, body) => {
|
||||
t.error(err)
|
||||
t.deepEqual(body, { hello: 'world' })
|
||||
})
|
||||
})
|
||||
})
|
||||
34
test/utils/buildServer.js
Normal file
34
test/utils/buildServer.js
Normal file
@ -0,0 +1,34 @@
|
||||
'use strict'
|
||||
|
||||
// allow self signed certificates for testing purposes
|
||||
process.env.NODE_TLS_REJECT_UNAUTHORIZED = 0
|
||||
|
||||
const { readFileSync } = require('fs')
|
||||
const { join } = require('path')
|
||||
const https = require('https')
|
||||
const http = require('http')
|
||||
|
||||
const secureOpts = {
|
||||
key: readFileSync(join(__dirname, '..', 'fixtures', 'https.key'), 'utf8'),
|
||||
cert: readFileSync(join(__dirname, '..', 'fixtures', 'https.cert'), 'utf8')
|
||||
}
|
||||
|
||||
function buildServer (handler, opts, cb) {
|
||||
if (cb == null) {
|
||||
cb = opts
|
||||
opts = {}
|
||||
}
|
||||
|
||||
const server = opts.secure
|
||||
? https.createServer(secureOpts)
|
||||
: http.createServer()
|
||||
|
||||
server.on('request', handler)
|
||||
server.listen(0, () => {
|
||||
server.unref()
|
||||
const port = server.address().port
|
||||
cb(Object.assign({}, secureOpts, { port }), server)
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = buildServer
|
||||
7
test/utils/index.js
Normal file
7
test/utils/index.js
Normal file
@ -0,0 +1,7 @@
|
||||
'use strict'
|
||||
|
||||
const buildServer = require('./buildServer')
|
||||
|
||||
module.exports = {
|
||||
buildServer
|
||||
}
|
||||
Reference in New Issue
Block a user