-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathparser.ts
More file actions
181 lines (158 loc) · 4.64 KB
/
Copy pathparser.ts
File metadata and controls
181 lines (158 loc) · 4.64 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
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
import assert from 'node:assert';
import { PacketType } from '../src/packet-format';
import buildEncoder from '../src/encoder';
import buildDecoder from '../src/decoder';
import helpers from './helpers.js';
const Encoder = buildEncoder(); // eslint-disable-line @typescript-eslint/naming-convention
const Decoder = buildDecoder(); // eslint-disable-line @typescript-eslint/naming-convention
describe('parser', () => {
it('exposes types', () => {
assert.equal(typeof PacketType.CONNECT, 'number');
assert.equal(typeof PacketType.DISCONNECT, 'number');
assert.equal(typeof PacketType.EVENT, 'number');
assert.equal(typeof PacketType.ACK, 'number');
assert.equal(typeof PacketType.CONNECT_ERROR, 'number');
});
it('encodes connection', done => {
helpers(
{
type: PacketType.CONNECT,
nsp: '/woot',
data: {
token: '123',
},
},
done,
);
});
it('encodes disconnection', done => {
helpers(
{
type: PacketType.DISCONNECT,
nsp: '/woot',
},
done,
);
});
it('encodes an event', done => {
helpers(
{
type: PacketType.EVENT,
data: ['a', 1, {}],
nsp: '/',
},
done,
);
});
it('encodes an event (with an integer as event name)', done => {
helpers(
{
type: PacketType.EVENT,
data: [1, 'a', {}],
nsp: '/',
},
done,
);
});
it('encodes an event (with ack)', done => {
helpers(
{
type: PacketType.EVENT,
data: ['a', 1, {}],
id: 1,
nsp: '/test',
},
done,
);
});
it('encodes an ack', done => {
helpers(
{
type: PacketType.ACK,
data: ['a', 1, {}],
id: 123,
nsp: '/',
},
done,
);
});
it('encodes an connect error', done => {
helpers(
{
type: PacketType.CONNECT_ERROR,
data: 'Unauthorized',
nsp: '/',
},
done,
);
});
it('encodes an connect error (with object)', done => {
helpers(
{
type: PacketType.CONNECT_ERROR,
data: {
message: 'Unauthorized',
},
nsp: '/',
},
done,
);
});
it('throws an error when encoding circular objects', () => {
const a: Record<string, unknown> = {};
a.b = a;
const data = {
type: PacketType.EVENT,
data: a,
id: 1,
nsp: '/',
};
const encoder = new Encoder();
assert.throws(() => encoder.encode(data));
});
it('decodes a bad binary packet', () => {
const decoder = new Decoder();
const errorObject: any = '5';
assert.throws(() => {
decoder.add(errorObject); // eslint-disable-line @typescript-eslint/no-unsafe-argument
}, /invalid packet/);
});
it('decodes with bad namespace', () => {
const data = {
type: PacketType.EVENT,
data: [],
nsp: null,
};
const encoder = new Encoder();
const encodedPackets = encoder.encode(data);
const decoder = new Decoder();
assert.throws(() => {
decoder.add(encodedPackets[0]);
}, /invalid packet namespace/);
});
it('decodes with bad payload', () => {
const data = {
type: PacketType.EVENT,
data: null,
nsp: '/',
};
const encoder = new Encoder();
const encodedPackets = encoder.encode(data);
const decoder = new Decoder();
assert.throws(() => {
decoder.add(encodedPackets[0]);
}, /invalid packet payload/);
});
it('throw an error upon parsing error', () => {
const isInvalidPayload = (errorObject: any) => {
assert.throws(() => {
new Decoder().add(errorObject); // eslint-disable-line @typescript-eslint/no-unsafe-argument
});
};
isInvalidPayload('442["some","data"');
isInvalidPayload('0/admin,"invalid"');
isInvalidPayload('1/admin,{}');
isInvalidPayload('2/admin,"invalid');
isInvalidPayload('2/admin,{}');
});
});