summaryrefslogtreecommitdiffstats
path: root/test/webidl/converters.js
blob: 95bea88f01630aeef18a6976c792b4e8b57eb255 (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
193
194
195
196
197
198
199
200
201
202
'use strict'

const { test } = require('tap')
const { webidl } = require('../../lib/fetch/webidl')

test('sequence', (t) => {
  const converter = webidl.sequenceConverter(
    webidl.converters.DOMString
  )

  t.same(converter([1, 2, 3]), ['1', '2', '3'])

  t.throws(() => {
    converter(3)
  }, TypeError, 'disallows non-objects')

  t.throws(() => {
    converter(null)
  }, TypeError)

  t.throws(() => {
    converter(undefined)
  }, TypeError)

  t.throws(() => {
    converter({})
  }, TypeError, 'no Symbol.iterator')

  t.throws(() => {
    converter({
      [Symbol.iterator]: 42
    })
  }, TypeError, 'invalid Symbol.iterator')

  t.throws(() => {
    converter(webidl.converters.sequence({
      [Symbol.iterator] () {
        return {
          next: 'never!'
        }
      }
    }))
  }, TypeError, 'invalid generator')

  t.end()
})

test('webidl.dictionaryConverter', (t) => {
  t.test('arguments', (t) => {
    const converter = webidl.dictionaryConverter([])

    t.throws(() => {
      converter(true)
    }, TypeError)

    for (const value of [{}, undefined, null]) {
      t.doesNotThrow(() => {
        converter(value)
      })
    }

    t.end()
  })

  t.test('required key', (t) => {
    const converter = webidl.dictionaryConverter([
      {
        converter: () => true,
        key: 'Key',
        required: true
      }
    ])

    t.throws(() => {
      converter({ wrongKey: 'key' })
    }, TypeError)

    t.doesNotThrow(() => {
      converter({ Key: 'this key was required!' })
    })

    t.end()
  })

  t.end()
})

test('ArrayBuffer', (t) => {
  t.throws(() => {
    webidl.converters.ArrayBuffer(true)
  }, TypeError)

  t.throws(() => {
    webidl.converters.ArrayBuffer({})
  }, TypeError)

  t.throws(() => {
    const sab = new SharedArrayBuffer(1024)
    webidl.converters.ArrayBuffer(sab, { allowShared: false })
  }, TypeError)

  t.doesNotThrow(() => {
    const sab = new SharedArrayBuffer(1024)
    webidl.converters.ArrayBuffer(sab)
  })

  t.doesNotThrow(() => {
    const ab = new ArrayBuffer(8)
    webidl.converters.ArrayBuffer(ab)
  })

  t.end()
})

test('TypedArray', (t) => {
  t.throws(() => {
    webidl.converters.TypedArray(3)
  }, TypeError)

  t.throws(() => {
    webidl.converters.TypedArray({})
  }, TypeError)

  t.throws(() => {
    const uint8 = new Uint8Array([1, 2, 3])
    Object.defineProperty(uint8, 'buffer', {
      get () {
        return new SharedArrayBuffer(8)
      }
    })

    webidl.converters.TypedArray(uint8, Uint8Array, {
      allowShared: false
    })
  }, TypeError)

  t.end()
})

test('DataView', (t) => {
  t.throws(() => {
    webidl.converters.DataView(3)
  }, TypeError)

  t.throws(() => {
    webidl.converters.DataView({})
  }, TypeError)

  t.throws(() => {
    const buffer = new ArrayBuffer(16)
    const view = new DataView(buffer, 0)

    Object.defineProperty(view, 'buffer', {
      get () {
        return new SharedArrayBuffer(8)
      }
    })

    webidl.converters.DataView(view, {
      allowShared: false
    })
  })

  const buffer = new ArrayBuffer(16)
  const view = new DataView(buffer, 0)

  t.equal(webidl.converters.DataView(view), view)

  t.end()
})

test('BufferSource', (t) => {
  t.doesNotThrow(() => {
    const buffer = new ArrayBuffer(16)
    const view = new DataView(buffer, 0)

    webidl.converters.BufferSource(view)
  })

  t.throws(() => {
    webidl.converters.BufferSource(3)
  }, TypeError)

  t.end()
})

test('ByteString', (t) => {
  t.doesNotThrow(() => {
    webidl.converters.ByteString('')
  })

  // https://github.com/nodejs/undici/issues/1590
  t.throws(() => {
    const char = String.fromCharCode(256)
    webidl.converters.ByteString(`invalid${char}char`)
  }, {
    message: 'Cannot convert argument to a ByteString because the character at ' +
             'index 7 has a value of 256 which is greater than 255.'
  })

  t.end()
})