From c990ed43d421238caf661c221d0bedc7e5de02b8 Mon Sep 17 00:00:00 2001 From: delvedor Date: Mon, 11 Mar 2019 17:12:53 +0100 Subject: [PATCH] Updated test --- test/behavior/sniff.test.js | 16 ++-- test/types/index.ts | 16 ++-- test/unit/errors.test.js | 93 ++++++++++++++++++++++ test/unit/events.test.js | 151 +++++++++++++++++++++++++----------- 4 files changed, 218 insertions(+), 58 deletions(-) create mode 100644 test/unit/errors.test.js diff --git a/test/behavior/sniff.test.js b/test/behavior/sniff.test.js index b88c91e83..c76709b49 100644 --- a/test/behavior/sniff.test.js +++ b/test/behavior/sniff.test.js @@ -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 diff --git a/test/types/index.ts b/test/types/index.ts index 51ece57b5..fd1260bab 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -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) => {}) diff --git a/test/unit/errors.test.js b/test/unit/errors.test.js new file mode 100644 index 000000000..213194cff --- /dev/null +++ b/test/unit/errors.test.js @@ -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() +}) diff --git a/test/unit/events.test.js b/test/unit/events.test.js index 0d314a6e6..432194a29 100644 --- a/test/unit/events.test.js +++ b/test/unit/events.test.js @@ -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)