08-27-周三_17-09-29

This commit is contained in:
2025-08-27 17:10:05 +08:00
commit 86df397d8f
12735 changed files with 1145479 additions and 0 deletions

View File

@@ -0,0 +1,175 @@
var Client = require('../../../lib/faye/websocket/client')
JS.ENV.WebSocketSteps = JS.Test.asyncSteps({
server: function(port, secure, callback) {
this._adapter = new EchoServer()
this._adapter.listen(port, secure)
this._port = port
setTimeout(callback, 100)
},
stop: function(callback) {
this._adapter.stop()
setTimeout(callback, 100)
},
open_socket: function(url, protocols, callback) {
var done = false,
self = this,
resume = function(open) {
if (done) return
done = true
self._open = open
callback()
}
this._ws = new Client(url, protocols, {verify: false})
this._ws.onopen = function() { resume(true) }
this._ws.onclose = function() { resume(false) }
},
close_socket: function(callback) {
var self = this
this._ws.onclose = function() {
self._open = false
callback()
}
this._ws.close()
},
check_open: function(callback) {
this.assert( this._open )
callback()
},
check_closed: function(callback) {
this.assert( !this._open )
callback()
},
check_protocol: function(protocol, callback) {
this.assertEqual( protocol, this._ws.protocol )
callback()
},
listen_for_message: function(callback) {
var self = this
this._ws.addEventListener('message', function(message) { self._message = message.data })
callback()
},
send_message: function(message, callback) {
this._ws.send(message)
setTimeout(callback, 100)
},
check_response: function(message, callback) {
this.assertEqual( message, this._message )
callback()
},
check_no_response: function(callback) {
this.assert( !this._message )
callback()
}
})
JS.ENV.ClientSpec = JS.Test.describe("Client", function() { with(this) {
include(WebSocketSteps)
before(function() {
this.protocols = ["foo", "echo"]
this.plain_text_url = "ws://localhost:8000/bayeux"
this.secure_url = "wss://localhost:8000/bayeux"
})
sharedBehavior("socket client", function() { with(this) {
it("can open a connection", function() { with(this) {
open_socket(socket_url, protocols)
check_open()
check_protocol("echo")
}})
it("cannot open a connection with unacceptable protocols", function() { with(this) {
open_socket(socket_url, ["foo"])
check_closed()
}})
it("can close the connection", function() { with(this) {
open_socket(socket_url, protocols)
close_socket()
check_closed()
}})
describe("in the OPEN state", function() { with(this) {
before(function() { with(this) {
open_socket(socket_url, protocols)
}})
it("can send and receive messages", function() { with(this) {
listen_for_message()
send_message("I expect this to be echoed")
check_response("I expect this to be echoed")
}})
it("sends numbers as strings", function() { with(this) {
listen_for_message()
send_message(13)
check_response("13")
}})
it("sends booleans as strings", function() { with(this) {
listen_for_message()
send_message(false)
check_response("false")
}})
it("sends arrays as strings", function() { with(this) {
listen_for_message()
send_message([13,14,15])
check_response("13,14,15")
}})
}})
describe("in the CLOSED state", function() { with(this) {
before(function() { with(this) {
open_socket(socket_url, protocols)
close_socket()
}})
it("cannot send and receive messages", function() { with(this) {
listen_for_message()
send_message("I expect this to be echoed")
check_no_response()
}})
}})
}})
describe("with a plain-text server", function() { with(this) {
before(function() {
this.socket_url = this.plain_text_url
this.blocked_url = this.secure_url
})
before(function() { this.server(8000, false) })
after (function() { this.stop() })
behavesLike("socket client")
}})
describe("with a secure server", function() { with(this) {
before(function() {
this.socket_url = this.secure_url
this.blocked_url = this.plain_text_url
})
before(function() { this.server(8000, true) })
after (function() { this.stop() })
behavesLike("socket client")
}})
}})

View File

@@ -0,0 +1,72 @@
var Draft75Parser = require('../../../lib/faye/websocket/draft75_parser')
JS.ENV.Draft75ParserSpec = JS.Test.describe("Draft75Parser", function() { with(this) {
before(function() { with(this) {
this.webSocket = {dispatchEvent: function() {}}
this.parser = new Draft75Parser(webSocket)
}})
describe("parse", function() { with(this) {
sharedBehavior("draft-75 parser", function() { with(this) {
it("parses text frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
}})
it("parses multiple frames from the same packet", function() { with(this) {
expect(webSocket, "receive").given("Hello").exactly(2)
parser.parse([0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff, 0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
}})
it("parses text frames beginning 0x00-0x7F", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x66, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
}})
it("ignores frames with a length header", function() { with(this) {
expect(webSocket, "receive").exactly(0)
parser.parse([0x80, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
}})
it("parses text following an ignored block", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x80, 0x02, 0x48, 0x65, 0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff])
}})
it("parses multibyte text frames", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parser.parse([0x00, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf, 0xff])
}})
it("parses frames received in several packets", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parser.parse([0x00, 0x41, 0x70, 0x70, 0x6c, 0x65])
parser.parse([0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf, 0xff])
}})
it("parses fragmented frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parser.parse([0x00, 0x48, 0x65, 0x6c])
parser.parse([0x6c, 0x6f, 0xff])
}})
}})
behavesLike("draft-75 parser")
it("does not close the socket if a 76 close frame is received", function() { with(this) {
expect(webSocket, "close").exactly(0)
expect(webSocket, "receive").given("")
parser.parse([0xFF, 0x00])
}})
}})
describe("frame", function() { with(this) {
it("returns the given string formatted as a WebSocket frame", function() { with(this) {
assertBufferEqual( [0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff], parser.frame("Hello") )
}})
it("encodes multibyte characters correctly", function() { with(this) {
assertBufferEqual( [0x00, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf, 0xff], parser.frame("Apple = ") )
}})
}})
}})

