|
| 1 | +import { strict as assert } from 'assert'; |
| 2 | +import { SinonSpy, spy } from 'sinon'; |
| 3 | +import RESP2Decoder from './decoder'; |
| 4 | +import { ErrorReply } from '../../errors'; |
| 5 | + |
| 6 | +interface DecoderAndSpies { |
| 7 | + decoder: RESP2Decoder; |
| 8 | + returnStringsAsBuffersSpy: SinonSpy; |
| 9 | + onReplySpy: SinonSpy; |
| 10 | +} |
| 11 | + |
| 12 | +function createDecoderAndSpies(returnStringsAsBuffers: boolean): DecoderAndSpies { |
| 13 | + const returnStringsAsBuffersSpy = spy(() => returnStringsAsBuffers), |
| 14 | + onReplySpy = spy(); |
| 15 | + |
| 16 | + return { |
| 17 | + decoder: new RESP2Decoder({ |
| 18 | + returnStringsAsBuffers: returnStringsAsBuffersSpy, |
| 19 | + onReply: onReplySpy |
| 20 | + }), |
| 21 | + returnStringsAsBuffersSpy, |
| 22 | + onReplySpy |
| 23 | + }; |
| 24 | +} |
| 25 | + |
| 26 | +function writeChunks(stream: RESP2Decoder, buffer: Buffer) { |
| 27 | + let i = 0; |
| 28 | + while (i < buffer.length) { |
| 29 | + stream.write(buffer.slice(i, ++i)); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +type Replies = Array<Array<unknown>>; |
| 34 | + |
| 35 | +interface TestsOptions { |
| 36 | + toWrite: Buffer; |
| 37 | + returnStringsAsBuffers: boolean; |
| 38 | + replies: Replies; |
| 39 | +} |
| 40 | + |
| 41 | +function generateTests({ |
| 42 | + toWrite, |
| 43 | + returnStringsAsBuffers, |
| 44 | + replies |
| 45 | +}: TestsOptions): void { |
| 46 | + it('single chunk', () => { |
| 47 | + const { decoder, returnStringsAsBuffersSpy, onReplySpy } = |
| 48 | + createDecoderAndSpies(returnStringsAsBuffers); |
| 49 | + decoder.write(toWrite); |
| 50 | + assert.equal(returnStringsAsBuffersSpy.callCount, replies.length); |
| 51 | + testReplies(onReplySpy, replies); |
| 52 | + }); |
| 53 | + |
| 54 | + it('multiple chunks', () => { |
| 55 | + const { decoder, returnStringsAsBuffersSpy, onReplySpy } = |
| 56 | + createDecoderAndSpies(returnStringsAsBuffers); |
| 57 | + writeChunks(decoder, toWrite); |
| 58 | + assert.equal(returnStringsAsBuffersSpy.callCount, replies.length); |
| 59 | + testReplies(onReplySpy, replies); |
| 60 | + }); |
| 61 | +} |
| 62 | + |
| 63 | +function testReplies(spy: SinonSpy, replies: Replies): void { |
| 64 | + if (!replies) { |
| 65 | + assert.equal(spy.callCount, 0); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + assert.equal(spy.callCount, replies.length); |
| 70 | + for (const [i, reply] of replies.entries()) { |
| 71 | + assert.deepEqual( |
| 72 | + spy.getCall(i).args, |
| 73 | + reply |
| 74 | + ); |
| 75 | + } |
| 76 | +} |
| 77 | + |
| 78 | +describe('RESP2Parser', () => { |
| 79 | + describe('Simple String', () => { |
| 80 | + describe('as strings', () => { |
| 81 | + generateTests({ |
| 82 | + toWrite: Buffer.from('+OK\r\n'), |
| 83 | + returnStringsAsBuffers: false, |
| 84 | + replies: [['OK']] |
| 85 | + }); |
| 86 | + }); |
| 87 | + |
| 88 | + describe('as buffers', () => { |
| 89 | + generateTests({ |
| 90 | + toWrite: Buffer.from('+OK\r\n'), |
| 91 | + returnStringsAsBuffers: true, |
| 92 | + replies: [[Buffer.from('OK')]] |
| 93 | + }); |
| 94 | + }); |
| 95 | + }); |
| 96 | + |
| 97 | + describe('Error', () => { |
| 98 | + generateTests({ |
| 99 | + toWrite: Buffer.from('-ERR\r\n'), |
| 100 | + returnStringsAsBuffers: false, |
| 101 | + replies: [[new ErrorReply('ERR')]] |
| 102 | + }); |
| 103 | + }); |
| 104 | + |
| 105 | + describe('Integer', () => { |
| 106 | + describe('-1', () => { |
| 107 | + generateTests({ |
| 108 | + toWrite: Buffer.from(':-1\r\n'), |
| 109 | + returnStringsAsBuffers: false, |
| 110 | + replies: [[-1]] |
| 111 | + }); |
| 112 | + }); |
| 113 | + |
| 114 | + describe('0', () => { |
| 115 | + generateTests({ |
| 116 | + toWrite: Buffer.from(':0\r\n'), |
| 117 | + returnStringsAsBuffers: false, |
| 118 | + replies: [[0]] |
| 119 | + }); |
| 120 | + }); |
| 121 | + }); |
| 122 | + |
| 123 | + describe('Bulk String', () => { |
| 124 | + describe('null', () => { |
| 125 | + generateTests({ |
| 126 | + toWrite: Buffer.from('$-1\r\n'), |
| 127 | + returnStringsAsBuffers: false, |
| 128 | + replies: [[null]] |
| 129 | + }); |
| 130 | + }); |
| 131 | + |
| 132 | + describe('as strings', () => { |
| 133 | + generateTests({ |
| 134 | + toWrite: Buffer.from('$2\r\naa\r\n'), |
| 135 | + returnStringsAsBuffers: false, |
| 136 | + replies: [['aa']] |
| 137 | + }); |
| 138 | + }); |
| 139 | + |
| 140 | + describe('as buffers', () => { |
| 141 | + generateTests({ |
| 142 | + toWrite: Buffer.from('$2\r\naa\r\n'), |
| 143 | + returnStringsAsBuffers: true, |
| 144 | + replies: [[Buffer.from('aa')]] |
| 145 | + }); |
| 146 | + }); |
| 147 | + }); |
| 148 | + |
| 149 | + describe('Array', () => { |
| 150 | + describe('null', () => { |
| 151 | + generateTests({ |
| 152 | + toWrite: Buffer.from('*-1\r\n'), |
| 153 | + returnStringsAsBuffers: false, |
| 154 | + replies: [[null]] |
| 155 | + }); |
| 156 | + }); |
| 157 | + |
| 158 | + const arrayBuffer = Buffer.from( |
| 159 | + '*5\r\n' + |
| 160 | + '+OK\r\n' + |
| 161 | + '-ERR\r\n' + |
| 162 | + ':0\r\n' + |
| 163 | + '$1\r\na\r\n' + |
| 164 | + '*0\r\n' |
| 165 | + ); |
| 166 | + |
| 167 | + describe('as strings', () => { |
| 168 | + generateTests({ |
| 169 | + toWrite: arrayBuffer, |
| 170 | + returnStringsAsBuffers: false, |
| 171 | + replies: [[[ |
| 172 | + 'OK', |
| 173 | + new ErrorReply('ERR'), |
| 174 | + 0, |
| 175 | + 'a', |
| 176 | + [] |
| 177 | + ]]] |
| 178 | + }); |
| 179 | + }); |
| 180 | + |
| 181 | + describe('as buffers', () => { |
| 182 | + generateTests({ |
| 183 | + toWrite: arrayBuffer, |
| 184 | + returnStringsAsBuffers: true, |
| 185 | + replies: [[[ |
| 186 | + Buffer.from('OK'), |
| 187 | + new ErrorReply('ERR'), |
| 188 | + 0, |
| 189 | + Buffer.from('a'), |
| 190 | + [] |
| 191 | + ]]] |
| 192 | + }); |
| 193 | + }); |
| 194 | + }); |
| 195 | +}); |
0 commit comments