From 95c4369f9b03d0ccb7e1ff5766734860f36f9e6e Mon Sep 17 00:00:00 2001 From: delvedor Date: Wed, 30 Jan 2019 09:33:13 +0100 Subject: [PATCH] Updated test --- test/unit/api-async.js | 61 ++++++++++++++++++++++++++++++++++++++++++ test/unit/api.test.js | 4 +++ 2 files changed, 65 insertions(+) create mode 100644 test/unit/api-async.js diff --git a/test/unit/api-async.js b/test/unit/api-async.js new file mode 100644 index 000000000..dbfa7ea7f --- /dev/null +++ b/test/unit/api-async.js @@ -0,0 +1,61 @@ +'use strict' + +const { Client } = require('../../index') +const { buildServer } = require('../utils') + +function runAsyncTest (test) { + test('async await (search)', t => { + t.plan(1) + + function handler (req, res) { + res.setHeader('Content-Type', 'application/json;utf=8') + res.end(JSON.stringify({ hello: 'world' })) + } + + buildServer(handler, async ({ port }, server) => { + const client = new Client({ + node: `http://localhost:${port}` + }) + + try { + const { body } = await client.search({ + index: 'test', + type: 'doc', + q: 'foo:bar' + }) + t.deepEqual(body, { hello: 'world' }) + } catch (err) { + t.fail(err) + } + server.stop() + }) + }) + + test('async await (index)', t => { + t.plan(1) + + function handler (req, res) { + res.setHeader('Content-Type', 'application/json;utf=8') + res.end(JSON.stringify({ hello: 'world' })) + } + + buildServer(handler, async ({ port }, server) => { + const client = new Client({ + node: `http://localhost:${port}` + }) + + try { + await client.index({ + index: 'test', + body: { foo: 'bar' } + }) + t.pass('ok') + } catch (err) { + t.fail(err) + } + server.stop() + }) + }) +} + +module.exports = runAsyncTest diff --git a/test/unit/api.test.js b/test/unit/api.test.js index 50ebceb88..2a6b0eedc 100644 --- a/test/unit/api.test.js +++ b/test/unit/api.test.js @@ -249,3 +249,7 @@ test('Pass unknown parameters as query parameters (and get a warning)', t => { }) }) }) + +if (Number(process.version.split('.')[0].slice(1)) >= 8) { + require('./api-async')(test) +}