LimitedStreamTests.js 943 B

123456789101112131415161718192021222324252627282930
  1. const { expect } = require('chai')
  2. const { LimitedStream, SizeExceededError } = require('../../index')
  3. describe('LimitedStream', function () {
  4. it('should emit an error if the stream size exceeds the limit', function (done) {
  5. const maxSize = 10
  6. const limitedStream = new LimitedStream(maxSize)
  7. limitedStream.on('error', err => {
  8. expect(err).to.be.an.instanceOf(SizeExceededError)
  9. done()
  10. })
  11. limitedStream.write(Buffer.alloc(maxSize + 1))
  12. })
  13. it('should pass through data if the stream size does not exceed the limit', function (done) {
  14. const maxSize = 15
  15. const limitedStream = new LimitedStream(maxSize)
  16. let data = ''
  17. limitedStream.on('data', chunk => {
  18. data += chunk.toString()
  19. })
  20. limitedStream.on('end', () => {
  21. expect(data).to.equal('hello world')
  22. done()
  23. })
  24. limitedStream.write('hello')
  25. limitedStream.write(' world')
  26. limitedStream.end()
  27. })
  28. })