summaryrefslogtreecommitdiffstats
path: root/fastify-busboy/test/decoder.test.js
blob: fa4ce691dbb85e0747e84a492a88c74c871675e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
'use strict'

const { test } = require('tap')
const Decoder = require('../lib/utils/Decoder')

test('Decoder', t => {
  const tests =
  [
    {
      source: ['Hello world'],
      expected: 'Hello world',
      what: 'No encoded bytes'
    },
    {
      source: ['Hello%20world'],
      expected: 'Hello world',
      what: 'One full encoded byte'
    },
    {
      source: ['Hello%20world%21'],
      expected: 'Hello world!',
      what: 'Two full encoded bytes'
    },
    {
      source: ['Hello%', '20world'],
      expected: 'Hello world',
      what: 'One full encoded byte split #1'
    },
    {
      source: ['Hello%2', '0world'],
      expected: 'Hello world',
      what: 'One full encoded byte split #2'
    },
    {
      source: ['Hello%20', 'world'],
      expected: 'Hello world',
      what: 'One full encoded byte (concat)'
    },
    {
      source: ['Hello%2Qworld'],
      expected: 'Hello%2Qworld',
      what: 'Malformed encoded byte #1'
    },
    {
      source: ['Hello%world'],
      expected: 'Hello%world',
      what: 'Malformed encoded byte #2'
    },
    {
      source: ['Hello+world'],
      expected: 'Hello world',
      what: 'Plus to space'
    },
    {
      source: ['Hello+world%21'],
      expected: 'Hello world!',
      what: 'Plus and encoded byte'
    },
    {
      source: ['5%2B5%3D10'],
      expected: '5+5=10',
      what: 'Encoded plus'
    },
    {
      source: ['5+%2B+5+%3D+10'],
      expected: '5 + 5 = 10',
      what: 'Spaces and encoded plus'
    }
  ]
  t.plan(tests.length + 1)

  tests.forEach((v) => {
    t.test(v.what, t => {
      t.plan(1)

      const dec = new Decoder()
      let result = ''
      v.source.forEach(function (s) {
        result += dec.write(s)
      })
      const msg = 'Decoded string mismatch.\n' +
                'Saw: ' + result + '\n' +
                'Expected: ' + v.expected
      t.strictSame(result, v.expected, msg)
    })
  })

  t.test('reset sets internal buffer to undefined', t => {
    t.plan(2)

    const dec = new Decoder()
    dec.write('Hello+world%2')

    t.notSame(dec.buffer, undefined)
    dec.reset()
    t.equal(dec.buffer, undefined)
  })
})