Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@elastic/elasticsearch-mock

Package Overview
Dependencies
Maintainers
80
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@elastic/elasticsearch-mock - npm Package Compare versions

Comparing version 1.0.0 to 2.0.0

4

index.d.ts

@@ -20,3 +20,3 @@ /*

import { Connection } from '@elastic/elasticsearch'
import { BaseConnection } from '@elastic/elasticsearch'

@@ -29,3 +29,3 @@ declare class ClientMock {

clearAll(): ClientMock
getConnection(): typeof Connection
getConnection(): typeof BaseConnection
}

@@ -32,0 +32,0 @@

@@ -24,5 +24,4 @@ /*

const querystring = require('querystring')
const { Connection, errors } = require('@elastic/elasticsearch')
const { BaseConnection, errors } = require('@elastic/elasticsearch')
const Router = require('find-my-way')
const intoStream = require('into-stream')
const equal = require('fast-deep-equal')

@@ -123,67 +122,68 @@ const kRouter = Symbol('elasticsearch-mock-router')

function buildConnectionClass (mocker) {
class MockConnection extends Connection {
request (params, callback) {
class MockConnection extends BaseConnection {
request (params, options) {
const abortListener = () => {
aborted = true
}
let aborted = false
normalizeParams(params, prepareResponse)
if (options.signal != null) {
options.signal.addEventListener('abort', abortListener, { once: true })
}
function prepareResponse (error, params) {
/* istanbul ignore next */
if (aborted) {
return callback(new RequestAbortedError(), null)
}
/* istanbul ignore next */
if (error) {
return callback(new ConnectionError(error.message), null)
}
return new Promise((resolve, reject) => {
normalizeParams(params, prepareResponse)
function prepareResponse (error, params) {
/* istanbul ignore next */
if (options.signal != null) {
if ('removeEventListener' in options.signal) {
options.signal.removeEventListener('abort', abortListener)
} else {
options.signal.removeListener('abort', abortListener)
}
}
/* istanbul ignore next */
if (aborted) {
return reject(new RequestAbortedError())
}
/* istanbul ignore next */
if (error) {
return reject(new ConnectionError(error.message))
}
let stream = null
let payload = ''
let statusCode = 200
const response = {}
let payload = ''
let statusCode = 200
const resolver = mocker.get(params)
const resolver = mocker.get(params)
if (resolver === null) {
// return info route for product check unless configured otherwise
if (params.method === 'GET' && params.path === '/') {
payload = { version: { number: '8.0.0-SNAPSHOT' } }
} else {
if (resolver === null) {
payload = { error: 'Mock not found' }
statusCode = 404
} else {
payload = resolver(params)
if (payload instanceof ResponseError) {
statusCode = payload.statusCode
payload = payload.body
} else if (payload instanceof ElasticsearchClientError) {
return reject(payload)
}
}
stream = intoStream(JSON.stringify(payload))
} else {
payload = resolver(params)
if (payload instanceof ResponseError) {
statusCode = payload.statusCode
payload = payload.body
} else if (payload instanceof ElasticsearchClientError) {
return callback(payload, null)
response.body = typeof payload === 'string' ? payload : JSON.stringify(payload)
response.statusCode = statusCode
response.headers = {
'content-type': typeof payload === 'string'
? 'text/plain;utf=8'
: 'application/json;utf=8',
date: new Date().toISOString(),
connection: 'keep-alive',
'x-elastic-product': 'Elasticsearch',
'content-length': Buffer.byteLength(
typeof payload === 'string' ? payload : JSON.stringify(payload)
)
}
stream = intoStream(
typeof payload === 'string' ? payload : JSON.stringify(payload)
)
}
stream.statusCode = statusCode
stream.headers = {
'content-type': typeof payload === 'string'
? 'text/plain;utf=8'
: 'application/json;utf=8',
date: new Date().toISOString(),
connection: 'keep-alive',
'x-elastic-product': 'Elasticsearch',
'content-length': Buffer.byteLength(
typeof payload === 'string' ? payload : JSON.stringify(payload)
)
resolve(response)
}
callback(null, stream)
}
return {
/* istanbul ignore next */
abort () {
aborted = true
}
}
})
}

@@ -190,0 +190,0 @@ }

{
"name": "@elastic/elasticsearch-mock",
"version": "1.0.0",
"version": "2.0.0",
"description": "Mock utility for the Elasticsearch's Node.js client",

@@ -37,13 +37,14 @@ "main": "index.js",

"devDependencies": {
"@elastic/elasticsearch": "^7.14.0",
"ava": "^3.15.0",
"@elastic/elasticsearch": "^8.1.0",
"ava": "^4.1.0",
"node-abort-controller": "^3.0.1",
"nyc": "^15.1.0",
"standard": "^16.0.3",
"tsd": "^0.17.0"
"standard": "^16.0.4",
"tsd": "^0.19.1"
},
"dependencies": {
"fast-deep-equal": "^3.1.3",
"find-my-way": "^4.3.3",
"find-my-way": "^5.2.0",
"into-stream": "^6.0.0"
}
}

