| 1 | /*global describe, it*/ |
| 2 | var should = require('should'); |
| 3 | var base64encode = require('../lib/encode.js'); |
| 4 | |
| 5 | describe('Base64Encode', function () { |
| 6 | /** |
| 7 | * This function emits an array of string chunks to the stream, and then compares the output to a given value. |
| 8 | * @param stream |
| 9 | * @param inputs |
| 10 | * @param output |
| 11 | */ |
| 12 | function testStream(stream, inputs, output) { |
| 13 | for ( var i = 0; i < inputs.length; i++ ) |
| 14 | stream.write(inputs[i]); |
| 15 | |
| 16 | stream.end(); |
| 17 | |
| 18 | var result = stream.read(); |
| 19 | result.should.be.an.instanceOf(Buffer); |
| 20 | result.toString().should.equal(output); |
| 21 | } |
| 22 | |
| 23 | describe('constructor', function() { |
| 24 | it('should return an instance of Base64Encode whether or not using "new"', function() { |
| 25 | var stream1 = new base64encode(); |
| 26 | stream1.should.be.an.instanceOf(base64encode); |
| 27 | |
| 28 | var stream2 = base64encode(); |
| 29 | stream2.should.be.an.instanceOf(base64encode); |
| 30 | }); |
| 31 | }); |
| 32 | |
| 33 | describe('input in a single chunk', function () { |
| 34 | it('should properly encode a string', function () { |
| 35 | testStream(base64encode(), ['any carnal pleasur'], 'YW55IGNhcm5hbCBwbGVhc3Vy'); |
| 36 | }); |
| 37 | |
| 38 | it('should properly encode a Buffer', function () { |
| 39 | testStream(base64encode(), [new Buffer('any carnal pleasur')], 'YW55IGNhcm5hbCBwbGVhc3Vy'); |
| 40 | }); |
| 41 | |
| 42 | it('should properly encode a Buffer and include padding', function () { |
| 43 | testStream(base64encode(), ['any carnal pleasure.'], 'YW55IGNhcm5hbCBwbGVhc3VyZS4='); |
| 44 | }); |
| 45 | }); |
| 46 | |
| 47 | describe('input in multiple chunks, lengths divisible by 3', function () { |
| 48 | it('should properly encode a Buffer', function () { |
| 49 | testStream(base64encode(), [ |
| 50 | new Buffer('any ca'), new Buffer('rnal p'), new Buffer('leasur') |
| 51 | ], 'YW55IGNhcm5hbCBwbGVhc3Vy'); |
| 52 | }); |
| 53 | |
| 54 | it('should properly encode a Buffer and include padding', function () { |
| 55 | testStream(base64encode(), [ |
| 56 | new Buffer('any ca'), new Buffer('rnal p'), new Buffer('leasure.') |
| 57 | ], 'YW55IGNhcm5hbCBwbGVhc3VyZS4='); |
| 58 | }); |
| 59 | }); |
| 60 | |
| 61 | describe('input in multiple chunks, lengths not divisible by 3', function () { |
| 62 | it('should properly encode a Buffer', function () { |
| 63 | testStream(base64encode(), [ |
| 64 | new Buffer('any carn'), new Buffer('al pl'), new Buffer('easur') |
| 65 | ], 'YW55IGNhcm5hbCBwbGVhc3Vy'); |
| 66 | }); |
| 67 | |
| 68 | it('should properly encode a Buffer and include padding', function () { |
| 69 | testStream(base64encode(), [ |
| 70 | new Buffer('any carn'), new Buffer('al pl'), new Buffer('easure.') |
| 71 | ], 'YW55IGNhcm5hbCBwbGVhc3VyZS4='); |
| 72 | }); |
| 73 | }); |
| 74 | |
| 75 | describe('input in multiple chunks, line length specified', function () { |
| 76 | it('should properly encode a Buffer', function () { |
| 77 | testStream(base64encode(5), [ |
| 78 | new Buffer('any carn'), new Buffer('al pl'), new Buffer('easur') |
| 79 | ], 'YW55I\r\nGNhcm\r\n5hbCB\r\nwbGVh\r\nc3Vy'); |
| 80 | }); |
| 81 | |
| 82 | it('should properly encode a Buffer and include padding', function () { |
| 83 | testStream(base64encode(5), [ |
| 84 | new Buffer('any carn'), new Buffer('al pl'), new Buffer('easure.') |
| 85 | ], 'YW55I\r\nGNhcm\r\n5hbCB\r\nwbGVh\r\nc3VyZ\r\nS4='); |
| 86 | }); |
| 87 | }); |
| 88 | }); |