Slide 24
Slide 24 text
socket.io/tests/parser.test.js go-socketio/parser_test.go
'decoding error packet with reason and advice': function () {
parser.decodePacket('7:::2+0').should().eql({
type: 'error'
, reason: 'unauthorized'
, advice: 'reconnect'
, endpoint: ''
});
},
'decoding error packet with endpoint': function () {
parser.decodePacket('7::/woot').should().eql({
type: 'error'
, reason: ''
, advice: ''
, endpoint: '/woot'
});
},
'decoding ack packet': function () {
parser.decodePacket('6:::140').should().eql({
type: 'ack'
, ackId: '140'
, endpoint: ''
, args: []
});
},
'decoding ack packet with args': function () {
parser.decodePacket('6:::12+["woot","wa"]').should().eql({
type: 'ack'
, ackId: '12'
, endpoint: ''
, args: ['woot', 'wa']
});
},
'decoding ack packet with bad json': function () {
var thrown = false;
try {
parser.decodePacket('6:::1+{"++]').should().eql({
type: 'ack'
, ackId: '1'
, endpoint: ''
, args: []
});
} catch (e) {
thrown = true;
type TestFrame struct {
! Packet string
! Type uint8
! Endpoint string
! Parsable bool
}
var frames []TestFrame
func TestDecodePackets(t *testing.T) {
! frames = append(frames, TestFrame{Packet: `7:::0`, Type: PACKET_ERROR, Parsable: true})
! frames = append(frames, TestFrame{Packet: `7:::`, Type: PACKET_ERROR, Parsable: true})
! frames = append(frames, TestFrame{Packet: `7:::2+0`, Type: PACKET_ERROR, Parsable: true})
! frames = append(frames, TestFrame{Packet: `7::/woot`, Type: PACKET_ERROR, Parsable: true})
! frames = append(frames, TestFrame{Packet: `6:::140`, Type: PACKET_ACK, Parsable: true})
! frames = append(frames, TestFrame{Packet: `6:::12+["woot","wa"]`, Type: PACKET_ACK, Parsable: true
! frames = append(frames, TestFrame{Packet: `6:::1+{"++]`, Type: PACKET_ACK, Parsable: false})
! frames = append(frames, TestFrame{Packet: `4:::"2"`, Type: PACKET_JSONMESSAGE, Parsable: true})
! frames = append(frames, TestFrame{Packet: `4:1+::{"a":"b"}`, Type: PACKET_JSONMESSAGE, Parsable: t
! frames = append(frames, TestFrame{Packet: `5:::{"name":"ping","args":[{"id":1}]}`, Type: PACKET_EV
! frames = append(frames, TestFrame{Packet: `5:::{"name":"ping","args":{"id":1}}`, Type: PACKET_EVEN
! frames = append(frames, TestFrame{Packet: `5:::{"name":"ping","args":[{"id":"1"}]}`, Type: PACKET_
! frames = append(frames, TestFrame{Packet: `5:::{"name":"woot"}`, Type: PACKET_EVENT, Parsable: tru
! frames = append(frames, TestFrame{Packet: `5:1+::{"name":"tobi"}`, Type: PACKET_EVENT, Parsable: t
! frames = append(frames, TestFrame{Packet: `5:::{"name":"edwald","args":[{"a": "b"},2,"3"]}`, Type:
! frames = append(frames, TestFrame{Packet: `3:::woot`, Type: PACKET_MESSAGE, Parsable: true})
! frames = append(frames, TestFrame{Packet: `3:5:/tobi`, Type: PACKET_MESSAGE, Endpoint: "/tobi", Pa
! frames = append(frames, TestFrame{Packet: `2:::`, Type: PACKET_HEARTBEAT, Parsable: true})
! frames = append(frames, TestFrame{Packet: `1::/tobi`, Type: PACKET_CONNECT, Endpoint: "/tobi", Par
! frames = append(frames, TestFrame{Packet: `1::/test:?test=1`, Type: PACKET_CONNECT, Endpoint: "/te
! frames = append(frames, TestFrame{Packet: `0::/woot`, Type: PACKET_DISCONNECT, Endpoint: "/woot",
! totalFrames := len(frames)
! decodedFrames := 0
! for _, data := range frames {
! ! packet, err := decodePacket([]byte(data.Packet))
! ! // log.Println(data.Packet)
! ! if data.Parsable == false {
! ! ! // Should not be parsable and raise an error
! ! ! if err != nil {
! ! ! } else {
! ! ! ! t.Fatal("Expected an error parsing invalid packet")
! ! ! }
! ! } else {
! ! ! if err != nil {
! ! ! ! t.Fatalf("Error with packet '%s': %s", data.Packet, err.Error())
! ! ! }
Thursday, 16 May 13
Tests based on the socket.io client tests