spec/integration/draft76_spec.rb in em-websocket-0.2.1 vs spec/integration/draft76_spec.rb in em-websocket-0.3.0
- old
+ new
@@ -1,6 +1,7 @@
require 'helper'
+require 'integration/shared_examples'
describe "WebSocket server draft76" do
before :each do
@request = {
:port => 80,
@@ -28,20 +29,34 @@
},
:body => "8jKS\'y:G*Co,Wxa-"
}
end
+ it_behaves_like "a websocket server" do
+ def start_server
+ EM::WebSocket.start(:host => "0.0.0.0", :port => 12345) { |ws|
+ yield ws
+ }
+ end
+
+ def start_client
+ client = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
+ client.send_data(format_request(@request))
+ yield client if block_given?
+ end
+ end
+
it "should send back the correct handshake response" do
EM.run do
EM.add_timer(0.1) do
EventMachine::WebSocket.start(:host => "0.0.0.0", :port => 12345) { }
# Create a fake client which sends draft 76 handshake
connection = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
connection.send_data(format_request(@request))
- connection.onopen = lambda {
+ connection.onopen {
connection.handshake_response.lines.sort.
should == format_response(@response).lines.sort
EM.stop
}
end
@@ -56,17 +71,17 @@
# Create a fake client which sends draft 76 handshake
connection = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
connection.send_data(format_request(@request))
# Send closing frame after handshake complete
- connection.onopen = lambda {
+ connection.onopen {
connection.send_data(EM::WebSocket::Handler76::TERMINATE_STRING)
}
# Check that this causes a termination string to be returned and the
# connection close
- connection.onclose = lambda {
+ connection.onclose {
connection.packets[0].should ==
EM::WebSocket::Handler76::TERMINATE_STRING
EM.stop
}
end
@@ -86,16 +101,16 @@
# Create a fake client which sends draft 76 handshake
connection = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
connection.send_data(format_request(@request))
# Send closing frame after handshake complete, followed by another msg
- connection.onopen = lambda {
+ connection.onopen {
connection.send_data(EM::WebSocket::Handler76::TERMINATE_STRING)
connection.send('foobar')
}
- connection.onclose = lambda {
+ connection.onclose {
EM.stop
}
end
end
end
@@ -112,16 +127,16 @@
# Create a fake client which sends draft 76 handshake
connection = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
connection.send_data(format_request(@request))
# Send closing frame after handshake complete
- connection.onopen = lambda {
+ connection.onopen {
connection.send_data("\000\n\000\377")
connection.send_data(EM::WebSocket::Handler76::TERMINATE_STRING)
}
- connection.onclose = lambda {
+ connection.onclose {
EM.stop
}
end
end
end
@@ -142,11 +157,11 @@
# This particular frame indicates a message length of
# 1180591620717411303296 bytes. Such a message would previously cause
# a "bignum too big to convert into `long'" error.
# However it is clearly unreasonable and should be rejected.
- client.onopen = lambda {
+ client.onopen {
client.send_data("\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x00")
}
end
end
@@ -162,11 +177,11 @@
# Create a fake client which sends draft 76 handshake
client = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
client.send_data(format_request(@request))
- client.onopen = lambda {
+ client.onopen {
client.send_data("foobar") # Does not start with \x00 or \xff
}
end
end
@@ -194,10 +209,10 @@
connection = EM.connect('0.0.0.0', 12345, FakeWebSocketClient)
data = format_request(@request)
# Sends first half of the request
connection.send_data(data[0...(data.length / 2)])
- connection.onopen = lambda {
+ connection.onopen {
connection.handshake_response.lines.sort.
should == format_response(@response).lines.sort
EM.stop
}