@@ -11,3 +11,3 @@ <img align="right" width="auto" height="auto" src="https://www.elastic.co/static-res/images/elastic-logo-200.png">

This library is compatible with `@elastic/elasticsearch` ≤ v7.
Use `v1.0.0` for `@elastic/elasticsearch` ≤ v7 compatibility and `v2.0.0` for `@elastic/elasticsearch` ≥ v8 compatibility.

@@ -44,3 +44,5 @@ ### Features

client.cat.health(console.log)
client.cat.health()
.then(console.log)
.catch(console.log)
```

@@ -151,4 +153,9 @@

client.cat.health(console.log) // => 404 error
client.cat.health({ pretty: true }, console.log) // => { status: 'ok' }
client.cat.health()
.then(console.log)
.catch(console.log) // 404 error
client.cat.health({ pretty: true })
.then(console.log) // { status: 'ok' }
.catch(console.log)
```

@@ -195,4 +202,9 @@

client.count({ index: 'foo' }, console.log) // => { count: 42 }
client.count({ index: 'bar' }, console.log) // => { count: 42 }
client.count({ index: 'foo' })
.then(console.log) // => { count: 42 }
.catch(console.log)
client.count({ index: 'bar' })
.then(console.log) // => { count: 42 }
.catch(console.log)
```

@@ -206,7 +218,12 @@

}, () => {
return { status: 'ok' }
return ''
})
client.indices.exists({ index: 'foo' }, console.log) // => { status: 'ok' }
client.ping(console.log) // => { status: 'ok' }
client.indices.exists({ index: 'foo' })
.then(console.log) // => true
.catch(console.log)
client.ping()
.then(console.log) // => true
.catch(console.log)
```

@@ -213,0 +230,0 @@

