spec/javascript/web_socket/protocol8parser_spec.js in faye-0.6.8 vs spec/javascript/web_socket/protocol8parser_spec.js in faye-0.7.0
- old
+ new
@@ -11,10 +11,18 @@
var bytes = [];
for (var i = 0, n = arguments.length; i < n; i++) bytes = bytes.concat(arguments[i])
this.parser.parse(new Buffer(bytes))
})
+ define("buffer", function(string) {
+ return {
+ equals: function(buffer) {
+ return buffer.toString('utf8', 0, buffer.length) === string
+ }
+ }
+ })
+
describe("parse", function() { with(this) {
define("mask", function() {
return this._mask = this._mask || Faye.map([1,2,3,4], function() { return Math.floor(Math.random() * 255) })
})
@@ -29,10 +37,15 @@
it("parses unmasked text frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parse([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
}})
+ it("parses empty text frames", function() { with(this) {
+ expect(webSocket, "receive").given("")
+ parse([0x81, 0x00])
+ }})
+
it("parses fragmented text frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parse([0x01, 0x03, 0x48, 0x65, 0x6c])
parse([0x80, 0x02, 0x6c, 0x6f])
}})
@@ -40,21 +53,31 @@
it("parses masked text frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parse([0x81, 0x85], mask(), maskMessage([0x48, 0x65, 0x6c, 0x6c, 0x6f]))
}})
+ it("parses masked empty text frames", function() { with(this) {
+ expect(webSocket, "receive").given("")
+ parse([0x81, 0x80], mask(), maskMessage([]))
+ }})
+
it("parses masked fragmented text frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parse([0x01, 0x81], mask(), maskMessage([0x48]))
parse([0x80, 0x84], mask(), maskMessage([0x65, 0x6c, 0x6c, 0x6f]))
}})
it("closes the socket if the frame has an unrecognized opcode", function() { with(this) {
- expect(webSocket, "close").given("protocol_error")
+ expect(webSocket, "close").given(1002, null, false)
parse([0x83, 0x00])
}})
+ it("closes the socket if a close frame is received", function() { with(this) {
+ expect(webSocket, "close").given(1000, "Hello", false)
+ parse([0x88, 0x07, 0x03, 0xe8, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
+ }})
+
it("parses unmasked multibyte text frames", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parse([0x81, 0x0b, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf])
}})
@@ -84,11 +107,11 @@
expect(webSocket, "receive").given("HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello")
parse([129, 254, 0, 200], mask(), maskMessage([72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111, 72, 101, 108, 108, 111]))
}})
it("replies to pings with a pong", function() { with(this) {
- expect(webSocket, "send").given("OHAI", "pong")
+ expect(webSocket, "send").given(buffer("OHAI"), "pong")
parse([0x89, 0x04, 0x4f, 0x48, 0x41, 0x49])
}})
}})
describe("frame", function() { with(this) {
@@ -116,10 +139,10 @@
parser.frame(message)
assertEqual( output, socket.read() )
}})
it("encodes close frames with an error code", function() { with(this) {
- parser.frame("Hello", "close", "protocol_error")
+ parser.frame("Hello", "close", 1002)
assertEqual( [0x88, 0x07, 0x03, 0xea, 0x48, 0x65, 0x6c, 0x6c, 0x6f], socket.read() )
}})
it("encodes pong frames", function() { with(this) {
parser.frame("", "pong")