Skip to content

Commit b6f3e10

Browse files
test: increase unit coverage for CI
1 parent 4322768 commit b6f3e10

2 files changed

Lines changed: 73 additions & 0 deletions

File tree

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import chai from 'chai'
2+
import sinon from 'sinon'
3+
import sinonChai from 'sinon-chai'
4+
5+
import { getHealthRequestHandler } from '../../../../src/handlers/request-handlers/get-health-request-handler'
6+
7+
chai.use(sinonChai)
8+
const { expect } = chai
9+
10+
describe('getHealthRequestHandler', () => {
11+
it('responds with OK plain text and calls next', () => {
12+
const req = {} as any
13+
const res = {
14+
status: sinon.stub().returnsThis(),
15+
setHeader: sinon.stub().returnsThis(),
16+
send: sinon.stub().returnsThis(),
17+
} as any
18+
const next = sinon.stub()
19+
20+
getHealthRequestHandler(req, res, next)
21+
22+
expect(res.status).to.have.been.calledOnceWithExactly(200)
23+
expect(res.setHeader).to.have.been.calledOnceWithExactly('content-type', 'text/plain; charset=utf8')
24+
expect(res.send).to.have.been.calledOnceWithExactly('OK')
25+
expect(next).to.have.been.calledOnce
26+
})
27+
})

test/unit/handlers/subscribe-message-handler.spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ describe('SubscribeMessageHandler', () => {
236236
await expect(promise).to.eventually.be.rejectedWith(error)
237237
expect(closeSpy).to.have.been.called
238238
})
239+
240+
it('destroys event stream if aborted', async () => {
241+
const error = new Error('aborted')
242+
error.name = 'AbortError'
243+
isClientSubscribedToEventStub.returns(always(true))
244+
245+
const fetch = () => (handler as any).fetchAndSend(subscriptionId, filters)
246+
const destroySpy = sandbox.spy(stream, 'destroy')
247+
248+
const promise = fetch()
249+
250+
stream.emit('error', error)
251+
252+
await expect(promise).to.eventually.be.rejectedWith(error)
253+
expect(destroySpy).to.have.been.called
254+
})
239255
})
240256

241257
describe('.isClientSubscribedToEvent', () => {
@@ -350,5 +366,35 @@ describe('SubscribeMessageHandler', () => {
350366
'Too many filters: Number of filters per susbscription must be less then or equal to 1',
351367
)
352368
})
369+
370+
it('returns reason if subscription id is too long', () => {
371+
settingsFactory.returns({
372+
limits: {
373+
client: {
374+
subscription: {
375+
maxSubscriptionIdLength: 5,
376+
},
377+
},
378+
},
379+
})
380+
381+
expect((handler as any).canSubscribe('123456', filters)).to.equal(
382+
'Subscription ID too long: Subscription ID must be less or equal to 5',
383+
)
384+
})
385+
386+
it('returns undefined if subscription id matches max length', () => {
387+
settingsFactory.returns({
388+
limits: {
389+
client: {
390+
subscription: {
391+
maxSubscriptionIdLength: 6,
392+
},
393+
},
394+
},
395+
})
396+
397+
expect((handler as any).canSubscribe('123456', filters)).to.be.undefined
398+
})
353399
})
354400
})

0 commit comments

Comments
 (0)