spec/faye/websocket/client_spec.rb in faye-websocket-0.1.2 vs spec/faye/websocket/client_spec.rb in faye-websocket-0.2.0
- old
+ new
@@ -13,22 +13,22 @@
def stop(&callback)
@server.stop
EM.next_tick(&callback)
end
- def open_socket(url, &callback)
+ def open_socket(url, protocols, &callback)
done = false
resume = lambda do |open|
unless done
done = true
@open = open
callback.call
end
end
- @ws = Faye::WebSocket::Client.new(url)
+ @ws = Faye::WebSocket::Client.new(url, protocols)
@ws.onopen = lambda { |e| resume.call(true) }
@ws.onclose = lambda { |e| resume.call(false) }
end
@@ -48,10 +48,15 @@
def check_closed(&callback)
@open.should == false
callback.call
end
+ def check_protocol(protocol, &callback)
+ @ws.protocol.should == protocol
+ callback.call
+ end
+
def listen_for_message(&callback)
@ws.add_event_listener('message', lambda { |e| @message = e.data })
callback.call
end
@@ -72,47 +77,54 @@
end
describe Faye::WebSocket::Client do
include WebSocketSteps
+ let(:protocols) { ["foo", "echo"] }
let(:plain_text_url) { "ws://0.0.0.0:8000/" }
let(:secure_url) { "wss://0.0.0.0:8000/" }
before do
Thread.new { EM.run }
sleep(0.1) until EM.reactor_running?
end
shared_examples_for "socket client" do
it "can open a connection" do
- open_socket(socket_url)
+ open_socket(socket_url, protocols)
check_open
+ check_protocol("echo")
end
it "cannot open a connection to the wrong host" do
- open_socket(blocked_url)
+ open_socket(blocked_url, protocols)
check_closed
end
+ it "cannot open a connection with unacceptable protocols" do
+ open_socket(socket_url, ["foo"])
+ check_closed
+ end
+
it "can close the connection" do
- open_socket(socket_url)
+ open_socket(socket_url, protocols)
close_socket
check_closed
end
describe "in the OPEN state" do
- before { open_socket(socket_url) }
+ before { open_socket(socket_url, protocols) }
it "can send and receive messages" do
listen_for_message
send_message
check_response
end
end
describe "in the CLOSED state" do
before do
- open_socket(socket_url)
+ open_socket(socket_url, protocols)
close_socket
end
it "cannot send and receive messages" do
listen_for_message