Skip to content

Commit 8080532

Browse files
authored
Improve test coverage (#153)
* test: 100% branch coverage in `parseParam.js` * test: 100% branch coverage in `Decoder.js` * test: improve branch coverage in `urlencode.js` * test: 100% test coverage in `sbmh.js` * test: 100% branch coverage in `main.js` * chore: fix linting
1 parent e62384c commit 8080532

4 files changed

Lines changed: 125 additions & 1 deletion

File tree

test/decoder.test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ test('Decoder', t => {
1616
expected: 'Hello world',
1717
what: 'One full encoded byte'
1818
},
19+
{
20+
source: ['%20Hello World'],
21+
expected: ' Hello World',
22+
what: 'Start with full encoded byte'
23+
},
1924
{
2025
source: ['Hello%20world%21'],
2126
expected: 'Hello world!',

test/parse-params.test.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ test('parse-params', t => {
5151
expected: ['text/plain', ['greeting', 'hello "world"']],
5252
what: 'Quotes within quoted'
5353
},
54+
{
55+
source: 'text/plain; greeting="hello \\\\world\\\\"',
56+
expected: ['text/plain', ['greeting', 'hello \\world\\']],
57+
what: 'Escape within quoted'
58+
},
5459
{
5560
source: 'text/plain; encoding=""',
5661
expected: ['text/plain', ['encoding', '']],
@@ -86,6 +91,11 @@ test('parse-params', t => {
8691
expected: ['text/plain', ['filename', '£ rates'], ['altfilename', 'foobarbaz']],
8792
what: 'Mixed regular and extended parameters (RFC 5987)'
8893
},
94+
{
95+
source: "text/plain; filename*=iso-8859-1'en';",
96+
expected: ['text/plain', ['filename', '']],
97+
what: 'Mixed regular and extended parameters (RFC 5987) with separator'
98+
},
8999
{
90100
source: "text/plain; filename=\"foobarbaz\"; altfilename*=iso-8859-1'en'%A3%20rates",
91101
expected: ['text/plain', ['filename', 'foobarbaz'], ['altfilename', '£ rates']],

test/streamsearch.test.js

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const { test } = require('tap')
44
const Streamsearch = require('../deps/streamsearch/sbmh')
55

66
test('streamsearch', t => {
7-
t.plan(17)
7+
t.plan(18)
88

99
t.test('should throw an error if the needle is not a String or Buffer', t => {
1010
t.plan(1)
@@ -335,6 +335,40 @@ test('streamsearch', t => {
335335
s.push(chunks[3])
336336
})
337337

338+
t.test('should process four chunks with repeted starting overflowing needle', t => {
339+
t.plan(13)
340+
341+
const expected = [
342+
[false, Buffer.from('\n\n\0'), 0, 1],
343+
[true, undefined, undefined, undefined],
344+
[false, Buffer.from('\r\nhello'), 1, 7]
345+
]
346+
const needle = '\n\n\r'
347+
const s = new Streamsearch(needle)
348+
const chunks = [
349+
Buffer.from('\n'),
350+
Buffer.from('\n'),
351+
Buffer.from('\n'),
352+
Buffer.from('\r\nhello')
353+
]
354+
let i = 0
355+
s.on('info', (isMatched, data, start, end) => {
356+
t.strictSame(isMatched, expected[i][0])
357+
t.strictSame(data, expected[i][1])
358+
t.strictSame(start, expected[i][2])
359+
t.strictSame(end, expected[i][3])
360+
i++
361+
if (i >= 3) {
362+
t.pass()
363+
}
364+
})
365+
366+
s.push(chunks[0])
367+
s.push(chunks[1])
368+
s.push(chunks[2])
369+
s.push(chunks[3])
370+
})
371+
338372
t.test('should process four chunks with a potentially overflowing needle', t => {
339373
t.plan(17)
340374

test/types-urlencoded.test.js

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,13 @@ const tests = [
2727
what: 'Unassigned and assigned value',
2828
plan: 5
2929
},
30+
{
31+
source: ['foo&baz=bla'],
32+
expected: [['foo', '', false, false]],
33+
what: 'Unassigned and assigned value with limit',
34+
limits: { fields: 1 },
35+
plan: 4
36+
},
3037
{
3138
source: ['foo=bar&baz'],
3239
expected: [['foo', 'bar', false, false],
@@ -80,6 +87,13 @@ const tests = [
8087
what: 'Two assigned values, one with encoded bytes',
8188
plan: 5
8289
},
90+
{
91+
source: ['foo=bar&baz=bla', 'foo=bar'],
92+
expected: [],
93+
what: 'Exceeded limits',
94+
limits: { fields: 0 },
95+
plan: 3
96+
},
8397
{
8498
source: ['foo=bar&baz=bla'],
8599
expected: [],
@@ -110,6 +124,22 @@ const tests = [
110124
limits: { fieldNameSize: 2 },
111125
plan: 5
112126
},
127+
{
128+
source: ['f%20=bar&baz=bla'],
129+
expected: [['f ', 'bar', false, false],
130+
['ba', 'bla', true, false]],
131+
what: 'Limits: truncated field name with encoded bytes',
132+
limits: { fieldNameSize: 2 },
133+
plan: 5
134+
},
135+
{
136+
source: ['foo=b%20&baz=bla'],
137+
expected: [['foo', 'b ', false, false],
138+
['baz', 'bl', false, true]],
139+
what: 'Limits: truncated field value with encoded bytes',
140+
limits: { fieldSize: 2 },
141+
plan: 5
142+
},
113143
{
114144
source: ['foo=bar&baz=bla'],
115145
expected: [['foo', 'ba', false, true],
@@ -208,3 +238,48 @@ tests.forEach((v) => {
208238
busboy.end()
209239
})
210240
})
241+
242+
test('Call parser end twice', t => {
243+
t.plan(1)
244+
245+
let finishes = 0
246+
const busboy = new Busboy({
247+
headers: {
248+
'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
249+
}
250+
})
251+
252+
busboy.on('finish', function () {
253+
t.ok(++finishes === 1, 'finish emitted')
254+
})
255+
256+
busboy.write(Buffer.from('Hello world', 'utf8'), EMPTY_FN)
257+
258+
busboy._parser.end()
259+
busboy._parser.end()
260+
})
261+
262+
test('Call emit finish twice', t => {
263+
t.plan(2)
264+
265+
let fields = 0
266+
let finishes = 0
267+
268+
const busboy = new Busboy({
269+
headers: {
270+
'content-type': 'application/x-www-form-urlencoded; charset=utf-8'
271+
}
272+
})
273+
busboy.on('field', function () {
274+
t.ok(++fields === 1, 'field emitted')
275+
})
276+
277+
busboy.on('finish', function () {
278+
t.ok(++finishes === 1, 'finish emitted')
279+
})
280+
281+
busboy.write(Buffer.from('Hello world', 'utf8'), EMPTY_FN)
282+
283+
busboy.emit('finish')
284+
busboy.emit('finish')
285+
})

0 commit comments

Comments
 (0)