Updated test
This commit is contained in:
167
test/unit/client.test.js
Normal file
167
test/unit/client.test.js
Normal 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()
|
||||
})
|
||||
Reference in New Issue
Block a user