-
-
Notifications
You must be signed in to change notification settings - Fork 257
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rabbitmq): a fix for routing key check. The check should respect …
…wildcards (#713) * fix(rabbitmq): a fix for routing key check. The check should respect wildcards fix #712 * test(rabbitmq): added tests for matchesRoutingKey and logic improved re #712 --------- Co-authored-by: andriusm <andriusm@softpauer.com>
- Loading branch information
1 parent
bc86da9
commit 450d420
Showing
6 changed files
with
186 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
export function matchesRoutingKey( | ||
routingKey: string, | ||
pattern: string[] | string | undefined | ||
): boolean { | ||
if (!pattern) return false; | ||
const patterns = Array.isArray(pattern) ? pattern : [pattern]; | ||
for (const pattern of patterns) { | ||
if (routingKey === pattern) return true; | ||
const splitKey = routingKey.split('.'); | ||
const splitPattern = pattern.split('.'); | ||
let starFailed = false; | ||
for (let i = 0; i < splitPattern.length; i++) { | ||
if (splitPattern[i] === '#') return true; | ||
|
||
if (splitPattern[i] !== '*' && splitPattern[i] !== splitKey[i]) { | ||
starFailed = true; | ||
break; | ||
} | ||
} | ||
|
||
if (!starFailed && splitKey.length === splitPattern.length) return true; | ||
} | ||
|
||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import { matchesRoutingKey } from '../amqp/utils'; | ||
|
||
describe('matchesRoutingKey', () => { | ||
const userCreated = 'user.created'; | ||
it('should return true when routing key matches star pattern', () => { | ||
const routingKey = 'user.created.new'; | ||
const pattern = 'user.*.new'; | ||
const result = matchesRoutingKey(routingKey, pattern); | ||
expect(result).toBe(true); | ||
}); | ||
it('should return true when routing key matches hash pattern', () => { | ||
const routingKey = 'user.updated.new'; | ||
const pattern = 'user.#'; | ||
const result = matchesRoutingKey(routingKey, pattern); | ||
expect(result).toBe(true); | ||
}); | ||
it('should return false when routing key does not match pattern', () => { | ||
const routingKey = 'user.updated'; | ||
const pattern = userCreated; | ||
const result = matchesRoutingKey(routingKey, pattern); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return true when routing key matches any star pattern in the array', () => { | ||
const routingKey = userCreated; | ||
const pattern = ['event.*', 'user.*']; | ||
const result = matchesRoutingKey(routingKey, pattern); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true when routing key matches precise pattern in the array with wildcards', () => { | ||
const routingKey = userCreated; | ||
const pattern = ['user.*.new', userCreated, 'event.#']; | ||
const result = matchesRoutingKey(routingKey, pattern); | ||
expect(result).toBe(true); | ||
}); | ||
|
||
it('should return true when routing key matches any hash pattern in the array', () => { | ||
const routingKey = 'user.created.new'; | ||
const pattern = ['event.#', 'user.#']; | ||
const result = matchesRoutingKey(routingKey, pattern); | ||
expect(result).toBe(true); | ||
}); | ||
it('should return false when routing key does not match any pattern in the array', () => { | ||
const routingKey = 'user.updated'; | ||
const pattern = [userCreated, 'event.created']; | ||
const result = matchesRoutingKey(routingKey, pattern); | ||
expect(result).toBe(false); | ||
}); | ||
|
||
it('should return false when pattern is undefined', () => { | ||
const routingKey = userCreated; | ||
const pattern = undefined; | ||
const result = matchesRoutingKey(routingKey, pattern); | ||
expect(result).toBe(false); | ||
}); | ||
}); |