Skip to content

Commit

Permalink
fix: KeyMatch5 does not match the expected result (#458)
Browse files Browse the repository at this point in the history
* fix: KeyMatch5 does not match the expected result

* fix: KeyMatch5 does not match the expected result:remove spaces
  • Loading branch information
aundertaker authored Sep 19, 2023
1 parent a09493c commit 0df458d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
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

0 comments on commit 0df458d

Please sign in to comment.