From a86a552a85c97a3b9fdd56a530b6f1519bb8ce06 Mon Sep 17 00:00:00 2001 From: delvedor Date: Wed, 12 Dec 2018 16:49:06 +0100 Subject: [PATCH] Updated test --- test/integration/index.js | 2 +- test/integration/test-runner.js | 3 +- test/types/index.ts | 26 +++++++++++++++ test/unit/api.test.js | 56 +++++++++++++++++++++++++++++++++ test/unit/transport.test.js | 21 ++++++++----- 5 files changed, 99 insertions(+), 9 deletions(-) diff --git a/test/integration/index.js b/test/integration/index.js index 9b5a353c0..67f5a1473 100644 --- a/test/integration/index.js +++ b/test/integration/index.js @@ -46,7 +46,7 @@ Runner.prototype.start = function () { client.info((err, { body }) => { if (err) { this.log.fail(err.message) - return + process.exit(1) } const { number: version, build_hash: sha } = body.version diff --git a/test/integration/test-runner.js b/test/integration/test-runner.js index 228eadf35..4c227b366 100644 --- a/test/integration/test-runner.js +++ b/test/integration/test-runner.js @@ -252,7 +252,8 @@ TestRunner.prototype.set = function (key, name) { TestRunner.prototype.do = function (action, done) { const cmd = this.parseDo(action) const api = delve(this.client, cmd.method).bind(this.client) - api(cmd.params, (err, { body, warnings }) => { + const options = { ignore: cmd.params.ignore } + api(cmd.params, options, (err, { body, warnings }) => { if (action.warnings && warnings === null) { this.tap.fail('We should get a warning header', action.warnings) } else if (!action.warnings && warnings !== null) { diff --git a/test/types/index.ts b/test/types/index.ts index 30629eeb5..17e6669a3 100644 --- a/test/types/index.ts +++ b/test/types/index.ts @@ -26,6 +26,18 @@ client.index({ body: { hello: 'world' } }, (err: Error | null, result: ApiResponse) => {}) +// request options +client.index({ + index: 'test', + type: 'test', + id: 'test', + body: { hello: 'world' } +}, { + maxRetries: 2, + ignore: [404], + requestTimeout: 2000 +}, (err: Error | null, result: ApiResponse) => {}) + // Promises client.info() .then((result: ApiResponse) => {}) @@ -39,3 +51,17 @@ client.index({ }) .then((result: ApiResponse) => {}) .catch((err: Error) => {}) + +// request options +client.index({ + index: 'test', + type: 'test', + id: 'test', + body: { hello: 'world' } +}, { + maxRetries: 2, + ignore: [404], + requestTimeout: 2000 +}) + .then((result: ApiResponse) => {}) + .catch((err: Error) => {}) diff --git a/test/unit/api.test.js b/test/unit/api.test.js index 4ce2ca6e2..13264379f 100644 --- a/test/unit/api.test.js +++ b/test/unit/api.test.js @@ -165,3 +165,59 @@ test('Abort is not supported in promises', t => { t.type(request.abort, 'undefined') }) }) + +test('Basic (options and callback)', 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 client = new Client({ + node: `http://localhost:${port}` + }) + + client.search({ + index: 'test', + type: 'doc', + q: 'foo:bar' + }, { + requestTimeout: 10000 + }, (err, { body }) => { + t.error(err) + t.deepEqual(body, { hello: 'world' }) + server.stop() + }) + }) +}) + +test('Basic (options and promises)', t => { + t.plan(1) + + function handler (req, res) { + res.setHeader('Content-Type', 'application/json;utf=8') + res.end(JSON.stringify({ hello: 'world' })) + } + + buildServer(handler, ({ port }, server) => { + const client = new Client({ + node: `http://localhost:${port}` + }) + + client + .search({ + index: 'test', + type: 'doc', + q: 'foo:bar' + }, { + requestTimeout: 10000 + }) + .then(({ body }) => { + t.deepEqual(body, { hello: 'world' }) + server.stop() + }) + .catch(t.fail) + }) +}) diff --git a/test/unit/transport.test.js b/test/unit/transport.test.js index 48c341a90..108ed0d14 100644 --- a/test/unit/transport.test.js +++ b/test/unit/transport.test.js @@ -509,7 +509,8 @@ test('Custom retry mechanism', t => { transport.request({ method: 'GET', - path: '/hello', + path: '/hello' + }, { maxRetries: 1 }, (err, { body }) => { t.error(err) @@ -727,7 +728,8 @@ test('Override requestTimeout', t => { transport.request({ method: 'GET', - path: '/hello', + path: '/hello' + }, { requestTimeout: 2000 }, (err, { body }) => { t.error(err) @@ -1028,7 +1030,8 @@ test('Ignore status code', t => { transport.request({ method: 'GET', - path: '/404', + path: '/404' + }, { ignore: [404] }, (err, { body }) => { t.error(err) @@ -1044,7 +1047,8 @@ test('Ignore status code', t => { transport.request({ method: 'GET', - path: '/404', + path: '/404' + }, { ignore: [403, 405] }, (err, { body }) => { t.ok(err instanceof ResponseError) @@ -1148,7 +1152,8 @@ test('timeout option', t => { transport.request({ method: 'GET', - path: '/hello', + path: '/hello' + }, { requestTimeout: 500 }, (err, { body }) => { t.ok(err instanceof TimeoutError) @@ -1213,7 +1218,8 @@ test('timeout option', t => { transport.request({ method: 'GET', - path: '/hello', + path: '/hello' + }, { requestTimeout: '0.5s' }, (err, { body }) => { t.ok(err instanceof TimeoutError) @@ -1499,7 +1505,8 @@ test('asStream set to true', t => { transport.request({ method: 'GET', - path: '/hello', + path: '/hello' + }, { asStream: true }, (err, { body, headers }) => { t.error(err)