View File

@@ -0,0 +1,28 @@
var Draft76Parser = require('../../../lib/faye/websocket/draft76_parser')
JS.ENV.Draft76ParserSpec = JS.Test.describe("Draft76Parser", function() { with(this) {
before(function() { with(this) {
this.webSocket = {dispatchEvent: function() {}}
this.parser = new Draft76Parser(webSocket)
parser._handshakeComplete = true
}})
describe("parse", function() { with(this) {
behavesLike("draft-75 parser")
it("closes the socket if a close frame is received", function() { with(this) {
expect(webSocket, "close")
parser.parse([0xFF, 0x00])
}})
}})
describe("frame", function() { with(this) {
it("returns the given string formatted as a WebSocket frame", function() { with(this) {
assertBufferEqual( [0x00, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 0xff], parser.frame("Hello") )
}})
it("encodes multibyte characters correctly", function() { with(this) {
assertBufferEqual( [0x00, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf, 0xff], parser.frame("Apple = ") )
}})
}})
}})

View File

@@ -0,0 +1,148 @@
var HybiParser = require('../../../lib/faye/websocket/hybi_parser')
JS.ENV.HybiParserSpec = JS.Test.describe("HybiParser", function() { with(this) {
before(function() { with(this) {
this.webSocket = {dispatchEvent: function() {}}
this.parser = new HybiParser(webSocket)
}})
define("parse", function() {
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 || [1,2,3,4].map(function() { return Math.floor(Math.random() * 255) })
})
define("maskMessage", function(bytes) {
var output = []
Array.prototype.forEach.call(bytes, function(b, i) {
output[i] = bytes[i] ^ this.mask()[i % 4]
}, this)
return output
})
it("parses unmasked text frames", function() { with(this) {
expect(webSocket, "receive").given("Hello")
parse([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f])
}})
it("parses multiple frames from the same packet", function() { with(this) {
expect(webSocket, "receive").given("Hello").exactly(2)
parse([0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f, 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])
}})
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(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])
}})
it("parses frames received in several packets", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parse([0x81, 0x0b, 0x41, 0x70, 0x70, 0x6c])
parse([0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf])
}})
it("parses fragmented multibyte text frames", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parse([0x01, 0x0a, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3])
parse([0x80, 0x01, 0xbf])
}})
it("parses masked multibyte text frames", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parse([0x81, 0x8b], mask(), maskMessage([0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf]))
}})
it("parses masked fragmented multibyte text frames", function() { with(this) {
expect(webSocket, "receive").given("Apple = ")
parse([0x01, 0x8a], mask(), maskMessage([0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3]))
parse([0x80, 0x81], mask(), maskMessage([0xbf]))
}})
it("parses unmasked medium-length text frames", function() { with(this) {
expect(webSocket, "receive").given("HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello")
parse([129, 126, 0, 200, 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("parses masked medium-length text frames", function() { with(this) {
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(buffer("OHAI"), "pong")
parse([0x89, 0x04, 0x4f, 0x48, 0x41, 0x49])
}})
}})
describe("frame", function() { with(this) {
it("returns the given string formatted as a WebSocket frame", function() { with(this) {
assertBufferEqual( [0x81, 0x05, 0x48, 0x65, 0x6c, 0x6c, 0x6f], parser.frame("Hello") )
}})
it("encodes multibyte characters correctly", function() { with(this) {
assertBufferEqual( [0x81, 0x0b, 0x41, 0x70, 0x70, 0x6c, 0x65, 0x20, 0x3d, 0x20, 0xef, 0xa3, 0xbf], parser.frame("Apple = ") )
}})
it("encodes medium-length strings using extra length bytes", function() { with(this) {
assertBufferEqual( [129, 126, 0, 200, 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], parser.frame("HelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHelloHello") )
}})
it("encodes close frames with an error code", function() { with(this) {
assertBufferEqual( [0x88, 0x07, 0x03, 0xea, 0x48, 0x65, 0x6c, 0x6c, 0x6f], parser.frame("Hello", "close", 1002) )
}})
it("encodes pong frames", function() { with(this) {
assertBufferEqual( [0x8a, 0x00], parser.frame("", "pong") )
}})
}})
}})