|
| 1 | +"use strict"; |
| 2 | + |
| 3 | +describe('sqs.processor', () => { |
| 4 | + |
| 5 | + const sqs = require('../lib/sqs'); |
| 6 | + |
| 7 | + it('context should be passed through', () => { |
| 8 | + const actionSpy = jasmine.createSpy('action'); |
| 9 | + |
| 10 | + const context = {bla: "blup"}; |
| 11 | + const sqsCfg = {routes: [{source: /.*/, action: actionSpy}]}; |
| 12 | + const event = {Records: [{eventSource: 'aws:sqs', body: 'B'}]}; |
| 13 | + |
| 14 | + sqs(sqsCfg, event, context); |
| 15 | + |
| 16 | + expect(actionSpy).toHaveBeenCalledWith([event.Records[0].body], context); |
| 17 | + }); |
| 18 | + |
| 19 | + it('should ignore event if it is no SQS event', () => { |
| 20 | + const sqsCfg = {routes: [{source: /.*/, action: () => 1}]}; |
| 21 | + expect(sqs(sqsCfg, {})).toBe(null); |
| 22 | + expect(sqs(sqsCfg, {Records: 1})).toBe(null); |
| 23 | + expect(sqs(sqsCfg, {Records: []})).toBe(null); |
| 24 | + }); |
| 25 | + |
| 26 | + it('should match null source for ".*"', () => { |
| 27 | + const sqsCfg = {routes: [{source: /.*/, action: () => 1}]}; |
| 28 | + expect(sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: null}]})).toBe(1); |
| 29 | + }); |
| 30 | + |
| 31 | + it('should match empty subject for ".*"', () => { |
| 32 | + const sqsCfg = {routes: [{subject: /.*/, action: () => 1}]}; |
| 33 | + expect(sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', body: 'B'}]})).toBe(1); |
| 34 | + }); |
| 35 | + |
| 36 | + it('should match source for "/porter/"', () => { |
| 37 | + const sqsCfg = {routes: [{source: /porter/, action: () => 1}]}; |
| 38 | + expect(sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'importer'}]})).toBe(1); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should call action with sqs-message', () => { |
| 42 | + const sqsCfg = {routes: [{source: /porter/, action: (events) => events}]}; |
| 43 | + const event = {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'importer', body: 'B'}]}; |
| 44 | + |
| 45 | + expect(sqs(sqsCfg, event)).toEqual([event.Records[0].body]); |
| 46 | + }); |
| 47 | + |
| 48 | + it('should call first action with matching subject', () => { |
| 49 | + const sqsCfg = { |
| 50 | + routes: [ |
| 51 | + {source: /^123$/, action: () => 1}, |
| 52 | + {source: /123/, action: () => 2}, |
| 53 | + {source: /1234/, action: () => 3} |
| 54 | + ] |
| 55 | + }; |
| 56 | + const event = {Records: [{eventSource: 'aws:sqs', eventSourceARN: '1234', body: 'B'}]}; |
| 57 | + expect(sqs(sqsCfg, event)).toBe(2); |
| 58 | + }); |
| 59 | + |
| 60 | + it('should match complete source', () => { |
| 61 | + const sqsCfg = {routes: [{source: 'aws:123:importer', action: () => 1}]}; |
| 62 | + expect(sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'aws:123:importer'}]})).toBe(1); |
| 63 | + }); |
| 64 | + |
| 65 | + it('should not throw error on missing source', () => { |
| 66 | + const sqsCfg = {routes: [{action: () => 1}]}; |
| 67 | + sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'importer'}]}); |
| 68 | + }); |
| 69 | + |
| 70 | + it('should fail on missing action', () => { |
| 71 | + const sqsCfg = {routes: [{source: /.*/}]}; |
| 72 | + try { |
| 73 | + sqs(sqsCfg, {Records: [{eventSource: 'aws:sqs', eventSourceARN: 'importer'}]}); |
| 74 | + fail(); |
| 75 | + } catch (e) { |
| 76 | + } |
| 77 | + }); |
| 78 | + |
| 79 | +}); |
0 commit comments