1 | const expect = require('chai').expect
|
2 | const nock = require('nock')
|
3 |
|
4 | describe('middleware', () => {
|
5 | const Copacetic = require('../')
|
6 | const Middleware = require('../').Middleware
|
7 |
|
8 | const nockIt = () => {
|
9 | nock('http://one-example.com')
|
10 | .get('/')
|
11 | .reply(200)
|
12 | nock('http://two-example.com')
|
13 | .get('/')
|
14 | .reply(400)
|
15 | }
|
16 |
|
17 | const res = { json: stuff => stuff, sendStatus: stuff => stuff }
|
18 |
|
19 | describe('when there is an interval', () => {
|
20 | it('it should poll a set of dependencies', () => {
|
21 | nockIt()
|
22 |
|
23 | const copacetic = Copacetic('test-service')
|
24 | .registerDependency({
|
25 | name: 'test-1',
|
26 | url: 'http://one-example.com'
|
27 | })
|
28 | .registerDependency({
|
29 | name: 'test-2',
|
30 | url: 'http://two-example.com'
|
31 | })
|
32 |
|
33 | const middleware = Middleware({
|
34 | copacetic,
|
35 | interval: '0.1 seconds',
|
36 | dependencies: [{ name: 'test-1' }]
|
37 | })
|
38 |
|
39 | copacetic.on('health', (info) => {
|
40 | expect(middleware(null, res)).to.deep.equal({
|
41 | name: 'test-service',
|
42 | healthy: true,
|
43 | dependencies: [
|
44 | {
|
45 | name: 'test-1',
|
46 | healthy: true,
|
47 | level: 'SOFT',
|
48 | lastChecked: info[0].lastChecked
|
49 | },
|
50 | {
|
51 | name: 'test-2',
|
52 | healthy: true,
|
53 | level: 'SOFT',
|
54 | lastChecked: undefined
|
55 | }
|
56 | ]
|
57 | })
|
58 | })
|
59 |
|
60 | copacetic.stop()
|
61 | })
|
62 |
|
63 | it('it should poll all dependencies', () => {
|
64 | nockIt()
|
65 |
|
66 | const copacetic = Copacetic('test-service')
|
67 | .registerDependency({
|
68 | name: 'test-1',
|
69 | url: 'http://one-example.com'
|
70 | })
|
71 | .registerDependency({
|
72 | name: 'test-2',
|
73 | url: 'http://two-example.com'
|
74 | })
|
75 |
|
76 | const middleware = Middleware({
|
77 | copacetic,
|
78 | interval: '0.1 seconds'
|
79 | })
|
80 |
|
81 | copacetic.on('health', (info) => {
|
82 | expect(middleware(null, res)).to.deep.equal({
|
83 | name: 'test-service',
|
84 | healthy: true,
|
85 | dependencies: [
|
86 | {
|
87 | name: 'test-1',
|
88 | healthy: true,
|
89 | level: 'SOFT',
|
90 | lastChecked: info[0].lastChecked
|
91 | },
|
92 | {
|
93 | name: 'test-2',
|
94 | healthy: false,
|
95 | level: 'SOFT',
|
96 | lastChecked: info[1].lastChecked
|
97 | }
|
98 | ]
|
99 | })
|
100 | })
|
101 |
|
102 | copacetic.stop()
|
103 | })
|
104 |
|
105 | it('should return a function', () => {
|
106 | expect(Middleware({ copacetic: Copacetic() })).to.be.a('function')
|
107 | })
|
108 | })
|
109 |
|
110 | describe('when there is no interval', () => {
|
111 | const copacetic = Copacetic('test-service')
|
112 | .registerDependency({
|
113 | name: 'test-1',
|
114 | url: 'http://one-example.com'
|
115 | })
|
116 | .registerDependency({
|
117 | name: 'test-2',
|
118 | url: 'http://two-example.com'
|
119 | })
|
120 |
|
121 | it('should return a function', () => {
|
122 | expect(Middleware({ copacetic })).to.be.a('function')
|
123 |
|
124 | expect(Middleware({ copacetic })(null, res)).to.deep.equal({
|
125 | name: 'test-service',
|
126 | healthy: true,
|
127 | dependencies: [
|
128 | {
|
129 | name: 'test-1',
|
130 | healthy: true,
|
131 | level: 'SOFT',
|
132 | lastChecked: undefined
|
133 | },
|
134 | {
|
135 | name: 'test-2',
|
136 | healthy: true,
|
137 | level: 'SOFT',
|
138 | lastChecked: undefined
|
139 | }
|
140 | ]
|
141 | })
|
142 | })
|
143 | })
|
144 |
|
145 | it('should return a status code', () => {
|
146 | const copacetic = Copacetic('test-service')
|
147 | .registerDependency({
|
148 | name: 'test-1',
|
149 | url: 'http://one-example.com'
|
150 | })
|
151 | .registerDependency({
|
152 | name: 'test-2',
|
153 | url: 'http://two-example.com'
|
154 | })
|
155 |
|
156 | expect(Middleware({ copacetic, verbose: false })(null, res)).to.deep.equal(200)
|
157 | })
|
158 | })
|