Repository navigation
Expand file tree
/
Copy pathdataChannel.transfer.test.js
More file actions
102 lines (93 loc) · 2.48 KB
/
Copy pathdataChannel.transfer.test.js
File metadata and controls
102 lines (93 loc) · 2.48 KB
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
const test = require('ava')
const wrtc = require('wrtc')
const fixture = require('./signal-server.fixture')(test)
const dataChannel = require('./dataChannel')
const { poll } = require('./util')
test('short string', async t => {
let msg
await (await Promise.all([
dataChannel('A', config()),
dataChannel('A', config(), data => {
msg = data
})
]))[0].sendAsync('hi')
await poll(() => !!msg)
t.is(msg, 'hi')
})
test('max size string', async t => {
const expectedMsg = 'x'.repeat(65536)
let actualMsg
await (await Promise.all([
dataChannel('A', config()),
dataChannel('A', config(), data => {
actualMsg = data
})
]))[0].sendAsync(expectedMsg)
await poll(() => !!actualMsg)
t.is(actualMsg, expectedMsg)
})
test('rejects string > 64 Kb', async t => {
const client = (await Promise.all([
dataChannel('A', config()),
dataChannel('A', config())
]))[0]
const error = await t.throwsAsync(async () =>
client.sendAsync('x'.repeat(65537))
)
t.is(
error.message,
'message too big, allowed are 65536 bytes, but message has 65537 bytes'
)
})
test('send Buffer', async t => {
const buffer = getBuffer(65536, 42)
let received
const client = (await Promise.all([
dataChannel('A', config()),
dataChannel('A', config(), data => {
received = data
})
]))[0]
await client.sendAsync(buffer)
await poll(() => !!received)
t.true(received instanceof ArrayBuffer)
t.is(received.byteLength, 65536)
t.is(new Uint8Array(received)[0], 42)
})
test('send typed array', async t => {
const array = new Uint8Array(65536)
array.fill(42)
let received
const client = (await Promise.all([
dataChannel('A', config()),
dataChannel('A', config(), data => {
received = data
})
]))[0]
await client.sendAsync(array)
await poll(() => !!received)
t.true(received instanceof ArrayBuffer)
t.is(received.byteLength, 65536)
t.is(new Uint8Array(received)[0], 42)
})
test('rejects buffer > 64 Kb', async t => {
const client = (await Promise.all([
dataChannel('A', config()),
dataChannel('A', config())
]))[0]
const error = await t.throwsAsync(async () =>
client.sendAsync(getBuffer(65537, 42))
)
t.is(
error.message,
'message too big, allowed are 65536 bytes, but message has 65537 bytes'
)
})
function config () {
return { wrtc, signalServerUrl: fixture.getServerUrl() }
}
function getBuffer (size, byte) {
const data = new Uint8Array(size)
data.fill(byte)
return Buffer.from(data)
}