Skip to content
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

fix: KeyMatch5 does not match the expected result #458

Merged
merged 2 commits into from
Sep 19, 2023
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
16 changes: 13 additions & 3 deletions src/util/builtinOperators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,21 @@ function keyMatch4Func(...args: any[]): boolean {
// For example, "/foo/bar?status=1&type=2" matches "/foo/bar"
function KeyMatch5(key1: string, key2: string): boolean {
const i: number = key1.indexOf('?');
if (i === -1) {
return key1 === key2;
if (i !== -1) {
key1 = key1.slice(0, i);
}

key2 = key2.replace(/\/\*/g, '/.*');

const regexp = new RegExp(/(.*){[^/]+}(.*)/g);
for (;;) {
if (!key2.includes('/{')) {
break;
}
key2 = key2.replace(regexp, '$1[^/]+$2');
}

return key1.slice(0, i) === key2;
return regexMatch(key1, '^' + key2 + '$');
}

// keyMatch5Func is the wrapper for KeyMatch5.
Expand Down
33 changes: 33 additions & 0 deletions test/util.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,39 @@ test('test keyMatch5Func', () => {
expect(util.keyMatch5Func('/parent/child/?status=1&type=2', '/parent/child/')).toEqual(true);
expect(util.keyMatch5Func('/parent/child/?status=1&type=2', '/parent/child')).toEqual(false);
expect(util.keyMatch5Func('/parent/child?status=1&type=2', '/parent/child/')).toEqual(false);

expect(util.keyMatch5Func('keyMatch5: expected 2 arguments, but got 1', '/foo')).toEqual(false);
expect(util.keyMatch5Func('keyMatch5: expected 2 arguments, but got 3', '/foo/create/123', '/foo/*', '/foo/update/123')).toEqual(false);
expect(util.keyMatch5Func('keyMatch5: argument must be a string', '/parent/123', true)).toEqual(false);

expect(util.keyMatch5Func('/foo', '/foo')).toEqual(true);
expect(util.keyMatch5Func('/foo', '/foo*')).toEqual(true);
expect(util.keyMatch5Func('/foo', '/foo/*')).toEqual(false);
expect(util.keyMatch5Func('/foo/bar', '/foo')).toEqual(false);
expect(util.keyMatch5Func('/foo/bar', '/foo*')).toEqual(false);
expect(util.keyMatch5Func('/foo/bar', '/foo/*')).toEqual(true);
expect(util.keyMatch5Func('/foobar', '/foo')).toEqual(false);
expect(util.keyMatch5Func('/foobar', '/foo*')).toEqual(false);
expect(util.keyMatch5Func('/foobar', '/foo/*')).toEqual(false);

expect(util.keyMatch5Func('/', '/{resource}')).toEqual(false);
expect(util.keyMatch5Func('/resource1', '/{resource}')).toEqual(true);
expect(util.keyMatch5Func('/myid', '/{id}/using/{resId}')).toEqual(false);
expect(util.keyMatch5Func('/myid/using/myresid', '/{id}/using/{resId}')).toEqual(true);

expect(util.keyMatch5Func('/proxy/myid', '/proxy/{id}/*')).toEqual(false);
expect(util.keyMatch5Func('/proxy/myid/', '/proxy/{id}/*')).toEqual(true);
expect(util.keyMatch5Func('/proxy/myid/res', '/proxy/{id}/*')).toEqual(true);
expect(util.keyMatch5Func('/proxy/myid/res/res2', '/proxy/{id}/*')).toEqual(true);
expect(util.keyMatch5Func('/proxy/myid/res/res2/res3', '/proxy/{id}/*')).toEqual(true);
expect(util.keyMatch5Func('/proxy/', '/proxy/{id}/*')).toEqual(false);

expect(util.keyMatch5Func('/proxy/myid?status=1&type=2', '/proxy/{id}/*')).toEqual(false);
expect(util.keyMatch5Func('/proxy/myid/', '/proxy/{id}/*')).toEqual(true);
expect(util.keyMatch5Func('/proxy/myid/res?status=1&type=2', '/proxy/{id}/*')).toEqual(true);
expect(util.keyMatch5Func('/proxy/myid/res/res2?status=1&type=2', '/proxy/{id}/*')).toEqual(true);
expect(util.keyMatch5Func('/proxy/myid/res/res2/res3?status=1&type=2', '/proxy/{id}/*')).toEqual(true);
expect(util.keyMatch5Func('/proxy/', '/proxy/{id}/*')).toEqual(false);
});

test('test ipMatchFunc', () => {
Expand Down
Loading