Skip to content

Ensure router will correctly match paths with URL-encoded characters #56

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 4 commits into from
Apr 18, 2025
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -104,3 +104,4 @@ dist
.tern-port

.DS_Store
package-lock.json
14 changes: 14 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ class Mocker {
if (typeof pattern.path !== 'string') throw new ConfigurationError('The path is not defined')
if (typeof fn !== 'function') throw new ConfigurationError('The resolver function is not defined')

// workaround since find-my-way no longer decodes URI escaped chars
// https://github.com/delvedor/find-my-way/pull/282
// https://github.com/delvedor/find-my-way/pull/286
if (pattern.path.indexOf('%') > -1) pattern.path = decodeURIComponent(pattern.path)

const handler = this[kRouter].find(pattern.method, pattern.path)
if (handler) {
handler.store.push({ ...pattern, fn })
Expand All @@ -64,10 +69,19 @@ class Mocker {
} else {
this[kRouter].on(pattern.method, pattern.path, noop, [{ ...pattern, fn }])
}

return this
}

get (params) {
if (typeof params.method !== 'string') throw new ConfigurationError('The method is not defined')
if (typeof params.path !== 'string') throw new ConfigurationError('The path is not defined')

// workaround since find-my-way no longer decodes URI escaped chars
// https://github.com/delvedor/find-my-way/pull/282
// https://github.com/delvedor/find-my-way/pull/286
if (params.path.indexOf('%') > -1) params.path = decodeURIComponent(params.path)

const handler = this[kRouter].find(params.method, params.path)
if (!handler) return null
for (const { body, querystring, fn } of handler.store) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
},
"homepage": "https://github.com/elastic/elasticsearch-js-mock#readme",
"devDependencies": {
"@elastic/elasticsearch": "8.17.1",
"@elastic/elasticsearch": "9.0.0",
"ava": "6.2.0",
"node-abort-controller": "3.1.1",
"nyc": "17.1.0",
Expand Down
89 changes: 89 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -880,3 +880,92 @@ test('Should clear all mocks', async t => {
t.is(err.statusCode, 404)
}
})

test('Path should match URL-encoded characters for e.g. comma in multi-index operations', async t => {
t.plan(1)

const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})

const spy = (_req, _res, _params, _store, _searchParams) => {
t.pass('Callback function was called')
return {}
}

mock.add(
{
method: 'DELETE',
path: '/some-type-index-123tobedeleted%2Csome-type-index-456tobedeleted'
},
spy
)

await client.indices.delete({
index: [
'some-type-index-123tobedeleted',
'some-type-index-456tobedeleted'
]
})
})

test('Path should match unencoded comma in path', async t => {
t.plan(1)

const mock = new Mock()
const client = new Client({
node: 'http://localhost:9200',
Connection: mock.getConnection()
})

const spy = (_req, _res, _params, _store, _searchParams) => {
t.pass('Callback function was called')
return {}
}

mock.add(
{
method: 'DELETE',
path: '/some-type-index-123tobedeleted,some-type-index-456tobedeleted'
},
spy
)

await client.indices.delete({
index: [
'some-type-index-123tobedeleted',
'some-type-index-456tobedeleted'
]
})
})

test('Validate types on get()', t => {
t.plan(4)

const mock = new Mock()
mock.add(
{
method: 'GET',
path: '/foo'
},
() => {}
)

try {
mock.get({ method: 'GET', path: null })
t.fail('should throw')
} catch (err) {
t.true(err instanceof errors.ConfigurationError)
t.is(err.message, 'The path is not defined')
}

try {
mock.get({ method: null, path: '/foo' })
t.fail('should throw')
} catch (err) {
t.true(err instanceof errors.ConfigurationError)
t.is(err.message, 'The method is not defined')
}
})