@@ -24,2 +24,3 @@ /*

const { Client, errors } = require('@elastic/elasticsearch')
const { AbortController } = require('node-abort-controller')
const intoStream = require('into-stream')

@@ -42,3 +43,3 @@ const Mock = require('./')

const response = await client.cat.indices()
const response = await client.cat.indices({}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -82,6 +83,6 @@ t.is(response.statusCode, 200)

index: 'test',
body: { query: { match_all: {} } }
query: { match_all: {} }
})
t.deepEqual(response.body, {
t.deepEqual(response, {
hits: {

@@ -95,6 +96,6 @@ total: { value: 1, relation: 'eq' },

index: 'test',
body: { query: { match: { foo: 'bar' } } }
query: { match: { foo: 'bar' } }
})
t.deepEqual(response.body, {
t.deepEqual(response, {
hits: {

@@ -122,6 +123,6 @@ total: { value: 0, relation: 'eq' },

let response = await client.count({ index: 'foo' })
t.deepEqual(response.body, { count: 42 })
t.deepEqual(response, { count: 42 })
response = await client.count({ index: 'bar' })
t.deepEqual(response.body, { count: 42 })
t.deepEqual(response, { count: 42 })
})

@@ -150,3 +151,3 @@

node: 'http://localhost:9200',
compression: 'gzip',
compression: true,
Connection: mock.getConnection()

@@ -182,6 +183,6 @@ })

index: 'test',
body: { query: { match_all: {} } }
query: { match_all: {} }
})
t.deepEqual(response.body, {
t.deepEqual(response, {
hits: {

@@ -194,3 +195,3 @@ total: { value: 1, relation: 'eq' },

test('Should handle streaming body', async t => {
test('Should handle streaming body with transport.request', async t => {
const mock = new Mock()

@@ -227,8 +228,9 @@ const client = new Client({

const response = await client.search({
index: 'test',
const response = await client.transport.request({
method: 'POST',
path: '/test/_search',
body: intoStream(JSON.stringify({ query: { match: { foo: 'bar' } } }))
})
t.deepEqual(response.body, {
t.deepEqual(response, {
hits: {

@@ -241,7 +243,7 @@ total: { value: 0, relation: 'eq' },

test('Should handle compressed streaming body', async t => {
test('Should handle compressed streaming body with transport.request', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
compression: 'gzip',
compression: true,
Connection: mock.getConnection()

@@ -275,8 +277,9 @@ })

const response = await client.search({
index: 'test',
const response = await client.transport.request({
method: 'POST',
path: '/test/_search',
body: intoStream(JSON.stringify({ query: { match: { foo: 'bar' } } }))
})
t.deepEqual(response.body, {
t.deepEqual(response, {
hits: {

@@ -289,3 +292,3 @@ total: { value: 0, relation: 'eq' },

test.cb('Abort a request (with callbacks)', t => {
test('Abort a request', async t => {
const mock = new Mock()

@@ -297,20 +300,6 @@ const client = new Client({

const r = client.cat.indices((err, result) => {
t.true(err instanceof errors.RequestAbortedError)
t.end()
})
const ac = new AbortController()
const p = client.cat.indices({}, { signal: ac.signal })
ac.abort()
r.abort()
})
test('Abort a request (with promises)', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
const p = client.cat.indices()
p.abort()
try {

@@ -376,3 +365,3 @@ await p

node: 'http://localhost:9200',
compression: 'gzip',
compression: true,
Connection: mock.getConnection()

@@ -388,6 +377,6 @@ })

index: 'test',
body: { query: { match_all: {} } }
query: { match_all: {} }
})
t.deepEqual(response.body, {
t.deepEqual(response, {
method: 'POST',

@@ -440,6 +429,6 @@ path: '/test/_search',

pretty: true,
body: { query: { match_all: {} } }
query: { match_all: {} }
})
t.deepEqual(response.body, {
t.deepEqual(response, {
hits: {

@@ -454,6 +443,6 @@ total: { value: 1, relation: 'eq' },

pretty: true,
body: { query: { match: { foo: 'bar' } } }
query: { match: { foo: 'bar' } }
})
t.deepEqual(response.body, {
t.deepEqual(response, {
hits: {

@@ -488,3 +477,3 @@ total: { value: 0, relation: 'eq' },

const response = await client.cat.indices({ pretty: true })
const response = await client.cat.indices({ pretty: true }, { meta: true })
t.deepEqual(response.body, { querystring: true })

@@ -541,3 +530,3 @@ t.is(response.statusCode, 200)

const response = await client.cat.indices()
const response = await client.cat.indices({}, { meta: true })
t.is(response.body, 'ok')

@@ -562,3 +551,3 @@ t.is(response.statusCode, 200)

const response = await client.cat.indices()
const response = await client.cat.indices({}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -613,3 +602,3 @@ t.is(response.statusCode, 200)

q: 'foo:bar'
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -620,6 +609,4 @@ t.is(response.statusCode, 200)

index: 'test',
body: {
query: { match: { foo: 'bar' } }
}
})
query: { match: { foo: 'bar' } }
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -646,3 +633,3 @@ t.is(response.statusCode, 200)

q: 'foo:bar'
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -654,3 +641,3 @@ t.is(response.statusCode, 200)

q: 'foo:bar'
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -677,3 +664,3 @@ t.is(response.statusCode, 200)

q: 'foo:bar'
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -685,3 +672,3 @@ t.is(response.statusCode, 200)

q: 'foo:bar'
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -692,6 +679,4 @@ t.is(response.statusCode, 200)

index: 'test1',
body: {
query: { match: { foo: 'bar' } }
}
})
query: { match: { foo: 'bar' } }
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -702,6 +687,4 @@ t.is(response.statusCode, 200)

index: 'test2',
body: {
query: { match: { foo: 'bar' } }
}
})
query: { match: { foo: 'bar' } }
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -730,7 +713,7 @@ t.is(response.statusCode, 200)

const response = await client.bulk({
body: [
operations: [
{ foo: 'bar' },
{ baz: 'fa\nz' }
]
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -745,3 +728,3 @@ t.is(response.statusCode, 200)

Connection: mock.getConnection(),
compression: 'gzip'
compression: true
})

@@ -761,7 +744,7 @@

const response = await client.bulk({
body: [
operations: [
{ foo: 'bar' },
{ baz: 'fa\nz' }
]
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -771,3 +754,3 @@ t.is(response.statusCode, 200)

test('ndjson API support (as stream)', async t => {
test('ndjson API support (as stream) with transport.request', async t => {
const mock = new Mock()

@@ -790,8 +773,10 @@ const client = new Client({

const response = await client.bulk({
body: intoStream(client.serializer.ndserialize([
const response = await client.transport.request({
method: 'POST',
path: '/_bulk',
bulkBody: intoStream(client.serializer.ndserialize([
{ foo: 'bar' },
{ baz: 'fa\nz' }
]))
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -801,3 +786,3 @@ t.is(response.statusCode, 200)

test('ndjson API support (as stream with compression)', async t => {
test('ndjson API support (as stream with compression) with transport.request', async t => {
const mock = new Mock()

@@ -807,3 +792,3 @@ const client = new Client({

Connection: mock.getConnection(),
compression: 'gzip'
compression: true
})

@@ -822,8 +807,10 @@

const response = await client.bulk({
body: intoStream(client.serializer.ndserialize([
const response = await client.transport.request({
method: 'POST',
path: '/_bulk',
bulkBody: intoStream(client.serializer.ndserialize([
{ foo: 'bar' },
{ baz: 'fa\nz' }
]))
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -854,3 +841,3 @@ t.is(response.statusCode, 200)

q: 'foo:bar'
})
}, { meta: true })
t.deepEqual(response.body, { status: 'ok' })

@@ -933,23 +920,1 @@ t.is(response.statusCode, 200)

})
test('Override product check', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: 'GET',
path: '/'
}, () => {
return { something: 'else' }
})
try {
await client.cat.nodes()
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.ProductNotSupportedError)
}
})

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc