Skip to content

Commit 9a6a228

Browse files
authored
Merge pull request #6 from elastic/feat-2
Support for multiple methods and/or paths at the same time
2 parents 33a2860 + d8a8cf7 commit 9a6a228

File tree

5 files changed

+147
-3
lines changed

5 files changed

+147
-3
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,17 @@ mock.add({
6464
})
6565
```
6666

67+
You can also specify multiple methods and/or paths at the same time:
68+
```js
69+
// This mock will catch every search request against any index
70+
mock.add({
71+
method: ['GET', 'POST'],
72+
path: ['/_search', '/:index/_search']
73+
}, () => {
74+
return { status: 'ok' }
75+
})
76+
```
77+
6778
#### `get`
6879

6980
Returns the matching resolver function for the given pattern, it returns `null` if there is not a matching pattern.

index.d.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ declare class ClientMock {
1414
export declare type ResolverFn = (params: MockPattern) => Record<string, any> | string
1515

1616
export interface MockPattern {
17-
method: string
18-
path: string
17+
method: string | string[]
18+
path: string | string[]
1919
querystring?: Record<string, string>
20-
body?: Record<string, any>
20+
body?: Record<string, any> | Record<string, any>[]
2121
}
2222

2323
export default ClientMock

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ class Mocker {
2828
}
2929

3030
add (pattern, fn) {
31+
for (const key of ['method', 'path']) {
32+
if (Array.isArray(pattern[key])) {
33+
for (const value of pattern[key]) {
34+
this.add({ ...pattern, [key]: value }, fn)
35+
}
36+
return this
37+
}
38+
}
39+
3140
if (typeof pattern.method !== 'string') throw new ConfigurationError('The method is not defined')
3241
if (typeof pattern.path !== 'string') throw new ConfigurationError('The path is not defined')
3342
if (typeof fn !== 'function') throw new ConfigurationError('The resolver function is not defined')

index.test-d.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ mock.add({
2020
return { status: 'ok' }
2121
})
2222

23+
mock.add({
24+
method: ['GET', 'POST'],
25+
path: ['/_search', '/:index/_search']
26+
}, params => {
27+
expectType<MockPattern>(params)
28+
return { status: 'ok' }
29+
})
30+
2331
mock.add({
2432
method: 'GET',
2533
path: '/',
@@ -39,6 +47,15 @@ mock.add({
3947
return { status: 'ok' }
4048
})
4149

50+
mock.add({
51+
method: 'POST',
52+
path: '/_bulk',
53+
body: [{ foo: 'bar' }]
54+
}, params => {
55+
expectType<MockPattern>(params)
56+
return { status: 'ok' }
57+
})
58+
4259
mock.add({
4360
method: 'GET',
4461
path: '/'

test.js

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -557,3 +557,110 @@ test('.add should throw if method and path are not defined', async t => {
557557
t.is(err.message, 'The resolver function is not defined')
558558
}
559559
})
560+
561+
test('Define multiple methods at once', async t => {
562+
const mock = new Mock()
563+
const client = new Client({
564+
node: 'http://localhost:9200',
565+
Connection: mock.getConnection()
566+
})
567+
568+
mock.add({
569+
method: ['GET', 'POST'],
570+
path: '/:index/_search'
571+
}, () => {
572+
return { status: 'ok' }
573+
})
574+
575+
let response = await client.search({
576+
index: 'test',
577+
q: 'foo:bar'
578+
})
579+
t.deepEqual(response.body, { status: 'ok' })
580+
t.is(response.statusCode, 200)
581+
582+
response = await client.search({
583+
index: 'test',
584+
body: {
585+
query: { match: { foo: 'bar' } }
586+
}
587+
})
588+
t.deepEqual(response.body, { status: 'ok' })
589+
t.is(response.statusCode, 200)
590+
})
591+
592+
test('Define multiple paths at once', async t => {
593+
const mock = new Mock()
594+
const client = new Client({
595+
node: 'http://localhost:9200',
596+
Connection: mock.getConnection()
597+
})
598+
599+
mock.add({
600+
method: 'GET',
601+
path: ['/test1/_search', '/test2/_search']
602+
}, () => {
603+
return { status: 'ok' }
604+
})
605+
606+
let response = await client.search({
607+
index: 'test1',
608+
q: 'foo:bar'
609+
})
610+
t.deepEqual(response.body, { status: 'ok' })
611+
t.is(response.statusCode, 200)
612+
613+
response = await client.search({
614+
index: 'test2',
615+
q: 'foo:bar'
616+
})
617+
t.deepEqual(response.body, { status: 'ok' })
618+
t.is(response.statusCode, 200)
619+
})
620+
621+
test('Define multiple paths and method at once', async t => {
622+
const mock = new Mock()
623+
const client = new Client({
624+
node: 'http://localhost:9200',
625+
Connection: mock.getConnection()
626+
})
627+
628+
mock.add({
629+
method: ['GET', 'POST'],
630+
path: ['/test1/_search', '/test2/_search']
631+
}, () => {
632+
return { status: 'ok' }
633+
})
634+
635+
let response = await client.search({
636+
index: 'test1',
637+
q: 'foo:bar'
638+
})
639+
t.deepEqual(response.body, { status: 'ok' })
640+
t.is(response.statusCode, 200)
641+
642+
response = await client.search({
643+
index: 'test2',
644+
q: 'foo:bar'
645+
})
646+
t.deepEqual(response.body, { status: 'ok' })
647+
t.is(response.statusCode, 200)
648+
649+
response = await client.search({
650+
index: 'test1',
651+
body: {
652+
query: { match: { foo: 'bar' } }
653+
}
654+
})
655+
t.deepEqual(response.body, { status: 'ok' })
656+
t.is(response.statusCode, 200)
657+
658+
response = await client.search({
659+
index: 'test2',
660+
body: {
661+
query: { match: { foo: 'bar' } }
662+
}
663+
})
664+
t.deepEqual(response.body, { status: 'ok' })
665+
t.is(response.statusCode, 200)
666+
})

0 commit comments

Comments
 (0)