summaryrefslogtreecommitdiffstats
path: root/fastify-busboy/test/dicer-headerparser.test.js
blob: 73da283233b891f5357db0883a00192d548cf29a (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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict'

const { test } = require('tap')
const HeaderParser = require('../deps/dicer/lib/HeaderParser')

test('dicer-headerparser', t => {
  const DCRLF = '\r\n\r\n'
  const MAXED_BUFFER = Buffer.allocUnsafe(128 * 1024)
  MAXED_BUFFER.fill(0x41) // 'A'

  const tests = [
    {
      source: DCRLF,
      expected: {},
      what: 'No header'
    },
    {
      source: ['Content-Type:\t  text/plain',
        'Content-Length:0'
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': ['  text/plain'], 'content-length': ['0'] },
      what: 'Value spacing'
    },
    {
      source: ['Content-Type:\t  text/plain',
        'Content-Length:0'
      ].join('\r\n') + DCRLF,
      cfg: {
        maxHeaderPairs: 0
      },
      expected: {},
      what: 'should enforce maxHeaderPairs of 0'
    },
    {
      source: ['Content-Type:\t  text/plain',
        'Content-Length:0'
      ].join('\r\n') + DCRLF,
      cfg: {
        maxHeaderPairs: 1
      },
      expected: { 'content-type': ['  text/plain'] },
      what: 'should enforce maxHeaderPairs of 1'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        'Foo:\r\n bar\r\n baz'
      ].join('\r\n') + DCRLF,
      expected: {},
      cfg: {
        maxHeaderSize: 0
      },
      what: 'should enforce maxHeaderSize of 0'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        'Foo:\r\n bar\r\n baz'
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [' text/plai'] },
      cfg: {
        maxHeaderSize: 25
      },
      what: 'should enforce maxHeaderSize of 25'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        'Foo:\r\n bar\r\n baz'
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [' text/plain'] },
      cfg: {
        maxHeaderSize: 31
      },
      what: 'should enforce maxHeaderSize of 31 and ignore the second header'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        'Foo:\r\n bar\r\n baz'
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [' text/plain'], foo: [''] },
      cfg: {
        maxHeaderSize: 32
      },
      what: 'should enforce maxHeaderSize of 32 and only add key of second header'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        'Foo:\r\n bar\r\n baz'
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [' text/plain'], foo: ['\r'] },
      cfg: {
        maxHeaderSize: 33
      },
      what: 'should enforce maxHeaderSize of 32 and get only first character of second pair'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        ' : '
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [' text/plain : '] },
      what: 'should not break if invalid header pair (colon exists but empty key and value) is provided'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        'FoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobaz'
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [' text/plain'] },
      what: 'should not break if invalid header pair (no distinctive colon) is provided'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        ':FoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobazFoobaz'
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [' text/plain'] },
      what: 'should not break if invalid header pair (no key) is provided'
    },
    {
      source: ['Content-Type:\t  text/plain',
        'Content-Length:0'
      ].join('\r\n') + DCRLF,
      cfg: {
        maxHeaderPairs: 2
      },
      expected: { 'content-type': ['  text/plain'], 'content-length': ['0'] },
      what: 'should enforce maxHeaderPairs of 2'
    },
    {
      source: ['Content-Type:\r\n text/plain',
        'Foo:\r\n bar\r\n baz'
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [' text/plain'], foo: [' bar baz'] },
      what: 'Folded values'
    },
    {
      source: [
        'Foo: bar',
        'Foo: baz'
      ].join('\r\n') + DCRLF,
      expected: { foo: ['bar', 'baz'] },
      what: 'Folded values'
    },
    {
      source: ['Content-Type:',
        'Foo: '
      ].join('\r\n') + DCRLF,
      expected: { 'content-type': [''], foo: [''] },
      what: 'Empty values'
    },
    {
      source: MAXED_BUFFER.toString('ascii') + DCRLF,
      expected: {},
      what: 'Max header size (single chunk)'
    },
    {
      source: ['ABCDEFGHIJ', MAXED_BUFFER.toString('ascii'), DCRLF],
      expected: {},
      what: 'Max header size (multiple chunks #1)'
    },
    {
      source: [MAXED_BUFFER.toString('ascii'), MAXED_BUFFER.toString('ascii'), DCRLF],
      expected: {},
      what: 'Max header size (multiple chunk #2)'
    }
  ]

  t.plan(tests.length)

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

      const cfg = {
        ...v.cfg
      }

      const parser = Object.keys(cfg).length ? new HeaderParser(cfg) : new HeaderParser()
      let fired = false

      parser.on('header', function (header) {
        t.ok(!fired, `${v.what}: Header event fired more than once`)
        fired = true
        t.strictSame(header,
          v.expected,
          `${v.what}: Parsed result mismatch`)
      })
      if (!Array.isArray(v.source)) { v.source = [v.source] }
      v.source.forEach(function (s) {
        parser.push(s)
      })
      t.ok(fired, `${v.what}: Did not receive header from parser`)
      t.pass()
    })
  })
})