弹性搜索节点.js 客户端模拟工具
在测试你的应用时 你并不总是需要 一个弹性搜索实例 并运行, 但您可能需要使用客户端获取一些数据. 如果你面临这种情况,这个图书馆就是你需要的.
使用 v1.0.0 页:1 @elastic/elasticsearch v7 兼容性和 v2.0.0 页:1 @elastic/elasticsearch v8 兼容性.
特征
- 简单而直观的 API
- 只嘲笑 http 层, 剩下的客户端照常工作
- 多亏了"收缩"或"放荡"的嘲弄
安装
npm install @elastic/elasticsearch-mock --save-dev
使用量
const { Client } = require('@elastic/elasticsearch')
const Mock = require('@elastic/elasticsearch-mock')
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
mock.add({
method: 'GET',
path: '/_cat/health'
}, () => {
return { status: 'ok' }
})
client.cat.health()
.then(console.log)
.catch(console.log)
API 密码
Constructor
在开始使用库之前, 您需要创建新实例 :
const Mock = require('@elastic/elasticsearch-mock')
const mock = new Mock()
add
添加给定模式的新模拟并指定给解析器函数.
// every GET request to the `/_cat/health` path
// will return `{ status: 'ok' }`
mock.add({
method: 'GET',
path: '/_cat/health'
}, () => {
return { status: 'ok' }
})
也可以同时指定多种方法和/或路径:
// This mock will catch every search request against any index
mock.add({
method: ['GET', 'POST'],
path: ['/_search', '/:index/_search']
}, () => {
return { status: 'ok' }
})
get
返回给定模式的匹配解析器函数, 它返回 null 如果不存在匹配模式.
const fn = mock.get({
method: 'GET',
path: '/_cat/health'
})
clear
清除/删除对特定路线的模拟.
mock.clear({
method: ['GET'],
path: ['/_search', '/:index/_search']
})
clearAll
清除所有嘲笑.
mock.clearAll()
getConnection
返回自定义 Connection 上课你 必须 传递到 Elasticsearch 客户端实例 .
const { Client } = require('@elastic/elasticsearch')
const Mock = require('@elastic/elasticsearch-mock')
const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})
模拟图案
一个图案是描述 http 查询到 Elasticsearch 的对象, 它看起来像:
interface MockPattern {
method: string
path: string
querystring?: Record<string, string>
body?: Record<string, any>
}
字段越多,模拟会越严格,例如:
mock.add({
method: 'GET',
path: '/_cat/health'
querystring: { pretty: 'true' }
}, () => {
return { 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)
您可以为不同的查询自定义回复 :
mock.add({
method: 'POST',
path: '/indexName/_search'
body: { query: { match_all: {} } }
}, () => {
return {
hits: {
total: { value: 1, relation: 'eq' },
hits: [{ _source: { baz: 'faz' } }]
}
}
})
mock.add({
method: 'POST',
path: '/indexName/_search',
body: { query: { match: { foo: 'bar' } } }
}, () => {
return {
hits: {
total: { value: 0, relation: 'eq' },
hits: []
}
}
})
您也可以指定动态 urls :
mock.add({
method: 'GET',
path: '/:index/_count'
}, () => {
return { 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)
圆通也相助.
mock.add({
method: 'HEAD',
path: '*'
}, () => {
return ''
})
client.indices.exists({ index: 'foo' })
.then(console.log) // => true
.catch(console.log)
client.ping()
.then(console.log) // => true
.catch(console.log)
动态反应
解析器函数取一个代表客户端已调用的API的单一参数. 你可以用它来设计动态响应.
mock.add({
method: 'POST',
path: '/indexName/_search',
}, params => {
return { query: params.body.query }
})
错误
此工具使用 Elasticsearch 客户端相同的错误类, 如果您想要返回特定 API 调用错误, 请使用 ResponseError 类 :
const { errors } = require('@elastic/elasticsearch')
const Mock = require('@elastic/elasticsearch-mock')
const mock = new Mock()
mock.add({
method: 'GET',
path: '/_cat/health'
}, () => {
return new errors.ResponseError({
body: { errors: {}, status: 500 },
statusCode: 500
})
})
许可证
该软件根据 Apache 2 许可证。 。 。 .