Updated test

This commit is contained in:
delvedor
2018-11-09 17:17:05 +01:00
parent 3dd4f01370
commit 9df4fcbbb6
6 changed files with 256 additions and 71 deletions

View File

@ -22,10 +22,10 @@ function Runner (opts) {
} }
opts = opts || {} opts = opts || {}
assert(opts.host, 'Missing host') assert(opts.node, 'Missing base node')
this.bailout = opts.bailout this.bailout = opts.bailout
this.client = new elasticsearch.Client({ this.client = new elasticsearch.Client({
host: opts.host node: opts.node
}) })
this.log = ora('Loading yaml suite').start() this.log = ora('Loading yaml suite').start()
} }
@ -234,11 +234,11 @@ Runner.prototype.createFolder = function (name) {
if (require.main === module) { if (require.main === module) {
const opts = minimist(process.argv.slice(2), { const opts = minimist(process.argv.slice(2), {
string: ['host', 'version'], string: ['node', 'version'],
boolean: ['bailout'], boolean: ['bailout'],
default: { default: {
// host: 'http://elastic:passw0rd@localhost:9200', // node: 'http://elastic:passw0rd@localhost:9200',
host: 'http://localhost:9200', node: 'http://localhost:9200',
version: '6.4', version: '6.4',
bailout: false bailout: false
} }

View File

@ -14,15 +14,13 @@ test('Basic (callback)', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const client = new Client({ const client = new Client({
host: `http://localhost:${port}` node: `http://localhost:${port}`
}) })
client.search({ client.search({
index: 'test', index: 'test',
type: 'doc', type: 'doc',
query: { q: 'foo:bar'
match: { foo: 'bar' }
}
}, (err, { body }) => { }, (err, { body }) => {
t.error(err) t.error(err)
t.deepEqual(body, { hello: 'world' }) t.deepEqual(body, { hello: 'world' })
@ -40,16 +38,14 @@ test('Basic (promises)', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const client = new Client({ const client = new Client({
host: `http://localhost:${port}` node: `http://localhost:${port}`
}) })
client client
.search({ .search({
index: 'test', index: 'test',
type: 'doc', type: 'doc',
query: { q: 'foo:bar'
match: { foo: 'bar' }
}
}) })
.then(({ body }) => t.deepEqual(body, { hello: 'world' })) .then(({ body }) => t.deepEqual(body, { hello: 'world' }))
.catch(t.fail) .catch(t.fail)
@ -67,15 +63,13 @@ test('Error (callback)', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const client = new Client({ const client = new Client({
host: `http://localhost:${port}` node: `http://localhost:${port}`
}) })
client.search({ client.search({
index: 'test', index: 'test',
type: 'doc', type: 'doc',
query: { q: 'foo:bar'
match: { foo: 'bar' }
}
}, (err, { body }) => { }, (err, { body }) => {
t.ok(err) t.ok(err)
}) })
@ -93,16 +87,14 @@ test('Error (promises)', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const client = new Client({ const client = new Client({
host: `http://localhost:${port}` node: `http://localhost:${port}`
}) })
client client
.search({ .search({
index: 'test', index: 'test',
type: 'doc', type: 'doc',
query: { q: 'foo:bar'
match: { foo: 'bar' }
}
}) })
.then(t.fail) .then(t.fail)
.catch(err => t.ok(err)) .catch(err => t.ok(err))
@ -119,15 +111,13 @@ test('Abort method (callback)', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const client = new Client({ const client = new Client({
host: `http://localhost:${port}` node: `http://localhost:${port}`
}) })
const request = client.search({ const request = client.search({
index: 'test', index: 'test',
type: 'doc', type: 'doc',
query: { q: 'foo:bar'
match: { foo: 'bar' }
}
}, (err, { body }) => { }, (err, { body }) => {
t.error(err) t.error(err)
t.deepEqual(body, { hello: 'world' }) t.deepEqual(body, { hello: 'world' })
@ -147,15 +137,13 @@ test('Abort is not supported in promises', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const client = new Client({ const client = new Client({
host: `http://localhost:${port}` node: `http://localhost:${port}`
}) })
const request = client.search({ const request = client.search({
index: 'test', index: 'test',
type: 'doc', type: 'doc',
query: { q: 'foo:bar'
match: { foo: 'bar' }
}
}) })
request request

167
test/unit/client.test.js Normal file
View File

@ -0,0 +1,167 @@
'use strict'
const { test } = require('tap')
const { URL } = require('url')
const { Client, kConnectionPool } = require('../../index')
test('Configure host', t => {
t.test('Single string', t => {
const client = new Client({
node: 'http://localhost:9200'
})
const pool = client[kConnectionPool]
t.match(pool.connections.get('http://localhost:9200/'), {
url: new URL('http://localhost:9200'),
id: 'http://localhost:9200/',
ssl: null,
deadCount: 0,
resurrectTimeout: 0,
roles: {
master: true,
data: true,
ingest: true,
coordinating: true,
machine_learning: true
}
})
t.end()
})
t.test('Array of strings', t => {
const client = new Client({
nodes: ['http://localhost:9200', 'http://localhost:9201']
})
const pool = client[kConnectionPool]
t.match(pool.connections.get('http://localhost:9200/'), {
url: new URL('http://localhost:9200'),
id: 'http://localhost:9200/',
ssl: null,
deadCount: 0,
resurrectTimeout: 0,
roles: {
master: true,
data: true,
ingest: true,
coordinating: true,
machine_learning: true
}
})
t.match(pool.connections.get('http://localhost:9201/'), {
url: new URL('http://localhost:9201'),
id: 'http://localhost:9201/',
ssl: null,
deadCount: 0,
resurrectTimeout: 0,
roles: {
master: true,
data: true,
ingest: true,
coordinating: true,
machine_learning: true
}
})
t.end()
})
t.test('Single object', t => {
const client = new Client({
node: {
url: new URL('http://localhost:9200'),
id: 'node',
roles: {
master: true,
data: false
},
ssl: 'ssl'
}
})
const pool = client[kConnectionPool]
t.match(pool.connections.get('node'), {
url: new URL('http://user@passwordlocalhost:9200'),
id: 'node',
ssl: 'ssl',
deadCount: 0,
resurrectTimeout: 0,
roles: {
master: true,
data: false
}
})
t.end()
})
t.test('Array of objects', t => {
const client = new Client({
nodes: [{
url: new URL('http://localhost:9200'),
id: 'node1',
roles: {
master: true,
data: false
},
ssl: 'ssl'
}, {
url: new URL('http://localhost:9200'),
id: 'node2',
roles: {
master: false,
data: true
},
ssl: 'ssl'
}]
})
const pool = client[kConnectionPool]
t.match(pool.connections.get('node1'), {
url: new URL('http://user@passwordlocalhost:9200'),
id: 'node1',
ssl: 'ssl',
deadCount: 0,
resurrectTimeout: 0,
roles: {
master: true,
data: false
}
})
t.match(pool.connections.get('node2'), {
url: new URL('http://user@passwordlocalhost:9200'),
id: 'node2',
ssl: 'ssl',
deadCount: 0,
resurrectTimeout: 0,
roles: {
master: false,
data: true
}
})
t.end()
})
t.test('Custom headers', t => {
const client = new Client({
node: {
url: new URL('http://localhost:9200'),
headers: { 'x-foo': 'bar' },
id: 'node'
}
})
const pool = client[kConnectionPool]
t.match(pool.connections.get('node'), {
url: new URL('http://user@passwordlocalhost:9200'),
headers: { 'x-foo': 'bar' }
})
t.end()
})
t.test('Missing node conf', t => {
try {
new Client() // eslint-disable-line
t.fail('Should fail')
} catch (err) {
t.ok(err)
}
t.end()
})
t.end()
})

View File

@ -255,7 +255,7 @@ test('API', t => {
const url = 'http://localhost:9200' const url = 'http://localhost:9200'
t.deepEqual( t.deepEqual(
pool.urlToHost(url), pool.urlToHost(url),
{ host: new URL(url) } { url: new URL(url) }
) )
t.end() t.end()
}) })
@ -278,7 +278,7 @@ test('API', t => {
} }
t.deepEqual(pool.nodesToHost(nodes), [{ t.deepEqual(pool.nodesToHost(nodes), [{
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a1', id: 'a1',
roles: { roles: {
master: true, master: true,
@ -286,7 +286,7 @@ test('API', t => {
ingest: true ingest: true
} }
}, { }, {
host: new URL('http://127.0.0.1:9201'), url: new URL('http://127.0.0.1:9201'),
id: 'a2', id: 'a2',
roles: { roles: {
master: true, master: true,
@ -307,7 +307,7 @@ test('API', t => {
} }
const pool = new CustomConnectionPool() const pool = new CustomConnectionPool()
pool.addConnection([{ pool.addConnection([{
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a1', id: 'a1',
roles: { roles: {
master: true, master: true,
@ -315,7 +315,7 @@ test('API', t => {
ingest: true ingest: true
} }
}, { }, {
host: new URL('http://127.0.0.1:9201'), url: new URL('http://127.0.0.1:9201'),
id: 'a2', id: 'a2',
roles: { roles: {
master: true, master: true,
@ -325,11 +325,11 @@ test('API', t => {
}]) }])
pool.update([{ pool.update([{
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a1', id: 'a1',
roles: null roles: null
}, { }, {
host: new URL('http://127.0.0.1:9201'), url: new URL('http://127.0.0.1:9201'),
id: 'a2', id: 'a2',
roles: null roles: null
}]) }])
@ -348,7 +348,7 @@ test('API', t => {
} }
const pool = new CustomConnectionPool() const pool = new CustomConnectionPool()
const conn1 = pool.addConnection({ const conn1 = pool.addConnection({
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a1', id: 'a1',
roles: { roles: {
master: true, master: true,
@ -358,7 +358,7 @@ test('API', t => {
}) })
const conn2 = pool.addConnection({ const conn2 = pool.addConnection({
host: new URL('http://127.0.0.1:9201'), url: new URL('http://127.0.0.1:9201'),
id: 'a2', id: 'a2',
roles: { roles: {
master: true, master: true,
@ -371,11 +371,11 @@ test('API', t => {
pool.markDead(conn2) pool.markDead(conn2)
pool.update([{ pool.update([{
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a1', id: 'a1',
roles: null roles: null
}, { }, {
host: new URL('http://127.0.0.1:9201'), url: new URL('http://127.0.0.1:9201'),
id: 'a2', id: 'a2',
roles: null roles: null
}]) }])
@ -388,7 +388,7 @@ test('API', t => {
t.plan(2) t.plan(2)
const pool = new ConnectionPool() const pool = new ConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a1', id: 'a1',
roles: { roles: {
master: true, master: true,
@ -398,11 +398,11 @@ test('API', t => {
}) })
pool.update([{ pool.update([{
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a1', id: 'a1',
roles: null roles: null
}, { }, {
host: new URL('http://127.0.0.1:9201'), url: new URL('http://127.0.0.1:9201'),
id: 'a2', id: 'a2',
roles: null roles: null
}]) }])
@ -415,17 +415,17 @@ test('API', t => {
t.plan(3) t.plan(3)
const pool = new ConnectionPool() const pool = new ConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a1', id: 'a1',
roles: null roles: null
}) })
pool.update([{ pool.update([{
host: new URL('http://127.0.0.1:9200'), url: new URL('http://127.0.0.1:9200'),
id: 'a2', id: 'a2',
roles: null roles: null
}, { }, {
host: new URL('http://127.0.0.1:9201'), url: new URL('http://127.0.0.1:9201'),
id: 'a3', id: 'a3',
roles: null roles: null
}]) }])

View File

@ -21,7 +21,7 @@ test('Basic (http)', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`) url: new URL(`http://localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -60,7 +60,7 @@ test('Basic (https)', t => {
buildServer(handler, { secure: true }, ({ port }, server) => { buildServer(handler, { secure: true }, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`https://localhost:${port}`) url: new URL(`https://localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -99,7 +99,7 @@ test('Basic (https with ssl agent)', t => {
buildServer(handler, { secure: true }, ({ port, key, cert }, server) => { buildServer(handler, { secure: true }, ({ port, key, cert }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`https://localhost:${port}`), url: new URL(`https://localhost:${port}`),
ssl: { key, cert } ssl: { key, cert }
}) })
connection.request({ connection.request({
@ -139,7 +139,7 @@ test('Disable keep alive', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
agent: { keepAlive: false } agent: { keepAlive: false }
}) })
connection.request({ connection.request({
@ -170,7 +170,7 @@ test('Timeout support', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`) url: new URL(`http://localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -193,7 +193,7 @@ test('querystring', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`) url: new URL(`http://localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -215,7 +215,7 @@ test('querystring', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`) url: new URL(`http://localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -246,7 +246,7 @@ test('Body request', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`) url: new URL(`http://localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -274,7 +274,7 @@ test('Should handle compression', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`) url: new URL(`http://localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -313,7 +313,7 @@ test('Should handle compression', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`) url: new URL(`http://localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -349,7 +349,7 @@ test('Should not close a connection if there are open requests', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}`) url: new URL(`http://localhost:${port}`)
}) })
setTimeout(() => { setTimeout(() => {
@ -387,7 +387,7 @@ test('Url with auth', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://foo:bar@localhost:${port}`) url: new URL(`http://foo:bar@localhost:${port}`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -408,7 +408,7 @@ test('Url with querystring', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const connection = new Connection({ const connection = new Connection({
host: new URL(`http://localhost:${port}?foo=bar`) url: new URL(`http://localhost:${port}?foo=bar`)
}) })
connection.request({ connection.request({
path: '/hello', path: '/hello',
@ -419,3 +419,33 @@ test('Url with querystring', t => {
}) })
}) })
}) })
test('Custom headers for connection', t => {
t.plan(3)
function handler (req, res) {
t.match(req.headers, {
'x-custom-test': 'true',
'x-foo': 'bar'
})
res.end('ok')
}
buildServer(handler, ({ port }, server) => {
const connection = new Connection({
url: new URL(`http://localhost:${port}`),
headers: { 'x-foo': 'bar' }
})
connection.request({
path: '/hello',
method: 'GET',
headers: {
'X-Custom-Test': true
}
}, (err, res) => {
t.error(err)
// should not update the default
t.deepEqual(connection.headers, { 'x-foo': 'bar' })
})
})
})

View File

@ -274,7 +274,7 @@ test('TimeoutError (should call markDead on the failing connection)', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new CustomConnectionPool() const pool = new CustomConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })
@ -311,7 +311,7 @@ test('ConnectionError (should call markDead on the failing connection)', t => {
server.close() server.close()
const pool = new CustomConnectionPool() const pool = new CustomConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })
@ -353,13 +353,13 @@ test('Retry mechanism', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new ConnectionPool() const pool = new ConnectionPool()
pool.addConnection([{ pool.addConnection([{
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}, { }, {
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node2' id: 'node2'
}, { }, {
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node3' id: 'node3'
}]) }])
@ -401,7 +401,7 @@ test('Should call markAlive with a successful response', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new CustomConnectionPool() const pool = new CustomConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })
@ -442,7 +442,7 @@ test('Should call resurrect on every request', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new CustomConnectionPool() const pool = new CustomConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })
@ -479,7 +479,7 @@ test('Should return a request aborter utility', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new ConnectionPool() const pool = new ConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })
@ -945,7 +945,7 @@ test('timeout option', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new ConnectionPool() const pool = new ConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })
@ -974,7 +974,7 @@ test('timeout option', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new ConnectionPool() const pool = new ConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })
@ -1008,7 +1008,7 @@ test('timeout option', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new ConnectionPool() const pool = new ConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })
@ -1037,7 +1037,7 @@ test('timeout option', t => {
buildServer(handler, ({ port }, server) => { buildServer(handler, ({ port }, server) => {
const pool = new ConnectionPool() const pool = new ConnectionPool()
pool.addConnection({ pool.addConnection({
host: new URL(`http://localhost:${port}`), url: new URL(`http://localhost:${port}`),
id: 'node1' id: 'node1'
}) })