@elastic/elasticsearch-mock
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -18,3 +18,3 @@ // Licensed to Elasticsearch B.V under one or more agreements. | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/health' | ||
}, () => { | ||
@@ -68,3 +68,3 @@ return { status: 'ok' } | ||
client.info(console.log) | ||
client.cat.health(console.log) | ||
@@ -71,0 +71,0 @@ client.ping(console.log) |
21
index.js
@@ -109,6 +109,7 @@ // Licensed to Elasticsearch B.V under one or more agreements. | ||
request (params, callback) { | ||
var aborted = false | ||
let aborted = false | ||
normalizeParams(params, prepareResponse) | ||
function prepareResponse (error, params) { | ||
/* istanbul ignore next */ | ||
if (aborted) { | ||
@@ -122,11 +123,17 @@ return callback(new RequestAbortedError(), 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 { | ||
@@ -152,2 +159,3 @@ payload = resolver(params) | ||
connection: 'keep-alive', | ||
'x-elastic-product': 'Elasticsearch', | ||
'content-length': Buffer.byteLength( | ||
@@ -162,2 +170,3 @@ typeof payload === 'string' ? payload : JSON.stringify(payload) | ||
return { | ||
/* istanbul ignore next */ | ||
abort () { | ||
@@ -164,0 +173,0 @@ aborted = true |
{ | ||
"name": "@elastic/elasticsearch-mock", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Mock utility for the Elasticsearch's Node.js client", | ||
@@ -37,13 +37,13 @@ "main": "index.js", | ||
"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" | ||
} | ||
} |
@@ -34,3 +34,3 @@ <img align="right" width="auto" height="auto" src="https://www.elastic.co/static-res/images/elastic-logo-200.png"> | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/health' | ||
}, () => { | ||
@@ -40,3 +40,3 @@ return { status: 'ok' } | ||
client.info(console.log) | ||
client.cat.health(console.log) | ||
``` | ||
@@ -59,7 +59,7 @@ | ||
```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' | ||
}, () => { | ||
@@ -88,3 +88,3 @@ return { status: 'ok' } | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/health' | ||
}) | ||
@@ -143,3 +143,3 @@ ``` | ||
method: 'GET', | ||
path: '/', | ||
path: '/_cat/health' | ||
querystring: { pretty: 'true' } | ||
@@ -150,4 +150,4 @@ }, () => { | ||
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' } | ||
``` | ||
@@ -236,3 +236,3 @@ | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/health' | ||
}, () => { | ||
@@ -239,0 +239,0 @@ return new errors.ResponseError({ |
56
test.js
@@ -21,3 +21,3 @@ // Licensed to Elasticsearch B.V under one or more agreements. | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/indices' | ||
}, () => { | ||
@@ -27,3 +27,3 @@ return { status: 'ok' } | ||
const response = await client.info() | ||
const response = await client.cat.indices() | ||
t.deepEqual(response.body, { status: 'ok' }) | ||
@@ -119,3 +119,3 @@ t.is(response.statusCode, 200) | ||
try { | ||
await client.info() | ||
await client.cat.indices() | ||
t.fail('Should throw') | ||
@@ -273,3 +273,3 @@ } catch (err) { | ||
const r = client.info((err, result) => { | ||
const r = client.cat.indices((err, result) => { | ||
t.true(err instanceof errors.RequestAbortedError) | ||
@@ -289,3 +289,3 @@ t.end() | ||
const p = client.info() | ||
const p = client.cat.indices() | ||
p.abort() | ||
@@ -310,3 +310,3 @@ | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/indices' | ||
}, () => { | ||
@@ -320,3 +320,3 @@ return new errors.ResponseError({ | ||
try { | ||
await client.info() | ||
await client.cat.indices() | ||
t.fail('Should throw') | ||
@@ -338,3 +338,3 @@ } catch (err) { | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/indices' | ||
}, () => { | ||
@@ -345,3 +345,3 @@ return new errors.TimeoutError() | ||
try { | ||
await client.info() | ||
await client.cat.indices() | ||
t.fail('Should throw') | ||
@@ -452,3 +452,3 @@ } catch (err) { | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/indices' | ||
}, () => { | ||
@@ -460,3 +460,3 @@ return { querystring: false } | ||
method: 'GET', | ||
path: '/', | ||
path: '/_cat/indices', | ||
querystring: { pretty: 'true' } | ||
@@ -467,3 +467,3 @@ }, () => { | ||
const response = await client.info({ pretty: true }) | ||
const response = await client.cat.indices({ pretty: true }) | ||
t.deepEqual(response.body, { querystring: true }) | ||
@@ -482,3 +482,3 @@ t.is(response.statusCode, 200) | ||
method: 'GET', | ||
path: '/', | ||
path: '/_cat/indices', | ||
querystring: { human: 'true' } | ||
@@ -491,3 +491,3 @@ }, () => { | ||
method: 'GET', | ||
path: '/', | ||
path: '/_cat/indices', | ||
querystring: { pretty: 'true' } | ||
@@ -499,3 +499,3 @@ }, () => { | ||
try { | ||
await client.info() | ||
await client.cat.indices() | ||
t.fail('Should throw') | ||
@@ -518,3 +518,3 @@ } catch (err) { | ||
method: 'GET', | ||
path: '/' | ||
path: '/_cat/indices' | ||
}, () => { | ||
@@ -524,3 +524,3 @@ return 'ok' | ||
const response = await client.info() | ||
const response = await client.cat.indices() | ||
t.is(response.body, 'ok') | ||
@@ -898,1 +898,23 @@ 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
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
48999
1157
+ Addedfind-my-way@4.5.1(transitive)
+ Addedinto-stream@6.0.0(transitive)
- Removedfind-my-way@2.2.5(transitive)
- Removedinto-stream@5.1.1(transitive)
Updatedfast-deep-equal@^3.1.3
Updatedfind-my-way@^4.3.3
Updatedinto-stream@^6.0.0