-
Notifications
You must be signed in to change notification settings - Fork 105
/
parser.js
145 lines (124 loc) · 3.28 KB
/
parser.js
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
const { PacketType, Decoder, Encoder } = require("..");
const expect = require("expect.js");
const helpers = require("./helpers.js");
describe("socket.io-parser", () => {
it("exposes types", () => {
expect(PacketType.CONNECT).to.be.a("number");
expect(PacketType.DISCONNECT).to.be.a("number");
expect(PacketType.EVENT).to.be.a("number");
expect(PacketType.ACK).to.be.a("number");
expect(PacketType.CONNECT_ERROR).to.be.a("number");
expect(PacketType.BINARY_EVENT).to.be.a("number");
expect(PacketType.BINARY_ACK).to.be.a("number");
});
it("encodes connection", () => {
return helpers.test({
type: PacketType.CONNECT,
nsp: "/woot",
data: {
token: "123",
},
});
});
it("encodes disconnection", () => {
return helpers.test({
type: PacketType.DISCONNECT,
nsp: "/woot",
});
});
it("encodes an event", () => {
return helpers.test({
type: PacketType.EVENT,
data: ["a", 1, {}],
nsp: "/",
});
});
it("encodes an event (with an integer as event name)", () => {
return helpers.test({
type: PacketType.EVENT,
data: [1, "a", {}],
nsp: "/",
});
});
it("encodes an event (with ack)", () => {
return helpers.test({
type: PacketType.EVENT,
data: ["a", 1, {}],
id: 1,
nsp: "/test",
});
});
it("encodes an ack", () => {
return helpers.test({
type: PacketType.ACK,
data: ["a", 1, {}],
id: 123,
nsp: "/",
});
});
it("encodes an connect error", () => {
return helpers.test({
type: PacketType.CONNECT_ERROR,
data: "Unauthorized",
nsp: "/",
});
});
it("encodes an connect error (with object)", () => {
return helpers.test({
type: PacketType.CONNECT_ERROR,
data: {
message: "Unauthorized",
},
nsp: "/",
});
});
it("throws an error when encoding circular objects", () => {
const a = {};
a.b = a;
const data = {
type: PacketType.EVENT,
data: a,
id: 1,
nsp: "/",
};
const encoder = new Encoder();
expect(() => encoder.encode(data)).to.throwException();
});
it("decodes a bad binary packet", () => {
try {
const decoder = new Decoder();
decoder.add("5");
} catch (e) {
expect(e.message).to.match(/Illegal/);
}
});
it("throw an error upon parsing error", () => {
const isInvalidPayload = (str) =>
expect(() => new Decoder().add(str)).to.throwException(
/^invalid payload$/
);
isInvalidPayload('442["some","data"');
isInvalidPayload('0/admin,"invalid"');
isInvalidPayload("1/admin,{}");
isInvalidPayload('2/admin,"invalid');
isInvalidPayload("2/admin,{}");
expect(() => new Decoder().add("999")).to.throwException(
/^unknown packet type 9$/
);
expect(() => new Decoder().add(999)).to.throwException(
/^Unknown type: 999$/
);
});
it("should resume decoding after calling destroy()", () => {
return new Promise((resolve) => {
const decoder = new Decoder();
decoder.on("decoded", (packet) => {
expect(packet.data).to.eql(["hello"]);
resolve();
});
decoder.add('51-["hello"]');
decoder.destroy();
decoder.add('2["hello"]');
});
});
});