Files
socket.io/test/helpers.js
Kevin Roark 5bea0bf41c Protocol is now class-based
Separated the encoding and decoding into two public-facing objects,
Encoder and Decoder.

Both objects take nothing on construction. Encoder has a single method,
encode, that mimics the previous version's function encode (takes a
packet object and a callback). Decoder has a single method too, add, that
takes any object (packet string or binary data). Decoder emits a 'decoded'
event when it has received all of the parts of a packet. The only
parameter for the decoded event is the reconstructed packet.

I am hesitant about the Encoder.encode vs Decoder.add thing. Should it be
more consistent, or should it stay like this where the function names are
more descriptive?

Also, rewrote the test helper functions to deal with new event-based
decoding. Wrote a new test in test/arraybuffer.js that tests for memory
leaks in Decoder as well.
2014-02-27 17:46:20 -05:00

47 lines
1.3 KiB
JavaScript

var parser = require('../index.js');
var expect = require('expect.js');
var encoder = new parser.Encoder();
// tests encoding and decoding a single packet
module.exports.test = function(obj){
encoder.encode(obj, function(encodedPackets) {
var decoder = new parser.Decoder();
decoder.on('decoded', function(packet) {
expect(packet).to.eql(obj);
});
decoder.add(encodedPackets[0]);
});
}
// tests encoding of binary packets
module.exports.test_bin = function test_bin(obj) {
var originalData = obj.data;
encoder.encode(obj, function(encodedPackets) {
var decoder = new parser.Decoder();
decoder.on('decoded', function(packet) {
obj.data = originalData;
obj.attachments = undefined;
expect(obj).to.eql(packet);
});
for (var i = 0; i < encodedPackets.length; i++) {
decoder.add(encodedPackets[i]);
}
});
}
// array buffer's slice is native code that is not transported across
// socket.io via msgpack, so regular .eql fails
module.exports.testArrayBuffers = function(buf1, buf2) {
buf1.slice = undefined;
buf2.slice = undefined;
expect(buf1).to.eql(buf2);
}
module.exports.testPacketMetadata = function(p1, p2) {
expect(p1.type).to.eql(p2.type);
expect(p1.id).to.eql(p2.id);
expect(p1.nsp).to.eql(p2.nsp);
}