|
1 | 1 | import {
|
2 | 2 | decodePacket,
|
| 3 | + decodePacketFromBinary, |
3 | 4 | decodePayload,
|
4 | 5 | encodePacket,
|
| 6 | + encodePacketToBinary, |
5 | 7 | encodePayload,
|
6 | 8 | Packet
|
7 | 9 | } from "..";
|
@@ -111,4 +113,113 @@ describe("engine.io-parser (browser only)", () => {
|
111 | 113 | });
|
112 | 114 | }
|
113 | 115 | });
|
| 116 | + |
| 117 | + describe("single packet (to/from Uint8Array)", function() { |
| 118 | + if (!withNativeArrayBuffer) { |
| 119 | + // @ts-ignore |
| 120 | + return this.skip(); |
| 121 | + } |
| 122 | + |
| 123 | + it("should encode a plaintext packet", done => { |
| 124 | + const packet: Packet = { |
| 125 | + type: "message", |
| 126 | + data: "1€" |
| 127 | + }; |
| 128 | + encodePacketToBinary(packet, encodedPacket => { |
| 129 | + expect(encodedPacket).to.be.an(Uint8Array); |
| 130 | + expect(encodedPacket).to.eql(Uint8Array.from([52, 49, 226, 130, 172])); |
| 131 | + |
| 132 | + const decoded = decodePacketFromBinary( |
| 133 | + encodedPacket, |
| 134 | + false, |
| 135 | + "arraybuffer" |
| 136 | + ); |
| 137 | + expect(decoded).to.eql(packet); |
| 138 | + done(); |
| 139 | + }); |
| 140 | + }); |
| 141 | + |
| 142 | + it("should encode a binary packet (Uint8Array)", done => { |
| 143 | + const packet: Packet = { |
| 144 | + type: "message", |
| 145 | + data: Uint8Array.from([1, 2, 3]) |
| 146 | + }; |
| 147 | + encodePacketToBinary(packet, encodedPacket => { |
| 148 | + expect(encodedPacket === packet.data).to.be(true); |
| 149 | + done(); |
| 150 | + }); |
| 151 | + }); |
| 152 | + |
| 153 | + it("should encode a binary packet (Blob)", done => { |
| 154 | + const packet: Packet = { |
| 155 | + type: "message", |
| 156 | + data: new Blob([Uint8Array.from([1, 2, 3])]) |
| 157 | + }; |
| 158 | + encodePacketToBinary(packet, encodedPacket => { |
| 159 | + expect(encodedPacket).to.be.an(Uint8Array); |
| 160 | + expect(encodedPacket).to.eql(Uint8Array.from([1, 2, 3])); |
| 161 | + done(); |
| 162 | + }); |
| 163 | + }); |
| 164 | + |
| 165 | + it("should encode a binary packet (ArrayBuffer)", done => { |
| 166 | + const packet: Packet = { |
| 167 | + type: "message", |
| 168 | + data: Uint8Array.from([1, 2, 3]).buffer |
| 169 | + }; |
| 170 | + encodePacketToBinary(packet, encodedPacket => { |
| 171 | + expect(encodedPacket).to.be.an(Uint8Array); |
| 172 | + expect(encodedPacket).to.eql(Uint8Array.from([1, 2, 3])); |
| 173 | + done(); |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it("should encode a binary packet (Uint16Array)", done => { |
| 178 | + const packet: Packet = { |
| 179 | + type: "message", |
| 180 | + data: Uint16Array.from([1, 2, 257]) |
| 181 | + }; |
| 182 | + encodePacketToBinary(packet, encodedPacket => { |
| 183 | + expect(encodedPacket).to.be.an(Uint8Array); |
| 184 | + expect(encodedPacket).to.eql(Uint8Array.from([1, 0, 2, 0, 1, 1])); |
| 185 | + done(); |
| 186 | + }); |
| 187 | + }); |
| 188 | + |
| 189 | + it("should decode a binary packet (Blob)", () => { |
| 190 | + const decoded = decodePacketFromBinary( |
| 191 | + Uint8Array.from([1, 2, 3]), |
| 192 | + false, |
| 193 | + "blob" |
| 194 | + ); |
| 195 | + |
| 196 | + expect(decoded.type).to.eql("message"); |
| 197 | + expect(decoded.data).to.be.a(Blob); |
| 198 | + }); |
| 199 | + |
| 200 | + it("should decode a binary packet (ArrayBuffer)", () => { |
| 201 | + const decoded = decodePacketFromBinary( |
| 202 | + Uint8Array.from([1, 2, 3]), |
| 203 | + false, |
| 204 | + "arraybuffer" |
| 205 | + ); |
| 206 | + |
| 207 | + expect(decoded.type).to.eql("message"); |
| 208 | + expect(decoded.data).to.be.an(ArrayBuffer); |
| 209 | + expect(areArraysEqual(decoded.data, Uint8Array.from([1, 2, 3]))); |
| 210 | + }); |
| 211 | + |
| 212 | + it("should decode a binary packet (with binary header)", () => { |
| 213 | + // 52 === "4".charCodeAt(0) |
| 214 | + const decoded = decodePacketFromBinary( |
| 215 | + Uint8Array.from([52]), |
| 216 | + true, |
| 217 | + "arraybuffer" |
| 218 | + ); |
| 219 | + |
| 220 | + expect(decoded.type).to.eql("message"); |
| 221 | + expect(decoded.data).to.be.an(ArrayBuffer); |
| 222 | + expect(areArraysEqual(decoded.data, Uint8Array.from([52]))); |
| 223 | + }); |
| 224 | + }); |
114 | 225 | });
|
0 commit comments