| 1 | /*global describe, it*/ |
| 2 | var should = require('should'); |
| 3 | var base64decode = require('../lib/decode.js'); |
| 4 | |
| 5 | describe('Base64Decode', 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 Base64Decode whether or not using "new"', function() { |
| 25 | var stream1 = new base64decode(); |
| 26 | stream1.should.be.an.instanceOf(base64decode); |
| 27 | |
| 28 | var stream2 = base64decode(); |
| 29 | stream2.should.be.an.instanceOf(base64decode); |
| 30 | }); |
| 31 | }); |
| 32 | |
| 33 | describe('input in a single chunk', function () { |
| 34 | it('should properly decode a Buffer', function () { |
| 35 | testStream(base64decode(), [new Buffer('YW55IGNhcm5hbCBwbGVhc3VyZS4=')], 'any carnal pleasure.'); |
| 36 | }); |
| 37 | |
| 38 | it('should properly decode a string', function () { |
| 39 | testStream(base64decode(), ['YW55IGNhcm5hbCBwbGVhc3VyZS4='], 'any carnal pleasure.'); |
| 40 | }); |
| 41 | |
| 42 | it('should properly decode string containing newlines', function () { |
| 43 | testStream(base64decode(), ['YW55I\nGNhcm\n5hbCB\nwbGVh\nc3VyZ\nS4='], 'any carnal pleasure.'); |
| 44 | }); |
| 45 | |
| 46 | it('should properly decode a string without padding', function () { |
| 47 | testStream(base64decode(), ['YW55IGNhcm5hbCBwbGVhc3VyZS4'], 'any carnal pleasure.'); |
| 48 | }); |
| 49 | }); |
| 50 | |
| 51 | describe('input in multiple chunks, lengths divisible by 4', function () { |
| 52 | it('should properly decode a string', function () { |
| 53 | testStream(base64decode(), ['YW55IGNhcm5h', 'bCBwbGVhc3Vy', 'ZS4='], 'any carnal pleasure.'); |
| 54 | }); |
| 55 | |
| 56 | it('should properly decode string containing newlines', function () { |
| 57 | testStream(base64decode(), ['YW55IGNhcm5h\n', 'bCBwbGVhc3Vy\n', 'ZS4=\n'], 'any carnal pleasure.'); |
| 58 | }); |
| 59 | |
| 60 | it('should properly decode a string without padding', function () { |
| 61 | testStream(base64decode(), ['YW55IGNhcm5h', 'bCBwbGVhc3Vy', 'ZS4'], 'any carnal pleasure.'); |
| 62 | }); |
| 63 | }); |
| 64 | |
| 65 | describe('input in multiple chunks, lengths not divisible by 4', function () { |
| 66 | it('should properly decode a string', function () { |
| 67 | testStream(base64decode(), ['YW55I', 'GNhcm5h', 'bCBwbGVhc3VyZ', 'S4='], 'any carnal pleasure.'); |
| 68 | testStream(base64decode(), ['YW55I', 'GNhcm5h', 'bCBwbGVhc3VyZS4', '='], 'any carnal pleasure.'); |
| 69 | }); |
| 70 | |
| 71 | it('should properly decode string containing newlines', function () { |
| 72 | testStream(base64decode(), ['YW55I\n', 'GNhcm5h\n', 'bCBwbGVhc3VyZ\n', 'S4=\n'], 'any carnal pleasure.'); |
| 73 | }); |
| 74 | |
| 75 | it('should properly decode a string without padding', function () { |
| 76 | testStream(base64decode(), ['YW55I', 'GNhcm5h', 'bCBwbGVhc3VyZ', 'S4'], 'any carnal pleasure.'); |
| 77 | }); |
| 78 | }); |
| 79 | }); |