Skip to content
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
12 changes: 8 additions & 4 deletions packages/rabbitmq/src/amqp/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ export function matchesRoutingKey(
routingKey: string,
pattern: string[] | string | undefined
): boolean {
if (!pattern) return false;
// An empty string is a valid pattern therefore
// we should only exclude null values and empty array
if (pattern === undefined || (Array.isArray(pattern) && pattern.length === 0))
return false;

const patterns = Array.isArray(pattern) ? pattern : [pattern];
for (const pattern of patterns) {
if (routingKey === pattern) return true;
for (const p of patterns) {
if (routingKey === p) return true;
const splitKey = routingKey.split('.');
const splitPattern = pattern.split('.');
const splitPattern = p.split('.');
let starFailed = false;
for (let i = 0; i < splitPattern.length; i++) {
if (splitPattern[i] === '#') return true;
Expand Down
106 changes: 55 additions & 51 deletions packages/rabbitmq/src/tests/rabbitmq.utils.spec.ts
Original file line number Diff line number Diff line change
@@ -1,57 +1,61 @@
import { matchesRoutingKey } from '../amqp/utils';

describe('matchesRoutingKey', () => {
describe(matchesRoutingKey.name, () => {
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);
it.each([
// [description, routingKey, pattern, expectedResult]
[
'should return true when routing key matches star pattern',
'user.created.new',
'user.*.new',
true,
],
[
'should return true when routing key matches hash pattern',
'user.updated.new',
'user.#',
true,
],
[
'should return false when routing key does not match pattern',
'user.updated',
userCreated,
false,
],
[
'should return true when routing key matches any star pattern in the array',
userCreated,
['event.*', 'user.*'],
true,
],
[
'should return true when routing key matches precise pattern in the array with wildcards',
userCreated,
['user.*.new', userCreated, 'event.#'],
true,
],
[
'should return true when routing key matches any hash pattern in the array',
'user.created.new',
['event.#', 'user.#'],
true,
],
['should return true with pattern as an empty string', '', '', true], //both are equal (empty strings) which is valid for an exact match
[
'should return false when routing key does not match any pattern in the array',
'user.updated',
[userCreated, 'event.created'],
false,
],
[
'should return false when pattern is undefined',
userCreated,
undefined,
false,
],
])('%s', (_, routingKey, pattern, expectedResult) => {
const result = matchesRoutingKey(routingKey, pattern);
expect(result).toBe(expectedResult);
});
});
Loading