Skip to content

Add support for product check #20

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Aug 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
node-version: [10.x, 12.x, 13.x]
node-version: [10.x, 12.x, 14.x, 16.x]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ const client = new Client({

mock.add({
method: 'GET',
path: '/'
path: '/_cat/health'
}, () => {
return { status: 'ok' }
})

client.info(console.log)
client.cat.health(console.log)
```

## API
Expand All @@ -54,11 +54,11 @@ const mock = new Mock()
Adds a new mock for a given pattern and assigns it to a resolver function.

```js
// every GET request to the `/` path
// every GET request to the `/_cat/health` path
// will return `{ status: 'ok' }`
mock.add({
method: 'GET',
path: '/'
path: '/_cat/health'
}, () => {
return { status: 'ok' }
})
Expand All @@ -82,7 +82,7 @@ Returns the matching resolver function for the given pattern, it returns `null`
```js
const fn = mock.get({
method: 'GET',
path: '/'
path: '/_cat/health'
})
```

Expand Down Expand Up @@ -136,14 +136,14 @@ The more field you specify, the more the mock will be strict, for example:
```js
mock.add({
method: 'GET',
path: '/',
path: '/_cat/health'
querystring: { pretty: 'true' }
}, () => {
return { status: 'ok' }
})

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

You can craft custom responses for different queries:
Expand Down Expand Up @@ -227,7 +227,7 @@ const Mock = require('@elastic/elasticsearch-mock')
const mock = new Mock()
mock.add({
method: 'GET',
path: '/'
path: '/_cat/health'
}, () => {
return new errors.ResponseError({
body: { errors: {}, status: 500 },
Expand Down
4 changes: 2 additions & 2 deletions example.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ const client = new Client({

mock.add({
method: 'GET',
path: '/'
path: '/_cat/health'
}, () => {
return { status: 'ok' }
})
Expand Down Expand Up @@ -64,7 +64,7 @@ mock.add({
}
})

client.info(console.log)
client.cat.health(console.log)

client.ping(console.log)

Expand Down
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ class Mocker {
function buildConnectionClass (mocker) {
class MockConnection extends Connection {
request (params, callback) {
var aborted = false
let aborted = false
normalizeParams(params, prepareResponse)

function prepareResponse (error, params) {
/* istanbul ignore next */
if (aborted) {
return callback(new RequestAbortedError(), null)
}
Expand All @@ -118,15 +119,21 @@ function buildConnectionClass (mocker) {
return callback(new ConnectionError(error.message), null)
}

var stream = null
var payload = ''
var statusCode = 200
let stream = null
let payload = ''
let statusCode = 200

const resolver = mocker.get(params)

if (resolver === null) {
payload = { error: 'Mock not found' }
// return info route for product check unless configured otherwise
if (params.method === 'GET' && params.path === '/') {
payload = { version: { number: '8.0.0-SNAPSHOT' } }
} else {
payload = { error: 'Mock not found' }
statusCode = 404
}
stream = intoStream(JSON.stringify(payload))
statusCode = 404
} else {
payload = resolver(params)
if (payload instanceof ResponseError) {
Expand All @@ -147,6 +154,7 @@ function buildConnectionClass (mocker) {
: '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)
)
Expand All @@ -156,6 +164,7 @@ function buildConnectionClass (mocker) {
}

return {
/* istanbul ignore next */
abort () {
aborted = true
}
Expand Down
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@
},
"homepage": "https://github.com/elastic/elasticsearch-js-mock#readme",
"devDependencies": {
"@elastic/elasticsearch": "^7.7.0-rc.2",
"ava": "^3.6.0",
"nyc": "^15.0.1",
"standard": "^14.3.3",
"tsd": "^0.11.0"
"@elastic/elasticsearch": "^7.14.0",
"ava": "^3.15.0",
"nyc": "^15.1.0",
"standard": "^16.0.3",
"tsd": "^0.17.0"
},
"dependencies": {
"fast-deep-equal": "^3.1.1",
"find-my-way": "^2.2.2",
"into-stream": "^5.1.1"
"fast-deep-equal": "^3.1.3",
"find-my-way": "^4.3.3",
"into-stream": "^6.0.0"
}
}
56 changes: 39 additions & 17 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,12 @@ test('Should mock an API', async t => {

mock.add({
method: 'GET',
path: '/'
path: '/_cat/indices'
}, () => {
return { status: 'ok' }
})

const response = await client.info()
const response = await client.cat.indices()
t.deepEqual(response.body, { status: 'ok' })
t.is(response.statusCode, 200)
})
Expand Down Expand Up @@ -114,7 +114,7 @@ test('If an API has not been mocked, it should return a 404', async t => {
})

try {
await client.info()
await client.cat.indices()
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.ResponseError)
Expand Down Expand Up @@ -267,7 +267,7 @@ test.cb('Abort a request (with callbacks)', t => {
Connection: mock.getConnection()
})

const r = client.info((err, result) => {
const r = client.cat.indices((err, result) => {
t.true(err instanceof errors.RequestAbortedError)
t.end()
})
Expand All @@ -282,7 +282,7 @@ test('Abort a request (with promises)', async t => {
Connection: mock.getConnection()
})

const p = client.info()
const p = client.cat.indices()
p.abort()

try {
Expand All @@ -302,7 +302,7 @@ test('Return a response error', async t => {

mock.add({
method: 'GET',
path: '/'
path: '/_cat/indices'
}, () => {
return new errors.ResponseError({
body: { errors: {}, status: 500 },
Expand All @@ -311,7 +311,7 @@ test('Return a response error', async t => {
})

try {
await client.info()
await client.cat.indices()
t.fail('Should throw')
} catch (err) {
t.deepEqual(err.body, { errors: {}, status: 500 })
Expand All @@ -328,13 +328,13 @@ test('Return a timeout error', async t => {

mock.add({
method: 'GET',
path: '/'
path: '/_cat/indices'
}, () => {
return new errors.TimeoutError()
})

try {
await client.info()
await client.cat.indices()
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.TimeoutError)
Expand Down Expand Up @@ -440,20 +440,20 @@ test('Discriminate on the querystring', async t => {

mock.add({
method: 'GET',
path: '/'
path: '/_cat/indices'
}, () => {
return { querystring: false }
})

mock.add({
method: 'GET',
path: '/',
path: '/_cat/indices',
querystring: { pretty: 'true' }
}, () => {
return { querystring: true }
})

const response = await client.info({ pretty: true })
const response = await client.cat.indices({ pretty: true })
t.deepEqual(response.body, { querystring: true })
t.is(response.statusCode, 200)
})
Expand All @@ -467,22 +467,22 @@ test('The handler for the route exists, but the request is not enough precise',

mock.add({
method: 'GET',
path: '/',
path: '/_cat/indices',
querystring: { human: 'true' }
}, () => {
return { status: 'ok' }
})

mock.add({
method: 'GET',
path: '/',
path: '/_cat/indices',
querystring: { pretty: 'true' }
}, () => {
return { status: 'ok' }
})

try {
await client.info()
await client.cat.indices()
t.fail('Should throw')
} catch (err) {
t.true(err instanceof errors.ResponseError)
Expand All @@ -500,12 +500,12 @@ test('Send back a plain string', async t => {

mock.add({
method: 'GET',
path: '/'
path: '/_cat/indices'
}, () => {
return 'ok'
})

const response = await client.info()
const response = await client.cat.indices()
t.is(response.body, 'ok')
t.is(response.statusCode, 200)
t.is(response.headers['content-type'], 'text/plain;utf=8')
Expand Down Expand Up @@ -878,3 +878,25 @@ test('Should clear all mocks', async t => {
t.is(err.statusCode, 404)
}
})

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)
}
})