/
04-routing.test.ts
68 lines (60 loc) · 1.84 KB
/
04-routing.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// tslint:disable:no-console
import { createConsumer, createProducer } from '../src';
import getMockConnector from './helpers/getMockConnector';
it('should be able to handle routing', done => {
const { createAmqpConnector, channel: engine } = getMockConnector({
onPublish: ({ exchange, routingKey, content, onMessage }) => {
// Simulate rabbit behaviour
if (routingKey === 'error') {
onMessage({
content,
fields: { exchange, routingKey },
properties: {}
});
}
}
});
const { sender, receiver } = createAmqpConnector({
declarations: {
exchanges: [
{
durable: false,
name: 'direct_logs',
type: 'direct'
}
]
},
uri: 'amqp://somerabbitserver'
});
const producer = createProducer(sender());
const consumer = createConsumer(
receiver({
bindings: ['error'].map(label => ({
pattern: label,
source: 'direct_logs'
})),
noAck: true
})
);
consumer.subscribe(msg => {
expect(engine.jestSpyCalls.mock.calls).toEqual([
['assertExchange', 'direct_logs', 'direct', { durable: false }],
['assertExchange', 'direct_logs', 'direct', { durable: false }],
['assertQueue', '', { exclusive: true }],
['bindQueue', 'server-queue', 'direct_logs', 'error', undefined],
['consume', 'server-queue', '_FUNCTION_', { noAck: true }],
['publish', 'direct_logs', 'warn', Buffer.from('"Hi I am a warning"')],
['publish', 'direct_logs', 'error', Buffer.from('"Hi I am an error"')]
]);
expect(msg.body).toEqual('Hi I am an error');
done();
});
producer.next({
body: 'Hi I am a warning',
to: { exchange: 'direct_logs', key: 'warn' }
});
producer.next({
body: 'Hi I am an error',
to: { exchange: 'direct_logs', key: 'error' }
});
});