Skip to content

Commit ac23fd9

Browse files
authored
Merge pull request #20 from elastic/fix-18
2 parents 686834d + f197398 commit ac23fd9

File tree

6 files changed

+74
-43
lines changed

6 files changed

+74
-43
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ jobs:
99

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

1515
steps:

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ const client = new Client({
3131

3232
mock.add({
3333
method: 'GET',
34-
path: '/'
34+
path: '/_cat/health'
3535
}, () => {
3636
return { status: 'ok' }
3737
})
3838

39-
client.info(console.log)
39+
client.cat.health(console.log)
4040
```
4141

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

5656
```js
57-
// every GET request to the `/` path
57+
// every GET request to the `/_cat/health` path
5858
// will return `{ status: 'ok' }`
5959
mock.add({
6060
method: 'GET',
61-
path: '/'
61+
path: '/_cat/health'
6262
}, () => {
6363
return { status: 'ok' }
6464
})
@@ -82,7 +82,7 @@ Returns the matching resolver function for the given pattern, it returns `null`
8282
```js
8383
const fn = mock.get({
8484
method: 'GET',
85-
path: '/'
85+
path: '/_cat/health'
8686
})
8787
```
8888

@@ -136,14 +136,14 @@ The more field you specify, the more the mock will be strict, for example:
136136
```js
137137
mock.add({
138138
method: 'GET',
139-
path: '/',
139+
path: '/_cat/health'
140140
querystring: { pretty: 'true' }
141141
}, () => {
142142
return { status: 'ok' }
143143
})
144144

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

149149
You can craft custom responses for different queries:
@@ -227,7 +227,7 @@ const Mock = require('@elastic/elasticsearch-mock')
227227
const mock = new Mock()
228228
mock.add({
229229
method: 'GET',
230-
path: '/'
230+
path: '/_cat/health'
231231
}, () => {
232232
return new errors.ResponseError({
233233
body: { errors: {}, status: 500 },

example.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const client = new Client({
1515

1616
mock.add({
1717
method: 'GET',
18-
path: '/'
18+
path: '/_cat/health'
1919
}, () => {
2020
return { status: 'ok' }
2121
})
@@ -64,7 +64,7 @@ mock.add({
6464
}
6565
})
6666

67-
client.info(console.log)
67+
client.cat.health(console.log)
6868

6969
client.ping(console.log)
7070

index.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ class Mocker {
106106
function buildConnectionClass (mocker) {
107107
class MockConnection extends Connection {
108108
request (params, callback) {
109-
var aborted = false
109+
let aborted = false
110110
normalizeParams(params, prepareResponse)
111111

112112
function prepareResponse (error, params) {
113+
/* istanbul ignore next */
113114
if (aborted) {
114115
return callback(new RequestAbortedError(), null)
115116
}
@@ -118,15 +119,21 @@ function buildConnectionClass (mocker) {
118119
return callback(new ConnectionError(error.message), null)
119120
}
120121

121-
var stream = null
122-
var payload = ''
123-
var statusCode = 200
122+
let stream = null
123+
let payload = ''
124+
let statusCode = 200
124125

125126
const resolver = mocker.get(params)
127+
126128
if (resolver === null) {
127-
payload = { error: 'Mock not found' }
129+
// return info route for product check unless configured otherwise
130+
if (params.method === 'GET' && params.path === '/') {
131+
payload = { version: { number: '8.0.0-SNAPSHOT' } }
132+
} else {
133+
payload = { error: 'Mock not found' }
134+
statusCode = 404
135+
}
128136
stream = intoStream(JSON.stringify(payload))
129-
statusCode = 404
130137
} else {
131138
payload = resolver(params)
132139
if (payload instanceof ResponseError) {
@@ -147,6 +154,7 @@ function buildConnectionClass (mocker) {
147154
: 'application/json;utf=8',
148155
date: new Date().toISOString(),
149156
connection: 'keep-alive',
157+
'x-elastic-product': 'Elasticsearch',
150158
'content-length': Buffer.byteLength(
151159
typeof payload === 'string' ? payload : JSON.stringify(payload)
152160
)
@@ -156,6 +164,7 @@ function buildConnectionClass (mocker) {
156164
}
157165

158166
return {
167+
/* istanbul ignore next */
159168
abort () {
160169
aborted = true
161170
}

package.json

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
},
3535
"homepage": "https://github.com/elastic/elasticsearch-js-mock#readme",
3636
"devDependencies": {
37-
"@elastic/elasticsearch": "^7.7.0-rc.2",
38-
"ava": "^3.6.0",
39-
"nyc": "^15.0.1",
40-
"standard": "^14.3.3",
41-
"tsd": "^0.11.0"
37+
"@elastic/elasticsearch": "^7.14.0",
38+
"ava": "^3.15.0",
39+
"nyc": "^15.1.0",
40+
"standard": "^16.0.3",
41+
"tsd": "^0.17.0"
4242
},
4343
"dependencies": {
44-
"fast-deep-equal": "^3.1.1",
45-
"find-my-way": "^2.2.2",
46-
"into-stream": "^5.1.1"
44+
"fast-deep-equal": "^3.1.3",
45+
"find-my-way": "^4.3.3",
46+
"into-stream": "^6.0.0"
4747
}
4848
}

test.js

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,12 @@ test('Should mock an API', async t => {
1818

1919
mock.add({
2020
method: 'GET',
21-
path: '/'
21+
path: '/_cat/indices'
2222
}, () => {
2323
return { status: 'ok' }
2424
})
2525

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

116116
try {
117-
await client.info()
117+
await client.cat.indices()
118118
t.fail('Should throw')
119119
} catch (err) {
120120
t.true(err instanceof errors.ResponseError)
@@ -267,7 +267,7 @@ test.cb('Abort a request (with callbacks)', t => {
267267
Connection: mock.getConnection()
268268
})
269269

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

285-
const p = client.info()
285+
const p = client.cat.indices()
286286
p.abort()
287287

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

303303
mock.add({
304304
method: 'GET',
305-
path: '/'
305+
path: '/_cat/indices'
306306
}, () => {
307307
return new errors.ResponseError({
308308
body: { errors: {}, status: 500 },
@@ -311,7 +311,7 @@ test('Return a response error', async t => {
311311
})
312312

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

329329
mock.add({
330330
method: 'GET',
331-
path: '/'
331+
path: '/_cat/indices'
332332
}, () => {
333333
return new errors.TimeoutError()
334334
})
335335

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

441441
mock.add({
442442
method: 'GET',
443-
path: '/'
443+
path: '/_cat/indices'
444444
}, () => {
445445
return { querystring: false }
446446
})
447447

448448
mock.add({
449449
method: 'GET',
450-
path: '/',
450+
path: '/_cat/indices',
451451
querystring: { pretty: 'true' }
452452
}, () => {
453453
return { querystring: true }
454454
})
455455

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

468468
mock.add({
469469
method: 'GET',
470-
path: '/',
470+
path: '/_cat/indices',
471471
querystring: { human: 'true' }
472472
}, () => {
473473
return { status: 'ok' }
474474
})
475475

476476
mock.add({
477477
method: 'GET',
478-
path: '/',
478+
path: '/_cat/indices',
479479
querystring: { pretty: 'true' }
480480
}, () => {
481481
return { status: 'ok' }
482482
})
483483

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

501501
mock.add({
502502
method: 'GET',
503-
path: '/'
503+
path: '/_cat/indices'
504504
}, () => {
505505
return 'ok'
506506
})
507507

508-
const response = await client.info()
508+
const response = await client.cat.indices()
509509
t.is(response.body, 'ok')
510510
t.is(response.statusCode, 200)
511511
t.is(response.headers['content-type'], 'text/plain;utf=8')
@@ -878,3 +878,25 @@ test('Should clear all mocks', async t => {
878878
t.is(err.statusCode, 404)
879879
}
880880
})
881+
882+
test('Override product check', async t => {
883+
const mock = new Mock()
884+
const client = new Client({
885+
node: 'http://localhost:9200',
886+
Connection: mock.getConnection()
887+
})
888+
889+
mock.add({
890+
method: 'GET',
891+
path: '/'
892+
}, () => {
893+
return { something: 'else' }
894+
})
895+
896+
try {
897+
await client.cat.nodes()
898+
t.fail('Should throw')
899+
} catch (err) {
900+
t.true(err instanceof errors.ProductNotSupportedError)
901+
}
902+
})

0 commit comments

Comments
 (0)