From 8e03828c3940d8fa61a6ba8eab7b1145063091f6 Mon Sep 17 00:00:00 2001 From: Steve Kinney Date: Tue, 20 Jul 2021 11:42:06 -0400 Subject: [PATCH 1/3] Add support for query parameters in path matching --- src/lib/utilities/path-matches.test.ts | 26 ++++++++++++++++++++++++++ src/lib/utilities/path-matches.ts | 10 ++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/src/lib/utilities/path-matches.test.ts b/src/lib/utilities/path-matches.test.ts index 564105692f..c9e10c166b 100644 --- a/src/lib/utilities/path-matches.test.ts +++ b/src/lib/utilities/path-matches.test.ts @@ -12,4 +12,30 @@ describe(pathMatches, () => { it('should return false if the second path is not a subdirectory of the first', () => { expect(pathMatches('/workflows', '/queries/abc')).toBe(false); }); + + it('should ignore query params if missing from the first path and the paths otherwise match', () => { + expect(pathMatches('/workflows', '/workflows?query=param')).toBe(true); + }); + + it('should ignore query params if missing from the second path ?query=param', () => { + expect(pathMatches('/workflows?query=param', '/workflows')).toBe(true); + }); + + it('should ignore query params if missing from the first path and the paths do not match', () => { + expect(pathMatches('/queries', '/workflows?query=param')).toBe(false); + }); + + it('should ignore query params if missing from the second path and the paths do not match', () => { + expect(pathMatches('/workflows?query=param', '/queries')).toBe(false); + }); + + it('should query params ignore do not match and the paths match', () => { + expect(pathMatches('/workflows?foo=bar', '/workflows?query=param')).toBe( + true, + ); + }); + + it('should query params ignore do not match and the paths do not match', () => { + expect(pathMatches('/workflows?query=param', '/query?foo=bar')).toBe(false); + }); }); diff --git a/src/lib/utilities/path-matches.ts b/src/lib/utilities/path-matches.ts index ec186a5268..854c31457d 100644 --- a/src/lib/utilities/path-matches.ts +++ b/src/lib/utilities/path-matches.ts @@ -1,6 +1,12 @@ +const dropQueryParameters = (url: string) => { + const queryParameterIndex = url.indexOf('?'); + if (queryParameterIndex > -1) return url.slice(0, queryParameterIndex); + return url; +}; + export const pathMatches = (first: string, second: string): boolean => { - const firstSegments = first.split('/'); - const secondSegments = second.split('/'); + const firstSegments = dropQueryParameters(first).split('/'); + const secondSegments = dropQueryParameters(second).split('/'); for (let index = 0; index < firstSegments.length; index++) { const firstSegment = firstSegments[index]; From 2ed6a4bc58590495f9cf07c085669ef4d1b973f4 Mon Sep 17 00:00:00 2001 From: Steve Kinney Date: Tue, 20 Jul 2021 11:53:54 -0400 Subject: [PATCH 2/3] Fix typos in test names --- src/lib/utilities/path-matches.test.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/lib/utilities/path-matches.test.ts b/src/lib/utilities/path-matches.test.ts index c9e10c166b..a1c660189f 100644 --- a/src/lib/utilities/path-matches.test.ts +++ b/src/lib/utilities/path-matches.test.ts @@ -17,7 +17,7 @@ describe(pathMatches, () => { expect(pathMatches('/workflows', '/workflows?query=param')).toBe(true); }); - it('should ignore query params if missing from the second path ?query=param', () => { + it('should ignore query params if missing from the second path and the paths otherwise match', () => { expect(pathMatches('/workflows?query=param', '/workflows')).toBe(true); }); @@ -29,13 +29,15 @@ describe(pathMatches, () => { expect(pathMatches('/workflows?query=param', '/queries')).toBe(false); }); - it('should query params ignore do not match and the paths match', () => { + it('should ignore query params if the paths match but the parameters do not', () => { expect(pathMatches('/workflows?foo=bar', '/workflows?query=param')).toBe( true, ); }); - it('should query params ignore do not match and the paths do not match', () => { - expect(pathMatches('/workflows?query=param', '/query?foo=bar')).toBe(false); + it('should ignore query params if both the paths and parameters do not match', () => { + expect(pathMatches('/workflows?query=param', '/workflows?foo=bar')).toBe( + false, + ); }); }); From b15e33f4538da64ae38c7f6dc2a879758d33e192 Mon Sep 17 00:00:00 2001 From: Steve Kinney Date: Tue, 20 Jul 2021 11:54:33 -0400 Subject: [PATCH 3/3] Change the last test back to what it's supposed to be --- src/lib/utilities/path-matches.test.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/lib/utilities/path-matches.test.ts b/src/lib/utilities/path-matches.test.ts index a1c660189f..ee9ed21320 100644 --- a/src/lib/utilities/path-matches.test.ts +++ b/src/lib/utilities/path-matches.test.ts @@ -36,7 +36,7 @@ describe(pathMatches, () => { }); it('should ignore query params if both the paths and parameters do not match', () => { - expect(pathMatches('/workflows?query=param', '/workflows?foo=bar')).toBe( + expect(pathMatches('/workflows?query=param', '/queries?foo=bar')).toBe( false, ); });