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

@elastic/elasticsearch-mock

Package Overview
Dependencies
Maintainers
64
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 0.1.0 to 0.2.0

6

index.d.ts

@@ -17,8 +17,8 @@ // Licensed to Elasticsearch B.V under one or more agreements.

export interface MockPattern {
method: string
path: string
method: string | string[]
path: string | string[]
querystring?: Record<string, string>
body?: Record<string, any>
body?: Record<string, any> | Record<string, any>[]
}
export default ClientMock

@@ -31,2 +31,11 @@ // Licensed to Elasticsearch B.V under one or more agreements.

add (pattern, fn) {
for (const key of ['method', 'path']) {
if (Array.isArray(pattern[key])) {
for (const value of pattern[key]) {
this.add({ ...pattern, [key]: value }, fn)
}
return this
}
}
if (typeof pattern.method !== 'string') throw new ConfigurationError('The method is not defined')

@@ -150,2 +159,3 @@ if (typeof pattern.path !== 'string') throw new ConfigurationError('The path is not defined')

const compression = params.headers['Content-Encoding'] === 'gzip'
const type = params.headers['Content-Type'] || ''

@@ -159,3 +169,5 @@ if (isStream(params.body)) {

stream.on('end', () => {
normalized.body = JSON.parse(normalized.body)
normalized.body = type.includes('x-ndjson')
? normalized.body.split(/\n|\n\r/).filter(Boolean).map(l => JSON.parse(l))
: JSON.parse(normalized.body)
callback(null, normalized)

@@ -170,7 +182,12 @@ })

}
normalized.body = JSON.parse(buffer)
buffer = buffer.toString()
normalized.body = type.includes('x-ndjson')
? buffer.split(/\n|\n\r/).filter(Boolean).map(l => JSON.parse(l))
: JSON.parse(buffer)
callback(null, normalized)
})
} else {
normalized.body = JSON.parse(params.body)
normalized.body = type.includes('x-ndjson')
? params.body.split(/\n|\n\r/).filter(Boolean).map(l => JSON.parse(l))
: JSON.parse(params.body)
setImmediate(callback, null, normalized)

@@ -177,0 +194,0 @@ }

@@ -24,2 +24,10 @@ // Licensed to Elasticsearch B.V under one or more agreements.

mock.add({
method: ['GET', 'POST'],
path: ['/_search', '/:index/_search']
}, params => {
expectType<MockPattern>(params)
return { status: 'ok' }
})
mock.add({
method: 'GET',

@@ -44,2 +52,11 @@ path: '/',

mock.add({
method: 'POST',
path: '/_bulk',
body: [{ foo: 'bar' }]
}, params => {
expectType<MockPattern>(params)
return { status: 'ok' }
})
mock.add({
method: 'GET',

@@ -46,0 +63,0 @@ path: '/'

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

@@ -5,0 +5,0 @@ "main": "index.js",

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

You can also specify multiple methods and/or paths at the same time:
```js
// This mock will catch every search request against any index
mock.add({
method: ['GET', 'POST'],
path: ['/_search', '/:index/_search']
}, () => {
return { status: 'ok' }
})
```
#### `get`

@@ -69,0 +80,0 @@

@@ -560,1 +560,222 @@ // Licensed to Elasticsearch B.V under one or more agreements.

})
test('Define multiple methods at once', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: ['GET', 'POST'],
path: '/:index/_search'
}, () => {
return { status: 'ok' }
})
let response = await client.search({
index: 'test',
q: 'foo:bar'
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
response = await client.search({
index: 'test',
body: {
query: { match: { foo: 'bar' } }
}
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})
test('Define multiple paths at once', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: 'GET',
path: ['/test1/_search', '/test2/_search']
}, () => {
return { status: 'ok' }
})
let response = await client.search({
index: 'test1',
q: 'foo:bar'
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
response = await client.search({
index: 'test2',
q: 'foo:bar'
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})
test('Define multiple paths and method at once', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: ['GET', 'POST'],
path: ['/test1/_search', '/test2/_search']
}, () => {
return { status: 'ok' }
})
let response = await client.search({
index: 'test1',
q: 'foo:bar'
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
response = await client.search({
index: 'test2',
q: 'foo:bar'
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
response = await client.search({
index: 'test1',
body: {
query: { match: { foo: 'bar' } }
}
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
response = await client.search({
index: 'test2',
body: {
query: { match: { foo: 'bar' } }
}
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})
test('ndjson API support', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: 'POST',
path: '/_bulk'
}, params => {
t.deepEqual(params.body, [
{ foo: 'bar' },
{ baz: 'fa\nz' }
])
return { status: 'ok' }
})
const response = await client.bulk({
body: [
{ foo: 'bar' },
{ baz: 'fa\nz' }
]
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})
test('ndjson API support (with compression)', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection(),
compression: 'gzip'
})
mock.add({
method: 'POST',
path: '/_bulk'
}, params => {
t.deepEqual(params.body, [
{ foo: 'bar' },
{ baz: 'fa\nz' }
])
return { status: 'ok' }
})
const response = await client.bulk({
body: [
{ foo: 'bar' },
{ baz: 'fa\nz' }
]
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})
test('ndjson API support (as stream)', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: 'POST',
path: '/_bulk'
}, params => {
t.deepEqual(params.body, [
{ foo: 'bar' },
{ baz: 'fa\nz' }
])
return { status: 'ok' }
})
const response = await client.bulk({
body: intoStream(client.serializer.ndserialize([
{ foo: 'bar' },
{ baz: 'fa\nz' }
]))
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})
test('ndjson API support (as stream with compression)', async t => {
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection(),
compression: 'gzip'
})
mock.add({
method: 'POST',
path: '/_bulk'
}, params => {
t.deepEqual(params.body, [
{ foo: 'bar' },
{ baz: 'fa\nz' }
])
return { status: 'ok' }
})
const response = await client.bulk({
body: intoStream(client.serializer.ndserialize([
{ foo: 'bar' },
{ baz: 'fa\nz' }
]))
})
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})
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