Updated test

This commit is contained in:
delvedor
2019-03-11 17:12:53 +01:00
parent 46df19fd7d
commit c990ed43d4
4 changed files with 218 additions and 58 deletions

View File

@ -43,9 +43,12 @@ test('Should update the connection pool', t => {
})
t.strictEqual(client.connectionPool.connections.size, 1)
client.on(events.SNIFF, (err, { reason }) => {
client.on(events.SNIFF, (err, request) => {
t.error(err)
t.strictEqual(reason, Transport.sniffReasons.DEFAULT)
t.strictEqual(
request.meta.sniff.reason,
Transport.sniffReasons.DEFAULT
)
})
// run the sniffer
@ -100,8 +103,9 @@ test('Sniff interval', t => {
})
// this event will be triggered by api calls
client.on(events.SNIFF, (err, { hosts, reason }) => {
client.on(events.SNIFF, (err, request) => {
t.error(err)
const { hosts, reason } = request.meta.sniff
t.strictEqual(
client.connectionPool.connections.size,
hosts.length
@ -135,8 +139,9 @@ test('Sniff on start', t => {
sniffOnStart: true
})
client.on(events.SNIFF, (err, { hosts, reason }) => {
client.on(events.SNIFF, (err, request) => {
t.error(err)
const { hosts, reason } = request.meta.sniff
t.strictEqual(
client.connectionPool.connections.size,
hosts.length
@ -207,8 +212,9 @@ test('Sniff on connection fault', t => {
t.strictEqual(client.connectionPool.connections.size, 2)
// this event will be triggered by the connection fault
client.on(events.SNIFF, (err, { hosts, reason }) => {
client.on(events.SNIFF, (err, request) => {
t.error(err)
const { hosts, reason } = request.meta.sniff
t.strictEqual(
client.connectionPool.connections.size,
hosts.length

View File

@ -22,9 +22,8 @@
import {
Client,
ApiResponse,
EventMeta,
SniffMeta,
ResurrectMeta,
RequestEvent,
ResurrectEvent,
events,
ClientExtendsCallbackOptions
} from '../../index'
@ -33,10 +32,13 @@ import { TransportRequestParams, TransportRequestOptions } from '../../lib/Trans
const client = new Client({ node: 'http://localhost:9200' })
client.on(events.REQUEST, (err: Error | null, meta: EventMeta) => {})
client.on(events.RESPONSE, (err: Error | null, meta: EventMeta) => {})
client.on(events.SNIFF, (err: Error | null, meta: SniffMeta) => {})
client.on(events.RESURRECT, (err: Error | null, meta: ResurrectMeta) => {})
client.on(events.RESPONSE, (err: Error | null, request: RequestEvent) => {
if (err) console.log(err)
const { body, statusCode } = request
const { params } = request.meta.request
console.log(params, body, statusCode)
})
client.on(events.RESURRECT, (err: Error | null, meta: ResurrectEvent) => {})
// Callbacks
client.info((err: Error | null, result: ApiResponse) => {})

93
test/unit/errors.test.js Normal file
View File

@ -0,0 +1,93 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
'use strict'
const { test } = require('tap')
const { errors } = require('../../index')
test('ElasticsearchClientError', t => {
const err = new errors.ElasticsearchClientError()
t.true(err instanceof Error)
t.end()
})
test('TimeoutError', t => {
const err = new errors.TimeoutError()
t.true(err instanceof Error)
t.true(err instanceof errors.ElasticsearchClientError)
t.true(err.hasOwnProperty('meta'))
t.end()
})
test('ConnectionError', t => {
const err = new errors.ConnectionError()
t.true(err instanceof Error)
t.true(err instanceof errors.ElasticsearchClientError)
t.true(err.hasOwnProperty('meta'))
t.end()
})
test('NoLivingConnectionsError', t => {
const err = new errors.NoLivingConnectionsError()
t.true(err instanceof Error)
t.true(err instanceof errors.ElasticsearchClientError)
t.true(err.hasOwnProperty('meta'))
t.end()
})
test('SerializationError', t => {
const err = new errors.SerializationError()
t.true(err instanceof Error)
t.true(err instanceof errors.ElasticsearchClientError)
t.false(err.hasOwnProperty('meta'))
t.end()
})
test('DeserializationError', t => {
const err = new errors.DeserializationError()
t.true(err instanceof Error)
t.true(err instanceof errors.ElasticsearchClientError)
t.false(err.hasOwnProperty('meta'))
t.end()
})
test('ConfigurationError', t => {
const err = new errors.ConfigurationError()
t.true(err instanceof Error)
t.true(err instanceof errors.ElasticsearchClientError)
t.false(err.hasOwnProperty('meta'))
t.end()
})
test('ResponseError', t => {
const meta = {
body: 1,
statusCode: 1,
headers: 1
}
const err = new errors.ResponseError(meta)
t.true(err instanceof Error)
t.true(err instanceof errors.ElasticsearchClientError)
t.true(err.hasOwnProperty('meta'))
t.ok(err.body)
t.ok(err.statusCode)
t.ok(err.headers)
t.end()
})

View File

@ -32,20 +32,41 @@ test('Should emit a request event when a request is performed', t => {
Connection: MockConnection
})
client.on(events.REQUEST, (err, meta) => {
client.on(events.REQUEST, (err, request) => {
t.error(err)
t.match(meta, {
connection: {
id: 'http://localhost:9200'
},
request: {
method: 'GET',
path: '/test/doc/_search',
querystring: 'q=foo%3Abar'
},
response: null,
attempts: 0,
aborted: false
t.match(request, {
body: null,
statusCode: null,
headers: null,
warnings: null,
meta: {
request: {
params: {
method: 'GET',
path: '/test/doc/_search',
body: '',
querystring: 'q=foo%3Abar',
headers: {
'Content-Type': 'application/json',
'Content-Length': '0'
}
},
options: {
ignore: null,
requestTimeout: null,
maxRetries: null,
asStream: false,
headers: null,
compression: false,
warnings: null
}
},
connection: {
id: 'http://localhost:9200'
},
attempts: 0,
aborted: false
}
})
})
@ -66,28 +87,44 @@ test('Should emit a response event in case of a successful response', t => {
Connection: MockConnection
})
client.on(events.RESPONSE, (err, meta) => {
client.on(events.RESPONSE, (err, request) => {
t.error(err)
t.match(meta, {
connection: {
id: 'http://localhost:9200'
t.match(request, {
body: { hello: 'world' },
statusCode: 200,
headers: {
'content-type': 'application/json;utf=8',
'connection': 'keep-alive'
},
request: {
method: 'GET',
path: '/test/doc/_search',
querystring: 'q=foo%3Abar'
},
response: {
body: { hello: 'world' },
statusCode: 200,
headers: {
'content-type': 'application/json;utf=8',
'connection': 'keep-alive'
warnings: null,
meta: {
request: {
params: {
method: 'GET',
path: '/test/doc/_search',
body: '',
querystring: 'q=foo%3Abar',
headers: {
'Content-Type': 'application/json',
'Content-Length': '0'
}
},
options: {
ignore: null,
requestTimeout: null,
maxRetries: null,
asStream: false,
headers: null,
compression: false,
warnings: null
}
},
warnings: null
},
attempts: 0,
aborted: false
connection: {
id: 'http://localhost:9200'
},
attempts: 0,
aborted: false
}
})
})
@ -109,27 +146,49 @@ test('Should emit a response event with the error set', t => {
maxRetries: 0
})
client.on(events.RESPONSE, (err, meta) => {
client.on(events.RESPONSE, (err, request) => {
t.ok(err instanceof TimeoutError)
t.match(meta, {
connection: {
id: 'http://localhost:9200'
},
request: {
method: 'GET',
path: '/test/doc/_search',
querystring: 'q=foo%3Abar'
},
response: null,
attempts: 0,
aborted: false
t.match(request, {
body: null,
statusCode: null,
headers: null,
warnings: null,
meta: {
request: {
params: {
method: 'GET',
path: '/test/doc/_search',
body: '',
querystring: 'q=foo%3Abar',
headers: {
'Content-Type': 'application/json',
'Content-Length': '0'
}
},
options: {
ignore: null,
requestTimeout: 500,
maxRetries: null,
asStream: false,
headers: null,
compression: false,
warnings: null
}
},
connection: {
id: 'http://localhost:9200'
},
attempts: 0,
aborted: false
}
})
})
client.search({
index: 'test',
type: 'doc',
q: 'foo:bar',
q: 'foo:bar'
}, {
requestTimeout: 500
}, (err, result) => {
t.ok(err instanceof TimeoutError)